Navendu Pottekkat
Navendu Pottekkat

Reputation: 335

Unable to save ML model to Google Drive in Google Colab

Here is the function that I am using to save the model. The folder 'models' does not exist. But shouldn't it create a folder 'models' and save the model with a filename as written. I cannot find a solution for this anywhere.

# Create a function to save a model
def save_model(model, suffix=None):
  """
  Saves a given model in a models directory and appends a suffix (str)
  for clarity and reuse.
  """
  # Create model directory with current time
  modeldir = os.path.join("/content/drive/My Drive/Dog Eyes/models/",
                          datetime.datetime.now().strftime("%Y%m%d-%H%M%s"))
  model_path = modeldir + "-" + suffix + ".h5" # save format of model
  print(f"Saving model to: {model_path}...")
  model.save(model_path)
  return model_path

And I am calling the function like

# Save our model trained on 1000 images
save_model(model, suffix="1000-images-mobilenetv2-Adam")

Here is the error that I am getting when executing the function

Unable to create file (unable to open file: name = '/content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 242)

The complete error message



Saving model to: /content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5...

---------------------------------------------------------------------------

OSError                                   Traceback (most recent call last)

<ipython-input-62-d56ea448536c> in <module>()
----> 1 save_model(model, suffix="1000-images-mobilenetv2-Adam")

5 frames

/tensorflow-2.1.0/python3.6/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    177         fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
    178     elif mode == 'w':
--> 179         fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
    180     elif mode == 'a':
    181         # Open in append mode (read/write).

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.create()

OSError: Unable to create file (unable to open file: name = '/content/drive/My Drive/Dog Eyes/models/20200306-18501583520636-1000-images-mobilenetv2-Adam.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 242)


Upvotes: 0

Views: 2977

Answers (2)

user17038175
user17038175

Reputation: 1

Try this:

model_path = 'saved_model/my_model_1'

model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)

or

model_path = 'saved_model/my_model_1.h5'

model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)

To check:

!mkdir -p saved_model

To inspect the saved model:

!ls saved_model

To check if it contains an assets folder, saved_model.pb, and variables folder.

!ls my_model_1.h5

Upvotes: 0

Navendu Pottekkat
Navendu Pottekkat

Reputation: 335

The folder 'models' was not created and that seems to have been the issue. Manually creating the 'models' folder would fix the issue and the model would get saved in the correct path

Upvotes: 1

Related Questions