Saving a model state

(Nikhil Gupta) #1

In Lesson 1, when I run the following line of code, where is the model being saved? Is it being saved in the local object only and will I lose this snapshot if I close the notebook?

learn.save(‘stage-1’)

Note that I am using Google Colab.

0 Likes

(Shivendra Jadon) #2

It is being saved in /content folder. But I needed to save it in my Google Drive folder. So I used the following snippets
First run this code from the start_colab help page from the course website

from google.colab import drive
drive.mount(’/content/gdrive’, force_remount=True)
root_dir = “/content/gdrive/My Drive/”
base_dir = root_dir + ‘fastai-v3/’

Then in the cell where the model is saved add the following lines
learn.save(‘stage-1’)

with open(’/content/gdrive/My Drive/fastai-v3/stage-1.pth’, ‘w’) as f:
f.write(‘content’)

It created a stage-1.pth file in my fastai-v3 folder in my drive and I was able to reload the model from that file path. For a few other options please find this link

Hopefully this code works for you too. Please point out any errors I may have made in my inexperience.

Best Regards,
Shivendra

0 Likes

(Nikhil Gupta) #3

Thank you. I will try this out. Did you have to make any changes to the loading step as well to ensure it picks it up from the Google Drive not the /content folder?

0 Likes

(Nikhil Gupta) #4

I think I figured it out. By default, it is saving it in the same directory as the data, but under a directory called “models”. It will pick it up from there itself.

image

Also, if you want to change this, you can change learn.path as per this post: How to save FAST AI resnet50 model to Google Drive after training in google colab notebook

1 Like