简体   繁体   中英

ValueError: TensorFlow2 Input 0 is incompatible with layer model

I am trying to code a ResNet CNN architecture based on the paper by using Python3, TensorFlow2 and CIFAR-10 dataset. You can access the Jupyter notebook here .

During training the model using "model.fit()", after just one epoch of training, I get the following error:

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

The training images are batched using batch_size = 128, hence the training loop gives the following 4-d tensor which TF Conv2D expects- (128, 32, 32, 3).

What's the source of this error?

Ok, I found a small issue in your code. The problem occurs in the test data set. You forget to transform it properly. So currently you have like this

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

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

You need to do the same transformation on the test as you did on the train set. But of course, things you consider: no shuffling, no augmentation.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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