End to End Adaptation of ResNet in Google Colab — Part 1

Original article was published by Dev Dash MD MPH on Deep Learning on Medium

End to End Adaptation of ResNet in Google Colab — Part 1

Train a deep neural network with only a few clicks

From Markus Spiske, Source: Unsplash.com

The code example I’m providing in this piece is a result of hours of frustration and sleuthing around on the internet.

When I first started teaching myself about deep neural networks, I wanted to find something I could readily download, plug and play and run — and this was hard to find. Borrowing from Jeremy Howard (again), I really appreciate how he starts out with a few lines of code to demonstrate that training a deep neural network shouldn’t be daunting. I wanted to do something similar with Google Colab.

I really wanted to experience how long it took to train, what the process looked like, and what the entire codebase of an ‘end to end’ network looked like, one that did the following four things:

  1. Downloaded and set up a deep neural network
  2. Obtained a dataset and formatted it for training
  3. Trained the neural network along with validation
  4. Was ready to accept an input for testing

Enter Google Colab. I will not belabor further without providing you this:

Cat & Dog Classifier using Resnet-18

You do not need to know any Python to run this, you just need two things — 1) A Google account so you can start the notebook and 2) A Kaggle account.

This code downloads a dataset of thousands of images of dogs and cats (labeled), runs it through a ResNet-18 architecture for training and once it’s done, provides you a way to easily test the network.

Let’s get started.

1. Get your Kaggle API JSON

First, log into Kaggle and click on your icon in the top right and go to ‘My Account’. Scroll half way down to the section titled ‘API’ and click on ‘Create New Token’. This will download a JSON file titled ‘kaggle.json’ (hopefully to your downloads folder).

2. Start the Notebook

See the screen shot below — make sure when you click ‘Change runtime type’ that GPU is selected, then click save. Then select ‘Run all’.

Runtime menu on Google Colab. Uploaded by author

3. Upload your Kaggle JSON

I have tried to make this as easy as possible. Scroll down a bit and observe that the code is waiting for you to upload the JSON file you downloaded in Step 1.

Click “Choose Files” and select your kaggle.json file. This authenticates you so you’re able to obtain the dataset.

Code is waiting for your Kaggle JSON file. Uploaded by author

4. Scroll all the way down

I say this not in a facetious sense, but once you upload the JSON file, the code starts moving quickly and has likely blown past where you are on the page and has probably started training, looking like this:

Model training in progress. Uploaded by author

This will take a little bit of time but not that much, it’s probably running on a high end GPU built for tasks like this.

Wait for the code to finish training. It will output a line like this:

‘Training complete in 4m 51s’
Best val Acc: 0.989370

5. Ready for a picture of a dog or a cat

Again, similar to how it was waiting for the JSON file above, the code is now waiting for you to upload a picture. Since it is only trained on dogs and cats, I would recommend picking one of these animals to test it out. Any other animals may provide aberrant but certainly unreliable results.

Click ‘Choose Files’ to select your picture.

I found this great picture of a cat on Encyclopedia Britannica:

Source: https://www.britannica.com/list/the-10-best-types-of-cat
Success! Uploaded by author

A Few Notes

I definitely glazed over some details about the code above as I wanted to show you this profoundly complicated process was accessible.

The beauty of Colab is that you don’t need a fast internet connection, or a high end GPU or significant amounts of memory to learn learn about the inner workings of a deep neural network.

We trained the model above (ResNet-18) on Intel Xeon processors and a NVIDIA graphics card. To the vast majority of the population around the world, this hardware is not locally accessible as they are cost prohibitive (thousands of $).

We trained the model above with a dataset that was almost 1Gb in size, which took the Colab servers mere seconds to download. The time and bandwidth required to run this locally is also not accessible to most people worldwide.

Teaching yourself about deep neural networks should not be hampered by the technology you have in front of you and you shouldn’t need to imagine in your head how this is done on hardware.

With the Colab notebook I provided, you can run this with a minimal internet connection and with a basic computer.

But this is only part 1! I hope you join me over the next few weeks as we will unpack this code in methodical steps.

Please leave any questions below and I will try to answer them as best I can.