# How to get high accuracy in CNNs?

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



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.

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

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.compile(loss='binary_crossentropy',optimizer=RMSprop(lr=0.0001),metrics=['accuracy'])


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.

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.

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

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.