javascript - 错误 : The shape of dict ['input' ] provided in model.execute(dict) 必须是 [1,224,224,3],但实际上是 [1,244,244,3]

标签 javascript python tensorflow tensorflow.js

我在使用tensorflow.js时遇到一个奇怪的错误:

Error: The shape of dict['input'] provided in model.execute(dict) must be [1,224,224,3], but was [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;

最佳答案

错误中的值不同:

Error: The shape of dict['input'] provided in model.execute(dict) must be [1,224,224,3], but was [1,244,244,3]

关于javascript - 错误 : The shape of dict ['input' ] provided in model.execute(dict) 必须是 [1,224,224,3],但实际上是 [1,244,244,3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325969/

相关文章:

python - 我正在编写一个学生成绩跟踪器,但不知道如何将学生添加到数据库中

python - 在python中缓存函数的最后k个结果

python - 尽管重用设置为 true,Tensorflow 仍创建新变量

c# - 我如何从 C# 调用 javascript

javascript - 为什么我的 HTML5 旁白部分没有显示在我的页面中?

javascript - 利用 p5.js/processing 在 GeoTIFF 上进行数据可视化?如何在网络墨卡托中绘制经度/纬度坐标?

python - 使用 ctypes 映射基本 typedef 别名?

python - Keras 模型中的权重和变量有什​​么区别?

tensorflow - 使用 tf.estimator.EstimatorSpec 时如何检查每个时期后的评估 auc?

javascript - 如何向最终用户隐藏 REST API Url?