Troubleshooting Google Colab for the Total Newbie

Anne Bonner
DataDrivenInvestor

--

Working in Google Colab for the first time has been completely awesome and pretty shockingly easy, but it hasn’t been without a couple of small challenges! I thought I’d document a few of the issues that I’ve faced so that other newbies like myself can save a little time getting up and running.

If you know Jupyter Notebooks at all, you’re pretty much good to go in Google Colab, but there are just a few little differences that can make the difference between flying off to freedom on the wings of free GPU and sitting at your computer, banging your head against the wall…

Photo by Gabriel Matula on Unsplash

For anyone who doesn’t already know, Google has done the coolest thing ever by providing a free cloud service based on Jupyter Notebooks that supports free GPU! Not only is this a great tool for improving your coding skills, but it also allows absolutely anyone to develop deep learning applications using popular libraries such as PyTorch, TensorFlow, Keras, and OpenCV.

Colab provides GPU and it’s totally free. Seriously!

There are, of course, limits. It supports Python 2.7 and 3.6, but not R or Scala yet. There is a limit to your sessions and size, but you can definitely get around that if you’re creative and don’t mind occasionally re-uploading your files…

You can create notebooks in Colab, upload notebooks, store notebooks, share notebooks, mount your Google Drive and use everything you’ve got stored in there, import most of your favorite directories, upload your personal Jupyter Notebooks, upload notebooks directly from GitHub, upload Kaggle files, download your notebooks, and do just about everything else that you might want to be able to do. It’s awesome.

I’m leaving a lot of the “getting started” basics out of here to simply focus on a little quick troubleshooting. There are some awesome articles out there that deal with really getting started from the beginning. If you’re brand new to Colab, I highly recommend checking them out! (This one might be my favorite!)

I started using Colab as a part of Udacity’s Facebook AI PyTorch Scholarship Challenge, so my experience is definitely PyTorch heavy, but here’s what I’ve learned so far:

*** UPDATE! (01/29)*** Colab now supports native PyTorch!!! You shouldn’t need to run the torch code below to import Torch, but I’m leaving it up just in case anyone is having any issues!

-You’ll definitely need to import PyTorch if you want to use PyTorch! That’s a pretty good place to start. Before any other Torch imports, you’ll want to run

# http://pytorch.org/
from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.1-{platform}-linux_x86_64.whl torchvision
import torch

Then you can continue with your imports!

-Not able to simply import something else that you want with an import statement? Try a pip install!

!pip install -q keras
import keras

or:

!pip3 install torch torchvision

and:

!apt-get install

is useful too!

-Want to mount your Google Drive? Use:

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

Then you’ll see a link, click on that, allow access, copy the code that pops up, paste it in the box, hit enter, and you’re good to go! If you don’t see your drive in the side box on the left, just hit “refresh” and it should show up.

(Run the cell, click the link, copy the code on the page, paste it in the box, hit enter, and you’ll see this when you’ve successfully mounted your drive):

-If you’d rather download a shared zip file link, you can use:

!wget 
!unzip

For example:

!wget -cq https://s3.amazonaws.com/content.udacity-data.com/courses/nd188/flower_data.zip
!unzip -qq flower_data.zip

That will give you Udacity’s flower data set in seconds!

If you’re uploading small files, you can just upload them directly with some simple code. However, if you want to, you can also just go to the left side of the screen and click “upload files” if you don’t feel like running some simple code to grab a local file.

Google Colab is incredibly easy to use on pretty much every level, especially if you’re at all familiar with Jupyter Notebooks. However, grabbing some large files and getting a couple of specific directories to work did trip me up for a minute or two.

-I found that Pillow can be sort of buggy, but you can solve that by running

import PIL
print(PIL.PILLOW_VERSION)

If you get anything below 5.3, go to the “runtime” dropdown menu, restart the runtime, and run the cell again. You should be good to go!

Want to use GPU? It’s as simple as going to the “runtime” dropdown menu, selecting “change runtime type” and selecting GPU in the hardware accelerator drop-down menu!

Then I like to run

# check if GPU is available
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('Bummer! Training on CPU ...')
else:
print('You are good to go! Training on GPU ...')

just to make sure it’s working.

That should be enough to at least get you up and running a large file on GPU! Please let me know if you run into any other newbie problems that I might be able to help you with. I’d love to help you if I can!

Photo by Sarah Cervantes on Unsplash

--

--