KerasModelImport: simple example of saving a keras model?

Hello,

I am trying to import a sequential model trained from keras, following the example at deeplearning4j-examples/tensorflow-keras-import-examples/src/main/java/org/deeplearning4j/modelimportexamples/keras/quickstart/SimpleSequentialMlpImport.java at 051c59bd06b38ed39ca92f5940a6ca43b0f34c0f · deeplearning4j/deeplearning4j-examples · GitHub

I am always getting errors, because however I am saving my model in keras is not what dl4j is expecting when I load the .h5 file. My current error:

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.(KerasSequentialModel.java:99)
at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.(KerasSequentialModel.java:56)
at org.deeplearning4j.nn.modelimport.keras.KerasModel$ModelBuilder.buildSequential(KerasModel.java:646)
at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:156)
at SimpleSequentialMlpImport.main(SimpleSequentialMlpImport.java:78)

Can someone provide me with a minimal example in python of saving a sequential model, that I could then try to load in java to troubleshoot my code?

This is the model I am saving

dmodel = tf.keras.Sequential([

        Conv2D(128, 1, activation=tf.keras.activations.tanh,

               kernel_initializer=tf.random_uniform_initializer), 

        tf.keras.layers.LayerNormalization(axis=-1), 

        Conv2D(self.channel_n, 1, activation=tf.keras.activations.sigmoid,

            kernel_initializer=tf.random_uniform_initializer),

    ])

[...]

dmodel.save("model_dl4j.h5", save_format='h5')

And how I am loading

final String SIMPLE_MLP = new File(dataLocalPath,"model_dl4j.h5").getAbsolutePath();

KerasModelImport.importKerasSequentialModelAndWeights(SIMPLE_MLP);

It also does not work with the default saving method of keras (“xxx.keras”) unzipped, whether I load the weights or the configuration. Am I correct to assume that the import function is just very old and no one uses it anymore?

Here is an example.
Config.json

{"module": "keras", "class_name": "Sequential", "config": {"name": "sequential_19", "trainable": true, "dtype": {"module": "keras", "class_name": "DTypePolicy", "config": {"name": "float32"}, "registered_name": null, "shared_object_id": 135345624290544}, "layers": [{"module": "keras.layers", "class_name": "InputLayer", "config": {"batch_shape": [1, 3, 3, 9], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_layer_13", "optional": false}, "registered_name": null}, {"module": "keras.layers", "class_name": "Conv2D", "config": {"name": "conv2d_38", "trainable": true, "dtype": {"module": "keras", "class_name": "DTypePolicy", "config": {"name": "float32"}, "registered_name": null}, "filters": 128, "kernel_size": [1, 1], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "GlorotUniform", "config": {"seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [1, 3, 3, 9]}}, {"module": "keras.layers", "class_name": "Conv2D", "config": {"name": "conv2d_39", "trainable": true, "dtype": {"module": "keras", "class_name": "DTypePolicy", "config": {"name": "float32"}, "registered_name": null, "shared_object_id": 135345624290544}, "filters": 3, "kernel_size": [1, 1], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "tensorflow.python.ops.init_ops_v2", "class_name": "Zeros", "config": {}, "registered_name": "Zeros"}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "registered_name": null, "build_config": {"input_shape": [1, 3, 3, 128]}}], "build_input_shape": [1, 3, 3, 9]}, "registered_name": null, "build_config": {"input_shape": [1, 3, 3, 9]}, "compile_config": {}}

Import:

 KerasModelImport.importKerasSequentialConfiguration("config.json")

If anyone can show me any working minimal example available anywhere, of saving any Keras model and importing it in Java…

@Lana which version of keras are you using? The M2.1 release does not support keras 3. That support will come in the next release. I am in the middle of final testing now.