[PYTHON] Google Colab Tips Organize

1. Introduction

Here are some useful tips for using Google Colabratory.

2. Google Drive mount

In Google Colab, files are generated and saved in / content / of the virtual environment. For example, suppose you want to use your own photo dataset. Instead of uploading the dataset directly to / content /, put the dataset somewhere in Google Drive in advance and then mount Google Drive. And it's easier to use if you specify a folder for your Google Drive photo dataset.

The sample code is shown below.

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

#Datasets Dir (Example)
base_dir = '/content/drive/My Drive/datasets/Autoencoder_Sample/train'

3. Save the learning model

When a training model is generated as a result of learning a neural network, there are two ways to save the file on your PC.

(1) How to download from Google Colab to your local PC (2) How to transfer to Google Drive


from google.colab import files

#Model Save (Keras)
model.save('autoencoder-trained.h5')

#1.Download model file
files.download('autoencoder-trained.h5')

#2.Copy model files directly to Google Drive
!cp autoencoder-trained.h5 "drive/My Drive/autoencoder-trained.h5"

If the size of the training model is several hundred MB or more, it will take time to download, so I personally recommend copying with the cp instruction. It is recommended to enclose the copy destination folder name + file name in "". Otherwise, you may get an error.

4. How to stay connected with Google Colab

Google Colab will automatically disconnect after 30 minutes if there is no user response. Of course, the calculation contents will be lost at the same time. : cold_sweat:

A method to prevent this has been introduced on the net, so I will introduce it. How to prevent Google Colab from disconnecting ?

Assuming that you are using Google Chrome, press ** Ctrl + Shift + i ** to make it the Browser's Inspector View. Then go to ** goto console ** and enter the code below. This solves the problem of Google Colab disconnecting in 30 minutes.


function ClickConnect(){
console.log("Working"); 
document.querySelector("colab-toolbar-button#connect").click() 
}setInterval(ClickConnect,60000)

Please refer to the video below for the actual operation method. https://www.youtube.com/watch?v=N2NEyr9Bf48&feature=youtu.be&t=257

5 Acknowledgments

We received a lot of advice from Mr. Kimura of ABeam Consulting regarding the use of Google Colab. I would like to take this opportunity to thank you.

Recommended Posts

Google Colab Tips Organize
About learning with google colab
Plotly Dash on Google Colab
Google Drive Api Tips (Python)
Play with Turtle on Google Colab
Use MeCab and neologd with Google Colab
Machine learning with Pytorch on Google Colab
Image segment using Oxford_iiit_pet on Google Colab