我正在尝试使用Keras在本地训练的NN模型进行部署。我在本地使用以下方式使用我的模型:
from keras.models import load_model
model = load_model("/path/to/model/model.h5")
prediction = model.predict(x)
现在,我需要在我的Lambda函数中使用相同的模型。我将模型上传到了一个s3
存储桶中。然后,我尝试访问文件:
model = load_model("https://s3-eu-west-1.amazonaws.com/my-bucket/models/model.h5")
但是它告诉我文件不存在。我猜是权限问题。我也尝试过像这样(类似于我从s3
读取JSON文件的方式):
client_s3 = boto3.client("s3")
result = client_s3.get_object(Bucket="my-bucket", Key='models/model.h5')
model = load_model(result["Body"].read())
但是我遇到了这个错误:
"stackTrace": [
[
"/var/task/lambda_function.py",
322,
"lambda_handler",
"model = load_model(result[\"Body\"].read())"
],
[
"/var/task/keras/models.py",
227,
"load_model",
"with h5py.File(filepath, mode='r') as f:"
],
[
"/var/task/h5py/_hl/files.py",
269,
"__init__",
"fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)"
],
[
"/var/task/h5py/_hl/files.py",
99,
"make_fid",
"fid = h5f.open(name, flags, fapl=fapl)"
],
[
"h5py/_objects.pyx",
54,
"h5py._objects.with_phil.wrapper",
null
],
[
"h5py/_objects.pyx",
55,
"h5py._objects.with_phil.wrapper",
null
],
[
"h5py/h5f.pyx",
78,
"h5py.h5f.open",
null
],
[
"h5py/defs.pyx",
621,
"h5py.defs.H5Fopen",
null
],
[
"h5py/_errors.pyx",
123,
"h5py._errors.set_exception",
null
]
],
"errorType": "UnicodeDecodeError",
"errorMessage": "'utf8' codec can't decode byte 0x89 in position 29: invalid start byte"
}
我怀疑
result["Body"].read()
函数不能与h5py
对象一起使用。如何最好地从s3
加载h5py
模型?
解决方案: 解决方案是将文件下载到/tmp/
文件夹中:result = client_s3.download_file("my-bucket",'model.h5', "/tmp/model.h5")
model = load_model("/tmp/day/model.h5")