How to Train Your First Deep Learning Model

Create your own cat/dog classifier in no time!

Tee Yee Yang
The Startup
4 min readSep 26, 2020

--

If you are trying to learn about Deep Learning today, there are tons of online courses, books and material for that. Then, something like this appears in the very first lesson:

Part of the backpropagation equations

Deep Learning is at it’s heart a data-analysis technique, thus the underlying concepts are definitely math-intensive. However, these complicated equations and formulas are really stressful to look at if we are just trying to learn something new! (Especially if we do not have PhDs in Math or Computer science. Or the last time we did integration was 10 years ago in school.)

This post will be the first part in a series where I introduce basic concepts of Deep Learning (DL), based on the fast.ai course. Fast.ai teaches DL using the top-down approach: which means showing students what they can do and providing hands-on experience from the start, then moving on to explain the underlying concepts. This is largely different from typical online courses we see, where knowledge is built ground-up, starting from the underlying concepts.

Getting Started

This will be an interactive post using Google Colab notebooks. If you have not used Google Colab before, there is a quick-start tutorial at tutorialspoint. You can access the notebook at this link: Train your first DL model. First, make a copy and save it into your Drive so that you can access it and make changes. Next, make sure the runtime is set to GPU so you can make use of the free resources provided by Google.

Running the first code block will install the fastbook package onto the Google Colab environment. Don’t worry, nothing will be installed onto your computer!

Running the second code block will perform a bunch of things:

  1. Images from the Oxford-IIIT Pet Dataset that contains about 7,000 images of cats and dogs from 37 breeds will be downloaded and stored onto the Google Colab environment.
  2. A DataLoader object will be created, and this will feed the images to your model for training. Next, a few random images from the DataLoader will be presented in the output. This is one of the advantages of working in a notebook environment; we can easily look at our data as we work!
Samples from the Oxford-IIIT Pets Dataset. Available under CC License.

The third code block performs the actual training of the model. You will see a progress bar as this happens, and it should be completed in about 2 minutes if you selected the GPU runtime properly. There are many terms introduced such as loss and epochs, but these will be covered in future lessons. For now, just focus on the error rate: a value of 0.0054 means that only 0.54% of images were wrongly classified, that’s pretty good for just 2 minutes of training!

Results of model training
Photo by Jae Park on Unsplash

The final code block will create an interactive application within the notebook itself. Here you will see two buttons: Upload and Classify. I downloaded a picture of a cat on the left from Unsplash and uploaded that onto the application. Afterwards, click on classify so that your model can predict what animal it is. The prediction will be printed below the image, along with the probability. Do try it out with your own images of cats and dogs!

Conclusion

In just 30 minutes, you have created your own cat/dog Deep Learning classifier! Even if you did not understand any of the code, at least we were able to create a fully functioning and interactive application. Hopefully, this will deepen your interest in Deep Learning and its limitless functions!

--

--

Tee Yee Yang
The Startup

PhD Candidate at Nanyang Technological University, Singapore