hlamba28/One-Shot-Learning-with-Siamese-Networks

Unable to Load Saved Model

rohanneps opened this issue · 1 comments

I trained the model on a custom dataset using a custom generator as opposed to loading in-memory and have saved the Keras sequential model using the ModelCheckpoint callback.

However, on loading model as model = load_model('..'), I get the following error:

Traceback (most recent call last):
File "predict.py", line 25, in
model = load_model(os.path.join('models','binary_custom.hdf5'))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/sequential.py", line 300, in from_config
custom_objects=custom_objects)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1109, in from_config
return cls(**config)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/convolutional.py", line 490, in init
**kwargs)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/layers/convolutional.py", line 117, in init
self.kernel_initializer = initializers.get(kernel_initializer)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/initializers.py", line 511, in get
return deserialize(config)
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/initializers.py", line 503, in deserialize
printable_module_name='initializer')
File "/home/ubuntu/image_matching/env/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown initializer: initialize_weights

My python version is 3.6.7 with Keras==2.2.4 and tensorflow==1.13.1.

Any suggestions would be higly appreciated.

SOLVED!

For anyone facing the same issue, please note that we would need to pass the custom weight and bias initializer functions as custom objects through a callable(class) as shown below
...
from keras.utils.generic_utils import get_custom_objects
def initialize_weights(shape, name=None):
# same function as in training script
return np.random.normal(loc = 0.0, scale = 1e-2, size = shape)

class CustomWeightInitializer:
def call(self, shape):
return initialize_weights(shape)

get_custom_objects().update({'initialize_weights': CustomWeightInitializer,'initialize_bias': CustomBiasInitializer})

model = load_model('...')