Hidden features of Google Colaboratory

Ankit Shah
2 min readMay 9, 2019
Source

When working with deep learning some of the major stoppers are training data and computation resources. Google provides free solution for computation resources with Colaboratory, Google’s free environment which have GPU. Colaboratory provides online jupyter notebook where you can select GPU as back-end server. You can read more about the Colab here.

Here I will add some of the features which might be helpful for you in some scenarios.

Upload files directly from Colab

Usually to use any file in Colab you need to upload it on Google drive and then use it. You can use Colab’s upload functionality for the same, it provides upload button directly in the notebook.

from google.colab import files
files.upload()

Download trained models

Suppose you have a trained model and you need to download the model then you can use the download functionality.

from google.colab import files
files.download("File Name")

Suppose you want to download a trained Keras model.

model.save('trained_model.h5') #where model is already trained
from google.colab import files
files.download("trained_model.h5")

You can use also python to load data form web and directly work on it. The benefit of it will be that your single python notebook will also work in all other notebook environments (with disadvantage of downloading the dataset for each environment).

from urllib.request import urlretrieve
import os
from zipfile import ZipFile
def download(url, file):
if not os.path.isfile(file):
print("Download file... " + file + " ...")
urlretrieve(url,file)
print("File downloaded")
download('Url of the file','Name of the file to be saved')
print("All the files are downloaded")
#If the downloaded file is a zip file than you can use below function to unzip it.
def
uncompress_features_labels(dir):
if(os.path.isdir('data')):
print('Data extracted')
else:
with ZipFile(dir) as zipf:
zipf.extractall('data')
uncompress_features_labels('data.zip')

Executing command

If you want to execute commands to install python package, update python package or for any other task than you can do the same just by putting ! before the command. Like to update the tensorflow to latest version use below command:

!pip install — upgrade tensorflow

Original Post — Keshav Agarwal

--

--

Ankit Shah

LLM Architecture Associate Director at Accenture.