Up your ML game with Google Colab

Shrinand Kadekodi
5 min readMay 24, 2021

--

Back when I was hunting for a new laptop, I was clear with one requirement. It had to have a GPU … and that too an NVidia one. Anyone who wants to work on Machine Learning projects, more specifically Deep Learning, needs to have a GPU in their device for those core hungry neural nets. And with CUDA and cudaNN, Nvidia was the choice of most. But the cost of such laptops are really high and these are not future proof as there will be continuous improvement in hardware which will keep on driving the prices high 😥.

I did get one with a weak GPU (MX150), it was better than nothing. But running some Image Processing stuff made me realize the futility of my GPU. The constant crashing and blue screen error made me realize that with the current GPU I would not progress much in Deep Learning. So searching the net for sites which gave free GPU I came across Google Colab. Free GPU for testing/running/understanding the ML/Deep learning algorithms 😊.

Google Colaboratory

Google Colaboratory or Colab for short allows you to write and execute code in your browser, with no configuration and most importantly free GPU resource. Google Colab uses Jupyter notebook for writing and executing the codes. You can share and import the codes from a variety of resources like github, drive or local files. Ideally for the free version one can run the GPU for 12hrs in Google Colab but a lot depends on the then VM resource availability.
Colab also allows execution of already written python codes. This can be done by uploading the code to Google drive and then using it in the Colab Jupyter notebook. Let’s see how we can do this step by step.

Upload resources

For the first step upload all the resources to your Google drive. This could include the code alongwith any files like image, text etc which the code may require. In my case I have uploaded the code along with the images that is required for the task.

Login To Colab

The next step is to login to Colab. This is pretty simple. Just login with the same account where the files were uploaded. Once logging in you will be shown a pop up which you can cancel for now.

Now go to the little files option seen at the left corner of the bar. Click on it and you can see some options at the top.

To get the drive in your environment click on Mount Drive option at the top. You will be asked to run a code and authorize the mounting process.

Allow Google to access the different information ( 🙄 ) and copy the authorization code. Paste the code in the box. Once done you can see the drive contents in your environment. Create a new file and click on the Mount Drive option. This time there is no need to do the authorization and the drive contents will be shown.

Change Runtime

To get the free GPU, first step is to go to the Runtime option of the Jupyter notebook and click on Change Runtime Type and select GPU. The below images will help in understanding this process.

Checking the GPU

Once this is done run the below command with which you can see the GPU allocated. For me it allocated the Tesla T4.

Install Dependencies

Its best to first install all the dependencies which is utilized in code. This is easily done by using below command. Do note that in Colab ‘!’ is used before for running any python command.

Running the File

The below command was used to run the file.

This is the code to detect faces in an image. To run this code a few modification had to be made in the path of images. Also another change made was for showing the image. Colab has some problem in using cv2.imshow() and the workaround is by running the below code:
from google.colab.patches import cv2_imshow
Also replacing the cv2.imshow() by cv2_imshow. This will display the image. But I was unable to display the image on the notebook 😥. But after copying the code to the notebook, I was able to see the output image with faces recognized. I tried running some small codes in the Jupyter notebook of Colab which ran just fine. So I changed the code to write an output image with which I can check the image 😅. The output can be seen below. This is the same code used for Face detection in this blog —
https://evrythngunder3d.wordpress.com/2020/07/24/exploring-python-modules-part-3/

or on medium — https://shrinandkadekodi.medium.com/exploring-python-packages-opencv-and-face-recognition-628bbcb686a3

With this we come to end of this post. Google Colab has definitely made Deep Learning much more accessible to learners. Using this resource will definitely help in increasing the knowledge of Deep Learning packages like tensorflow, keras etc.
I hope this post did give a small introduction to Google Colaboratory. Cheers!

References: A lot of googling amongst which stackoverflow.com and youtube

Originally published at http://evrythngunder3d.wordpress.com on May 24, 2021.

--

--

Shrinand Kadekodi
Shrinand Kadekodi

Written by Shrinand Kadekodi

Simply curious about everything!

No responses yet