【问题标题】:Unable to save ML model to Google Drive in Google Colab无法在 Google Colab 中将 ML 模型保存到 Google Drive
【发布时间】:2020-06-19 13:07:05
【问题描述】:

这是我用来保存模型的函数。文件夹“模型”不存在。但它不应该创建一个文件夹'models'并使用写入的文件名保存模型。我在任何地方都找不到解决方案。

# 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

我正在调用这个函数

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

这是我在执行函数时遇到的错误

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)

完整的错误信息



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)


【问题讨论】:

    标签: python tensorflow machine-learning google-drive-api google-colaboratory


    【解决方案1】:

    文件夹“模型”未创建,这似乎是问题所在。手动创建“模型”文件夹将解决问题,模型将保存在正确的路径中

    【讨论】:

      【解决方案2】:

      试试这个:

      model_path = 'saved_model/my_model_1'
      
      model.save_pretrained(model_path)
      tokenizer.save_pretrained(model_path)
      

      model_path = 'saved_model/my_model_1.h5'
      
      model.save_pretrained(model_path)
      tokenizer.save_pretrained(model_path)
      

      检查:

      !mkdir -p saved_model
      

      检查保存的模型:

      !ls saved_model
      

      检查它是否包含资产文件夹、saved_model.pb 和变量文件夹。

      !ls my_model_1.h5
      

      【讨论】:

      • 这个答案的格式相当混乱。请将代码放在代码块中,并一致地格式化标题。
      猜你喜欢
      • 2019-08-30
      • 2021-01-30
      • 2021-02-24
      • 1970-01-01
      • 2018-09-27
      • 2022-01-27
      • 2022-08-20
      • 2022-07-20
      • 1970-01-01
      相关资源
      最近更新 更多