For the complete documentation index, see llms.txt. This page is also available as Markdown.

Getting Started

Step-by-step guide to importing Keras models — saving in Python, loading in Java, and running inference

Getting Started with Keras Model Import

This guide walks through the complete workflow: defining and saving a Keras model in Python, adding the Maven dependency, loading the model in Java, and running inference. A complete working example is provided at the end.


Prerequisites

  • Java 8 or later

  • Maven 3.x

  • A trained Keras model (Keras 1.x or 2.x with any backend: TensorFlow, Theano, CNTK)


Step 1: Save Your Keras Model in Python

Keras models must be serialized before they can be imported. There are three serialization modes, each suitable for different scenarios.

This saves the model architecture, weights, and training configuration together in a single HDF5 file:

model.save('my_model.h5')

This is the recommended approach. When you load this file in DL4J, you get a fully configured model including weights. If the model was compiled in Keras, training configuration (optimizer, loss, metrics) is also preserved.

Option B: Save Architecture and Weights Separately

Save the JSON configuration and weights independently:

Use this when you want to version architecture and weights separately. Note that training configuration is not included.

Option C: Architecture Only (No Weights)

Use this to inspect or configure the model before initializing weights independently.


Step 2: Add the Maven Dependency

Add the following dependency to your pom.xml. The version should match the rest of your DL4J dependencies:

You also need ND4J with a backend. For CPU:

For GPU (CUDA):


Step 3: Load a Sequential Model as MultiLayerNetwork

A Keras Sequential model maps to a DL4J MultiLayerNetwork.

Loading from a Full Model File

If the Keras model was not compiled (no training configuration), use:

Loading from Separate Config and Weights Files

Loading Configuration Only (No Weights)


Step 4: Load a Functional API Model as ComputationGraph

A Keras Model (Functional API) maps to a DL4J ComputationGraph, which supports arbitrary graph topologies.

Loading from a Full Model File

Without training configuration enforcement:

Loading from Separate Files


Step 5: Run Inference

After import, the model is a standard DL4J network and inference works identically to a natively-built DL4J model.

MultiLayerNetwork Inference

ComputationGraph Inference


Step 6: Save the Imported Model

After import, save the model in DL4J's native format to avoid re-importing from Keras on subsequent loads:


Complete Example: Sequential MLP

The following is a self-contained example from Python training to Java inference.

Python

Java


Common Issues

Training configuration not found

Symptom: InvalidKerasConfigurationException: Could not find training configuration

Fix: The model was not compiled in Keras before saving, so no training config exists in the HDF5 file. Pass false as the enforceTrainingConfig parameter:

Unsupported layer

Symptom: UnsupportedKerasConfigurationException

Fix: The model uses a layer not yet supported. Check Supported Features. Consider replacing the unsupported layer with a supported equivalent, or implement a custom layer mapper.

ClassPathResource not found

Symptom: IOException: Could not find resource

Fix: Ensure the .h5 file is placed in src/main/resources/ of your Maven project, or provide an absolute file path directly.


Next Steps

Last updated

Was this helpful?