How to use Jupyter Notebook with Deep Learning Frameworks on a Google Compute Engine

Sep 23, 2017

In this tutorial we will create a Google Compute Engine with 16 vCPUs, 60 GB memory, 500GB SSD disk, and 2 x NVIDIA Tesla K80 GPUs.

Warning! You must stop VM instances when you do not use them becasue you will be charged for about $2 per hour for this configuration as long as the VM is running even though you do not use it. To stop the VM instance go to VM instances under Compute Engine menu or by runing sudo shutdown -h now while you are logged into the VM via Terminal.

In order to create a Google Compute Engine we need a Google account, if you do not have one please Create your Google Account and continue.

Google offers a generous $300 free credit for everyone with an account to try the provided services. We should visit Google Cloud Platform and click TRY IT FREE. We will be asked to enter contact and credit card information, however, we will only be charged upon finishing $300 free credit and if we upgrade our account, which we will in order to be able to use GPUs.


Google Cloud Platform

Filling out the information, will direct us to Google Cloud Platform Dashbord. We should go to VM instances under Compute Engine menu and wait until the compute engine is ready.


VM Instances

Now, we can visit Quotas section under IAM & admin menu to increase the quota of GPUs to 2 Nvidia K80, which is zero in default. First select the project as shown in the picture below.


VM Instances

Then we should find the Google Compute Engine API related to NVidia K80 GPUs in the us-west1 zone, check the box next to it and click EDIT QUOTAS on the top bar. Afterwards, we fill out the information asked in the right menu and request a Change to 2 GPUs and submit it. In a few minutes we will receive an approaval email.


VM Instances

Upon receiving the approval email, we go to VM instances under Compute Engine menu and start configuring the Virtual Machine instance. You can use any configuration you prefer, but as a purpose of this blog post we will use the following configuration


  • Name: Choose any name you prefer
  • Zone: us-west1-b
  • Cores: 16 vCPUs
  • Memory: 60 GB
  • Number of GPUs: 2
  • GPU type: NVIDIA Tesla K80
  • OS image: Ubuntu 16.04 LTS
  • Boot disk type: SSD persistent disk
  • Size: 500 GB
  • Firewall: Check both 'Allow HTTP traffic' and 'Allow HTTPS traffic'
  • Management: Find, copy and paste the startup script related to UBUNTU 16.04 LTS from Adding GPUs to Instances in Installing GPU drivers using scripts section to install Nvidia Driver and CUDA on startup.
  • Disks: Uncheck the box next to Delete boot disk when instance is deleted

Create VM Instances
Create VM Instances

Now we click create and wait for about 20 minutes while virtual machine is getting ready and Nvidia Drivers and CUDA are being installed. You can come later to continue or watch these tow TED Talks.


What would happen if we upload our brains to computers?
Can a robot pass a university entrance exam?

After about 20 minutes we should visit External IP addresses under VPC network and change the ip Type to Static. We will use the static ip address to connect from our local browser to Jupyter Notebook running on the virtual machine.

Create VM Instances

Now we should define a new Firewall rule. Go to Firewall rulesunder VPC network and click CREATE FIREWALL RULE on the top menu and fill out the following information.

  • Name: any name you want
  • Targets: All instances in the network
  • Source IP ranges: 0.0.0.0/0
  • Protocols and ports: select Specified protocols and ports and enter tcp:7000 then click create.

Create VM Instances

We are ready to install Deep Learning libraries on our Google Compute Engine. In my opinion the most convenient way to connect to a Google Compute Engine is via Terminal, however you can activate Google Cloud Shell from an icon similar to >- and skip installig Google Cloud SDK. Go to Google Cloud SDK and install it using the scripts in 1, 2, 3, and 4, before going to next section.

Google Cloud SDK

When you finish installing SDK run gcloud auth login to allow Google Cloud SDK to access your account.

Google Cloud SDK

Then copy gcloud command as shown in the below and run it on Terminal.

Google Cloud SDK

Now we are ready to install Python Deep Learning Libraries on our machine. First copy and paste the following code on Terminal (be sure that you are connected to Google Compute Engine instance) to download the scripts we need.


wget https://raw.githubusercontent.com/ImOmid/Deep-Learning-on-Google-Compute-Engine/master/gcp_ubuntu16_dl.sh

Then run bash gcp_ubuntu16_dl.sh on Terminal to install the libraries. This process will take about 1 hours, so be patient and do your other task without closing Terminal. After it is done a message will be shown on Terminal which guid you to check if everything is done successfully.


Google Cloud SDK

When you see a message similar to the one in the picture above run the followings in order on Terminal to be sure that the python virtual environment was created successfully with deep learning libraries and OpenCV 3.3.0 installed. If there is no erros now you have created a Powerful virtual machine which you can use to train your models.

  1. First you must activate virtualenv: To activate run source ~/venv/bin/activate
  2. To check the librariy enter ipython: To enter run ipython
  3. Check if you can import Tensorflow: run import tensorflow as tf
  4. Check if you can import keras: run import keras
  5. Check if you can import torch: run import torch
  6. Check if you can import OpenCV: run import cv2
  7. To exit from ipython: run exit
  8. To activate Jupyter Nptebook: run jupyter notebook --no-browser --port 7000

You will see a URL similar to the one in the picture below. Copy the url and paste it on your browser address bar and replace the localhost with the external ip address of you virtual machine which you can find at External IP addresses under VPC network. Hit the Enter and you are running Jupyter Notebook of your Google Compute Engine instance locally.

Jupyter Notebook Token

Jupyter Notebook Token

Warning! You must stop VM instances when you do not use them becasue you will be charged for about $2 per hour for this configuration as long as the VM is running even though you do not use it. To stop the VM instance go to VM instances under Compute Engine menu or by runing sudo shutdown -h now while you are logged into the VM via Terminal.