2
$\begingroup$

ML and Data Science world.

I am a newbie to CNNs, but do possess a basic understanding of ML and Neural Networks.

I wanted to create my own CNN that works on the Cats and Dogs Dataset. I preprocessed the data and built my network, but when I fit the model with the data, I am not able to get more than 55% accuracy, which means the model isn't learning anything.

Can anybody explain what I am doing wrong here?

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3),padding='SAME', input_shape=X[0].shape))

model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))

model.add(Conv2D(filters=64, kernel_size=(3,3), padding='SAME'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))
model.add(Dropout(rate=0.4))

model.add(Conv2D(filters=128, kernel_size=(3,3), padding='SAME'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))
model.add(Dropout(rate=0.35))

#model.add(Conv2D(filters=64, kernel_size=(3,3), padding='SAME'))
#model.add(Activation('relu'))
#model.add(MaxPooling2D(pool_size=(2,2), dim_ordering='th'))

model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(rate=0.3))

model.add(Dense(2))
model.add(Activation('softmax'))

And this is the optimizer part:

opt = keras.optimizers.SGD(lr=0.0001, decay=0.0)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics['accuracy'])
print(model.summary())

model.fit(X, np.array(Y), validation_data=(test_x, np.array(test_y)), epochs=30, verbose=2)

I've been stuck on this for the past 2 days. Any and all help is appreciated.

Thanks.

$\endgroup$
  • $\begingroup$ it would be helpful if you would include what you have tried so far $\endgroup$ – oW_ Jun 25 '19 at 23:45
0
$\begingroup$

Adding multiple layers before pooling will increase the feature extraction, softmax function is used when there are more than 2 categories/classes, try with sigmoid fuction,

Also, Data Augmentation will help you with tuning hyper-parameters(tilt, width-shift, shear, rotation etc). Go through the documentation.

Give a try with this code, If RMSprop yieds less accuracy try adam,

model = Sequential()
model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=input_shape, activation='relu'))
model.add(Conv2D(32, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(Conv2D(256, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))    
model.compile(loss='binary_crossentropy',optimizer=RMSprop(lr=0.0001),metrics=['accuracy'])
$\endgroup$
0
$\begingroup$

I have a few suggestions to improve your accuracy (these are hardly original with me):

  1. Use an ImageDataGenerator from keras.preprocessing.image to augment your data.
  2. Use k-fold validation to use your training data more thoroughly.
  3. Try the keras.optimizers.Adam() optimizer.
$\endgroup$
0
$\begingroup$

You can check this great article Link. I think you you will get a good intuition. Although this link gives you very generalize ideas for improving the overall model, it would be a great help for building a good model in the future.

$\endgroup$
0
$\begingroup$

Following things you can do to improve accuracy

  1. Perform data augmentation check this https://keras.io/preprocessing/image/
  2. Transfer learning is also a good approach in the case of availability of the large pre-trained models. In your case, you can try VGG16, Inception V3, ResNet52 & others. Refer this article for the same https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
$\endgroup$
0
$\begingroup$

Here is the same dataset examined in a Udacity course. Based on your code you can try the following:

Your output is softmax categorization, so you might want to try sparse categorical cross entropy as output instead of binary cross entropy.

Also, is there a reason you are using SGD with a much lower learning rate compared to default parameters? As suggested before, try adam optimizer with default settings.

And finally, you can also try different paddings for the convolutional layers.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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