Image Classification PyT
Image Classification PyT is a PyTorch-based image-classification model included in TAO. It supports the following tasks:
train
evaluate
inference
export
These tasks can be invoked from the TAO Launcher using the following convention on the command-line:
tao model classification_pyt <sub_task> <args_per_subtask>
Where, args_per_subtask
are the command-line arguments required for a given subtask. Each
subtask is explained in detail in the following sections.
TAO has been updated to the latest MMPretrain version instead of the deprecated MMClassification. .. Note:: Image Classification (PyT) is based off of MMPretrain. Hence, most parameters are adopted from the MMPretrain 1.x format.
See the Data Annotation Format page for more information about the data format for image classification.
The train
classification experiment specification consists of three main components:
dataset
train
model
Here is an example of dataset specification file for classification PyT with a FAN backbone:
dataset:
data:
samples_per_gpu: 128
workers_per_gpu: 8
train:
data_prefix: "/raid/ImageNet2012/ImageNet2012/train"
pipeline: # Augmentations alone
- type: RandomResizedCrop
scale: 224
- type: RandomFlip
prob: 0.5
direction: "horizontal"
- type: ColorJitter
brightness: 0.4
contrast: 0.4
saturation: 0.4
- type: RandomErasing
erase_prob: 0.3
val:
data_prefix: /raid/ImageNet2012/ImageNet2012/val
test:
data_prefix: /raid/ImageNet2012/ImageNet2012/val
The table below describes the configurable parameters in dataset
.
Parameter | Datatype | Default | Description | Supported Values |
sampler |
dict config | None | The dataset sampler type | |
|
dict config |
None |
Contains the following configurable parameters: |
> 0 |
data |
dict config | None | Parameters related to training. Refer to data for more details. |
data
Parameter | Datatype | Default | Description | Supported Values |
samples_per_gpu |
int | None | The dataset sampler type | |
|
str |
Dict |
Contains the following configurable parameters: |
> 0 |
|
dict config str |
None |
Contains the training dataset configuration: |
Imagenet Classes |
|
dict config str |
None |
Contains the test dataset configuration: |
Imagenet Classes |
|
dict config str |
None |
Contains the validation dataset configuration: |
Imagenet Classes |
Refer to the MMPretrain 1.x format documentation for more details.
pipeline
The following is an example pipeline config with different augmentations:
pipeline: # Augmentations alone
- type: RandomResizedCrop
scale: 224
backend: "pillow"
- type: RandomFlip
prob: 0.5
direction: "horizontal"
- type: ColorJitter
brightness: 0.4
contrast: 0.4
saturation: 0.4
- type: RandomErasing
erase_prob: 0.3
Some of the widely adopted augmentations and the parameters are listed below. More information, refer to the MMPretrain documentation for transforms
Parameter | Datatype | Default | Description | Supported Values |
|
dict config |
None |
Contains the following configurable parameters: |
> 0 |
|
dict config |
None |
Contains the following configurable parameters: |
0-1 |
|
dict config
int |
None |
Contains the following configurable parameters: |
> 0 |
|
dict config |
None |
The ColorJitter augmentation contains the following parameters: |
0-1 |
|
dict config |
None |
The RandomErasing augmentation contains the following parameters: |
0-1 |
Here is an example of a train specification file for Image Classification PyT:
train:
train_config:
runner:
max_epochs: 300
checkpoint_config:
interval: 1
logging:
interval: 5000
validate: True
evaluation:
interval: 10
custom_hooks:
- type: "EMAHook"
momentum: 0.00008
priority: "ABOVE_NORMAL"
lr_config:
policy: CosineAnnealing
T_max: 95
by_epoch: True
begin: 5
end: 100
optimizer:
type: AdamW
lr: 0.005
weight_decay: 0.05
The table below describes the configurable parameters in the train
specification.
Parameter | Datatype | Default | Description | Supported Values |
|
dict config |
None |
Contains the following configurable parameters: |
> 0 |
train_config |
dict config | None | Parameters related to training. For more information, refer to train_config | |
results_dir |
str | None | The path for saving the checkpoint and logs | str |
train_config
Parameter | Datatype | Default | Description | Supported Values |
|
dict config |
None |
Contains the following configurable parameters: |
|
|
dict config |
None |
Contains the following configurable parameters:
|
>0 |
|
dict config |
None |
Contains the following configurable parameters:
|
>0 |
|
dict config |
None |
Contains the configurable parameters for different optimizers, as detailed in |
|
|
dict config |
None |
Contains the following parameters: |
>=0.0 |
|
dict config |
None |
Contains the following configurable parameters: |
|
validate |
bool | False | A flag that enables validation during training | |
find_unused_parameters |
bool | False | Sets this parameter in DDP. For more information, refer DDP_PyT. | |
lr_config |
dict | None | The learning-rate scheduler configuration. For more details, refer to lr_config | |
load_from |
str | None | The checkpoint path from where the end-end model weights including head can be loaded | |
custom_hooks |
dict | None | The custom training hooks configuration. For more details, refer to custom_hooks. | |
resume_training_checkpoint_path |
str | None | The checkpoint path to resume the training from |
optimizer
The following optimizers are supported:
SGD
Parameter | Datatype | Default | Description | Supported Values |
|
dict config |
None |
Contains the following configurable parameters: |
|
AdamW
Parameter | Datatype | Default | Description | Supported Values |
|
dict config |
None |
Contains the following configurable parameters: |
|
lr_config
The lr_config
parameter defines the parameters for the learning-rate scheduler. Some of the widely adopted learning-rate schedulers
and the parameters are listed below. More information, refer to the MMPretrain documentation for lr schedulers
CosineAnnealingLR
Parameter | Datatype | Default | Description | Supported Values |
T_max |
int | Maximum number of iterations. | >=0.0 | |
by_epoch |
bool | True | Whether the scheduled learning rate is updated by epochs | Less than 1.0 |
begin |
int | 0 | Step at which to start updating the learning rate. | In the interval (0, INF). |
end |
int | INF | Step at which to stop updating the learning rate. | In the interval (0, INF). |
MultiStepLR
Parameter | Datatype | Typical value | Description | Supported Values |
gamma |
float | – | The base (maximum) learning rate | Usually less than 1.0 |
by_epoch |
bool | True | Whether the scheduled learning rate is updated by epochs | Less than 1.0 |
begin |
int | 0 | Step at which to start updating the learning rate. | In the interval (0, INF). |
end |
int | INF | Step at which to stop updating the learning rate. | In the interval (0, INF). |
LinearLR
Parameter | Datatype | Typical value | Description | Supported Values |
by_epoch |
bool | True | Whether the scheduled learning rate is updated by epochs | Less than 1.0 |
begin |
int | 0 | Step at which to start updating the learning rate. | In the interval (0, INF). |
end |
int | INF | Step at which to stop updating the learning rate. | In the interval (0, INF). |
PolyLR
Parameter | Datatype | Typical value | Description | Supported Values |
eta_min |
float | 0 | Minimum learning rate at the end of scheduling | |
by_epoch |
bool | True | Whether the scheduled learning rate is updated by epochs | Less than 1.0 |
begin |
int | 0 | Step at which to start updating the learning rate. | In the interval (0, INF). |
end |
int | INF | Step at which to stop updating the learning rate. | In the interval (0, INF). |
StepLR
Parameter | Datatype | Typical value | Description | Supported Values |
gamma |
float | – | The base (maximum) learning rate | Usually less than 1.0 |
by_epoch |
bool | True | Whether the scheduled learning rate is updated by epochs | Less than 1.0 |
begin |
int | 0 | Step at which to start updating the learning rate. | In the interval (0, INF). |
end |
int | INF | Step at which to stop updating the learning rate. | In the interval (0, INF). |
custom_hooks
The following is an example of how a custom hook from the MMPretrain to Hydra config is provided for EMAHook`
:
MMPretrain config:
custom_hooks = [ dict(type='EMAHook', interval=100, priority='HIGH')]
Equivalent TAO Hydra config:
custom_hooks: - type: "EMAHook" momentum: 0.00008 priority: "ABOVE_NORMAL"
For more detail on custom_hooks
, refer to the MMPretrain documentation for custom hooks.
Here is an example model-specification file for Image Classification PyT with a FAN backbone:
model:
backbone:
type: "fan_tiny_8_p4_hybrid"
custom_args:
drop_path_rate: 0.1
freeze: False
pretrained: <Path to pretrained weights>
head:
type: "TAOLinearClsHead"
num_classes: 1000
custom_args:
head_init_scale: 1
loss:
type: LabelSmoothLoss
label_smooth_val: 0.1
mode: 'original'
train_cfg:
augments:
- type: Mixup
alpha: 0.8
- type: CutMix
alpha: 1.0
The model
parameter primarily configures the backbone and head.
Parameter | Datatype | Default | Description | Supported Values |
|
Dict |
None |
The |
|
|
Dict |
None |
Contains the following configurable parameters
*
*
Refer to the Foundation Models section for Foundation Models |
FAN Variants
GCViT Variants
FasterViT Variants False/True |
head |
Dict | None | The config parameters for the classification head | – |
train_cfg |
Dict | None | Contains advanced augmentation parameters. | – |
Foundation Models
model:
backbone:
type: "ViT-B-32"
custom_args:
drop_path_rate: 0.1
freeze: False
pretrained: laion400m_e31
head:
type: LinearClsHead
num_classes: 1000
in_channels: 512
loss:
type: CrossEntropyLoss
loss_weight: 1.0
use_soft: False
topk: [1, 5]
Subset of the supported arch and the pre-train datasets. Please note that the in_channels
should be updated under the head :
CLIP Image Backbones:
Arch | Pretrained Dataset | in_channels |
|
* laion400m_e31 |
512 |
ViT-B-16 |
laion400m_e31 | 512 |
ViT-L-14 |
laion400m_e31 | 768 |
ViT-H-14 |
laion2b_s32b_b79k | 1024 |
ViT-g-14 |
laion2b_s12b_b42k | 1024 |
EVA - CLIP Image Backbones:
Arch | Pretrained Dataset | in_channels |
EVA02-L-14 |
merged2b_s4b_b131k | 768 |
EVA02-L-14-336 |
laion400m_e31 | 768 |
EVA02-E-14 |
laion400m_e31 | 1024 |
EVA02-E-14-plus |
laion2b_s32b_b79k | 1024 |
head
Parameter | Datatype | Default | Description | Supported Values |
type |
string | None | Parameters for Beta distribution to generate the mixing ratio | TAOLinearClsHead, FANLinearClsHead, LogisticRegressionHead |
num_classes |
Dict | None | The number of training classes | >=0 |
loss |
Dict | {“type”:”CrossEntropyLoss”} | Refer to losses for different types of loss and their parameters | |
topk |
List | [1,] | The number of classes | >=0 |
|
Dict |
None |
Any custom parameters to be passed to |
– |
|
Dict |
None |
Parameters used for Logistic Regression Head |
– |
lr_head
Logistic Regression head is defined by the following parameters:
|
Dict |
None |
Contains the following tunable parameters: |
– |
Logistic Regression head involves freezing the backbone weights. Please note that the freeze
parameter should be updated to True under the backbone config as under:
model:
backbone:
type: "ViT-B-32"
custom_args:
drop_path_rate: 0.1
freeze: True
pretrained: laion400m_e31
head:
type: "LogisticRegressionHead"
num_classes: 1000
lr_head:
C: 0.316
max_iter: 5000
train_cfg
Mixup
Parameter | Datatype | Default | Description | Supported Values |
alpha |
string | None | Parameters for Beta distribution to generate the mixing ratio | 0-1 |
CutMix
Parameter | Datatype | Default | Description | Supported Values |
alpha |
string | None | Parameters for Beta distribution to generate the mixing ratio | 0-1 |
cutmix_minmax |
list[float] | None | The min/max area ratio of the patches. | |
correct_lam |
bool | True | Whether to apply lambda correction when cutmix bbox clipped by image borders. | False/True |
loss
Some Important Losses for classification losses are shown below. Please note that all supported losses in MMPretrain can be used by following the Hydra config for TAO. For a list of MMPretrain losses, refer to the losses_mmpretrain documentation.
LabelSmoothLoss
Parameter | Datatype | Default | Description | Supported Values |
label_smooth_val |
string | None | The degree of label smoothing | 0-1 |
use_sigmoid |
bool | None | Specifies whether prediction should use the sigmoid of softmax | False/ True |
num_classes |
int | None | The number of classes | >=0 |
mode |
string | None | Parameters for Beta distribution to generate the mixing ratio | 0-1 |
reduction |
str | None | The method used to reduce the loss | mean, sum |
loss_weight |
float | 1.0 | The weight of the loss | >=0 |
CrossEntropyLoss
Parameter | Datatype | Default | Description | Supported Values |
use_sigmoid |
bool | False | Specifies whether prediction should use the sigmoid of softmax | 0-1 |
use_soft |
bool | False | Specifies whether to use the soft version of CrossEntropyLoss | 0-1 |
loss_weight |
float | 1.0 | The weight of the loss | 0-1 |
reduction |
str | None | The method used to reduce the loss | mean, sum |
class_weight |
list[float] | None | The weight for each class | 0-1 |
Use the tao model classification_pyt train
command to train a classification pytorch model:
tao model classification_pyt train [-h] -e <experiment_spec_file>
[results_dir=<global_results_dir>]
[model.<model_option>=<model_option_value>]
[dataset.<dataset_option>=<dataset_option_value>]
[train.<train_option>=<train_option_value>]
[train.gpu_ids=<gpu indices>]
[train.num_gpus=<number of gpus>]
Required Arguments
The only required argument is the path to the experiment spec:
-e, --experiment_spec
: The experiment specification file to set up the training experiment
Optional Arguments
You can set optional arguments to override the option values in the experiment spec file.
-h, --help
: Show this help message and exit.model.<model_option>
: The model options.dataset.<dataset_option>
: The dataset options.train.<train_option>
: The train options.train.train_config.optimizer.<optim_option>
: The optimizer options
For training, evaluation, and inference, we expose 2 variables for each respective task: num_gpus
and gpu_ids
, which
default to 1
and [0]
, respectively. If both are passed, but inconsistent, for example num_gpus = 1
,
gpu_ids = [0, 1]
, then they are modified to follow the setting with more GPUs, for example num_gpus = 1 -> num_gpus = 2
.
The evaluate
config defines the hyperparameters of the evaluation process. The following is an example
config:
evaluate:
checkpoint: /path/to/model.pth
topk: 1
After the model has been trained using the experiment config file and by following the steps to
train a model, the next step is to evaluate this model on a test set to measure the
accuracy of the model. TAO includes the tao model classification_pyt evaluate
command to do this.
The classification app computes evaluation loss and Top-k accuracy.
After training, the model is stored in the output directory of your choice in
results_dir
.
evaluate:
checkpoint: /path/to/model.pth
tao model classification_pyt evaluate [-h] -e <experiment_spec>
evaluate.checkpoint=<model to be evaluated>
results_dir=<path to results dir>
[evaluate.<evaluate_option>=<evaluate_option_value>]
[evaluate.gpu_ids=<gpu indices>]
[evaluate.num_gpus=<number of gpus>]
Required Arguments
-e, --experiment_spec
: The experiment spec file to set up the evaluation experimentevaluate.checkpoint
: The.pth
model to be evaluated.results_dir
: The path where the results will be stored
Optional Arguments
evaluate.<evaluate_option>
: The evaluate options.
For classification, tao model classification_pyt inference
saves a .csv
file containing the image paths
and the corresponding labels for multiple images. TensorRT Python inference can also be enabled.
inference:
checkpoint: /path/to/model.pth
tao model classification_pyt inference [-h] -e <experiment_spec_file>
inference.checkpoint=<model to be inferenced>
results_dir=<path to results dir>
[inference.<inference_option>=<inference_option_value>]
[inference.gpu_ids=<gpu indices>]
[inference.num_gpus=<number of gpus>]
Required Arguments
-e, --experiment_spec
: The experiment spec file to set up the inference experimentinference.checkpoint
: The.pth
model to inference.results_dir
: The path where the results will be stored
Optional Arguments
inference.<inference_option>
: The inference options.
Exporting the model decouples the training process from inference and allows conversion to
TensorRT engines outside the TAO environment. TensorRT engines are specific to each hardware
configuration and should be generated for each unique inference environment.
The exported model may be used universally across training and deployment hardware.
The exported model format is referred to as .onnx
.
The export
parameter defines the hyperparameters of the export process.
export:
checkpoint: /path/to/model.pth
onnx_file: /path/to/model.onnx
opset_version: 12
verify: False
input_channel: 3
input_width: 224
input_height: 224
Here’s an example of the tao classification_pyt export
command:
tao model classification_pyt export [-h] -e <experiment spec file>
export.checkpoint=<model to export>
export.onnx_file=<onnx path>
[export.<export_option>=<export_option_value>]
Required Arguments
-e, --experiment_spec
: The path to an experiment spec fileexport.checkpoint
: The.pth
model to export.export.onnx_file
: The path where the.etlt
or.onnx
model is saved.
Optional Arguments
export.<export_option>
: The export options.
For TensorRT engine generation, validation, and INT8 calibration, refer to the TAO Deploy documentation.
Refer to the Integrating a Classification (TF1/TF2/PyTorch) Model page for more information about deploying a classification model with DeepStream.