作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将多个 Keras 模型或权重加载到已部署的 Azure Function 中。 Blob 以 .h5 格式保存。我已经访问了容器和特定的 blob,如下所示:
from azure.storage.blob import BlockBlobService
blob_service=BlockBlobService(account_name=STORAGE_ACCOUNT_NAME,account_key=STORAGE_ACCOUNT_KEY)
blob = blob_service.get_blob_to_bytes(CONTAINER_NAME,BLOB_NAME)
myBlob = blob.content
这会返回一个字节对象,Keras 的 load_model 方法无法使用该对象:
from keras.models import load_model
load_model(myBlob)
>>> ValueError: stat: embedded null character in path
有没有办法将 blob 读取为 .h5 而不是字节?或者以 Keras 可解释的方式读取字节对象的方法?
最佳答案
关于该问题,请引用以下代码
from keras.models import Sequential, load_model
import h5py
from azure.storage.blob import BlobClient
from io import BytesIO
con_str = ''
blob_client = BlobClient.from_connection_string(con_str, 'test', 'model.h5')
downloader = blob_client.download_blob(0)
with BytesIO() as f:
downloader.readinto(f)
with h5py.File(f, 'r') as h5file:
model = load_model(h5file)
model.summary()
关于azure - 读取存储为 .h5 的 blob 并在 Python 中的 Azure Functions 中加载 keras 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64120272/
我是一名优秀的程序员,十分优秀!