gpt4 book ai didi

azure - 读取存储为 .h5 的 blob 并在 Python 中的 Azure Functions 中加载 keras 模型

转载 作者:行者123 更新时间:2023-12-03 00:58:24 26 4
gpt4 key购买 nike

我正在尝试将多个 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()

enter image description here

关于azure - 读取存储为 .h5 的 blob 并在 Python 中的 Azure Functions 中加载 keras 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64120272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com