【发布时间】:2018-07-13 13:11:32
【问题描述】:
我在使用 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;
【问题讨论】:
-
您好,我也有类似的问题。可以分享一下
setState这个函数吗? -
你让它工作了吗?
标签: javascript python tensorflow tensorflow.js