Google COLAB free version saving Keras trained model

Viewed 384

I saved keras trained model in google colab free version

 model.save("my_model.h5")

i tried to retrieve model using below method

from keras.models import load_model
model = load_model('my_model.h5')

But it is throwing errors

OSError: Unable to open file (unable to open file: name = 'my_model.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

will i able to retrive saved model from free google colab version, can you any help on this I checked similar question in stackoverflow, i think these answers belongs to colab pro version

Otherwise, do i have to save model in specific path to local drive while training?

1 Answers

What is Problem

You are storing your model in runtime not in your google drive. After 12 hour runtime automatically deleted with data. So we have to save model in google drive.

How to store to Google Drive

First connect to google drive

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

Now you will find file explorer at left side which has drive directory. When you go inside that directory, it will take you to google drive.

Suppose I want to put my data in drive My Drive then

from keras.models import load_model

MODEL_PATH = './drive/My Drive/model.h5'

# Now save model in drive
model.save(MODEL_PATH)

# Load Model
model = load_model(MODEL_PATH)

When you open your drive, you will find file model.h5 in drive.

Related