DEV Community

loading...
Cover image for Setting up Google Colab for Deep Learning

Setting up Google Colab for Deep Learning

Azhan Mohammed
・3 min read

Introduction

Google Colaboratory or Colab is a free service provided by Google that allows a person to run Python notebooks without having to install python on their systems. Colab provides a user with numerous features, the most important of them being:

  • No need to configure for basic notebooks
  • Free and easy GPU access
  • Easily sharing of codes and link the notebooks to GitHub
  • Loading datasets straight from your Google Drive and saving notebooks/ trained models to your drive too. Google Colab also comes with a Pro feature, you can learn more about the details of Colab Pro from here. In this blog post we shall discuss about setting up your Colab notebook to get started with Machine Learning or Deep Learning development.

Installing dependencies

While Colab usually comes pre-installed with most of the basic dependencies like Tensorflow, PyTorch, scikit-learn, pandas and many more, there are chances that you have to install external packages at times. You can do that using the !pip install command. For example we can install the ttach library which is used for augmentation of images during test phase. This can be done using:

!pip install library-name

Running the cell will yeild an output like:
image
This way we can install custom libraries as per our needs.

Mounting Google Drive

You can mount your Google Drive using a simple the simple script given below:

from google.colab import drive
drive.mount('/content/drive')
Enter fullscreen mode Exit fullscreen mode

Upon running this you will get a link that will redicrect you to select the account whose google drive you want to mount. Select the account, accept the the permission request and copy the code that appears on the screen and paste that in the dialog box and press enter. Your Google Drive is now mounted and you can access it via the files menu on your left. The output would look something like this:
image

Cloning GitHub Repositories

At times you might need to clone your GitHub repository to your Colab library to work on complex projects that utilize multiple scripts. That can be done using !git clone link-to-repository
Running the cell would clone the repository to your working directory.
image
You can also clone a repository to your google drive if you have mounted it. That is done using !git clone link-to-repository path-to-drive. The sample output would look like:
image

Accessing GPU runtime

One of the best features of Colab is the free GPU runtime it provides. You can enable your GPU runtime by:

  1. Go to Runtime image
  2. Select Change runtime type image
  3. Select GPU from the drop down menu and click on save image That's it, now you can use your GPU runtime provided by Colab. You can also check which GPU you are provided using !nvidia-smi. The output shows GPU memory you are consuming and other details as well. image

This concludes our discussion for setting up Colab to run Machine Learning and Deep Learning applications. Now you can easily clone GitHub repositories to your drive and execute them using GPU runtime without having to install Python on your local machine.

Discussion (0)