Getting started with Google Colab

(This article first appeared on Towards Data Science)

Photo by FuYong Hua on Unsplash

You know it’s out there. You know there’s free GPU somewhere, hanging like a fat, juicy, ripe blackberry on a branch just slightly out of reach.

Beautiful lightning-fast speed waiting just for you.

Wondering how on earth to get it to work? You’re in the right place!

Photo by Breno Machado on Unsplash

For anyone who doesn’t already know, Google has done the coolest thing ever by providing a free cloud service based on Jupyter Notebooks that supports free GPU. Not only is this a great tool for improving your coding skills, but it also allows absolutely anyone to develop deep learning applications using popular libraries such as PyTorchTensorFlowKeras, and OpenCV.

Colab provides GPU and it’s totally free. Seriously!

There are, of course, limits. (Nitty gritty details are available on their faq page, of course.) It supports Python 2.7 and 3.6, but not R or Scala yet. There is a limit to your sessions and size, but you can definitely get around that if you’re creative and don’t mind occasionally re-uploading your files…

Colab is ideal for everything from improving your Python coding skills to working with deep learning libraries, like PyTorchKerasTensorFlow, and OpenCV. You can create notebooks in Colab, upload notebooks, store notebooks, share notebooks, mount your Google Drive and use whatever you’ve got stored in there, import most of your favorite directories, upload your personal Jupyter Notebooks, upload notebooks directly from GitHub, upload Kaggle files, download your notebooks, and do just about everything else that you might want to be able to do.

It’s awesome.

Working in Google Colab for the first time has been totally phenomenal and pretty shockingly easy, but it hasn’t been without a couple of small challenges! If you know Jupyter Notebooks at all, you’re pretty much good to go in Google Colab, but there are just a few little differences that can make the difference between flying off to freedom on the wings of free GPU and sitting at your computer, banging your head against the wall…

Enter a caption

This article is for anyone out there who is confused, frustrated, and just wants this thing to work!

Setting up your drive

Create a folder for your notebooks

(Technically speaking, this step isn’t totally necessary if you want to just start working in Colab. However, since Colab is working off of your drive, it’s not a bad idea to specify the folder where you want to work. You can do that by going to your Google Drive and clicking “New” and then creating a new folder. I only mention this because my Google Drive is embarrassingly littered with what looks like a million scattered Colab notebooks and now I’m going to have to deal with that.)

If you want, while you’re already in your Google Drive you can create a new Colab notebook. Just click “New” and drop the menu down to “More” and then select “Colaboratory.”

Otherwise, you can always go directly to Google Colab.

Game on!

You can rename your notebook by clicking on the name of the notebook and changing it or by dropping the “File” menu down to “Rename.”

Set up your free GPU

Want to use GPU? It’s as simple as going to the “runtime” dropdown menu, selecting “change runtime type” and selecting GPU in the hardware accelerator drop-down menu!

Get coding!

You can easily start running code now if you want! You are good to go!

Make it better

Want to mount your Google Drive? Use:

from google.colab import drive
drive.mount('/content/gdrive')

Then you’ll see a link, click on that, allow access, copy the code that pops up, paste it in the box, hit enter, and you’re good to go! If you don’t see your drive in the side box on the left, just hit “refresh” and it should show up.

(Run the cell, click the link, copy the code on the page, paste it in the box, hit enter, and you’ll see this when you’ve successfully mounted your drive):

Now you can see your drive right there on the left-hand side of the screen! (You may need to hit “refresh.”) Plus, you can reach your drive any time with

!ls "/content/gdrive/My Drive/"

If you’d rather download a shared zip file link, you can use:

!wget 
!unzip

For example:

!wget -cq https://s3.amazonaws.com/content.udacity-data.com/courses/nd188/flower_data.zip
!unzip -qq flower_data.zip

That will give you Udacity’s flower data set in seconds!

If you’re uploading small files, you can just upload them directly with some simple code. However, if you want to, you can also just go to the left side of the screen and click “upload files” if you don’t feel like running some simple code to grab a local file.

Google Colab is incredibly easy to use on pretty much every level, especially if you’re at all familiar with Jupyter Notebooks. However, grabbing some large files and getting a couple of specific directories to work did trip me up for a minute or two.

I covered getting started with Kaggle in Google Colab in a separate article, so if that’s what interests you, please check that out!

Importing libraries

Imports are pretty standard, with a few exceptions.

For the most part, you can import your libraries by running import like you do in any other notebook.

PyTorch is different! Before you run any other Torch imports, you’ll want to run

*** UPDATE! (01/29)*** Colab now supports native PyTorch!!! You shouldn’t need to run the code below, but I’m leaving it up just in case anyone is having any issues!

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision
import torch

Then you can continue with your imports. If you try to simply run import torch you’ll get an error message. I really recommend clicking on the extremely helpful links that pop up. If you do, you’ll get that code right away and you can just click on “INSTALL TORCH” to import it into your notebook. The code will pop up on the left-hand side of your screen, and then hit “INSERT.”

Not able to simply import something else that you want with an import statement? Try a pip install! Just be aware that Google Colab wants an exclamation point before most commands.

!pip install -q keras
import keras

or:

!pip3 install torch torchvision

and:

!apt-get install

is useful too!

I did find that Pillow can be sort of buggy, but you can solve that by running

import PIL
print(PIL.PILLOW_VERSION)

If you get anything below 5.3, go to the “runtime” dropdown menu, restart the runtime, and run the cell again. You should be good to go!

It’s easy to create a new notebook by dropping “File” down to “New Python 3 Notebook.” If you want to open something specific, drop the “File” menu down to “Open Notebook…”

Then you’ll see a screen that looks like this:

As you can see, you can open a recent file, files from your Google Drive, GitHub files, and you can upload a notebook right there as well.

The GitHub option is great! You can easily search by an organization or user to find files. If you don’t see what you’re looking for, try checking the repository drop-down menu!

Always be saving

Saving your work is simple! You can do a good ol’ “command-s” or drop the “File” menu down to save. You can create a copy of your notebook by dropping “File” -> “Save a Copy in Drive.” You can also download your workbook by going from “File” -> “download .ipyb” or “download .py.”

That should be enough to at least get you up and running on Colab and taking advantage of that sweet, sweet free GPU! Please let me know if you run into any other newbie problems that I might be able to help you with. I’d love to help you if I can!

If you’re just getting started with machine learning and AI, I have a few other articles you might want to check out:

Have you finished your deep learning or machine learning model, but you don’t know what to do with it next? Why not deploy it to the internet?

Get your model out there so everyone can see it!

Photo by Sarah Cervantes on Unsplash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s