A guide to Google Colab - Part 1

What is Google Colab?
Colaboratory is a cloud service. It provides a free Jupyter notebook environment that requires no setup (you do not need to install Python, Tensorflow, etc.). It runs entirely in the cloud. It has an intresting feature, especially for deep learning guys: a free GPU!
You can use a free GPU as a backend for 12 hours at a time. The GPU is NVIDIA Tesla K80 (at this time). The 12-hour limit is for a continuous assignment of a virtual machine (VM). What it means is that we can use it again even after the end of 12 hours by connecting to a different VM.

How to use it?

Google has Hello, Colaboratory. You can go there for an in-depth information on the Colab's usage. Also, you can read some of the frequently asked questions here.

A simple way to start is here:

  • Sign into your Gmail and open your Google Drive.
  • Create a folder on Google Drive. (E.g., Colab Notebooks)
  • Create a new notebook via Right click > More > Colaboratory

That's it! Open it via Right click > Open with > Colaboratory.
Now, you can do computations and saved it.

Import data

This notebook provides recipes for loading and saving data from external sources.
Here is a simple way:

  • Upload data into a folder in your Google drive. (E.g., My Drive/Colab Notebooks/data)
  • Mount your Google drive:

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

    You will get an authentication code. Enter it!
    Note that /myDrive/ is a custom name. You can use something else.

  • Load data. E.g.:

    root = '/myDrive/My Drive/Colab Notebooks/'
    # fname = root+'data/one.tif'
    fname = root+'data/testset/one.tif'
    print("Path: %s"%fname)
    

Display an image

For the following cell, you need to enter an authentication code.

In [7]:
from google.colab import drive
drive.mount('/myDrive/')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:
··········
Mounted at /myDrive/

After authentication, you can load the image (given its full path).

Let's first see where we are!

In [5]:
!ls
sample_data

I have created a folder named Colab Notebooks in my Google drive. I want to use this folder for storing my colab's notebooks. In order to list the folders and files in the root of your Google drive, you need to lunch the following commands: (For listing, you can also use the dir command.)

import os
os.chdir('/myDrive/My Drive/')
!ls

The above commands change the current directory to the root of your Google Drive.

In [9]:
root = '/myDrive/My Drive/Colab Notebooks/'
fname = root+'data/test_colab/zebra.bmp'
print("INFO: Path is %s"%fname)
INFO: Path is /myDrive/My Drive/Colab Notebooks/data/test_colab/zebra.bmp
In [10]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img

Y=img.imread(fname)
print("INFO: Data type is",Y.dtype) # uint8
scaled_Y=Y/255.

fig=plt.figure()
fig.suptitle('original image')
plt.imshow(scaled_Y)
plt.axis('off')
plt.show()
INFO: Data type is uint8

Note: I experienced some difficulties for displaying an image in Colab's jupyter notebooks. For example, I could not cv2.imshow() or PIL's show method.

Save an image

Let's add some noise, then save the noisy image.

In [0]:
Yn=Y+50*np.random.randn(*Y.shape)
Yn_scaled = ((Yn - Yn.min()) / (Yn.max() - Yn.min()))

fname = root+'data/test_colab/zebra_noisy.png'
img.imsave(fname,Yn_scaled,format='png')

Let's load the saved image and show it:

In [12]:
Yn=img.imread(fname)

fig=plt.figure()
fig.suptitle('noisy image')
plt.imshow(Yn)
plt.axis('off')
plt.show()