[英]How to upload .h5 file(Keras) to Azure Blob Storage without saving it in disk in Python
I am creating a model in Keras and want to save the model to Azure Blob Storage.我正在 Keras 中创建 model 并想将 model 保存到 Z3A580F142203677F1F0BC335F9 存储。
I tried uploading it using upload_blob, but the model is sequential and the blob service expects serialized object.我尝试使用 upload_blob 上传它,但 model 是连续的,并且 blob 服务需要序列化的 object。 Although I am able to upload.pkl (pickle) format easily.
虽然我可以轻松上传.pkl(pickle)格式。
I tried serializing the model using pickle, but after uploading the model is corrupt and non-usable.我尝试使用 pickle 序列化 model,但在上传 model 后已损坏且无法使用。
# MLP for Pima Indians Dataset saved to single file
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf
import pickle
# load pima indians dataset
dataset = loadtxt("https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# save model and architecture to single file
model.save("model.h5")
print("Saved model to disk")
pickle_file = pickle.dumps(model)
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
connect_str = ""
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# # Create a unique name for the container
container_name = ""
remote_file_name = "model"
blob_client = blob_service_client.get_blob_client(container=container_name, blob=remote_file_name)
blob_client.upload_blob(pickle_file)
I am using the basic model from machinelearningmastery.com我正在使用来自 machinelearningmastery.com 的基本 model
I found a way for now.我暂时找到了办法。 I am splitting my.h5 sequential model to two parts, one as.json which will have the architecture and the second part as a binary which will have the weights.
我将 my.h5 顺序 model 拆分为两部分,一个 as.json 将具有架构,第二部分作为具有权重的二进制文件。 I can upload both of them in the blob storage, next time I need them I can download both of them, merge and use it.
我可以将它们都上传到 blob 存储中,下次我需要它们时,我可以下载它们,合并并使用它。
# serialize model to JSON
# model_weights = model.get_weights()
# model_json = model.to_json()
# model_weights_pickle = pickle.dumps(model_weights)
# model_json_pickle = pickle.dumps(model_json)
After this you can load the model using model_from_json
and model.set_weights
在此之后,您可以使用
model_from_json
和model.set_weights
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.