ValueError:TensorFlow2输入0与图层模型不兼容

2024-09-28 03:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用Python3、TensorFlow2和CIFAR-10数据集,基于paper编写ResNet CNN体系结构。您可以访问Jupyter笔记本here

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

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-d张量-(128、32、32、3)

这个错误的来源是什么


Tags: 数据modelhere体系结构错误笔记本jupytercnn
1条回答
网友
1楼 · 发布于 2024-09-28 03:14:54

好的,我在你的代码中发现了一个小问题。问题发生在测试数据集中。你忘了正确地变换它。那么现在你有这样的

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]))

相关问题 更多 >

    热门问题