Want to build and train a Deep Learning model to achieve state-of-the-art result in classifying cats and dogs? You are at the right place!
You DO NOT need any prior math knowledge to follow along with me. Just high school math and a little bit of experience in python programming are more than sufficient! I will be taking you through every step to make and train your first model.
1. Setup
For the sake of simplicity, we shall be running our model on a cloud GPU (What is a GPU?). You can check out the blog below to understand the importance of GPU’s in Deep Learning.
There are a lot of services that provide free as well as paid instances of cloud GPU’s. We shall be working with Gradient, which offers a Free Tier of GPU and CPU instances. We shall also be using fast.ai’s vision library to create our model.
- First, click here to create an account on Gradient
- Login to your account and select ‘Gradient’
- Click on ‘Notebook‘
- Give your instance a name(Optional)
- Select the Paperspace + Fast.AI base container

- Choose any of the free GPU instances

- Click on Create Notebook

Your Notebook will go from Pending →Provisioning → Running!
- Click on ‘OPEN V1(CLASSIC)’
For this model, we shall be scraping images off Bing and DuckDuckGo, which will serve as our dataset. We shall install the jmd_imagescraper library by Joe Dockrill, which will get the job done for us.
- Click on New →Terminal.

Type and click enter:
!pip install -q jmd_imagescraper
For more information on the library you can check out its official page:
That brings us to the end of the setup. Close the terminal and open the notebook once again. Time to code!
Jupyter Notebooks
Jupyter Notebook is a web application that allows you to create documents that contain live code, equations and text. You can do anything from writing code to publishing books and building a stand-alone web application using Voilà! For more information on working with Jupyter notebooks, you can click here. We shall be writing and executing our code on Jupyter notebooks provided by Gradient.
Jupyter notebooks are composed of numerous cells. You can control your workflow by deciding the order in which you execute cells.
Click on New →Python 3 to open a new notebook.

You can write text(in Markdown cells) or write python code(in code cells)and execute them like this:

Play around until your comfortable with it. Use the Run and Add cells option to execute your code and add more cells respectively.


2. Code
Let us begin the fun stuff, shall we? You can copy each code block onto different cells and run them along with me.
Let us first import fast.ai’s vision library and jmd_imagescraper.
Since we aim to classify images of cats and dogs lets go ahead and make a folder called ‘animals‘ to which we can download and save our images.
The last line sets the path variable to the ‘animals‘ folder in the current working directory.
Call for the _duckduckgosearch() function from jmd_imagescraper library that takes in the following as input:
- download directory(path variable assigned above)
- folder name(cat)
- keyword to search on DuckDuckGo("cats")
-
number of images to download(100)
Wait until it searches for the images, download and saves them directly to a folder named ‘cat‘ in ‘animals‘. The result should look something like this:

Let us now do the same for images of dogs.
Often there can be unrelated images which would hamper our training. It is better to go through individual files and delete the ones that are not cats or dogs. Jupyter provides an interactive GUI to delete the unrelated images efficiently.

Switch between folders and delete unrelated images. I found a few images like these that had snuck its way into cats and dogs.

Look for failed downloads and unlink them from the folder
Let us now create a Datablock object that sends the images to a DataLoaders class. The DataLoaders class converts the given data into pieces of information that the Learner can interpret.
Intuitively the Learner is the one that looks at all the given images and finds a pattern. Do not worry if you come across complicated keywords. Everything will start making sense once you run the model.
It is important to note that on line 4, we are reserving 20% of the data to check our model at the end of each training iteration called an epoch.
Create a DataLoaders object called dls.
Take a look at a few images from a single batch in the dls object.

Call for a Learner that learns based on the input images in 4 different training iterations or epochs. This should take some time depending on your network speed.

You will probably not see the same results that are in the image, but the error rate should be well less than 0.05.
Now, let us plot a confusion matrix for our validation set. Confusion matrix tells us how many of the images in the validation set were correctly or incorrectly classified.

The confusion matrix speaks about how good our Classifier is. The numbers indicated on dark blue diagonal tells us the number of correctly predicted images. Hence our Classifier is doing a pretty good job!
We shall get to see the two incorrectly predicted pictures in the next line of code.

Now let us put our model to the real test, shall we? Download a cat or dog image of your choice and save it locally.
This would create a widget inside your Jupyter notebook that looks something like this:

Click on upload and choose your image.
Now, let us resize the image so that our model can predict. The last line displays the resized image.

The following code predicts the animal in the image and displays it in a format that we can understand.
……………….

YES!!!
With just ~200 images, we were able to create a model that could classify cats and dogs. Now you can go ahead and make changes to create your classifier! You can also work with three or more categories.
Hope you find this post helpful. If you get stuck, please feel free to comment or contact me by mail at [email protected].
Notebook on GitHub:
fast.ai Courses
All thanks to fast.ai courses, forum and the entire community and its efforts to make Deep Learning available for everyone.
Special thanks to Jeremy Howard.
Keep clapping!