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

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

问题描述

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

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

我无法在pydatalab上加载模型.将模型保存在本地计算机上时,可以使用load_model(filepath)打开它. 我也确实将keras.backend导入为K,基于 打开使用Tensorflow Backend的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))

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

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

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

错误:IO错误:无法打开文件(无法打开文件:name ='gs://mybucket/model.h5',errno = 2,错误消息='无此类文件或目录',标志= 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)

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

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

推荐答案

从gs存储加载文件

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天全站免登陆