Image Classification using CNN in Python
Here in this tutorial, we use CNN(Convolutional Neural Networks) to classify cats and dogs using the infamous cats and dogs dataset. You can find the dataset here
We are going to use Keras which is an open-source neural network library and running on top of Tensorflow.
Let’s start by importing the libraries needed.
# Importing the Keras libraries and packages from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense
Building the CNN
Here we use a very simple architecture:
- Fully Connected layer
We use Relu(Rectified Linear Units) as the activation function for both our convolutional layers.
We use Adam optimizer which is considered conventionally best for image classification by Andrew Ng in his Standford Course. And of course, we use binary-cross-entropy as our loss function because our problem is basically binary-classification and the metric used is accuracy.
Note: We aren’t using the latest version of TensorFlow which is why we are getting the warnings of some functions getting deprecated soon but don’t worry we can just ignore those for the time being!!
Below is our Python code:
#Initialising the CNN classifier = Sequential() # Step 1 - Convolution classifier.add(Convolution2D(32, 3, 3, input_shape = (64,64, 3), activation = 'relu')) # Step 2 - Pooling classifier.add(MaxPooling2D(pool_size = (2, 2))) # Adding a second convolutional layer classifier.add(Convolution2D(32, 3, 3, activation = 'relu')) classifier.add(MaxPooling2D(pool_size = (2, 2))) # Step 3 - Flattening classifier.add(Flatten()) # Step 4 - Full connection classifier.add(Dense(output_dim = 128, activation = 'relu')) classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) # Compiling the CNN classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
You might get some deprecation warning but we all know what to do with warnings.
Fitting the images to the CNN
We inculcate Data Augmentation for our training set which would make our training more generalized on the go.
from keras.preprocessing.image import ImageDataGenerator # Data Augmentation train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) test_datagen = ImageDataGenerator(rescale = 1./255) training_set = train_datagen.flow_from_directory('dataset/training_set', target_size = (64,64), batch_size = 32, class_mode = 'binary') test_set = test_datagen.flow_from_directory('dataset/test_set', target_size = (64,64), batch_size = 32, class_mode = 'binary')
Found 8000 images belonging to 2 classes. Found 2000 images belonging to 2 classes
Let’s start the training
classifier.fit_generator(training_set,# the training set samples_per_epoch = 8000, nb_epoch = 10,# number of epochs validation_data = test_set,# the test set nb_val_samples = 2000)
Epoch 1/10 250/250 [==============================] - 166s 663ms/step - loss: 0.6868 - acc: 0.5539 - val_loss: 0.6495 - val_acc: 0.6190 Epoch 2/10 250/250 [==============================] - 164s 657ms/step - loss: 0.6208 - acc: 0.6601 - val_loss: 0.5869 - val_acc: 0.6980 Epoch 3/10 250/250 [==============================] - 163s 653ms/step - loss: 0.5677 - acc: 0.7039 - val_loss: 0.5602 - val_acc: 0.7140 Epoch 4/10 250/250 [==============================] - 189s 756ms/step - loss: 0.5314 - acc: 0.7331 - val_loss: 0.5069 - val_acc: 0.7539 Epoch 5/10 250/250 [==============================] - 199s 797ms/step - loss: 0.5071 - acc: 0.7501 - val_loss: 0.4913 - val_acc: 0.7608 Epoch 6/10 250/250 [==============================] - 199s 797ms/step - loss: 0.4819 - acc: 0.7661 - val_loss: 0.5490 - val_acc: 0.7411 Epoch 7/10 250/250 [==============================] - 202s 810ms/step - loss: 0.4749 - acc: 0.7709 - val_loss: 0.4829 - val_acc: 0.7695 Epoch 8/10 250/250 [==============================] - 296s 1s/step - loss: 0.4524 - acc: 0.7889 - val_loss: 0.5021 - val_acc: 0.7741 Epoch 9/10 250/250 [==============================] - 267s 1s/step - loss: 0.4392 - acc: 0.7921 - val_loss: 0.4695 - val_acc: 0.7777 Epoch 10/10 250/250 [==============================] - 351s 1s/step - loss: 0.4238 - acc: 0.8039 - val_loss: 0.4583 - val_acc: 0.7888
So the accuracy of our neural network comes out to be 80%(training) and 78.8%(validation) which is pretty good considering its simplicity and also the fact that we only trained for 10 epochs.
So there you have it, the power of Convolutional Neural Networks is now at your fingertips. We did the image classification task using CNN in Python. I haven’t included the testing part in this tutorial but if you need any help in that you will find it here