cancel
Showing results for 
Search instead for 
Did you mean: 

How to store model weights in an artifact and reload ?

marcus_schiffer
Active Participant
0 Kudos

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.

Accepted Solutions (0)

Answers (2)

Answers (2)

elham
Discoverer
0 Kudos

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

elham
Discoverer
0 Kudos

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())