I am working on multiclass classification of images. For this I created a CNN model in keras. I already pre-processed all images to size (150,150,3). Here is model summary-
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 146, 146, 32) 2432
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 71, 71, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 33, 33, 64) 36928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 128) 73856
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 6272) 0
_________________________________________________________________
dense_1 (Dense) (None, 300) 1881900
_________________________________________________________________
dense_2 (Dense) (None, 10) 3010
=================================================================
Total params: 2,016,622
Trainable params: 2,016,622
Non-trainable params: 0
I am also using data augmentation and flow_from_directory
method-
train_datagen = image.ImageDataGenerator( rescale = 1./255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')
test_datagen = image.ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory( new_train, batch_size=20)
validation_generator = test_datagen.flow_from_directory( new_valid, batch_size=20)
Then I compile the model and run fit_generator
-
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics = ['acc',metrics.categorical_accuracy])
history = model.fit_generator( train_generator, steps_per_epoch=100, epochs=10, validation_data=validation_generator, validation_steps=50)
At this part I get error-
ValueError: Error when checking input: expected conv2d_1_input to have shape (150, 150, 3) but got array with shape (256, 256, 3)
I don't understand when all input images have size (150, 150, 3), how can it get (256, 256, 3)? Please tell me where I am going wrong.
EDIT
The code with which I created model is-
model = models.Sequential()
model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(300, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
For image preprocessing, I used following-
for image_name in os.listdir(train_dir):
im = cv2.resize(cv2.imread(os.path.join(train_dir,image_name)), (150, 150)).astype(np.float32)
if image_name in validation_img:
cv2.imwrite(os.path.join(new_valid,image_name), im)
else:
cv2.imwrite(os.path.join(new_train,image_name), im)
cv2
library, their size is 150x150 in both train and validation folders. $\endgroup$summary()
? $\endgroup$