TensofrlowJS输入与模型预期输入不匹配

时间:2020-02-13 16:22:00

标签: keras tensorflow.js tensorflowjs-converter

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

topology.ts:1114输入张量的形状([null,1024])与层密度的期望值不匹配:[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();

  }

如何解决警告消息?

1 个答案:

答案 0 :(得分:1)

错误很直接。该模型期望输入形状为[b,1024](批处理大小为b)。您正在将形状为[1、224、224、3]的图像作为参数传递给模型。不用说它是行不通的。

要使预测生效,模型的输入应与预测的张量形状匹配。输入模型发生更改或图像以适合模型的方式重塑。