Solved: load model with custom objects

Loading a machine learning model with custom objects is an important element in the development process. This is even more significant when we are dealing with pre-processing functions, create custom layers, or any other custom objects in a model.

When a model makes use of custom layers or functions, it cannot be saved and loaded in the same way as a regular model because those custom-defined parts won’t be recognized. Not unless we explicitly tell our program what those custom parts are.

This process can be a bit challenging, but donโ€™t worry! In this article, we will delve into the details of loading a model with custom objects in python using Keras. Weโ€™ll take a close look at the problem, the solution, and a step-by-step walkthrough of the Python code.

Understanding the problem

When using Keras, building and training a model is generally straightforward. The issues come when we introduce customized layers, metrics, or other objects. These may be necessary for certain applications, but inherently use user-defined Python code.

def custom_function(x):
    return x+1

This ‘custom_function’ can be used as a custom metric in a model:

model.compile(loss='binary_crossentropy', 
              optimizer='adam', 
              metrics=[custom_function])

But if we now try to save and load this model:

model.save('path_to_my_model.h5')
del model
model = keras.models.load_model('path_to_my_model.h5')

We run into an error:

ValueError: Unknown metric function: custom_function

The solution

The solution is to include a dictionary of the custom objects when loading the model. The dictionary should map the name of the function to the actual function.

Here the change required is to provide the name of the function and its actual implementation in a dictionary.

model = keras.models.load_model('path_to_my_model.h5', 
                                custom_objects={'custom_function': custom_function})

Step by step explanation of the code

Defining the custom function:
First step involves defining our custom function, which might be a layer, a metric, an activation function, or anything else that was used in the model.

Compiling the model with the custom function:
We use the custom function as a metric while compiling the model. If our custom function was a layer or an activation function, it would be used in a similar way while defining the model architecture.

Saving the model:
Post training, we save our model for future use employing the model.save() function.

Loading the model:
When we need to use the model again, we load it using keras.models.load_model() function. However, we must also pass a dictionary in the โ€˜custom_objectsโ€™ parameter where the keys are the names of our custom function, and the values are the function itself.

To sum up, loading models with custom objects can seem complex at first, but by keeping track of those objects and ensuring they’re specified at load time, we keep our machine learning workflow smooth and flexible. Thus, whether you’re tinkering with unique ways to optimize your layers or testing out innovative functions, custom objects won’t stand in your way.

Related posts:

Leave a Comment