keras - 如何修复keras模型中的输入形状错误
问题描述
我尝试使用本教程执行语义分割: https ://github.com/nikhilroxtomar/UNet-Segmentation-in-Keras-TensorFlow/blob/master/unet-segmentation.ipynb 我修改了一点他的笔记本,但我成功地训练了50% 准确率的模型。
我已经尝试重塑输入数组,但它不起作用。这是代码:
test = X_train[0]
test.shape
>>> (480, 640, 4)
parallel_model.predict(test)
>>> ValueError: Error when checking input: expected input_3 to have 4 dimensions, but got array with shape (480, 640, 4)
这是模型:
def UNet():
f = [16, 32, 64, 128, 256]
inputs = keras.layers.Input((480, 640, 4))
p0 = inputs
c1, p1 = down_block(p0, f[0]) #128 -> 64
c2, p2 = down_block(p1, f[1]) #64 -> 32
c3, p3 = down_block(p2, f[2]) #32 -> 16
c4, p4 = down_block(p3, f[3]) #16->8
bn = bottleneck(p4, f[4])
u1 = up_block(bn, c4, f[3]) #8 -> 16
u2 = up_block(u1, c3, f[2]) #16 -> 32
u3 = up_block(u2, c2, f[1]) #32 -> 64
u4 = up_block(u3, c1, f[0]) #64 -> 128
outputs = keras.layers.Conv2D(4, (1, 1), padding="same", activation="sigmoid")(u4)
model = keras.models.Model(inputs, outputs)
return model
我知道这是一个菜鸟错误,但我真的很想解决它!
解决方案
Keras 使用“批量”,从不使用单个图像。
这意味着它需要 4 个维度(batch_size, 480, 640, 4)
。
如果您要使用单个图像进行预测,那么您需要带有 shape 的输入数组(1, 480, 640, 4)
。
test = X_train[0]
test.shape
>>> (480, 640, 4)
test = test.reshape((-1, 480, 640, 4))
test.shape
>>> (1, 480, 640, 4)
现在你可以了parallel_model.predict(test)
。
推荐阅读
- c - How to download graphics.h in devc++?
- html - How to save all embedded images of local html file in Windows
- html - Align text in ReactJS in a proper way
- python - Python sort nested lists
- database - Is it advisable to separate prime attribute partial dependencies when normalising?
- r - How to make inactive multiple line R code in a script
- r - How can I run all subset regression and get p-values of variables per each linear regression as dataframe using dplyr?
- ruby-on-rails - Pre-defined fields for nested attribute in nested form using checkbox
- node.js - TypeError: Cannot read property 'authenticate' of null
- javascript - trim() : unexpected behavior