Reputation: 213
I'm trying to create a CNN model for image classification, however, I'm getting an error with the input shape and I don´t understand why. Please, see below the code:
import pandas as pd
from keras_preprocessing.image import ImageDataGenerator
import numpy as np
#CREATING 3 DATAFRAMES FROM 3 .TXT FILES
trainingfile = pd.read_table('data/training.txt', delim_whitespace=True, names=('class', 'image'))
testingfile = pd.read_table('data/testing.txt', delim_whitespace=True, names=('class', 'image'))
validationfile = pd.read_table('data/validation.txt', delim_whitespace=True, names=('class', 'image'))
# CHANGING TYPE OF TARGET ATTRIBUTE
trainingfile = trainingfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])
testingfile = testingfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])
validationfile = validationfile.replace([0, 1, 2], ['class0', 'class1', 'class2'])
#DATA AUGMENTATION
datagen=ImageDataGenerator()
train_datagen = ImageDataGenerator(
rotation_range=5,
zoom_range=0.1)
#Generating train, test and validation datasets with RGB, Batch = 32.
train=train_datagen.flow_from_dataframe(dataframe=trainingfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb',batch_size=32)
test=datagen.flow_from_dataframe(dataframe=testingfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb',batch_size=32)
#No data augmentation to the validation set
validation=datagen.flow_from_dataframe(dataframe=validationfile, directory="data/", x_col="image", y_col="class", class_mode="categorical", target_size=(256,256),color_mode='rgb', batch_size=32)
And now is when I start designing the CNN model:
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Activation, Dropout, MaxPooling2D, BatchNormalization
from keras.constraints import maxnorm
#CNN model
model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3), activation='relu', input_shape=(32, 250, 250, 3)))
As you can see the input_shape is 32 (batch), 250 x 250 image size and 3 channels because of RGB. However, I get the following error:
ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5
Upvotes: 0
Views: 280
Reputation: 1235
The problem is the input_shape of the Conv2D layer, you don't have to set the batch size. Change input_shape=(32, 250, 250, 3)
to input_shape=(250, 250, 3)
.
Upvotes: 1
Reputation: 19310
The input_shape
in the convolutional layer should not include the batch dimension. Here's an excerpt from the Keras documentation:
When using [Conv2D] as the first layer in a model, provide the keyword argument
input_shape
(tuple of integers, does not include the sample axis), e.g.input_shape=(128, 128, 3)
for 128x128 RGB pictures indata_format="channels_last"
.
So the solution would be to change your model definition, as below. You had another error in the input_shape
-- it should be 256x256x3, not 250x250x3.
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(256, 256, 3)))
You do not need to specify the batch size explicitly in the model definition because it can vary.
Upvotes: 1