繁体   English   中英

Tensorflow js:错误:检查时出错:预期 conv2d_13_input 有 4 个维度,但得到形状为 [100,120,3] 的数组

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

谁能向我解释这里发生了什么?

这是我的代码:

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

它产生错误消息:

未捕获(承诺)错误:检查时出错:预期 conv2d_13_input 有 4 个维度,但得到形状为 [100,120,3] 的数组

这是来自 model.json 的 conv2d_13 batch_input_shape 属性:

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

我想知道怎么了...

编辑:这是我的模型摘要:

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不是就地运算符。 您需要将结果分配回变量sample_image或使用另一个变量

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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