ValueError: TensorFlow2 Input 0 is incompatible with layer model

ValueError: TensorFlow2 Input 0 is incompatible with layer model

我正在尝试编写基于 paper by using Python3, TensorFlow2 and CIFAR-10 dataset. You can access the Jupyter notebook here 的 ResNet CNN 架构。

在使用“model.fit()”训练模型的过程中,经过一个 epoch 的训练,我得到以下错误:

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

训练图像使用 batch_size = 128 进行批处理,因此训练循环给出了 TF Conv2D 期望的以下 4 维张量 - (128, 32, 32, 3)。

此错误的来源是什么?

好的,我在您的代码中发现了一个小问题。问题出现在测试数据集中。你忘了正确地转换它。所以目前你有这样的

images, labels = next(iter(test_dataset))
images.shape, labels.shape

(TensorShape([32, 32, 3]), TensorShape([10]))

您需要对测试进行与对训练集相同的转换。但是,当然,你要考虑的事情:没有洗牌,没有扩充。

def testaugmentation(x, y):
    x = tf.image.resize_with_crop_or_pad(x, HEIGHT + 8, WIDTH + 8)
    x = tf.image.random_crop(x, [HEIGHT, WIDTH, NUM_CHANNELS])
    return x, y

def normalize(x, y):
    x = tf.image.per_image_standardization(x)
    return x, y

test_dataset = (test_dataset
        .map(testaugmentation)
        .map(normalize)
        .batch(batch_size = batch_size, drop_remainder = True))

images, labels = next(iter(test_dataset))
images.shape, labels.shape
(TensorShape([128, 32, 32, 3]), TensorShape([128, 10]))