【问题标题】:Error: The shape of dict['input'] provided in model.execute(dict) must be [1,224,224,3], but was [1,244,244,3]错误:model.execute(dict) 中提供的 dict['input'] 的形状必须为 [1,224,224,3],但为 [1,244,244,3]
【发布时间】: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


【解决方案1】:

错误中的值不同:

错误:model.execute(dict)中提供的dict['input']的形状必须是[1,224,224,3 ],但为 [1,244,244,3]

【讨论】:

  • 哇。当你不睡觉时会发生这种情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2020-06-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2021-06-20
相关资源
最近更新 更多