Hi,
we want to use a ML scenario for a keras model.
We can train the model and store the model weights as artifact.
s_buf = io.StringIO() model.keras_model.save_weights(s_buf) api.send("modelBlob", s_buf.read())
However, we fail to load the weights from the artifact input when applying the model.
How would that be done in Python ?
This e.g. does not work when importing a blob from the artifact storage.
model.load_weights(modelBlob)
Please advise.
There are two possibilities, either pickle the weights or save the whole model where the result is bytes.
p.s. in order to use save_model, be sure that your model doesn't contain customised layer.
saving_model:
import h5py from io import BytesIO model = BytesIO() with h5py.File(model, 'a') as f: bert_classifier.save(f, save_format="tf") api.send("model_blob", model.getvalue())
load_model:
def on_model(model_blob): import h5py model = io.BytesIO(model_blob) h = h5py.File(model,'r') model = tf.keras.models.load_model(h) model_ready = True
Add a comment