CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Keras model import functionality for DeepLearning4J

Pending
Overview
Eval results
Files

pretrained-models.mddocs/

Pre-trained Models (Deprecated)

Note: This functionality is deprecated. For new projects, use the deeplearning4j-zoo module which provides a more comprehensive and maintained set of pre-trained models.

Legacy support for popular pre-trained image classification models, specifically VGG16 variants with ImageNet weights.

TrainedModels Enum

The TrainedModels enum provides access to pre-trained models with automatic downloading and setup.

public enum TrainedModels {
    VGG16,      // VGG16 with ImageNet weights and classification head
    VGG16NOTOP; // VGG16 with ImageNet weights, no classification head
    
    // Get the complete model as ComputationGraph
    public ComputationGraph getComputationGraph() throws IOException;
    
    // Get appropriate preprocessor for the model
    public DataSetPreProcessor getDataSetPreProcessor();
    
    // Get ImageNet class labels
    public ArrayList<String> getLabels();
    
    // Get expected input shape
    public int[] getInputShape();
}

Available Models

VGG16

Complete VGG16 model with ImageNet weights and classification head.

TrainedModels.VGG16

Specifications:

  • Input Shape: 224 × 224 × 3 (RGB images)
  • Output: 1000 classes (ImageNet categories)
  • Weights: Pre-trained on ImageNet dataset
  • Architecture: 16-layer VGG network with classification head

VGG16NOTOP

VGG16 model without the final classification layers, useful for feature extraction and transfer learning.

TrainedModels.VGG16NOTOP

Specifications:

  • Input Shape: 224 × 224 × 3 (RGB images)
  • Output: Feature vectors (7 × 7 × 512)
  • Weights: Pre-trained on ImageNet dataset
  • Architecture: VGG16 without final dense layers

Usage Examples

Basic Image Classification

import org.deeplearning4j.nn.modelimport.keras.trainedmodels.TrainedModels;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.nd4j.linalg.dataset.api.preprocessor.DataSetPreProcessor;

// Load VGG16 model
ComputationGraph vgg16 = TrainedModels.VGG16.getComputationGraph();

// Get appropriate preprocessor
DataSetPreProcessor preprocessor = TrainedModels.VGG16.getDataSetPreProcessor();

// Load and preprocess image
BufferedImage image = ImageIO.read(new File("image.jpg"));
INDArray imageArray = convertImageToINDArray(image); // Custom conversion method
preprocessor.preProcess(new DataSet(imageArray, null));

// Make prediction
INDArray output = vgg16.outputSingle(imageArray);

// Get predicted class
int predictedClass = Nd4j.argMax(output, 1).getInt(0);
String predictedLabel = TrainedModels.VGG16.getLabels().get(predictedClass);

System.out.println("Predicted: " + predictedLabel);

Feature Extraction

import org.deeplearning4j.nn.modelimport.keras.trainedmodels.TrainedModels;
import org.deeplearning4j.nn.graph.ComputationGraph;

// Load VGG16 without top layers for feature extraction
ComputationGraph featureExtractor = TrainedModels.VGG16NOTOP.getComputationGraph();

// Extract features from image
INDArray imageArray = preprocessedImage; // Your preprocessed image
INDArray features = featureExtractor.outputSingle(imageArray);

// Features shape: [1, 7, 7, 512] - can be flattened for use in other models
INDArray flatFeatures = features.reshape(1, 7 * 7 * 512);

Transfer Learning Setup

// Load feature extractor
ComputationGraph baseModel = TrainedModels.VGG16NOTOP.getComputationGraph();

// Create new model with custom classification head
ComputationGraphConfiguration.GraphBuilder confBuilder = new NeuralNetConfiguration.Builder()
    .graphBuilder();

// Add VGG16 layers (frozen)
// ... copy layers from baseModel ...

// Add custom classification layers
confBuilder.addLayer("custom_dense", new DenseLayer.Builder()
    .nIn(7 * 7 * 512)
    .nOut(128)
    .activation(Activation.RELU)
    .build(), "vgg16_features");

