繁体   English   中英

Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer:

[英]Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer:

我对 CONV2D 层的 input_shape 有疑问。 正如官方文档所说:

4+D 张量,形状:batch_shape + (channels, rows, cols) 如果 data_format='channels_first' 或 4+D 张量,形状:batch_shape + (rows, cols, channels) 如果 data_format='channels_last'。

就我而言,我使用以下方法组装数据集:

tf.data.Dataset.from_tensor_slices()

细节是这样的。 首先,我将一堆图像转换为张量。 形状是:

(100, 128, 128, 3) -> in the format of (numbers of images, width, height, channels)

并且,标签的基本事实也被转换为张量。 在我的例子中,每个标签都是一个 155 长的带有 float32 的列表。 形状是:

(100, 155) -> in the format of (numbers of labels, labels)

然后,我使用上面提到的方法创建了数据集:

   dataset = tf.data.Dataset.from_tensor_slices((fetched_image_list, labels))

接下来,我在数据集中迭代了一个样本。

import pprint
pp = pprint.PrettyPrinter()
pp.pprint(f"---------Test----------")
iterator = iter(dataset)
data, label = iterator.get_next()
pp.pprint(data)
pp.pprint(data.shape)

结果是:

'---------Test----------'
<tf.Tensor: shape=(128, 128, 3), dtype=float64, numpy=
array([[[0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        ...,
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628]],

       [[0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        ...,
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628]],

       [[0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        ...,
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628],
        [0.        , 0.        , 0.01568628]],

       ...,

       [[0.94117653, 0.37254903, 0.36862746],
        [0.9571079 , 0.52573532, 0.44705883],
        [0.99215692, 0.86274517, 0.61960787],
        ...,
        [0.99607849, 0.80000007, 0.56078434],
        [0.99607849, 0.79460794, 0.55539221],
        [0.99607849, 0.79215693, 0.5529412 ]],

       [[0.94117653, 0.37254903, 0.36862746],
        [0.9571079 , 0.52573532, 0.44705883],
        [0.99215692, 0.86274517, 0.61960787],
        ...,
        [0.99607849, 0.80000007, 0.56078434],
        [0.99607849, 0.79460794, 0.55539221],
        [0.99607849, 0.79215693, 0.5529412 ]],

       [[0.94117653, 0.37254903, 0.36862746],
        [0.9571079 , 0.52573532, 0.44705883],
        [0.99215692, 0.86274517, 0.61960787],
        ...,
        [0.99607849, 0.80000007, 0.56078434],
        [0.99607849, 0.79460794, 0.55539221],
        [0.99607849, 0.79215693, 0.5529412 ]]])>
TensorShape([128, 128, 3])

但是,我提到了关于 CONV2D 的官方文档,其中输入应该是 (batch_shape, row, colts, channels) 或 (batch_shape, channels, rows, cols)。 所以我像这样配置了我的 CNN 模型:

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1), input_shape=(128, 128, 3), activation='relu'),
    tf.keras.layers.Conv2D(71, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1), activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 4), strides=(2,3), padding="same", dilation_rate=(1,1),activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 3), strides=(2,2), padding="same", dilation_rate=(1,1),activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 4), strides=(2, 3), padding="same", dilation_rate=(1, 1), activation='relu'),
    tf.keras.layers.Conv2D(128, (3, 3), strides=(2, 2), padding="same", dilation_rate=(1, 1), activation='relu'),
    tf.keras.layers.Dropout(0.20),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(155)
])

但我得到了这个错误作为回报:

    raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(128, 128, 3)

如果我将 input_shape 更改为 (32, 128, 128, 3)。 弹出了类似的错误:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 128, 128, 3), found shape=(128, 128, 3)

我认为问题存在于我创建数据集的方式中。

我该如何解决这个问题? 你的建议是什么?

您需要像这样为数据集设置批量大小

batch_size = 8 #Or some number
dataset = dataset.batch(batch_size)

如果您不这样做,您的图像会以(128,128,3)的形状进入模型,而不是 Conv2D 层所期望的(batch_size,128,128,3)

如果要一次发送 1 张图像,请将批量大小设置为 1

暂无
暂无

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

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