Beware of the Lions: Build a Simple Image Classifier using Google’s Colab — Part 1

Alessandro Scoccia Pappagallo
Unkempt Thoughts
Published in
4 min readAug 5, 2018

Humans are incredible pattern-recognition machines. Scientists believe modern human beings are so extraordinary at image processing because quickly recognizing predators granted us a phenomenon advantage in terms of natural selection.

Imagine for example you’re one of the first homo sapiens, you’re taking a nice stroll through the Kenyan Rift Valley when all of a sudden you spot a yellowish figure between the bushes. What do you do?

This is a typical image classification problem. If the figure belongs to a predator, e.g. a big cat, then making a mistake will likely cost you your life, which means you’re out of the genetic pool (unless you had kids already). On the other hand, running every time a bush rustles is the perfect recipe for a life of anxiety and discomfort — not to mention the fact you may be missing on some sort of reward by failing to investigate.

Assume a yellowish body moving between the trees may end up being:

Okay, if it’s A and you make a mistake you’re dead. But if it’s B and you make a mistake, you’ll lose on plenty of cuddling and playing time and that may be worse (I have done the math, it isn’t).

Now, how difficult could it be to train a classifier to distinguish between real and fake lions? Let’s give it a shot.

First, we’ll need the data. I created a simple dataset with 140 images of real lions and 140 images of fake ones. Not many, but enough to prototype, especially because we’re going to use data augmentation to produce different copies of each image. The dataset is available for download here.

The best ML tool for image classification is a Convolution Neural Network (CNN; more info here). We’ll use Keras for the actual implementation as it offers an intuitive but powerful API.

The code for the CNN will look something like:

(If you, like me, hate to read code in chunks do not despair: at the end of the post you’ll find a link to the whole pipeline in Colab.)

To feed the images to the classifier we can use Keras’ ImageDataGenerator, which also incorporates many data transformation techniques.

Now, even though our dataset is fairly small and our CNN very shallow, to train a neural network is always a slow business (our simple model still has 10,000,000+ parameters to optimize). Having access to an NVIDIA graphics card would be handy as we could save a lot of time by using the GPU for computation.

There are multiple cloud services offering GPU time but almost all of them come with a cost and I didn’t want to spend a dime for a lion classifier (as the quality of my life, contrary to the one of the first homo sapiens, depends very little on my ability to detect predators).

On the other hand, Google Colab (disclaimer: the company I currently work for) offers free access to an NVIDIA Tesla K80 GPU and an intuitive Jupyter-like cloud interface. Google Colab wins.

Screenshot of one of my Colab notebooks.

Okay, we’re ready to train our CNN, let’s see what the results look like.

Accuracy for train and validation tests during training.

Okay, not too bad. With a simple architecture, only 40 epochs and no hyper-parameter optimization we got an accuracy of 82.5% on the test set, actually better then the best result on validation set during training (see graph above; this is the kind of weird that comes with working with few data points).

It’s also clear the model starts to overfit at around epoch 20 (accuracy goes up for the train set but not for the validation set).

Overall, Colab’s GPU worked fairly okay and even though I had to restart the runtime a few times I could process an epoch in around 3 seconds, which is much faster than on my personal computer. The main issue with Colab was the fact that from time to time I would run into GPU memory issues (ResourceExhaustedError). The kind of problems that comes with free.

Here you can find my Colab, which contains all my CNN code plus a few cells related to Colab’s magic functionalities (e.g., how to use Google Drive to retrieve and save files directly from Colab).

In the second part of this two-part post we’ll use Hyperopt to improve on the current results and we’ll have a look at the misclassified images.

You can read the second part here.

Post Scriptum

Somehow unrelated but I felt the need to clarify that for as much as we know modern domesticated dogs only appeared around 14,000 years ago so the likelihood of one surprising you between the bushes 300,000 years ago was slim at best.

--

--

Alessandro Scoccia Pappagallo
Unkempt Thoughts

T&S Manager @ Google | ML Enthusiast | People think that the human brain is in the head. Nothing of the sort; it is carried by the wind from the Caspian Sea.