3
$\begingroup$

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)
$\endgroup$
9
  • $\begingroup$ Can you please add your Keras code. and please check the shape of the input data before feeding it to the Keras model. $\endgroup$
    – ahmed
    Commented Jun 14, 2018 at 11:26
  • $\begingroup$ @ahmed I don't understand which part of code. And I checked all images using cv2 library, their size is 150x150 in both train and validation folders. $\endgroup$
    – Ankit Seth
    Commented Jun 14, 2018 at 11:30
  • $\begingroup$ Can you add a line that simply loads an image from disk and prints its shape? Also, could you include the code that created the model for which you show the summary()? $\endgroup$
    – n1k31t4
    Commented Jun 14, 2018 at 11:30
  • $\begingroup$ That must mean the model you create has not be initialised for the input layer or something. The first layer requires you to specify the input dimensions. Following layers can deduce other dimensions. $\endgroup$
    – n1k31t4
    Commented Jun 14, 2018 at 11:31
  • $\begingroup$ for input data can you please find the shape of train_generator? you can use the following line of code " train_generator.shape ". $\endgroup$
    – ahmed
    Commented Jun 14, 2018 at 11:35

1 Answer 1

4
$\begingroup$

[EDIT:] Your problem is definitely in the generators, in that you do not set the target size, and its default it (256, 256) - as seen in the documentation fro flow_from_directory:

flow_from_directory(directory, target_size=(256, 256), color_mode='rgb', ...)

target_size: Tuple of integers (height, width), default: (256, 256). The dimensions to which all images found will be resized.

Try setting the target size parameter to (150, 150) and I think it will work. That default seems to be overwriting your preprocessing.


It must be in your generators - I ran the following code and a model trained as expected:

from keras import models, layers, metrics
import numpy as np

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

In [10]: 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
_________________________________________________________________
In [17]: model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics = ['
    ...: acc',metrics.categorical_accuracy])

# Create some fake data to match your inputs. Each label seems to be 10 points: (1, 10)
In [11]: fakes = np.random.randint(0, 255, (100, 150, 150, 3))
In [24]: labels = np.random.randint(0, 2, (100, 10))

In [25]: model.fit(fakes, labels, validation_split=0.2)

Epoch 1/10
80/80 [==============================] - 2s - loss: 62.8913 - acc: 0.1125 - categorical_accuracy: 0.1125 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 2/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 3/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 4/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 5/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 6/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 7/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 8/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 9/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
Epoch 10/10
80/80 [==============================] - 0s - loss: 67.0916 - acc: 0.0000e+00 - categorical_accuracy: 0.0000e+00 - val_loss: 71.7255 - val_acc: 0.0000e+00 - val_categorical_accuracy: 0.0000e+00
$\endgroup$
1
  • $\begingroup$ Yeah, I just tried, it started to run smoothly. I thought it takes image size by default. Thanks for help. $\endgroup$
    – Ankit Seth
    Commented Jun 14, 2018 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.