Persisting a Tensorflow model with custom layers
If you build a custom layer model with Tensorflow 2.1 and the tf.keras
library, follow these steps to persist the model to the Watson Machine Learning repository so you can successfully deploy the model.
Requirements for V4 GA endpoints
Note these requirements if you created a model that uses a V4 GA endpoint, on Cloud Pak for Data as a Service, or Cloud Pak for Data 3.5.
- Save the source code containing the custom layer or other custom object definitions in
/ml/v4/model_definitions
. - The model metadata should be referenced in the
model_definition.id
for themodel_definition
. - The model metadata should have metadata
user_defined_objects
populated with key-value pairs as its value. The key references the name of custom layer object and the corresponding value points to its class.
Example
For a model which uses CustomLayer tf.keras Layer
class in the file custom_layers.py
in source code, the user_defined_objects
metadata will look as follows:
{
"name": "mytfmodel",
"type": "tensorflow_2.1",
"software_spec": default_py3.7,
"user_defined_objects": {
"CustomLayer": "custom_layers.CustomLayer",
}
}
Requirements for V4-beta API endpoints
Note these requirements if you created a model that uses a V4-beta endpoint, such as on Watson Machine Learning Server 2.0.0.1, or Cloud Pak for Data 3.0.1 with Patch 5 applied.
- Save the source code containing the custom layer or other custom object definitions in
wml_model_definition
asset (present asmodel_definitions
in Python client). - The model metadata should be referenced in the
model_definition.id
for themodel_definition
. - The archive file in which the .h5 model is stored must have a
custom_object.json
defined in the same directory which will take the key-value pairs as its value. The key references the name of custom layer object and the corresponding value points to its class.
Example
For a model which uses CustomLayer tf.keras Layer
class present in the file custom_layers.py
in source code, the contents of custom_object.json
metadata will look like this:
{
"CustomLayer": "custom_layers.CustomLayer",
}