【问题标题】:loading saved keras model from gs to pydatalab将保存的 keras 模型从 gs 加载到 pydatalab
【发布时间】:2018-01-23 00:36:56
【问题描述】:

我的 keras 模型使用 model.save(model_name) 保存在谷歌存储中

我无法在 pydatalab 上加载模型。当我将模型保存在本地机器上时,我可以使用 load_model(filepath) 打开它。 我也将 keras.backend 导入为 K,基于 NameError when opening Keras model that uses Tensorflow Backend

我尝试了以下方法:

  1. model = load_model(tf.gfile.Open(model_file))
    

错误:TypeError:预期的 str、字节或 os.PathLike 对象,而不是 GFile

  1. load_model('gs://mybucket/model.h5')
    

错误:IOError:无法打开文件(无法打开文件:名称 = 'gs://mybucket/model.h5',errno = 2,错误消息 = '没有这样的文件或目录',标志 = 0, o_flags = 0)

  1. with file_io.FileIO(model_file, 'r') as f:
    modl = load_model(f)
    

错误:TypeError:预期的 str、字节或 os.PathLike 对象,而不是 FileIO

【问题讨论】:

    标签: python model keras google-cloud-datalab


    【解决方案1】:

    从 gs 存储中加载文件

    from tensorflow.python.lib.io import file_io
    model_file = file_io.FileIO('gs://mybucket/model.h5', mode='rb')
    

    在本地保存模型的临时副本

    temp_model_location = './temp_model.h5'
    temp_model_file = open(temp_model_location, 'wb')
    temp_model_file.write(model_file.read())
    temp_model_file.close()
    model_file.close()
    

    加载本地保存的模型

    model = load_model(temp_model_location)
    

    【讨论】:

    • 我猜这实际上是比将文件下载到本地路径更好的解决方案!
    【解决方案2】:

    我认为 Keras 不支持 TensorFlow 文件系统,而后者又知道如何从 GCS 中读取数据。

    您可以尝试从 GCS 下载到本地路径,然后从中读取以加载模型。

    【讨论】:

    • 感谢您的回答。这就是我目前正在做的事情。我的工作流程是在 datalab 中测试代码并在 google ml 引擎中训练模型。它会减慢我在本地下载模型和测试数据的过程。我正在寻找更好的方法!
    • 抱歉,“本地”是指相对于您加载模型的位置。不建议下载到您的物理本地桌面/笔记本电脑。因此,您无需将 gs:// 路径传递给 load_model,而是将二进制文件下载(可能使用 gsutil)到“本地”磁盘上的路径,然后在调用 load_model 时使用普通文件路径。希望对您有所帮助。
    • 我会试试的!谢谢!
    【解决方案3】:

    以下函数适用于在 gcloud 机器学习平台上重新训练已经训练的 keras 模型(使用新数据)(感谢 Tíarnán McGrath)。

    def load_models(model_file):
    
        model = conv2d_model() #the architecture of my model, not compiled yet
        file_stream = file_io.FileIO(model_file, mode='r')
        temp_model_location = './temp_model.h5'
        temp_model_file = open(temp_model_location, 'wb')
        temp_model_file.write(file_stream.read())
        temp_model_file.close()
        file_stream.close()
        model.load_weights(temp_model_location)
    
        return model
    

    由于某种原因,keras.models 中的load_model 不再适合我,所以我每次都必须构建模型。

    【讨论】:

      【解决方案4】:

      也可以使用操作系统级别的命令,以防有人在使用 Colab

      使用谷歌驱动器来堆砌

      from google.colab import drive
      drive.mount('/content/drive', force_remount=True)
      

      安装 GCS 的代码

      from google.colab import auth
      auth.authenticate_user()
      project_id = 'thirumalai_bucket'  #your bucket here
      !gcloud config set project {project_id}
      !gsutil ls
      

      !gsutil -m cp

      在你的情况下:

      !gsutil -m cp gs://mybucket/model.h5  /content/drive/My\ Drive/models/ 
      

      现在文件 model.h5 在 drive /content/drive/My Drive/models/ 中可用 使用以下命令移动到您的模型目录:

      !cd /content/drive/My\ Drive/models/
      
      load_model('model.h5')
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        • 2021-03-08
        • 2021-12-25
        相关资源
        最近更新 更多