TF CNN Errors: logits and labels must be broadcastable; Input to reshape is a tensor with # values, but the requested shape requires a multiple of #

Viewed 95

I am building a CNN classification model in TensorFlow using Keras, and I am getting 2 different errors (how lucky am I?) when I try to fit my model, depending on small changes I make to parameters. I will detail those tweaks below, but first, here are the two errors I'm getting.

Error 1: logits and labels must be broadcastable: logits_size=[11,8] labels_size=[10,8]
Error 2: Input to reshape is a tensor with 7534080 values, but the requested shape requires a multiple of 683648
Note that the numbers in the second error change as I adjust filters, batch sizes, epochs in my layers.

Data overview: I am using an image dataset that includes 790 images in 8 categories. All images are in color with aspect ratio of 400x443. I've split them into train (590 images) and test (200 images) sets, which is organized as follows:
images/train/category_1
images/train/category_2
images/train/category_3 (etc. to 8)
images/test/category_1
images/test/category_2
images/test/category_3 (etc. to 8)

I also used ImageDataGenerator to create batches of 10 images for each set.

Below is my code.

Setting up Train and Test data:

# Path to training and testing directories:
train_path = 'images/train' 
test_path = 'images/test'

# Setting up batches of images to feed into model:
train_batches = ImageDataGenerator().flow_from_directory(directory=train_path, 
                target_size=(440,443), 
                classes=['category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'category_6', 'category_7', 'category_8'], 
                batch_size=10)

test_batches = ImageDataGenerator().flow_from_directory(directory=test_path, 
                target_size=(440,443), 
                classes=['category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'category_6', 'category_7', 'category_8'], 
                batch_size=10)

# Checking shape of image and label data:
train_images, train_labels = next(train_batches)
print(train_images.shape)       # Output: (10, 440, 443, 3)
print(train_labels.shape)       # Output: (10, 8)

Setting up CNN model:
This is where the error code changes as I adjust parameters. In the Conv2D layers, padding='same' returns Error 1 (logits vs. labels) while padding='valid' returns Error 2 (input to reshape), all other things remaining the same. I've also tried setting the input layer with batch_input_shape = (10,400,443,3) instead of input_shape = (400,443,3) and I still got the same errors.

model = Sequential()

model.add(Conv2D(filters=32,                  
                kernel_size=(3,3),           
                activation = 'relu',         
                padding = 'same',
                input_shape = (400,443,3)))  

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

model.add(Conv2D(filters=64,                  
                kernel_size=(3,3),
                activation = 'relu',
                padding = 'same'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Flatten())
model.add(Dense(units=8, activation='softmax'))

Model Summary:

Model: "sequential_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_34 (Conv2D)           (None, 400, 443, 32)      896       
_________________________________________________________________
max_pooling2d_34 (MaxPooling (None, 200, 222, 32)      0         
_________________________________________________________________
conv2d_35 (Conv2D)           (None, 200, 222, 64)      18496     
_________________________________________________________________
max_pooling2d_35 (MaxPooling (None, 100, 111, 64)      0         
_________________________________________________________________
flatten_17 (Flatten)         (None, 710400)            0         
_________________________________________________________________
dense_17 (Dense)             (None, 8)                 5683208   
=================================================================
Total params: 5,702,600
Trainable params: 5,702,600
Non-trainable params: 0
_________________________________________________________________

Compile and Fit Model:

model.compile(optimizer='adam', 
             loss='categorical_crossentropy', 
             metrics=['accuracy'])

model.fit(x=train_batches,
    steps_per_epoch=len(train_batches),
    validation_data=test_batches,
    validation_steps=len(test_batches),
    epochs=10,
    verbose=2)

It's at the fit step that I get one of the two error codes:
Error 1: logits and labels must be broadcastable: logits_size=[11,8] labels_size=[10,8]
Error 2: Input to reshape is a tensor with 7534080 values, but the requested shape requires a multiple of 683648

When padding for my Conv2D layers is set to 'same' I get the first error, and when I change it to 'valid' or leave it as default, I tend to get the second error.

I've read all the articles on StackOverflow related to these two errors and have tried a number of suggestions from other posts, but so far nothing has worked. Any help would be much appreciated!

1 Answers

I think it would be best to resize your images so that they are squares (i.e. 400x400).

Related