keras demo code siamese_contrastive.py save and load model?

Viewed 125

According to the demo code

"Image similarity estimation using a Siamese Network with a contrastive loss" https://keras.io/examples/vision/siamese_contrastive/

I'm trying to save model by model.save to h5 or hdf5; however, after I used load_model (even tried load_weights) it showed error message for : unknown opcode

Have done googling job which all tells me it's python version problem between py3.5~py3.6 But actually I use only python 3.8.... other info say that there's some extra job need to be done either in model building or load_model

It would be very kind for any one to help provide the save and load model part to make this demo code more completed thanks!!

1 Answers

Actually here they are using two individual factors which come in a custom object.

Custom objects:

contrastive loss

embedding layer: where we are finding euclidean_distance.

Saving model: for the saving model, it's straightforward

<model_name>.save("siamese_contrastive.h5")

Loading model: Here the good part will come model will not load directly here because it doesn't have an understanding of two things one is your custom layer and 2nd is your loss.

model = tf.keras.models.load_model('siamese_contrastive.h5', custom_objects={ })

In the custom object mentioned above, you have to provide the definition of those two objects.

After that, it will accept your model and it will run separately at inferencing time.

Still figuring out how??

Have a look at my implementation let me know if you still have any questions: https://github.com/anukash/Keras_siamese_contrastive

Related