gpt4 book ai didi

python - Tensorflow js : Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), 但得到形状为 [100,120,3] 的数组

转载 作者:行者123 更新时间:2023-12-01 06:42:44 25 4
gpt4 key购买 nike

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

这是我的代码:

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

它会产生错误消息:

Uncaught (in promise) Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), but got array with shape [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)

关于python - Tensorflow js : Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), 但得到形状为 [100,120,3] 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59374748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com