How to save and upload Deep Learning/ Machine Learning models in Google Colab using Google Drive



Saving Trained Models into Google Drive

Use the following code to save models in google colab.

0. Install PyDrive library into Google Colab notebook.

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

1. Authenticate and create the PyDrive client

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth)

2. Save Model or weights on google drive and create on Colab directory in Google Drive

model.save('model.h5')
model_file = drive.CreateFile({'title' : 'model.h5'}) model_file.SetContentFile('model.h5') model_file.Upload()

3. Download the model to google drive

# download to google drive                       drive.CreateFile({'id': model_file.get('id')})

Loading models from Google Drive into Colab

file_obj = drive.CreateFile({'id': '16zbkEN4vqnPvIOvLMF1IaXKYCq5g3Yln'})                       file_obj.GetContentFile('keras.h5')

Source: Deep Learning on Medium