Google CoLab Tutorial — How to setup a Pytorch Environment on CoLab

Kathryn
Analytics Vidhya
Published in
5 min readJun 24, 2020

If you are a Python user, you must be familiar with Jupyter Notebook. The good news is Google has a cloud based service called CoLab. It is just like running Jupyter Notebook on Google Drive. No need to apply for additional accounts, and no need to apply for the service. All you need is a Google Drive account, and that’s it. Easy & Simple! The best thing is that it gives users free TPU access to run your task! Therefore, if your local machine does not have enough GPU power, for example, my T470P laptop has only 2G VRAM (graphic card memory) and couldn’t run a training task that needs approximately 4G memory, then Google CoLab can be one solution.

In this blog, I will introduce:

  • How to create a CoLab in Google Drive
  • How to install library in CoLab
  • How to import modules in CoLab

1. Create a Colab document

As the below image shows, use the normal way you created a Google doc to add a coLab document. If you are using it for the first time, you would have to add the service in the Connect more apps section and search Colaboratory in the pop-up App searching box.

2. Edit and run a Colab document

This is how it looks in a Colab document. It is almost identical to JupyterNoteBook. It has CODE and TEXT to allow you add a new cell to write your code or text, and when you hit SHIFT+ENTER, it will execute the cell. The TEXT cell also support Markdown syntax.

3. How to Install Python Library in Colab

Colab has command line support, so simply add ! exclamation mark to run Linux commands in the cell. You can simply run !pip install <package-name>to perform pip install. So when you go to Pytorch official installation website, and choose the specifications of the pytorch version you want to download, make sure you choose Linux, Pip, and Python, and then the CUDA version you want to install, and you would see a pip command line shown at the bottom:

Then copy paste that command to CoLab cell with a prefix !
So, simply run !pip install torch trochvision in the CoLab cell:

Then after it runs, the package is successfully installed.

Note: It seems that CoLab has already preinstalled Pytorch for you, so if you run this command, it’ll tell you “Requirement already satisfied”. However, you can use the same method to install any other python package through pip install.

4. Import Python Modules in Colab

Mount Google Drive

For the Colab to recognize your Google Drive main directory, the first step is to mount your Google drive to CoLab, by running these:

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

After you execute the above code, it will show a URL link that has the authorization code for you to grant admission. Click on the URL, and copy paste the authentification code to the input box here.

Now it’s done! Your Google Drive is mounted in this path /content/drive/My Drive/. Try listing out all the files under the dir.

!ls "/content/drive/My Drive/"

5. Import Module from My Drive

Now the platform can recognize "/content/drive/My Drive/" as the root path. Any Python modules under this main directory can be seen and imported. For example, if I have a Python file model.py under my Google Drive directory, then I can simply do import model in my Colab.

6. Add subfolder in Google Drive to System Path

However, sometimes we want to separate our projects and use different dir for different projects. Then we can add our project dir to the sys.path which allows Python to see everything under the project dir. For example, if my project folder is under code folder and then image-segmentation-keras folder. The absolute path is /content/drive/My Drive/code/image-segmentation-keras.

Then we can simply add the “code” folder path to the system path:

import sys

# Add your absolute path of your project folder to system path.
sys.path.append("/content/drive/My Drive/code/image-segmentation-keras")

# See the full list of paths in sys.path
print(sys.path)

After adding the project folder to sys.path, now I can import any Python modules in this folder. For example, I can import `keras_segmentation` module in my Colab:

import keras_segmentation

7. Setup TPU/GPU access in Colab

One last thing is Don't forget to setup free TPU for your CoLab page if you are using it to run a GPU intensive training task, e.g., a Deep Learning task. You can setup a TPU in Edit > Notebook settings. Choose GPU or TPU from the drop down menu Hardware accelerator and then you are good to go!

Lastly, although the abovementioned steps is all you need to get started using Pytorch in Google Colab, if you happened to be a VSCode user, and want to access Google Colab via VSCode interface, then I found this article very interesting to have a look.

That’s it! Hope you enjoy this tutorial about how to setup a Deep Learning environment on CoLab! If you find this post to be useful, please leave a “clap” to this post! You are also welcome to leave your thoughts, comments or feedback! Thanks for your reading!

--

--