kerastensorflow.jstensorflowjs-converter

TensofrlowJS input mismatch from model expected input


I converted a Keras model to tfjs and when running it in browser I get the following warning:

topology.ts:1114 The shape of the input tensor ([null,1024]) does not match the expectation of layer dense: [null,[224,224,3]]

The model summary looks like this:

_________________________________________________________________
 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

For the prediction I implemented the following method:

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();

  }

How can I solve the warning message?


Solution

  • The error is straight forward. The model is expecting an input of shape [b, 1024] (b for the batch size). You are passing as argument to the model an image of shape [1, 224, 224, 3]. Needless to say that it won't work.

    To make the prediction work, the input of the model should match with the predicted tensor shape. Either the input model changes or the image is reshaped in a way that is fitting to the model.