Skip to Content

How to store model weights in an artifact and reload ?

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.

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Mar 03 at 12:41 PM

    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())
    
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 03 at 12:42 PM

    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
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.