A Picture Guide for Setting Up Google Colaboratory for FastAI Courses

bigablecat
AI³ | Theory, Practice, Business
10 min readAug 19, 2019

Introduction

There are many platforms to work with our notebook when learning fastai courses. We could find details by click tabs under the ‘server setup’ menu item on the index page of the course https://course.fast.ai/index.html.

However, beginners may still encounter a problem when setting up the environment for experiments. In this article, we provide a hands-on tutorial about how to set up colab and run the course notebook.

Step 1: Open the colaboratory in Google Drive

1.1
Use a browser to go to Google Drive via https://drive.google.com/
When login, the address will automtically change to https://drive.google.com/drive/my-drive

1.2
Now we are in the ‘My Drive’ directory.
Let’s create a folder specifically for colab use.
Click the New button on the top left.

1.3
Then click the ‘Folder’ option on the popup menu.

1.4
Enter a name as you like in the ‘New folder’ dialog.
In this example, we name it as ‘colab’.
Then we click CREATE button.

1.5
There appears a new folder named ‘colab’ (or any other name we named) under ‘My Drive’.

1.6
Enter into the ‘colab’ directory.
Right click in the blank area of the page.
On the popup menu, move the cursor to the option ‘More’ and we will get a sub-menu of more applications.
Then click the ‘Google Colaboratory’ option on the sub-menu.

1.7
The browser will automcally open a new tab and redirect to the Colaboratory page where we can operate the notebook file.
The default name of the newly created notebook file is ‘Untitled0.ipynb’.

screenshot 1.7

1) Right click the .ipynb file to open:
We can also enter into the Colaboratory page by right clicking the .ipynb file in the Google Drive page.
Select ‘Open with => Google Colaboratory’ on the popup menu.
It will redirect to the Colaboratory page as shown in ‘screenshot 1.7’.

2) Double click the .ipynb file to open:
When we double click the .ipynb file in the Google Drive page.
There will be a mask page shown. We click the ‘Open with Google Colaboratory’ tab on the top, it goes to the Colaboratory page (screenshot 1.7) as well.

Step 2: Run ‘hello world’ in our Colaboratory

2.1
The Colaboratory is disconnected at the beginning. It will automatically connect to server when code is running.
We could also manually make it connected by click the Connect button at the top right of the tool bar above the notebook working area.

2.2
When a green tick appears, our Colaboratory or say the notebook is sucessfully connected to server.

2.3
Let’s type in the following code in the first code cell for test.

print(‘hello world’)

Then click the ‘Play icon’ in the left gutter of the cell.

2.4
Then output will show under the code cell.
We could also use shotcut to run the cell. Make sure the cursor focus is on the code cell we are going to run or just click the cell to make it focused. Then press [ctrl] + [enter] or [shift] + [enter] , the cell will run as well.

2.5
An Official introduction of Colaboratory describes some shortcuts to run the cell:

1) Type Cmd/Ctrl+Enter to run the cell in place;
2) Type Shift+Enter to run the cell and move focus to the next cell (adding one if none exists);
3) Type Alt+Enter to run the cell and insert a new code cell immediately below it.

2.6
To create a new cell, click the + Code button at the top left of the page.
The shortcuts for creating new cells are as following:

1) Type Cmd/Ctrl+m B to insert a new code cell below the current one.
2) Type Cmd/Ctrl+m A to insert a new code cell above the current one.

This Cmd/Ctrl+m B shortcut means:
after typing Cmd/Ctrl+m at first place, release the keyboard and then type B.

For more shortcuts, please use Cmd/Ctrl+m H to call the Keyboard preferences board.
Or click Tools =>Keyboard shortcuts in the tool bar of the page.

Step 3: Mount our Google Drive on Colaboratory

3.1
We have tried the newly create notebook file ‘Untitled0.ipynb’.

So how could we run some codes that already stored in our Google drive.

Firstly, the Google drive should be mounted onto our Colaboratory.

Click the arrow at the left side of the page.

3.2
When the menu shows up, we see a default active tab ‘Code snippets’. Click the ‘Files’ tab on the right side.

3.3
Then we have a menu of files.

3.4
Click the MOUNT DRIVE button at top right of the sub toolbar.

3.5
The Colaboratory generates a code cell for us.

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

Pay some attention to the path ‘content/drive’. The ‘content’ here is the default working path of our Google drive, we will discuss about the path issue later.

3.6
Click the ‘run’ button or use shortcut to run the cell.