confBuilder.addLayer("output", new OutputLayer.Builder()
    .nIn(128)
    .nOut(numCustomClasses)
    .activation(Activation.SOFTMAX)
    .lossFunction(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
    .build(), "custom_dense");

// Build and initialize custom model
ComputationGraph customModel = new ComputationGraph(confBuilder.build());
customModel.init();

ImageNet Labels

Access to ImageNet class labels for interpreting model predictions.

ImageNetLabels Utility Class

public class ImageNetLabels {
    // Get all 1000 ImageNet class labels
    public static ArrayList<String> getLabels();
    
    // Get specific class label by index (0-999)
    public static String getLabel(int n);
}

Usage Examples

import org.deeplearning4j.nn.modelimport.keras.trainedmodels.Utils.ImageNetLabels;

// Get all labels
ArrayList<String> allLabels = ImageNetLabels.getLabels();
System.out.println("Total classes: " + allLabels.size());

// Get specific label
String label = ImageNetLabels.getLabel(281); // "tabby, tabby cat"
System.out.println("Class 281: " + label);

// Top-k predictions
INDArray predictions = model.outputSingle(input);
int[] topK = getTopKIndices(predictions, 5); // Custom method to get top-5

System.out.println("Top 5 predictions:");
for (int i = 0; i < topK.length; i++) {
    double confidence = predictions.getDouble(topK[i]);
    String className = ImageNetLabels.getLabel(topK[i]);
    System.out.println((i+1) + ". " + className + " (" + confidence + ")");
}

Data Preprocessing

VGG16ImagePreProcessor

The VGG16 models require specific preprocessing to match training conditions.

// VGG16 preprocessing (from nd4j-dataset-api)
public class VGG16ImagePreProcessor implements DataSetPreProcessor {
    // Applies VGG16-specific preprocessing:
    // - Converts RGB to BGR
    // - Subtracts ImageNet mean values
    // - Scales appropriately
}

Custom Preprocessing Pipeline

import org.nd4j.linalg.dataset.api.preprocessor.VGG16ImagePreProcessor;
import org.datavec.image.loader.NativeImageLoader;

// Image loading and preprocessing pipeline
NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
VGG16ImagePreProcessor preprocessor = new VGG16ImagePreProcessor();

// Load image
BufferedImage image = ImageIO.read(new File("input.jpg"));
INDArray imageArray = loader.asMatrix(image);

// Apply preprocessing
DataSet ds = new DataSet(imageArray, null);
preprocessor.preProcess(ds);
INDArray processedImage = ds.getFeatures();

// Now ready for VGG16 inference
INDArray prediction = vgg16Model.outputSingle(processedImage);

Model Caching and Downloads

Automatic Model Management

The TrainedModels enum automatically handles:

  • Model downloading from remote URLs
  • Local caching in ~/.dl4j/trainedmodels/ directory
  • Version management and updates

Cache Structure

~/.dl4j/trainedmodels/
├── vgg16/
│   ├── vgg16.json          # Model architecture
│   └── vgg16_weights.h5    # Pre-trained weights
└── vgg16notop/
    ├── vgg16notop.json     # Model architecture  
    └── vgg16notop_weights.h5 # Pre-trained weights

Manual Cache Management

// Models are automatically downloaded on first use
// Cache location: System.getProperty("user.home") + "/.dl4j/trainedmodels/"

// To clear cache, delete the directory manually
File cacheDir = new File(System.getProperty("user.home"), ".dl4j/trainedmodels");
if (cacheDir.exists()) {
    // Delete cache directory if needed
}

Migration to DL4J Zoo

Recommended Migration Path

Instead of using the deprecated TrainedModels enum, use the deeplearning4j-zoo module:

<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-zoo</artifactId>
    <version>0.9.1</version>
</dependency>
// New approach with DL4J Zoo
import org.deeplearning4j.zoo.model.VGG16;
import org.deeplearning4j.zoo.PretrainedType;

// Load VGG16 from zoo
VGG16 vgg16 = VGG16.builder().build();
ComputationGraph model = (ComputationGraph) vgg16.initPretrained(PretrainedType.IMAGENET);

Benefits of DL4J Zoo

  • More Models: ResNet, AlexNet, LeNet, etc.
  • Better Maintenance: Active development and updates
  • Improved APIs: More consistent and feature-rich
  • Better Documentation: Comprehensive examples and guides
  • Performance: Optimized implementations

Limitations

Deprecated Status

  • No new features or models will be added
  • Limited bug fixes and maintenance
  • May be removed in future versions

Model Limitations

  • Only VGG16 variants available
  • Fixed input sizes (224×224 for VGG16)
  • ImageNet-specific preprocessing requirements
  • Limited customization options

Compatibility Issues

  • May not work with newer Keras/TensorFlow versions
  • HDF5 format dependencies
  • Network connectivity required for initial download

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