4 Google Colabs Tips and Tricks for Machine Learning

Colabs is a great resource but there are a few useful tools to make it better

Charlie Craine
DataDrivenInvestor

--

Sharing is Caring — 📸 Photo by Alexander Sinn on Unsplash

Tip One: Connect your notebook to Google Drive without code

This one is a great new-ish feature from Google Colabs.

First, Connect your NotebookClick the folder on the far left of your notebook (see image below), and then click on the Google Drive icon above “sample files” folder.

Orange circle = Google Drive connection

Then wait a minute and you are connected!

Tip Two: How to run large datasets in Colabs

I learned this the hard way. I was working on a Kaggle competition and even small image files took 20+ minutes per epoch to run. It was incredibly painful. I knew from prior experience these epochs should have run in 1–2 minutes each. Frankly, it was just a dumb mistake on my part. I needed to move the images into my notebook storage. Loading your content like this will kill your speed:

/content/drive/MyDrive/Kaggle

Instead, store a zip file to a Google Drive folder and then unzip the data into the notebook storage. Example:

from pathlib import Path
base = Path('/content/drive/MyDrive/Kaggle/HPA2021/kaggle-hpa')
zip_path = base/'hpa-512.zip'
!cp '{zip_path}' .
!unzip -q hpa-512.zip
!rm hpa-512.zip

This will grab the zip file from your Google Drive, then unzip the files and remove the zip drive from your notebook storage.

Now you’ll see the folders in your notebook storage. Click the folder image highlighted in orange below to see the files.

Now you have all your files ready to use quickly!

Tip Three: How to get the location of your files

You don’t have to guess and code your way to find files, you can easily click and get the precise location. Open the folder, click the three dots to the right of the folder or file:

Three dots and go!

Then you can go to “Copy path” and copy the path location:

In the example above MyDrive is your Google Drive folder and path is:

/content/drive/MyDrive/

Tip Four: Import a Github project right into Google Drive

This one is amazingly simple. Do Tip One above and connect to Google Drive. Then in the first cell type (for sanity check):

!ls

Your output should be “drive” and “sample_data”. If so, you are set. If the Drive folder is missing go back to Tip One.

Then, create the folder in Google Drive (beforehand) that you want to use the %cd command to go to the path location you want to put the Github project. To get the path to the location, see Tip Three. Copy the location like my example:

%cd /content/drive/MyDrive/Kaggle/HPA2021/

And next put in the following command and Github URL:

!git clone https://github.com/rwightman/pytorch-image-models

And that is it! Hopefully, these simple tips make Google Colabs even easier to use.

If you have questions or ideas reach out any time on LinkedIn.

--

--