有人可以告诉我这里发生了什么事吗?
这是我的代码:
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
_________________________________________________________________
答案 0 :(得分:1)
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)