繁体   English   中英

ValueError:层 sequential_3 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 已收到完整形状:(无、224、256)

[英]ValueError: Input 0 of layer sequential_3 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 224, 256)

我是 Keras 的新手,我正在尝试创建一个将(224,256,1)大小的图像作为输入的 CNN。

这是我不断收到的错误:

    ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 224, 256)

我对该错误的解释是,该层获得的数据具有 3 个维度,而该层至少需要 4 个维度。 根据 keras 文档,输入形状应为(batch size, x, y, channels) 我只使用单个图像,因为我认为批量大小应该只是1

这是制作 model 的代码:

model = keras.Sequential([
                          keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding='same', input_shape=(224,256,1), data_format='channels_last'),
                          keras.layers.MaxPool2D(pool_size=(2,2), padding='same'), 
                          keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'),
                          keras.layers.MaxPool2D(pool_size=(2,2), padding='same'), 
                          keras.layers.Flatten(),
                          keras.layers.Dense(8, activation="softmax")
])

这是预测代码:

img = get_image()
img = convert_to_greyscale(img)
img = tf.expand_dims(img, axis=0) # add dimension to represent batch to the front
prediction = model.predict(img) # ValueError Input 0 of sequential_3 ...

如果您需要更多信息,请告诉我,谢谢!

您需要向图像添加one dimension ,然后像下面那样batch扩展expand_dims :(将图像调整为模型的大小)

from skimage import io
img = io.imread('1.jpeg', as_gray=True)[...,None]
img = tf.image.resize(img, [224, 256])
# ------------------------- ^^^  ^^^ this is size of your input model
img = tf.expand_dims(img, axis=0)
model.predict(img) 

Output:

array([[0.1329774 , 0.1408476 , 0.13449876, 0.10563403, 0.11976303,
        0.12162709, 0.12393728, 0.12071485]], dtype=float32)

暂无
暂无

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

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