Backend
DL4J Keras model import is backend-agnostic — models trained with TensorFlow, Theano, or CNTK backends can all be imported into Eclipse Deeplearning4j 1.0.0-M2.1.
DL4J Keras model import is backend agnostic. Regardless of which backend was used to train the Keras model — TensorFlow, Theano, or CNTK — the model can be imported into DL4J. The importer reads the serialised HDF5 file and interprets the layer configurations and weights without relying on the original backend being present.
Supported Backends
TensorFlow
Yes
The most common backend; widest test coverage
Theano
Yes
Supported; some weight axis orderings differ
CNTK
Yes
Supported; limited production usage
Theano vs. TensorFlow Weight Ordering
The primary difference between backends relates to weight axis ordering for convolutional layers:
TensorFlow (channels-last,
NHWC): filter shape is(height, width, in_channels, out_channels)Theano (channels-first,
NCHW): filter shape is(out_channels, in_channels, height, width)
DL4J automatically detects the backend from the backend field stored in the Keras model config and transposes convolutional weights as needed during import. You do not need to configure this manually.
Detecting the Backend from an HDF5 File
The backend is stored in the Keras model config JSON inside the HDF5 file:
import h5py, json
with h5py.File("model.h5", "r") as f:
config = json.loads(f.attrs["model_config"])
backend = config.get("backend", "tensorflow")
print("Backend:", backend)Keras Version Compatibility
DL4J model import supports both Keras 1 and Keras 2 model formats. The major version is detected automatically from the HDF5 file metadata.
Keras 1.x
Yes
Legacy format; field names differ slightly
Keras 2.x
Yes
Current recommended format
tf.keras (TF 2.x)
Yes
Saved with model.save() in HDF5 format
Backend Configuration in DL4J
DL4J itself uses ND4J as its numerical backend, which can be accelerated with either CPU (OpenBLAS/MKL) or GPU (CUDA) via its own backend system. This is independent of the Keras training backend.
To configure ND4J backends, set the appropriate dependency in your pom.xml:
Saving a Keras Model for Import
Always save Keras models in the HDF5 format using model.save() to ensure both the architecture and weights are available:
Import Examples
Notes
Models saved in the TensorFlow SavedModel format (a directory rather than a single
.h5file) are not supported by the Keras import module. Use DL4J's TensorFlow import instead.Models saved with
tf.saved_model.save()ormodel.save('dir/')in TF2 use the SavedModel format by default. Passsave_format='h5'to force HDF5 output.The
keras.jsonbackend configuration file on the training machine does not need to be present on the DL4J inference machine.
Last updated
Was this helpful?