The world’s leading publication for data science, AI, and ML professionals.

End to End Adaptation of ResNet in Google Colab – Part 2: Hardware & Dataset Setup

Evaluating your hardware – CPU and GPU, and setting up your dataset

End to End Resnet

CPU, GPU, Dataset Setup

Source: Unsplash.com
Source: Unsplash.com

I hope you all had a chance to preview Part 1 of this series which did not require any knowledge of python, just a Google and Kaggle account (the latter only to authenticate yourself so you can download the dataset.)

As a quick overview, we were able to train the dog vs cat dataset (25,000 total images) on a ResNet-18 architecture. Once it was done training, you were able to upload a picture of your favorite cat or dog and observe the output.

I will split this series up into 5 parts:

  1. Introduction
  2. Hardware & Dataset Setup
  3. Image Pre-processing
  4. Architecture Training
  5. Real World Testing

An Aside

Once you’re done training, what happens when you feed in a picture of an owl?

Source: Unsplash.com
Source: Unsplash.com

Output is ‘dog’! Only 78.41% sure though. (If you are having problems with the last cell, just rerun it using the arrow on the left side.)

I have inserted everything from items to pictures of my friends (who look a little bit like cats) and the results are all over the place. Sometimes, the probability is in the high-90s.

I’m going to emphasize the point if it isn’t clear already – you must know the underlying data that the network was trained using, else your predictions will be unreliable and depending on what you’re using it for, dangerous. The better you know this data, the more informed your understanding of the output.

Not to use use the ‘AI’ buzzword but if I was going to describe what we’ve built, I’d call it ‘Narrow-AI’. This is not Artificial General Intelligence (AGI). We can only discern between dogs and cats here.

Library Import

These are the libraries you need to run the entire program. ‘torch’ is PyTorch (similar to Tensorflow, a machine learning library). ‘torchvision’ contains the most popular models and datasets as well as image transformations (to be discussed later). ‘matplotlib’ lets use create graphs and tables of images.

When you see a ‘from ABC import XYZ’, we are trying not to type in ABC.XYZ everytime and can just use XYZ. For example:

Now we can just use the ‘listdir’ command instead of typing ‘os.listdir’. This is a trivial example but condensing something like these following lines helps to simplify our code:

You will see at the end of the library import cell is:

Think of the ‘!’ as passing a command to the command line, instead of the python kernel so you can install libraries you need, like ‘kaggle’ (lets us download the dataset).

CPU, Memory, and GPU

I had mentioned the high end CPUs and GPUs available in Google Colab.

This passes a command to the command line to obtain information about the CPU and ‘grep’ (Global Regular Expression Print) searches plain text data. If you run ‘!lscpu’ you will get a long output list but we just want to know how many CPUs, the model, and GHz.

Similar to above, ‘!cat /proc/meminfo’ gives a long output list, but we just want to know total RAM (hence grep MemTotal)

CUDA (Compute Unified Device Architecture) was designed by Nvidia to let programmers like us access CUDA enabled GPUs to speed up our mathematics (in this case, image analysis). There are plenty of posts that discuss how this works in varying levels of detail.

Note we are using the pytorch library through ‘torch.cuda.is_available()’ which outputs a boolean (True vs False). If True, we can also use the pytorch library to get the name of the device.

This device name changes from time to time depending on what is available through Google Colab (and sometimes you can’t get a GPU at all since you’ve used up your quota for the day). This morning it was a Tesla P100. Sometimes I get a superfast Tesla T4 (for free!).

Obtaining the Dataset

The ‘kaggle.JSON’ file you downloaded is imported in this step using:

If you refer to the library import section, you will see this line:

The ‘files.upload()’ lets you upload your kaggle.JSON.

The commands above are all passed to the machine command line instead of the python kernel. A directory called ‘kaggle’ is made and the kaggle.json file you uploaded is copied into the newly made directory.

‘chmod 600’ lets the user read and write the kaggle.json file. This is important as the kaggle library uses the JSON file to authenticate you to download the dataset called ‘dogs-vs-cats’.

Finally, unzip (quietly, with -q) but also overwrite (-o) in case you already downloaded and unzipped it.

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

References

[1] Deep Learning with Pytorch, Accessed October 2020

[2] Neural Networks with Pytorch. Accessed October 2020

[3] Transfer Learning for Computer Vision. Accessed October 2020

[4] Kaggle API. Accessed October 2020


Related Articles