【问题标题】:TensofrlowJS input mismatch from model expected inputTensofrlowJS 输入与模型预期输入不匹配
【发布时间】:2023-04-08 11:07:02
【问题描述】:

我将 Keras 模型转换为 tfjs,并在浏览器中运行它时收到以下警告:

topology.ts:1114 输入张量的形状([null,1024])与layerdense的期望不匹配:[null,[224,224,3]]

模型摘要如下所示:

_________________________________________________________________
 Layer (type)                 Output shape              Param #   
=================================================================
 mobilenet_1.00_224 (Model)   [null,1024]               3228864   
_________________________________________________________________
 dense (Dense)                [null,256]                262400    
_________________________________________________________________
 dropout (Dropout)            [null,256]                0         
_________________________________________________________________
 dense_1 (Dense)              [null,512]                131584    
_________________________________________________________________
 dropout_1 (Dropout)          [null,512]                0         
_________________________________________________________________
 dense_2 (Dense)              [null,7]                  3591      
=================================================================
Total params: 3626439
Trainable params: 397575
Non-trainable params: 3228864

对于预测,我实现了以下方法:

async function classifyImage() {

    const cam = await tf.data.webcam(video); //video is a webcam element with 224x224 pixels
    const img = await cam.capture();
    console.log(img.shape);
    let new_frame = img.reshape([1, 224, 224, 3]);

    predictions = await model.predict(new_frame).print();

  }

如何解决警告信息?

【问题讨论】:

    标签: keras tensorflow.js tensorflowjs-converter


    【解决方案1】:

    错误是直截了当的。该模型期望输入形状为 [b, 1024](b 表示批量大小)。您将形状 [1, 224, 224, 3] 的图像作为参数传递给模型。不用说它不会起作用。

    为了使预测有效,模型的输入应该与预测的张量形状相匹配。要么输入模型发生变化,要么图像以适合模型的方式重塑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2021-03-09
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多