[英]how to read keras .h5 model from gcp storage bucket
我有一个 keras model 作为 h5 文件保存在 gcp 存储桶存储中,我使用以下代码读取:
from keras.models import load_model
import h5py
import gcsfs
FS = gcsfs.GCSFileSystem(project="bucketname")
with FS.open(fn_model, 'rb') as model_file:
model_gcs = h5py.File(model_file, 'r')
myModel = load_model(model_gcs)
我收到错误AttributeError: 'str' object has no attribute 'decode'
然后我尝试了:
from keras.models import load_model
import h5py
import gcsfs
FS = gcsfs.GCSFileSystem(project="bucketname")
with FS.open(fn_model, 'rb', "utf-8") as model_file:
model_gcs = h5py.File(model_file, 'r')
myModel = load_model(model_gcs)
现在错误是unsupported operand type(s) for +: 'int' and 'str'
我在stackoverflow中尝试了某人的答案,如下所示:
from tensorflow.python.lib.io import file_io
model_file = file_io.FileIO(fn_model, 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)
仍然有错误: 'str' object has no attribute 'decode'
我使用 gsutil 将.h5 文件复制到本地驱动器,然后尝试 load_model,但仍然错误'str' object has no attribute 'decode'
从 gcp 存储桶读取.h5 Keras model 的正确方法是什么?
从存储桶加载 model 时,我遇到了同样的问题。这就是我所做的。
/home/jupyter/project/
位置import subprocess
import os
model_name = "model_file.h5"
subprocess.call(f"gsutil cp gs://bucket_name/model_path/{model_name} /home/jupyter/project/",shell=True)
myModel = load_model(f"/home/jupyter/project/{model_name}")
os.remove(f"/home/jupyter/project/{model_name}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.