0% found this document useful (0 votes)
23 views

Colab

Google Colab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Colab

Google Colab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Colab Walkthrough

Neural Style Transfer

Let's say that for your final project you want to work on Neural Style Transfer. As a
starting point, you find a repository on Github
(​https://github.com/anishathalye/neural-style​) that can serve as a starting point for your
group. If you want to run this repository on your computer, you can do so using the
following steps.

Step 1:​ Installing git


Step 2:​ Using git to download the package
Step 3:​ Installing python3
Step 4:​ Installing pip3
Step 5:​ Installing virtualenv
Step 6:​ Creating a new virtual environment
Step 7:​ Installing the project's package dependencies
Step 8:​ Downloading the project's dataset / pretrained models
Step 9:​ Running the model

As you might notice, there are quite a few steps involved with testing something, and it
can be difficult to setup before running the project. Further, if you run the project on your
computer, chances are that it will take a few hours to run. This is because you are
running the project on your computer's CPU (these days models are mostly trained and
evaluated on GPUs, which are several orders of magnitude faster).

Instead of installing packages on your computer, if you want to test a project quickly,
there is an alternative - Google Colab.
What is Google Colab?

Colab (short for Colaboratory) is a product offered by Google Research that allows
machine learning researchers to work on projects in the browser. Similar to Google Docs,
it allows you to share projects between many people, and ​best of all, it gives free access
to GPUs for you to quickly train models without any signup​.

When working on your projects, we recommend testing your code on a portion of your
dataset on your own computer, or on Google Colab initially. You can use these tools as a
quick way to see if your code has bugs or if the output of your model is reasonable. After
testing initially, you can then copy your code and full dataset to your group's AWS
instance to run the full version of your model (as testing locally / on Colab is free,
whereas you will be using the course credits for running on AWS).
Getting Started

Visit ​http://colab.research.google.com​. You should see a page like this. Click the "New
Python 3 Notebook" in the bottom right corner.

That will bring you to a page like this. This is your new project's notebook, a document
that you can code in and run models in. You can click "Untitled.ipynb" in the top left
corner to change the title of your document, or click "Share" in the top right corner to
share it with other people.

Each notebook is composed of a list of cells. These cells can contain text (where you
might describe what you're doing) and code (where you can type Python and run it). This
notebook format is very similar to the Coursera homework assignments.
Before you start running code, there's one quick step to do first. Click on "Runtime" and
then "Change runtime type". You should see the following window pop up:

Make sure that "Python 3" is selected as the Runtime type (this is the version of Python
we are using). Then, make sure "GPU" is selected for the Hardware accelerator. This will
allow us to run deep learning models in the matter of minutes instead of hours. You can
then click save.

Now, click the "Connect" button in the top right. After doing so, you should see a green
checkmark, indicating you have successfully connected to a GPU to run your code.

To make sure everything is working, try writing some Python in the notebook (click on
the open cell). You can run it by pressing SHIFT + ENTER. As an example, you might see:
Running Neural Style Transfer

Now going back to our original neural style transfer project,


https://github.com/anishathalye/neural-style​, let's say you want to try running this on
Colab. There are a few steps to follow:

Step 1: Downloading the Project

By default, each cell in Colab runs Python code. However, we can also run special
commands to download projects and install packages using a ​!​ before the command. For
example, to download the project, you would run:

Now that the project has been downloaded, you want to enter that project directory. You
can do so using the ​cd​ command, which in colab requires a ​%​ before it:

The outputted ​/content/neural-style​ is the new directory you are in.

Step 2: Installing the Package Requirements

Generally Python projects come with ​requirements.txt​ files that indicate which packages
that codebase depends on. You can see this file using the ​cat​ command, as shown
below:
This project depends on numpy (for math operations), Pillow (for manipulating images),
scipy (for scientific operations), and tensorflow-gpu (for training neural networks on the
GPU). You can install these packages by running the following. You might get some
warnings/errors but you can ignore those.

Now as one final step, you'll need to install tensorflow 1.13.2. In the requirements file, the
author lists numpy, Pillow, scipy==1.1, and any version of tensorflow >= 1.0. Just recently a
new version of tensorflow was released (2.0) and this codebase no longer works with
2.0. To install the right version of tensorflow, run:

With that step complete, all the packages and source code you need to run the project
should be downloaded and you should be ready to go.

Step 2: Downloading the Pretrained Model

This repo contains a pretrained model called VGG that you will need to download before
running. You can do so using the ​wget​ command, as shown below:
Step 3: Running and Visualizing the Output

To run the model:

To visualize the inputs/outputs:

You might also like