3.7
The output shows a message ‘Go to this URL in a browser …’ followed by a hyperlink. Below the message we see a input box for authorization code.

Click the hyperlink firstly.

3.8
It takes us to a ‘sign in’ page.
Click our account that already signed in.

3.9
It then goes to a confirmation page.

3.10
Drag to the end of the page and click the ‘Allow’ button.

3.11
Copy the code given to us. It is used as a token to access our Google Drive.

3.12
Go back to the notebook page where we come from, paste the autorization code to the input box and press Enter to confirm.

3.13
Mounting will take for a while and ellipsis is showing on the output area to say it is runing now.

3.14
If mounted sucessfully, a message ‘Mounted at /content/drive’ will appear at the bottom of the code cell output area.

A ‘drive’ directory will also show up on the left menu. If it doesn’t show, click the REFRESH button at the sub toolbar to refresh the menu.

3.15
Click the ‘drive’ directory on the left menu and dive deep. We will find the ‘Untitled0.ipynb’ file that we are operating right now.

The path drive/My Drive/colab is identical to what we see at the Google drive page.

Step 4: Change working path

4.1
When we first mounted the Google Drive, the mounted position determines the default working path of the notebook file.

For the ‘Untitled0.ipynb’ case, its physical path is ‘/content/drive/My drive/colab/Untitled0.ipynb’ but its working path begins from ‘/content/’.

Let’s check the path to make sure.
Create a new code cell and type the following command:

import os
os.getcwd()

Run the code and in the output we get the current working path ‘/content’.

Why it begins from ‘/content’. It’s very likely that we used ‘content/drive’ in drive.mount(‘content/drive’) to allocate the location and ‘/content/’ became kind of ‘root’ path.

4.2
Click the ellipsis icon in the files menu. We will find that ‘content’ is one of the directories under the real root path ‘/’.

4.3
A more concise way to check where we are is to use !ls, this command gives what current path contains.

As we see ‘drive sample_data’, we know the working path is under the direcotry ‘/content’.

4.4
Now assuming we want to enter into ‘content/drvie/My drive/colab’.

Since the notebook is under ‘/content’, just type cd drive/My\ Drive/colab will work.

Here the backward slash ‘\’ after word ‘My’ represents an escape character to escape the blank space between ‘My’ and ‘Drive’.

4.5
Instead, we could also type cd drive, cd My\ Drive, cd colab in succession to enter into the target directory level by level.

Use cd ../../../ to go back to ‘content/’.

Try the three lines of commands cd drive, cd My\ Drive, cd colab, it enters into ‘content/drive/My Drive/colab’ as well.

4.6
It’s suggested to use the tab key in the keyboard to autocomplete the path (mac users should enable autocompletion for tab key firstly) and then we don’t need to type the long path character by character.

But tab autocompletion will not work after the level that has an escape character.
Which means, autocompletion works for cd drive/My\ Drive but doesn’t work for cd drive/My\ Drive/colab.

Step 5: Clone fastai course-v3 repo to our Google Drive

5.1
The fastai course-v3 repo could be directly cloned from github to our Google Drive.

Firstly, we go to its github repository page
https://github.com/fastai/course-v3

Click the green button Clone or download on the page and then there will be a popup dialog. Click the ‘copy’ icon on the popup to get the link to our clipboard (https://github.com/fastai/course-v3.git).

5.2
Go back to the notebook page, create a new cell and type in the following code:

!git clone https://github.com/fastai/course-v3.git

Run it and we will see the cloning process in the output area.

If sucessfully finished, it shows the message ‘Checking out files: 100% (xxx/xxx), done.’

5.3
As we have already entered into the ‘/content/drive/My Drive/colab’ directory in the previous step, the course-v3 repo will be placed under ‘colab/’.

Type !ls in a new code cell, we find course-v3 together with ‘Untitled0.ipynb’.

Also, course-v3 will be seen in the files menu on the left of page.

Step 5: Run some notebook of fastai course-v3

Assuming we are learning part II now, we want to open the first notebook in part II codes.

5.1
Go back to the Google Drive and enter into the direcotry ‘drive/My Drive/colab/course-v3/nbs/dl2’.

Find the ‘00_exports.ipynb’ file and double click to open it.

5.2
Click the Open with Google Colabratory tab.

5.3
Now we are dealing with the ‘00_exports.ipynb’.
Click Connect button to connect it to the server.

Some trouble shooting during runing a notebook

Step 6: Set up GPU for our Colaboratory

--

--