Getting started with AI — Determining a dog’s breed from an image.

“The secret of getting ahead is getting started” ~Mark Twain

This post is by no means a tutorial, it’s more of – doing my bit in terms of spreading awareness about how simple and intuitive some of the recent machine learning and deep learning libraries are and how easy it his now to get started with it. If you’re looking for a tutorial I cannot recommend enough Jeremy Howard’s He has this amazing top down approach of teaching deep learning where he shows you how to do some cool stuff first and then goes into the details of why and how everything actually works. It’s more like if you want to learn driving a car, you’d start by being in the driver’s seat first, learning how the steering wheel works, how the brake and accelerator works and not by studying how the engine works or how an automatic transmission works.

In this post I am going to walk through a Kaggle’s playground prediction competition — determining the breed of a dog in an image.

There are several GPU cloud computing options available in the market (getting the right hardware to get started is no longer an issue now). I am going to be using AWS as I think it is the most flexible and also has the most powerful GPU available in the cloud. Also it has community image for which already has all the required software and libraries needed for this example. Paperspace is another slightly cheaper option. I’ll be using the AMI shown below. For the instance type I’ll choose a GPU compute p2.xlarge instance. It’s the cheapest option available with everything I’ll need for this post. This image is built on top of an Ubuntu image. The same setup can also be done on Google Cloud Platform or Azure.

If you created an instance using AWS, it should already have everything installed in order for you to complete the required operations.

SSH into the instance and navigate to the fastai directory, do a ‘git pull’ to make sure you have the updated repository. Also run ‘conda env update’ to update the environment with all the dependencies and libraries.

cd fastai
git pull
conda env update
conda update --all
source activate fastai
jupyter notebook

Open ‘Jupyter Notebook’ from the terminal, (you can also bind the port 8888 to the port 8888 of your local host using tunneling if using Putty, so you can open Jupyter Notebook in your browser.)

Create a new notebook, make sure the kernel version is selected as the desired Python version

Before we start we need the data to train and test against. Kaggle has a CLI tool which you can use to download this data

Install Kaggle CLI using the command :

pip3 install kaggle-cli

Once you have Kaggle CLI installed, download the data for the competition from Kaggle using :

kg download -u 'jairathore' -p 'password' -c 'dog-breed-identification'

The data will be zipped so make sure you unzip the data using unzip utility, which should be installed in your environment by default. Copy all the files to the ‘data’ directory (I have set the path to test and train data to this directory in my code.)

With that you should be all set to run the code. I have uploaded all the code here :


The first part of the code imports all the required libraries and set the path to our data to be used for this competition

We can open one of the images from our training data set and look what our data looks like :

I have used Facebook AI research’s Resnext 101 architecture for image classification. Fastai provides a great level of abstraction and building our initial model takes merely 4 lines of code. You can see below the model is already predicting with more than 90 percent accuracy. In the code further I have tried to further improve the model by adding augmentation i.e. horizontally or vertically flipping the images, zooming in /out to add more images to our training data.

This is it for this post, in the next post I’ll add more details related to how you can publish your results to Kaggle for the competition and will also walk through running the prediction on a single image to determine the breed of the dog in the image.

Until then — Happy Learning!
Please please reach out to me if you any questions or issues following the steps shown above.

Source: Deep Learning on Medium