Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (2024)

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (3)

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

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.

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (4)
Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (5)

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.

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:

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (6)

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

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (7)

Then after it runs, the package is successfully installed.

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (8)

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.

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.

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (9)
Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (10)

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/"

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.

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (11)

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:

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (12)
import keras_segmentation

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!

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (13)
Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (14)

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!

Google CoLab Tutorial — How to setup a Deep Learning Environment on CoLab (2024)
Top Articles
Innovative Economy
How to Apply for UOB One Savings Account Singapore: A Step-by-Step Guide
Was ist ein Crawler? | Finde es jetzt raus! | OMT-Lexikon
Danatar Gym
Crocodile Tears - Quest
Kansas Craigslist Free Stuff
Flixtor The Meg
Txtvrfy Sheridan Wy
Devotion Showtimes Near Mjr Universal Grand Cinema 16
Wild Smile Stapleton
Ou Class Nav
Cvs Devoted Catalog
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
4Chan Louisville
Jasmine Put A Ring On It Age
MindWare : Customer Reviews : Hocus Pocus Magic Show Kit
Socket Exception Dunkin
Kaomoji Border
Baywatch 2017 123Movies
Byte Delta Dental
Keurig Refillable Pods Walmart
Dover Nh Power Outage
Webcentral Cuny
Clare Briggs Guzman
Form F-1 - Registration statement for certain foreign private issuers
SOGo Groupware - Rechenzentrum Universität Osnabrück
FAQ's - KidCheck
Democrat And Chronicle Obituaries For This Week
Bend Missed Connections
Downloahub
Wheeling Matinee Results
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Average weekly earnings in Great Britain
Shiftwizard Login Johnston
Green Bay Crime Reports Police Fire And Rescue
SF bay area cars & trucks "chevrolet 50" - craigslist
Leena Snoubar Net Worth
Sabrina Scharf Net Worth
888-822-3743
Isabella Duan Ahn Stanford
Cocaine Bear Showtimes Near Cinemark Hollywood Movies 20
Parent Portal Pat Med
Copd Active Learning Template
20 Mr. Miyagi Inspirational Quotes For Wisdom
What is a lifetime maximum benefit? | healthinsurance.org
N33.Ultipro
La Qua Brothers Funeral Home
Mlb Hitting Streak Record Holder Crossword Clue
Evil Dead Rise - Everything You Need To Know
Tenichtop
211475039
Intuitive Astrology with Molly McCord
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5675

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.