Getting the most out of Google Colab

I'm pretty sure you've heard of Google Colab by now.

You haven't? Well, it's a free cloud-computer service that Google provides that offers a high-quality code editor in your browser. 




With that being said, let's dive into getting the most out of Google Colab. Here are five tricks for you to get the most out of Google Colab:

  1. Mounting your Google Drive
  2. Uploading/Downloading files
  3. Making tuning Hyperparamenters very easy using Form fields
  4. Running JS and HTML
  5. Using the Webcam (and then turning those images into NP arrays)

Mounting your Google Drive

Google Colab can be flaky sometimes and you can never know for sure when you will disconnect from your runtime. So, whenever you're training on a big model, the very best advice we can give is to back it up to your Google Drive. 




Mounting is very easy:

This will do the job for you; creating an authorization link that provides a code for you to paste to the terminal. After you do that, your Google Drive is mounted and ready!

!ls drive/My\ Drive

Here's an example to make it easier for you:

Imagine you're training a model in Keras and need to save it to your Google Drive. This is how it's done”

THIS WILL WRITE FILES TO YOUR DRIVE.




Uploading/Downloading files

This is the most straightforward tip we're going to cover today, but it's important nonetheless.

To upload a file or multiple ones, you're going to run:

And vice versa for downloading:

And that's it!

Making tuning Hyperparamenters very easy using Form fields

Manually changing the numbers for hyperparameters can be very irritating and confusing. It would be amazing if you could simply slide a scale or a dropdown instead!

Google Colab has thought of that. They give the following example:

Running this will build the following:

And you can only imagine how easy it is to tune something likes this:

Running JS and HTML

 

Since it's a web-based tool, it would make perfectly good sense if Google Colad could run JS and HTML. And, lucky for us, it does.
JS

display is a built-in function




Or, we can also transform the whole cell into JS using the %%javascript magic


And as soon as you hit the F12 and navigate to the Console tab, you'll see something like this:

Simple Python-to-JS Interfacing

js_code = \
'''
let message = {};
console.log(message)
'''.format(9+10)

HTML is tremendously easy to display in Google Colab.

All you have to do is to run something like this:

And there is the result:

Using the Webcam

Getting the most out of Google Colab also includes using the webcam.




The Google Colab provides the following function if you want to take a picture using the webcam:

Wich you'll later call using something like this:

It's perfectly fine if you don't understand it right away. And to be fair, my JS knowledge is not quite adequate to understand it perfectly either. Nonetheless, the function does create a video that will take a picture when the button is pressed. Then, it saves the picture to a file you can later use.

Clicking "Capture" on the top left takes the picture
Clicking “Capture” on the top left takes the picture

Converting to a Numpy Array

If you would like to use the function to give you a Numpy array immediately, you can use the following:

Which then you'll call as:

After taking the photo, you will get an NP array you can visualize
After taking the photo, you will get an NP array you can visualize

Taking a picture instantly

And, last but not least, you can take a picture immediately too. And you can do that without even having to wait for the user confirmation, and then convert it to an NP array.

All of this can be done using the following:




Which you can later call as follows:




If you want to learn more about Python we suggest taking a look at one of our previous articles.