【问题标题】:how to fix 'Unable to create file' error when trying to save model weights while training如何在训练时尝试保存模型权重时修复“无法创建文件”错误
【发布时间】:2020-01-01 06:19:36
【问题描述】:

我在训练时正在做图像字幕项目,我试图节省模型重量 通过 model.save('./model_weights1/model_'+ str(i) + '.h5') 在每个时期之后,但我得到错误

返回

无法创建文件(无法打开文件:name = './model_weights1/model_0.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 602)

我已检查目录路径是否存在,路径似乎也正确

代码:

for i in range(epochs):
    generator = data_loader(train_descriptions,encoded_images,maxlen,batch_size,word_to_idx)
    model.fit_generator(generator,epochs=1,steps_per_epoch=steps,verbose = 1)
    model.save('./model_weights1/model_'+ str(i) + '.h5')

错误


OSError                                   Traceback (most recent call last)
<ipython-input-58-00c04165c7ca> in <module>()
      2     generator = data_loader(train_descriptions,encoded_images,maxlen,batch_size,word_to_idx)
      3     model.fit_generator(generator,epochs=1,steps_per_epoch=steps,verbose = 1)
----> 4     model.save('./model_weights1/model_'+ str(i) + '.h5',overwrite=True)

~/anaconda3/lib/python3.6/site-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
   1088             raise NotImplementedError
   1089         from ..models import save_model
-> 1090         save_model(self, filepath, overwrite, include_optimizer)
   1091 
   1092     def save_weights(self, filepath, overwrite=True):

~/anaconda3/lib/python3.6/site-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
    377         opened_new_file = False
    378 
--> 379     f = h5dict(filepath, mode='w')
    380 
    381     try:

~/anaconda3/lib/python3.6/site-packages/keras/utils/io_utils.py in __init__(self, path, mode)
    184             self._is_file = False
    185         elif isinstance(path, str):
--> 186             self.data = h5py.File(path, mode=mode)
    187             self._is_file = True
    188         elif isinstance(path, dict):

~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    267             with phil:
    268                 fapl = make_fapl(driver, libver, **kwds)
--> 269                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    270 
    271                 if swmr_support:

~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    103         fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
    104     elif mode == 'w':
--> 105         fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
    106     elif mode == 'a':
    107         # 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 = './model_weights1/model_0.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 602)

【问题讨论】:

    标签: python machine-learning deep-learning training-data


    【解决方案1】:

    经过数小时的搜索,这就是有效的方法。您需要输入完整的文件路径。比如/home/Desktop/Your_Directory/

    【讨论】:

      【解决方案2】:

      如果你只是想节省重量,比这更容易做到

      import os
      

      然后定义你的 wieghts_path 并保存它们:

      weights_path = os.getcwd() + "\\model_weights\\your_model.h5"
      model.save_weights(weights_path)
      

      【讨论】:

        【解决方案3】:

        路径有问题

        
        import os
        os.getcwd()
        
        

        知道当前的工作目录。 然后改变

        model.save('./model_weights1/BLABLABLA)
        

        路径不错

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-05
          • 2018-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-15
          • 1970-01-01
          • 2017-07-08
          相关资源
          最近更新 更多