# KerasModelImport: simple example of saving a keras model?

**URL:** <https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521>\
**Category:** DL4J\
**Created:** [June 12, 2026, 2:29pm UTC](https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521 "2026-06-12T14:29:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lana](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@Lana](https://community.konduit.ai/u/Lana)\
**Post date:** [June 12, 2026, 2:29pm UTC](https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521/1 "2026-06-12T14:29:40Z")

</div>

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](https://github.com/deeplearning4j/deeplearning4j-examples/blob/051c59bd/tensorflow-keras-import-examples/src/main/java/org/deeplearning4j/modelimportexamples/keras/quickstart/SimpleSequentialMlpImport.java#L79-L79)

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

```auto
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

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

KerasModelImport.importKerasSequentialModelAndWeights(SIMPLE_MLP);

```

---

<div class="post-metadata">

**Author:** ![Lana](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@Lana](https://community.konduit.ai/u/Lana)\
**Post date:** [June 15, 2026, 5:08am UTC](https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521/2 "2026-06-15T05:08:56Z")

</div>

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

```auto
{"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:

```auto
 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…

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [June 15, 2026, 7:47pm UTC](https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521/3 "2026-06-15T19:47:56Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Lana](https://avatars.discourse-cdn.com/v4/letter/l/d2c977/32.png) [@Lana](https://community.konduit.ai/u/Lana)\
**Post date:** [June 18, 2026, 12:57am UTC](https://community.konduit.ai/t/kerasmodelimport-simple-example-of-saving-a-keras-model/3521/4 "2026-06-18T00:57:17Z")

</div>

Ah, well that makes sense. I’d be using keras 3.13.2… Thank you for telling me what the issue is.  
I may downgrade and wait for the next update!
