CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-deeplearning4j--deeplearning4j-modelimport

Keras model import functionality for DeepLearning4J

Pending
Overview
Eval results
Files

configuration-import.mddocs/

Configuration Import

Import model configurations without weights, useful for creating model architectures that can be trained separately or loaded with different weights.

Functional API Configuration Import

Import configuration for Keras Functional API models from JSON files.

public static ComputationGraphConfiguration importKerasModelConfiguration(String modelJsonFilename)
    throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

public static ComputationGraphConfiguration importKerasModelConfiguration(String modelJsonFilename, boolean enforceTrainingConfig)
    throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

Parameters

  • modelJsonFilename (String): Path to JSON file containing Keras model configuration
  • enforceTrainingConfig (boolean): Whether to enforce training-related configurations

Returns

  • ComputationGraphConfiguration: DeepLearning4J computation graph configuration

Exceptions

  • IOException: File I/O errors when reading the JSON file
  • InvalidKerasConfigurationException: Malformed or invalid Keras model configuration
  • UnsupportedKerasConfigurationException: Keras features not supported by DeepLearning4J

Usage Examples

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
import org.deeplearning4j.nn.conf.ComputationGraphConfiguration;
import org.deeplearning4j.nn.graph.ComputationGraph;

// Import configuration only
ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

// Create model from configuration (without pre-trained weights)
ComputationGraph model = new ComputationGraph(config);
model.init();

// Now you can train the model or load weights separately

Sequential Configuration Import

Import configuration for Keras Sequential models from JSON files.

public static MultiLayerConfiguration importKerasSequentialConfiguration(String modelJsonFilename)
    throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

public static MultiLayerConfiguration importKerasSequentialConfiguration(String modelJsonFilename, boolean enforceTrainingConfig)
    throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

Parameters

  • modelJsonFilename (String): Path to JSON file containing Keras Sequential model configuration
  • enforceTrainingConfig (boolean): Whether to enforce training-related configurations

Returns

  • MultiLayerConfiguration: DeepLearning4J multi-layer network configuration

Exceptions

  • IOException: File I/O errors when reading the JSON file
  • InvalidKerasConfigurationException: Malformed or invalid Keras model configuration
  • UnsupportedKerasConfigurationException: Keras features not supported by DeepLearning4J

Usage Examples

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;

// Import Sequential configuration only
MultiLayerConfiguration config = KerasModelImport.importKerasSequentialConfiguration("sequential_config.json");

// Create model from configuration
MultiLayerNetwork model = new MultiLayerNetwork(config);
model.init();

// Train or load weights as needed

Generating Configuration Files

Keras model configurations can be saved to JSON using Keras/TensorFlow:

Python Code to Export Configuration

# In Python with Keras
import keras

# Load or create your model
model = keras.models.load_model('my_model.h5')

# Save configuration to JSON
model_json = model.to_json()
with open('model_config.json', 'w') as f:
    f.write(model_json)

# For Sequential models
if hasattr(model, 'get_config'):
    config = model.get_config()
    with open('sequential_config.json', 'w') as f:
        json.dump({'class_name': 'Sequential', 'config': config}, f)

Use Cases

Transfer Learning

// Import architecture from pre-trained model
ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("pretrained_config.json");

// Create new model with same architecture
ComputationGraph model = new ComputationGraph(config);
model.init();

// Train on your specific dataset
// ... training code ...

Model Architecture Exploration

// Import and examine model configuration
ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

// Access configuration details
System.out.println("Number of layers: " + config.getVertices().size());
System.out.println("Input types: " + Arrays.toString(config.getNetworkInputTypes()));
System.out.println("Output names: " + Arrays.toString(config.getNetworkOutputs()));

Custom Weight Loading

// Import configuration
ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

// Create model
ComputationGraph model = new ComputationGraph(config);
model.init();

// Load custom weights (not from Keras)
// ... custom weight loading logic ...

Configuration File Format

The JSON configuration file should follow Keras model serialization format:

Functional API Model Format

{
  "class_name": "Model",
  "config": {
    "name": "model_name",
    "layers": [...],
    "input_layers": [...],
    "output_layers": [...]
  }
}

Sequential Model Format

{
  "class_name": "Sequential",
  "config": [
    {
      "class_name": "Dense",
      "config": {...}
    },
    ...
  ]
}

Training Configuration Enforcement

When enforceTrainingConfig is set to true:

  • Unsupported training configurations will throw exceptions
  • All regularizers must be supported
  • All optimizers must be supported
  • All loss functions must be supported

When enforceTrainingConfig is set to false:

  • Unsupported configurations generate warnings but don't stop import
  • Allows importing models with partially supported features
  • Useful for inference-only scenarios

Install with Tessl CLI

npx tessl i tessl/maven-org-deeplearning4j--deeplearning4j-modelimport

docs

builder-pattern.md

configuration-import.md

index.md

layer-support.md

model-import.md

pretrained-models.md

separate-files-import.md

tile.json