【问题标题】:TensorflowJS: Error: The shape of dict['images'] provided in model.execute(dict) must be [-1,224,224,3], but was [400,600,1]TensorflowJS:错误:model.execute(dict) 中提供的 dict['images'] 的形状必须为 [-1,224,224,3],但为 [400,600,1]
【发布时间】:2018-10-15 22:46:01
【问题描述】:

我正在使用来自谷歌的mobilenet 神经网络对图像进行分类。我正在使用 Angular 6 + TensorFlow.js 来构建我的图像分类器应用程序。

我正在尝试按照tfjs-converter library readme 提供的步骤进行操作,并得出以下代码:

import * as tf from '@tensorflow/tfjs';
import { FrozenModel } from '@tensorflow/tfjs';

export class NeuralNetwork {
    private MODEL_PATH: string = 'http://localhost:4200/assets/model/tensorflowjs_model.pb';
    private WEIGHTS_PATH: string = 'http://localhost:4200/assets/model/weights_manifest.json';
    constructor(){}
    async loadModel() {
        const localModel: FrozenModel = (await tf.loadFrozenModel(this.MODEL_PATH, this.WEIGHTS_PATH));
        let image: any = document.getElementById('cat');

        let pixels =  tf.fromPixels(image, 1);
        let result = localModel.predict(pixels);
        
    }
    async predict(){
        let image: any = document.getElementById('cat');
        debugger;
        this.model.execute({input: tf.fromPixels(image)});
    }
    
}

图像 HTML 元素:

<img id="cat" src="http://localhost:4200/assets/images/cat.jpg"/>

当我尝试执行localModel.predict(pixels) 函数时,我收到以下错误:

未捕获(承诺):错误:model.execute(dict) 中提供的 dict['images'] 的形状必须为 [-1,224,224,3],但为 [400,600,1]

我是 TensorFlow 和 TensorFlow.js 技术的新手。有谁知道我做错了什么?

【问题讨论】:

  • 它的 TensOrflow,而不是“Tensaflow”。听起来像一些糟糕的说唱歌手哈哈

标签: angular typescript tensorflow tensorflow.js


【解决方案1】:

遇到了同样的问题,然后在 Github 中找到了解决方法:Input appears to be wrong shape

const img = document.getElementById('myimg');
const tfImg = tf.fromPixels(img);
const smalImg = tf.image.resizeBilinear(tfImg, [368, 432]);
const resized = tf.cast(smalImg, 'float32');
const t4d = tf.tensor4d(Array.from(resized.dataSync()),[1,368,432,3])

【讨论】:

    【解决方案2】:

    错误表明模型中定义的形状与传递给模型的输入形状不匹配。要解决这个问题,您必须重塑您的输入。

    但是使用输入的tf.reshape 将不起作用,因为没有整数k,例如k*224*224*3 将等于400*600*1。可以考虑使用tf.slice 对输入图像进行切片,以便只保留图像所需的形状。

    const a = tf.randomNormal([400, 600, 3])
    // a has shape [400, 600, 3]
    // a cannot be reshaped into [224, 224, 3]
    // a can be sliced into [224, 224, 3]
    const b = a.slice([0, 0, 0], [224, 224, 3])
    

    【讨论】:

      【解决方案3】:

      这个错误可能是因为模型需要一批图像而不是一个图像。要修复它,可以使用 expandDims 插入额外的维度。

      let result = localModel.predict(tf.fromPixels(image).expandDims(0));
      

      【讨论】:

        【解决方案4】:

        在使用 MobileNet 预测图像之前,您需要以特定方式对图像进行预处理。 以下是使用 TensorFlow.js 的方法:

        let image: any = document.getElementById('cat');
        
        // resize the input image to (224, 224)
        let tensor = tf.browser.fromPixels(image).resizeNearestNeighbor([224, 224]).toFloat();
        // feature scale tensor image to range [-1, 1]
        let offset = tf.scalar(127.5);
        tensor = tensor.sub(offset).div(offset).expandDims();
        

        现在您可以预测预处理后的图像(张量):

        let result = localModel.predict(tensor);
        

        更多相关代码可以看here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 2020-06-01
          • 1970-01-01
          • 2020-07-17
          • 2018-04-24
          • 1970-01-01
          相关资源
          最近更新 更多