简体   繁体   中英

Tensorflow js: Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), but got array with shape [100,120,3]

Can anyone explain to me what happened here ?

This is my code:

var model;

async function deploy() {
    console.log('Deploying model...');

    model = await tf.loadLayersModel('keras model/js_model/model.json');

    console.log('model loaded!');

    var sample_image = document.getElementById('test_image');
    sample_image = tf.browser.fromPixels(sample_image);

    var sample_image_height = sample_image.shape[0];
    var sample_image_width = sample_image.shape[1];

    sample_image.reshape([-1, sample_image_height, sample_image_width, 3]);

    result = await model.predict(sample_image);

    console.log(result);

}

deploy();

and it produce error message:

Uncaught (in promise) Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), but got array with shape [100,120,3]

This is the conv2d_13 batch_input_shape attribute from the model.json:

 "batch_input_shape": [null, 250, 250, 3]

I wonder what's wrong...

edit: This is my model summary:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 248, 248, 32)      896       
_________________________________________________________________
batch_normalization_1 (Batch (None, 248, 248, 32)      128       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 124, 124, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 122, 122, 64)      18496     
_________________________________________________________________
batch_normalization_2 (Batch (None, 122, 122, 64)      256       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 61, 61, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 59, 59, 64)        36928     
_________________________________________________________________
batch_normalization_3 (Batch (None, 59, 59, 64)        256       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 29, 29, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 27, 27, 64)        36928     
_________________________________________________________________
batch_normalization_4 (Batch (None, 27, 27, 64)        256       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 13, 13, 64)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 11, 11, 64)        36928     
_________________________________________________________________
batch_normalization_5 (Batch (None, 11, 11, 64)        256       
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 3, 3, 64)          36928     
_________________________________________________________________
batch_normalization_6 (Batch (None, 3, 3, 64)          256       
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 1, 1, 64)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               33280     
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 1026      
=================================================================
Total params: 465,474
Trainable params: 464,770
Non-trainable params: 704
_________________________________________________________________

sample_image.reshape([-1, sample_image_height, sample_image_width, 3]);

reshape is not an in-place operator. You need to assign back the result to the variable sample_image or use another variable

const sample_image_reshaped = sample_image.reshape([-1, sample_image_height, sample_image_width, 3]);
model.predict(sample_image_reshaped)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM