Google Colab Simplified Introduction. Tutorial for AI Beginners. Colab Explained.

ai-in-healthcare-about-book

 



Knowing about Google Colab is essential before you begin MedMantra Academy's Healthcare AI Application Programing Course. This tutorial will get you started with Google Colab in just 20-minutes.

Colab is short for "Colaboratory". It is a free tool by Google that allows you to write, execute and share Python code in your browser - from your phone, tablet or computer.
It is a better version of the "Jupyter Notebook" popular with data scientists and AI (artificial intelligence) researchers.

With Google Colab, you can quickly start AI programing without any configuration. All necessary AI, ML (machine learning), and Data Science libraries and packages are pre-installed in Colab. You also get free access to GPUs (Graphics Processing Units) and TPUs (Tensor Processing Units) to run your code faster.

To use Google Colab you just need a free Google account and internet access.

To go to Google Colab, search Google for "colab" and then click on colab.research.google.com

Sign-in to Google colab with your Google account. I prefer Google Chrome browser when using colab.

A “notebook” is the main part of Google Colab. Python code is written and executed inside a notebook. You can create a new notebook or open an existing notebook. All of your notebooks are saved to your Google Drive. You can open notebooks present in your Google Drive, local computer, or GitHub.

After creating a new notebook you can rename it suitably and click outside to save the new name. A copy of the notebook can be saved in your Google Drive or GitHub account. You can even download it to your computer and save as an ipynb (iPython Notebook) file. This can then be opened again in Google Colab or in the Jupyter Notebook. Any notebook can be easily shared with other people.

In Colab, all code is written in “code cells”. There are two types of cells in Colab: one is called a "code cell" and the other a “text cell”. The text cells are used to add comments, images, videos, headlines, lists and more to the code. In the text cell, the left side pane is a text editor and the right side pane shows the preview of the entered data.

A "code cell" has a play button icon on its left hand side. If you click it, the code inside the cell is executed. Another option to run a cell and execute the code inside it is to use “Command + Enter” on Mac or “Control + Enter” on Windows. You can create another "code cell" or "text cell" by clicking on “+ Code” or “+ Text” respectively. It is more convinient to press “Shift + Enter” which runs the current code cell and automatically move to the next cell. The "code cell" can be deleted or moved up or down using the icons appearing on its right hand side after clicking the cell.

We will execute following code:

# Import necessary libraries by using following code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# To display images use following code
from PIL import Image

# Import computer vision package to read the images dataset
import cv2
import os


Note that the text in the line beginning with "#" is ignored in a code cell. It is for commenting.

If a notebook is kept idle or unused for a long time, it gets disconnected. In this case, your code is automatically saved, however, your variables and states in the current runtime will be lost. To continue from where you left off, you have to run your code cells again. You can do this either by executing each cell sequentially (one by one) or by going to “Runtime” on the top left side of your notebook and click on “Run all” or "Restart and run all".

By default, a notebook in Google Colab runs on a computer CPU (Central Processing Unit) to process code and data. When you are working with a large amount of data and need more computing power, you change the runtime type to GPU or TPU depending on your computational needs. To do this, you can click on “Runtime” on the top left side of your notebook and then click on “Change runtime type”. From there, you can change your runtime type to GPU or TPU.

# After changing the runtime type to GPU, to check if the GPU is working, use following code:
import tensorflow as tf
tf.test.gpu_device_name()


To use the datasets and other files stored in your Google Drive inside a Google Colab notebook, you have to "mount" the google drive. To do this, click on the Files/Folder icon on the left side of the notebook and then click on the "Mount Drive" icon. You need to permit Google Colab to access your Google Drive. Also, it is important to make sure to use the same Google account to log in to both Google Colab and Google Drive. To access dataset files, first upload the files to your MyDrive. You can then, navigate to the file from Google Colab, copy the path to the file, and then paste it in your notebook.