GhostDede
GhostDede

Reputation: 446

Keras Conv2D input Shape

I am using Keras for my CNN Model. In that model, I am training it using images. My images are in shape 256*256. However I trained it as 64*64. When I resize my images as 64*64 and trained again my accuracy rate was decreased drastically. What am I missing ?

When I arrange Convolution2D input shape as

classifier.add(Convolution2D(32,3,3,input_shape = (256,256), activation ='relu'))

It takes many time. Because of that I arranged my Convolution2D as classifier.add(Convolution2D(32,3,3,input_shape = (64,64), activation ='relu')) and I trained my first model. It predicting very well.

When I resized my input images shape as 64*64 and training with Convolution2D as

classifier.add(Convolution2D(32,3,3,input_shape = (64,64) 

my accuracy rate was decreased. What is the problem ?

Here is the code

classifier = Sequential()
classifier.add(Convolution2D(32,3,3,input_shape = (64,64,3), activation ='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.fit_generator(
        training_set,
        steps_per_epoch=8000,
        epochs=10,
        validation_data=test_set,
        validation_steps=800)

Here is the my reshape code

from PIL import Image
import os
path = 'TestForTrain2'
for file in os.listdir('TestForTrain2'):
    img = Image.open(os.path.join('TestForTrain2', file))
    width, height = img.size
    stringName = str(file)
    print(width," === ",height)
    print(stringName)
    f, e = os.path.splitext(path + file)
    imResize = img.resize((64, 64), Image.ANTIALIAS)
    imResize.save( stringName + '.jpg', 'JPEG', quality=90)

Upvotes: 2

Views: 2113

Answers (2)

Arthur D
Arthur D

Reputation: 161

Your model definition does not seem complete, it seems to miss at least a final Dense() layer to perform the actual classification. However, decreasing the input resolution by 4 in 2 dimensions will decrease your 'raw information' input by 16 (4ˆ2) which, will ultimately negatively impact your prediction accuracy.

With a lot less information given it is only logical your model can't predict classes as accurately as before.

Upvotes: 1

Sohaib Anwaar
Sohaib Anwaar

Reputation: 1547

When to preprocess: This may be image of preprocessing. We only preprocess data when needed because when we pre-process our data we are losing some information. If we dont pre-process our data in certain cases than algorithms may take time to process big values(not preprocessed data).

  1. We will pre-process data when we have less re-sources to train our model
  2. We pre-process data because their are very large and very-small values in our data. than we normalize it standardize it and get our data in some range. like (0,1)
  3. Their are many other reasons also to preprocess.

But we don't preprocess every data. You have first know the nature of data and than pre-process it.

Your Solution: Now you are pre-processing your data by resizing your images. By converting your image from ((256,256)) to ((64,64)) now when you have large size image their are more pixel values in your image and every pixel give some information to us. Now when you resize your image you have less pixels so that why less information that your model can use to classify. But when you don't resize your data your machine takes time to process your images. Now find some middle way by experimentation that which size you select which will give enough information to your model and machine will do less effort to process that. try (180*180) (164 *164) keep going down until your achieve your required accuracy according to the data.

Upvotes: 1

Related Questions