将保存的 keras 模型从 gs 加载到 pydatalab [英] loading saved keras model from gs to pydatalab

查看:40
本文介绍了将保存的 keras 模型从 gs 加载到 pydatalab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My keras model is saved in google storage with model.save(model_name)

我无法在 pydatalab 上加载模型.当我将模型保存在本地机器上时,我可以使用 load_model(filepath) 打开它.我也确实将 keras.backend 导入为 K,基于打开使用 Tensorflow 后端的 Keras 模型时出现 NameError

I cannot load the model on pydatalab. When I save the model on my local machine, I can just open it with load_model(filepath). Also I did import keras.backend as K, based on NameError when opening Keras model that uses Tensorflow Backend

我尝试了以下方法:

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

错误:类型错误:预期的字符串、字节或 os.PathLike 对象,而不是 GFile

Error: TypeError: expected str, bytes or os.PathLike object, not GFile

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

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

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

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

错误:类型错误:预期的字符串、字节或 os.PathLike 对象,而不是 FileIO

error: TypeError: expected str, bytes or os.PathLike object, not FileIO

推荐答案

从 gs 存储加载文件

Load the file from gs storage

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

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

Save a temporary copy of the model locally

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)

这篇关于将保存的 keras 模型从 gs 加载到 pydatalab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