Set up Colab for PyTorch Projects

Wenjing Liu
Udacity PyTorch Challengers
3 min readJan 4, 2019

What is Colab?

Colab (short for Google Colaboratory) is a research tool for machine learning education and research. It is a Jupyter notebook environment that requires no setup to use. For more information, click here.

How to access Colab?

Go to your Google Drive, click on the “New” button in the upper left corner, then select “Colaboratory” to create a new file.

How to install PyTorch in Colab?

If you click on the arrow sign on the left side, and type “pytorch”, Colab will provide a code snippet to install PyTorch 0.4.1.

Or you might want to install some other version, e.g. PyTroch 1.0.0, then you need to copy and paste the following code manually.

How to set up Colab for Udacity Lab project?

If you are setting up Colab to train models for the final Lab project of Udacity PyTorch Challenge (Nov 2018-Jan 2019), you would need PyTorch 0.4.0. First run the following code block to install PyTorch 0.4.0 and PIL (pillow).

Restart the runtime by selecting the option “Runtime -> Restart runtime” in the menu. Then run the following code block to check the versions. The default PIL version in Colab so far is 4.0.0, which could cause error “AttributeError: module ‘PIL.Image’ has no attribute ‘register_extensions’”. And if you don’t restart the runtime, the version won’t get updated.

After that, run the following code block to mount Google Drive. You will be asked to go to a URL to get your authorization code, which allows the Jupyter notebook to access your Google Drive.

You could name it “drive” or something else. If it was “drive”, you’d be able to access any file in your Google Drive with path “drive/My Drive/<filename>”.

Now you have set up Colab for the Lab project.

How to check Colab GPU profile?

Run the following code to check the Colab GPU profile. If you want to check runtime information, select the option “Runtime -> Manage sessions” in the menu.

Why PyTorch 0.4.0?

Colab provides code snippet to install PyTorch 0.4.1 (as mentioned before).

However Udacity Lab uses PyTorch 0.4.0.

If two PyTorch versions don’t match, you might encounter errors when uploading your trained model (model’s state_dict) to Udacity Lab. Some suggested setting “strict=False” for method “load_state_dict()” to avoid the error. But I found the model weights would change if I did so, e.g. in this case half of the weights changed after the upload. Also some reported that her/his accuracy dropped dramatically from 90% in local machine to below 20% in Udacity Lab (it didn’t pass the test). Hence I would suggest setting the right PyTorch version for the Udacity Lab project. Good luck!

P.S. To get all the code in this post, you can visit here.

--

--