Two Class Image Classifications Using Convolutional Neural Nets

Emily Jaekle
Deep Learning Data 2040
4 min readMay 11, 2018

https://github.com/ejaekle/deeplearning

Based on the notebook from chapter 5.2 of “Deep Learning with Python” we can use convolutional neural nets to perform binary classification on images. The original notebook uses cats vs. dogs. We will try use images from The Simpsons dataset (available on Kaggle). Using the following methods you can create a dataset on your own (although this is specific to Mac users).

The Simpsons dataset on Kaggle contains folders with images of all the most popular characters from The Simpsons. I downloaded the entire dataset and decided to use the images of Homer and Marge. To do this with another set of photos you want to make sure you have at least a few hundred photos, I chose Marge and Homer because they both had over 1000 photos available.

Once I had chosen which two sets of photos to use I needed to relabel the images for the code. In order to do this I selected all the images in the folder (you can do this on a Mac by pressing Command and A) I then right clicked and selected the Rename option. For both Homer and Marge I did this to relabel all the images to have the format homer_#.jpg with the numbers starting at 0.

Rename Options Window on Mac

Once I had done this for each folder (Marge and Homer) I combined the folders so I would have one folder with all the images labeled homer_#.jpg and marge_#.jpg.

Now I could use the code from chapter 5.2 of “Deep Learning with Python” to create my model and classify the images. You could follow the above steps with almost any image dataset to create your own two class dataset.

The code then splits the data into three groups: training, validation and test for each of the two classes (for a total of six groups). Since I had just over 1250 images for each of my two classes I made the training set 500 images and the validation and test sets 250 images each.

Next the images were rescaled before we used the convnet.

The model summary for the convnet I used is the following:

I then used this with 30 epochs and 100 steps per epoch on the data. The code outputs a summary for each epoch.

Sample from the output of the model at each epoch

I then saved the model to plot the accuracy and loss for the training set and validation set.

The validation accuracy is quite high, at around 95%. The loss however is not as good as it could be, since it seems to end up around 0.4. In order to improve these results we can try augmenting the images.

To augment images there are many options such as rotation, width and height shifts, flipping the image, and shearing transformations. Here is an example of one of the images:

I then used the following model:

Model Summary

I then used this with 30 epochs and 100 steps per epoch on the data. The code outputs a summary for each epoch.

Sample from the output of the model at each epoch

Finally I saved the model and plotted the training and validation accuracy and loss.

Loss and Accuracy Plots

These are much better results than our previous model, it seems like the transformation of the photos really helped. The accuracy is still around 95% but the loss has decreased to around 0.1 which is much better than it was before.

--

--