使用 TAO Deploy 进行分类 (TF2)
从 tao export
生成的 TF2 分类 ONNX 文件被用作 tao deploy
的输入,以生成优化的 TensorRT 引擎。有关训练 TF2 分类的更多信息,请参阅 TF2 分类训练文档。
相同的 spec 文件可以与 tao model classification_tf2 export
命令一起使用。
GenTrtEngine 配置
gen_trt_engine
配置包含将 .onnx
模型导出到 TensorRT 引擎的参数,该引擎可用于部署。
字段 | 描述 | 数据类型和约束 | 推荐/典型值 |
onnx_file | 导出 .onnx 模型的路径 | 字符串 | |
trt_engine | 将存储生成的引擎的路径 | 字符串 | |
results_dir | 用于保存输出日志的目录。如果未指定,日志将保存在全局 $results_dir/gen_trt_engine 下 | 字符串 | |
tensorrt | TensorRT 配置 | 字典 |
tensorrt
配置包含 TensorRT 引擎和校准要求的规范。 +——————————+———————————————————————-+——————————-+——————————-+ | 字段 | 描述 | 数据类型和约束 | 推荐/典型值 | +——————————+———————————————————————-+——————————-+——————————-+ | data_type | 用于 TensorRT 引擎的精度 | 字符串 | FP32 | +——————————+———————————————————————-+——————————-+——————————-+ | min_batch_size | 用于优化配置文件形状的最小批次大小 | 无符号整数 | 1 | +——————————+———————————————————————-+——————————-+——————————-+ | opt_batch_size | 用于优化配置文件形状的最佳批次大小 | 无符号整数 | 1 | +——————————+———————————————————————-+——————————-+——————————-+ | max_batch_size | 用于优化配置文件形状的最大批次大小 | 无符号整数 | 1 | +——————————+———————————————————————-+——————————-+——————————-+ | max_workspace_size | TensorRT 引擎的最大工作区大小 | 无符号整数 | 2 | +——————————+———————————————————————-+——————————-+——————————-+ | calibration | 校准配置 | 字典 | | +——————————+———————————————————————-+——————————-+——————————-+
calibration
配置指定校准数据的位置以及保存校准缓存文件的位置。 +——————————+———————————————————————-+——————————-+——————————-+ | 字段 | 描述 | 数据类型和约束 | 推荐/典型值 | +——————————+———————————————————————-+——————————-+——————————-+ | cal_image_dir | 包含用于校准的图像的目录 | 字符串 | | +——————————+———————————————————————-+——————————-+——————————-+ | cal_cache_file | 校准缓存文件的路径 | 字符串 | | +——————————+———————————————————————-+——————————-+——————————-+ | cal_batches | 校准要迭代的批次数量 | 无符号整数 | 10 | +——————————+———————————————————————-+——————————-+——————————-+ | cal_batch_size | 每个批次的批次大小 | 无符号整数 | 1 | +——————————+———————————————————————-+——————————-+——————————-+ | cal_data_file | 校准数据文件的路径 | 字符串 | | +——————————+———————————————————————-+——————————-+——————————-+
以下是 TF2 分类的示例 spec 文件。
results_dir: '/results'
dataset:
num_classes: 20
train_dataset_path: "/workspace/tao-experiments/data/split/train"
val_dataset_path: "/workspace/tao-experiments/data/split/val"
preprocess_mode: 'torch'
augmentation:
enable_color_augmentation: True
enable_center_crop: True
train:
qat: False
checkpoint: ''
batch_size_per_gpu: 64
num_epochs: 80
optim_config:
optimizer: 'sgd'
lr_config:
scheduler: 'cosine'
learning_rate: 0.05
soft_start: 0.05
reg_config:
type: 'L2'
scope: ['conv2d', 'dense']
weight_decay: 0.00005
model:
backbone: 'efficientnet-b0'
input_width: 256
input_height: 256
input_channels: 3
input_image_depth: 8
evaluate:
dataset_path: '/workspace/tao-experiments/data/split/test'
checkpoint: ''
trt_engine: '/results/efficientnet-b0.fp32.engine'
top_k: 3
batch_size: 256
n_workers: 8
inference:
checkpoint: ''
trt_engine: '/results/efficientnet-b0.fp32.engine'
image_dir: '/workspace/tao-experiments/data/split/test/aeroplane'
classmap: '/results/train/classmap.json'
export:
checkpoint: ''
onnx_file: '/results/efficientnet-b0.onnx'
gen_trt_engine:
onnx_file: '/results/efficientnet-b0.onnx'
trt_engine: '/results/efficientnet-b0.fp32.engine'
tensorrt:
data_type: "fp32"
max_workspace_size: 4
max_batch_size: 16
calibration:
cal_image_dir: '/workspace/tao-experiments/data/split/test'
cal_data_file: '/results/calib.tensorfile'
cal_cache_file: '/results/cal.bin'
cal_batches: 10
使用以下命令运行 TF2 分类引擎生成
tao deploy classification_tf2 gen_trt_engine -e /path/to/spec.yaml \
export.onnx_file=/path/to/onnx/file \
export.trt_engine=/path/to/engine/file \
export.tensorrt.data_type=<data_type>
必需参数
-e, --experiment_spec
:用于设置 TensorRT 引擎生成的实验 spec 文件。这应与导出规范文件相同。
可选参数
-h, --help
:显示此帮助消息并退出。results_dir
:全局结果目录,其中实验输出和日志将写入<task>
子目录下。
示例用法
以下是使用 gen_trt_engine
命令生成 FP16 TensorRT 引擎的示例
tao deploy classification_tf2 gen_trt_engine -e $DEFAULT_SPEC
export.onnx_file=$ONNX_FILE \
export.trt_engine=$ENGINE_FILE \
export.tensorrt.data_type=fp16
与 TAO 评估 spec 文件相同的 spec 文件。
使用以下命令运行 TF2 分类引擎评估
tao deploy classification_tf2 evaluate -e /path/to/spec.yaml \
evaluate.trt_engine=/path/to/engine/file \
evaluate.results_dir=/path/to/outputs
必需参数
-e, --experiment_spec
:用于评估的实验 spec 文件。这应与 tao evaluate 规范文件相同。
可选参数
-h, --help
:显示此帮助消息并退出。results_dir
:全局结果目录,其中实验输出和日志将写入<task>
子目录下。
示例用法
以下是使用 evaluate
命令通过 TensorRT 引擎运行评估的示例
tao deploy classification_tf2 evaluate -e $DEFAULT_SPEC
evaluate.trt_engine=$ENGINE_FILE \
results_dir=$RESULTS_DIR
与 TAO 推理 spec 文件相同的 spec 文件。
使用以下命令运行 TF2 分类引擎推理
tao deploy classification_tf2 inference -e /path/to/spec.yaml \
inference.trt_engine=/path/to/engine/file \
results_dir=/path/to/outputs
必需参数
-e, --experiment_spec
:用于推理的实验 spec 文件。这应与 tao inference 规范文件相同。
可选参数
-h, --help
:显示此帮助消息并退出。results_dir
:全局结果目录,其中实验输出和日志将写入<task>
子目录下。
示例用法
以下是使用 inference
命令通过 TensorRT 引擎运行推理的示例
tao deploy classification_tf2 inference -e $DEFAULT_SPEC
inference.trt_engine=$ENGINE_FILE \
inference.results_dir=$RESULTS_DIR
csv 预测将存储在 $RESULTS_DIR/result.csv
下。