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

Image Data

Loading and preprocessing image data — ImageRecordReader, NativeImageLoader, and image transforms

DataVec provides specialized tooling for image datasets. The image pipeline handles the full journey from a directory of JPEG/PNG files through to normalized DataSet tensors ready for convolutional neural network training.

Directory Structure for Labeled Images

The most common image dataset format is a directory where each subdirectory is a class label:

/data/images/
    train/
        cat/
            cat_001.jpg
            cat_002.jpg
        dog/
            dog_001.jpg
            dog_002.jpg
    val/
        cat/
            cat_val_001.jpg
        dog/
            dog_val_001.jpg

DataVec's ImageRecordReader handles this structure automatically using a label generator that derives the label from the parent directory name.

ImageRecordReader

ImageRecordReader reads images from a FileSplit and resizes them to a fixed height, width, and channel count. It appends a one-hot label based on the directory structure.

The reader produces records with one NDArrayWritable (the image pixels) plus one IntWritable (the label index). When used with RecordReaderDataSetIterator, the label index tells the iterator which column is the label:

Getting the List of Labels

Image Format Support

ImageRecordReader uses JavaCV / OpenCV under the hood via NativeImageLoader. Supported formats include JPEG, PNG, BMP, GIF, TIFF, and WebP.

Image Transforms (Augmentation)

ImageTransform applies geometric or color transformations to images as they are read. Transforms are applied before the image is returned by the reader, making them transparent to the rest of the pipeline.

Pass an ImageTransform (or a list of them) to the ImageRecordReader:

Apply a single transform at reader initialization:

Pipeline of Transforms

Use PipelineImageTransform to apply multiple transforms in sequence:

Random Choice Transform

Randomly apply one transform from a list (useful for diverse augmentation):

NativeImageLoader

NativeImageLoader loads individual image files or BufferedImage objects directly into ND4J INDArray tensors, without the RecordReader abstraction. Use this for single-image inference or when building custom data pipelines.

Normalization After Loading

NativeImageLoader returns raw pixel values in the range [0, 255]. Normalize them before passing to a model:

Complete Image Training Pipeline

A complete pipeline from labeled image directory to model training:

Single Image Inference

For real-time inference on a single image:

Label Generators

DataVec provides two label generator strategies:

ParentPathLabelGenerator (most common): derives the label from the immediate parent directory name. The class cat in /data/cat/img001.jpg becomes label "cat".

PathLabelGenerator: a functional interface you can implement for custom label derivation logic:

Tips for Large Image Datasets

  • Keep images on a fast local SSD rather than a network mount — image loading is I/O-bound

  • Use multiple worker threads in AsyncDataSetIterator to prefetch batches while the GPU trains

  • Pre-resize images to your target dimensions offline to avoid repeated resize overhead at training time

  • Use FileSplit with Random to shuffle the file order for each epoch

Last updated

Was this helpful?