【发布时间】:2020-05-12 15:18:18
【问题描述】:
我正在尝试加载预训练的 Keras 模型,以便在谷歌云上继续训练。它在本地工作,只需使用
加载鉴别器和生成器 model = load_model('myPretrainedModel.h5')
但显然这在谷歌云上不起作用,我尝试使用与从谷歌存储桶中读取训练数据相同的方法:
fil = "gs://mygcbucket/myPretrainedModel.h5"
f = BytesIO(file_io.read_file_to_string(fil, binary_mode=True))
return np.load(f)
但这似乎不适用于加载模型,运行作业时出现以下错误。
ValueError:allow_pickle=False 时无法加载包含腌制数据的文件
添加allow_pickle=True,会引发另一个错误:
OSError: 无法将文件 <_io.bytesio object at> 解释为 pickle
然后我尝试了一些我发现的东西,因为有人建议我解决类似的问题,据我了解,它会暂时从存储桶中重新保存模型本地(与作业运行的位置相关),然后加载它,使用:
fil = "gs://mygcbucket/myPretrainedModel.h5"
model_file = file_io.FileIO(fil, mode='rb')
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_model(temp_model_location)
return model
但是,这会引发以下错误:
TypeError: Expected binary or unicode string, got tensorflow.python.lib.io.file_io.FileIO object
我必须承认,我不确定我需要做什么才能从我的存储桶中实际加载预训练的 keras 模型,以及在我在谷歌云的培训工作中的用途。非常感谢任何帮助。
【问题讨论】:
标签: python keras google-cloud-platform google-cloud-ai