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.
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:
Colab Notebooks
)That's it! Open it via Right click > Open with > Colaboratory.
Now, you can do computations and saved it.
This notebook provides recipes for loading and saving data from external sources.
Here is a simple way:
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)
For the following cell, you need to enter an authentication code.
from google.colab import drive
drive.mount('/myDrive/')
After authentication, you can load the image (given its full path).
Let's first see where we are!
!ls
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.
root = '/myDrive/My Drive/Colab Notebooks/'
fname = root+'data/test_colab/zebra.bmp'
print("INFO: Path is %s"%fname)
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()
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.
Let's add some noise, then save the noisy image.
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:
Yn=img.imread(fname)
fig=plt.figure()
fig.suptitle('noisy image')
plt.imshow(Yn)
plt.axis('off')
plt.show()