Implementation:NVIDIA DALI TF ResNet Model
| Knowledge Sources | |
|---|---|
| Domains | Image_Classification, TensorFlow |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Implements the ResNet-50 architecture in TensorFlow/Keras with identity and convolutional residual blocks, L2 regularization, and support for both channels-first and channels-last data formats.
Description
This module provides a complete implementation of the ResNet-50 convolutional neural network for image classification, built using TensorFlow Keras functional API. The architecture follows the standard ResNet-50 design with four stages of residual blocks, each consisting of bottleneck (1x1, 3x3, 1x1 convolution) layers with batch normalization and ReLU activation.
Two types of residual blocks are implemented: `identity_block` for blocks where the input and output dimensions match (using a direct skip connection), and `conv_block` for blocks where spatial dimensions or channel counts change (using a 1x1 convolution on the shortcut path). Both block types use He normal initialization for convolutional kernels and support optional L2 weight regularization (default weight decay: 1e-4).
The `resnet50()` function assembles the complete network: an initial 7x7 convolution with stride 2 and max pooling, followed by stage 2 (3 blocks with 64/64/256 filters), stage 3 (4 blocks with 128/128/512 filters), stage 4 (6 blocks with 256/256/1024 filters), and stage 5 (3 blocks with 512/512/2048 filters). The network concludes with global average pooling and a dense classification layer with softmax activation. The model accepts 224x224x3 input images and supports configurable batch normalization parameters (decay=0.9, epsilon=1e-5).
The implementation supports both channels-first and channels-last data formats (auto-detected from Keras backend), optional input rescaling from [0,1] to the range expected by ImageNet-pretrained models, and configurable L2 regularization.
Usage
Use this module to create a ResNet-50 model for ImageNet classification training with the DALI-accelerated data pipeline. The model is typically instantiated by the `train_ctl` runner function.
Code Reference
Source Location
- Repository: NVIDIA_DALI
- File: docs/examples/use_cases/tensorflow/resnet-n/resnet_model.py
- Lines: 1-415
Signature
def identity_block(input_tensor, kernel_size, filters, stage, block,
use_l2_regularizer=True) -> tf.Tensor:
"""The identity block with no conv layer at shortcut."""
...
def conv_block(input_tensor, kernel_size, filters, stage, block,
strides=(2, 2), use_l2_regularizer=True) -> tf.Tensor:
"""A block that has a conv layer at shortcut."""
...
def resnet50(num_classes, batch_size=None, use_l2_regularizer=True,
rescale_inputs=False) -> tf.keras.Model:
"""Instantiates the ResNet50 architecture."""
...
Import
from resnet_model import resnet50
model = resnet50(num_classes=1000, batch_size=256)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| num_classes | int | Yes | Number of output classes (e.g., 1000 for ImageNet) |
| batch_size | int | No | Static batch size for input shape (default: None for dynamic) |
| use_l2_regularizer | bool | No | Whether to apply L2 regularization to Conv/Dense layers (default: True) |
| rescale_inputs | bool | No | Whether to rescale inputs from [0,1] to ImageNet range (default: False) |
| input images | tf.Tensor | Yes | Image tensor of shape [batch, 224, 224, 3] |
Outputs
| Name | Type | Description |
|---|---|---|
| model | tf.keras.Model | ResNet-50 Keras model with input shape (224, 224, 3) |
| predictions | tf.Tensor | Softmax classification probabilities of shape [batch, num_classes] |
Usage Examples
Create and Train ResNet-50
from resnet_model import resnet50
from nvutils import image_processing
# Create model
model = resnet50(num_classes=1000, batch_size=256)
model.summary()
# Compile
model.compile(
optimizer=tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[
tf.keras.metrics.SparseTopKCategoricalAccuracy(k=1, name='top1'),
tf.keras.metrics.SparseTopKCategoricalAccuracy(k=5, name='top5'),
],
)
# Train with DALI data
dataset = image_processing.image_set(
train_files, batch_size=256, height=224, width=224,
training=True, use_dali="GPU", idx_filenames=idx_files,
)