Implementation:NVIDIA DALI TF Image Processing
| Knowledge Sources | |
|---|---|
| Domains | Image_Classification, TensorFlow |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Provides image loading, preprocessing, and data pipeline utilities for ResNet training, with support for both native TensorFlow data loading and NVIDIA DALI GPU-accelerated preprocessing.
Description
This module implements the complete image preprocessing pipeline for ImageNet-based ResNet training with two interchangeable backends: a standard TensorFlow `tf.data` pipeline and an NVIDIA DALI GPU-accelerated pipeline. Both pipelines read TFRecord files containing ImageNet data and produce normalized, augmented image batches.
The TensorFlow pipeline deserializes TFRecord examples containing JPEG-encoded images, class labels, bounding boxes, and class text. It applies a chain of preprocessing operations: JPEG decoding, random crop and resize (training) or central crop (evaluation) with configurable aspect ratios and area ranges, optional color distortion (brightness, saturation, hue, contrast jittering), random horizontal flipping, and channel mean subtraction (using ImageNet means: R=123.68, G=116.78, B=103.94).
The DALI pipeline (`get_dali_pipeline`) provides equivalent functionality using GPU-accelerated operators: `fn.readers.tfrecord` for data reading with automatic sharding across GPUs, `fn.decoders.image_random_crop` for fused decode-and-crop (training) or `fn.decoders.image` with `fn.resize` (evaluation), and `fn.crop_mirror_normalize` for normalization and random mirroring. The pipeline supports CPU or mixed (CPU+GPU) decode modes with pre-allocated memory hints for NVJPEG.
The `DALIPreprocessor` class wraps the DALI pipeline for use with TensorFlow, providing both a `DALIIterator`-based interface and a `DALIDataset` interface. The `image_set()` factory function creates the appropriate pipeline based on the `use_dali` flag, supporting 'CPU' and 'GPU' DALI modes or falling back to the TensorFlow pipeline. A `fake_image_set()` function generates synthetic data for benchmarking.
Usage
Use this module in the ResNet-N training pipeline to create training and validation data iterators. Select between DALI and native TF preprocessing by setting the `dali_mode` parameter. Horovod is used for distributed data sharding across multiple GPUs.
Code Reference
Source Location
- Repository: NVIDIA_DALI
- File: docs/examples/use_cases/tensorflow/resnet-n/nvutils/image_processing.py
- Lines: 1-322
Signature
@pipeline_def
def get_dali_pipeline(
tfrec_filenames, tfrec_idx_filenames,
height, width, shard_id, num_gpus,
dali_cpu=True, training=True,
): ...
class DALIPreprocessor(object):
def __init__(self, filenames, idx_filenames, height, width,
batch_size, num_threads, dtype=tf.uint8,
dali_cpu=True, deterministic=False, training=False): ...
def get_device_minibatches(self): ...
def get_device_dataset(self): ...
def image_set(filenames, batch_size, height, width, training=False,
distort_color=False, num_threads=10, nsummary=10,
deterministic=False, use_dali=None, idx_filenames=None): ...
def fake_image_set(batch_size, height, width, with_label=True): ...
def _deserialize_image_record(record): ...
def _decode_jpeg(imgdata, channels=3): ...
def _crop_and_resize_image(image, original_bbox, height, width,
deterministic=False, random_crop=False): ...
def _distort_image_color(image, order=0): ...
def _parse_and_preprocess_image_record(record, height, width,
deterministic=False, random_crop=False,
distort_color=False): ...
Import
from nvutils import image_processing
# Create DALI-accelerated dataset
dataset = image_processing.image_set(
filenames=train_files,
batch_size=256,
height=224,
width=224,
training=True,
use_dali="GPU",
idx_filenames=train_idx_files,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filenames | list[str] | Yes | List of TFRecord file paths |
| idx_filenames | list[str] | No | List of DALI index file paths (required when use_dali is set) |
| batch_size | int | Yes | Number of images per batch |
| height | int | Yes | Target image height (e.g., 224) |
| width | int | Yes | Target image width (e.g., 224) |
| training | bool | No | Whether to apply training augmentations (default: False) |
| distort_color | bool | No | Whether to apply color distortion (default: False) |
| use_dali | str or None | No | DALI mode: 'CPU', 'GPU', or None for TF native pipeline |
| deterministic | bool | No | Whether to use deterministic random seeds (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| images | tf.Tensor | Batch of preprocessed images [batch, height, width, 3] as float32 |
| labels | tf.Tensor | Batch of 0-based class labels [batch] as int32/int64 |
Usage Examples
DALI Pipeline for Training
from nvutils import image_processing
# Create DALI preprocessor
preprocessor = image_processing.DALIPreprocessor(
filenames=train_files,
idx_filenames=train_idx_files,
height=224, width=224,
batch_size=256,
num_threads=4,
dali_cpu=False,
training=True,
)
# Get dataset
dataset = preprocessor.get_device_dataset()
for images, labels in dataset:
# images: [256, 224, 224, 3], labels: [256]
pass