使用tensorflow.js时出现一个奇怪的错误:
错误:model.execute(dict)中提供的dict ['input']的形状必须 是[1,224,224,3],但是[1,244,244,3]
因此尺寸相同...但是不正确。任何帮助将不胜感激。
我的代码如下:
import React, { Component } from "react";
import "./App.css";
import * as tf from "@tensorflow/tfjs";
import { loadFrozenModel } from "@tensorflow/tfjs-converter";
class App extends Component {
// Set state
state = { selectedFile: null, input: null };
// File selected
fileChangedHandler = event => {
this.setState({ selectedFile: event.target.files[0] });
};
// File uploaded
uploadHandler = () => {
// Read file
let reader = new FileReader();
reader.addEventListener("loadend", () => {
// Set width and height of image
let img = new Image(244, 244);
img.src = reader.result;
// Convert to tensor
let imgTensor = tf.fromPixels(img);
// Init input with correct shape
let input = tf.zeros([1, 244, 244, 3]);
// Add img to input
input[0] = imgTensor;
this.setState({ input });
});
// Get img URL
reader.readAsDataURL(this.state.selectedFile);
};
componentDidMount() {
// Load model
loadFrozenModel(
"http://.../tf_models/tensorflowjs_model.pb",
"http://.../tf_models/weights_manifest.json"
).then(model => this.setState({ model }));
}
componentDidUpdate() {
// Image and model are ready
if (this.state.model && this.state.input) {
// Use model for prediction
this.state.model.execute({
input: this.state.input
}).print();
}
}
render() {
return (
<div className="App">
<input type="file" onChange={this.fileChangedHandler.bind(this)} />
<button onClick={this.uploadHandler.bind(this)}>Upload!</button>
</div>
);
}
}
export default App;
答案 0 :(得分:1)
错误中的值不同:
错误:model.execute(dict)中提供的dict ['input']的形状必须为[1,2 2 4,2 2 4,3 ],但为[1,2 4 4,2 4 4,3]