python - 将保存的 keras 模型从 gs 加载到 pydatalab

标签 python model keras google-cloud-datalab

我的 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、bytes 或 os.PathLike 对象,而不是 GFile

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

Error: IOError: Unable to open file (unable to open file: name = 'gs://mybucket/model.h5', errno = 2, error message = 'No such file or directory', 标志 = 0, o_flags = 0)

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

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

最佳答案

从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)

关于python - 将保存的 keras 模型从 gs 加载到 pydatalab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48392457/

相关文章:

python - Python和ElasticSearch:使用索引将CSV转换为JSON

python - Django 和 ManyToMany 字段

python - 在 Windows 10 上通过 TensorFlow 安装 Keras (Python 3.5.3)

tensorflow - TF2.0 中 Keras 损失中 `sample_weight` 参数的奇怪形状要求

Python SSL 套接字证书验证失败错误

Python 两个整数循环,+1 到 A 直到 A=​​B

objective-c - Objective C 域驱动设计

php - 获取模型名称以在 CakePHP 的 AppModel 中实现可重用的方法

ruby-on-rails - Rspec 没有看到我的模型类。未初始化常量错误

python - tensorflow.keras.preprocessing.text.Tokenizer.texts_to_sequences 的 Numpy 数组给出了奇怪的输出,list([2]) 而不是 [[2]]