TAO v5.5.0

部署 Segformer 模型到 Deepstream

要将 TAO 训练的 Segformer 模型部署到 DeepStream,您需要使用TAO Deploy生成特定于设备的优化 TensorRT 引擎,然后 DeepStream 可以摄取该引擎。

机器特定的优化是在引擎创建过程中执行的,因此您应该为每个环境和硬件配置生成不同的引擎。此外,如果推理环境的 TensorRT 或 CUDA 库更新(包括次要版本更新),或者生成了新模型,您将需要生成新的引擎。不支持运行使用不同版本的 TensorRT 和 CUDA 生成的引擎,并且会导致影响推理速度、准确性和稳定性的未知行为,或者可能完全无法运行。

有关如何导出 TAO 模型的更多详细信息,请参阅 SegFormer 的导出模型文档。

Segformer 模型需要 TensorRT OSS 构建,因为几个先决条件 TensorRT 插件仅在 TensorRT 开源仓库中可用。

如果您的部署平台是带有 NVIDIA GPU 的 x86 PC,请按照x86 上的 TensorRT OSS 说明进行操作;如果您的部署平台是 NVIDIA Jetson,请按照Jetson (ARM64) 上的 TensorRT OSS 说明进行操作。

x86 上的 TensorRT OSS

在 x86 上构建 TensorRT OSS

  1. 安装 Cmake (>=3.13)。

    注意

    TensorRT OSS 需要 cmake >= v3.13,因此如果您的 cmake 版本低于 3.13c,请安装 cmake 3.13

    复制
    已复制!
                

    sudo apt remove --purge --auto-remove cmake wget https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5.tar.gz tar xvf cmake-3.13.5.tar.gz cd cmake-3.13.5/ ./configure make -j$(nproc) sudo make install sudo ln -s /usr/local/bin/cmake /usr/bin/cmake


  2. 获取 GPU 架构。 GPU_ARCHS 值可以通过 deviceQuery CUDA 示例检索

    复制
    已复制!
                

    cd /usr/local/cuda/samples/1_Utilities/deviceQuery sudo make ./deviceQuery

    如果您的系统中不存在 /usr/local/cuda/samples,您可以从此GitHub 仓库下载 deviceQuery.cpp。编译并运行 deviceQuery

    复制
    已复制!
                

    nvcc deviceQuery.cpp -o deviceQuery ./deviceQuery

    此命令将输出类似这样的内容,指示 GPU_ARCHS 基于 CUDA Capability major/minor 版本为 75

    复制
    已复制!
                

    Detected 2 CUDA Capable device(s) Device 0: "Tesla T4" CUDA Driver Version / Runtime Version 10.2 / 10.2 CUDA Capability Major/Minor version number: 7.5

  3. 构建 TensorRT OSS

    复制
    已复制!
                

    git clone -b 21.08 https://github.com/nvidia/TensorRT cd TensorRT/ git submodule update --init --recursive export TRT_SOURCE=`pwd` cd $TRT_SOURCE mkdir -p build && cd build

    注意

    确保步骤 2 中的 GPU_ARCHS 在 TensorRT OSS CMakeLists.txt 中。如果 GPU_ARCHS 不在 TensorRT OSS CMakeLists.txt 中,请添加 -DGPU_ARCHS=<VER>,如下所示,其中 <VER> 代表步骤 2 中的 GPU_ARCHS

    复制
    已复制!
                

    /usr/local/bin/cmake .. -DGPU_ARCHS=xy -DTRT_LIB_DIR=/usr/lib/x86_64-linux-gnu/ -DCMAKE_C_COMPILER=/usr/bin/gcc -DTRT_BIN_DIR=`pwd`/out make nvinfer_plugin -j$(nproc)

    构建成功结束后,libnvinfer_plugin.so* 将在 \`pwd\`/out/. 下生成。

  4. 替换原始的 libnvinfer_plugin.so*

    复制
    已复制!
                

    sudo mv /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.x.y ${HOME}/libnvinfer_plugin.so.8.x.y.bak // backup original libnvinfer_plugin.so.x.y sudo cp $TRT_SOURCE/`pwd`/out/libnvinfer_plugin.so.8.m.n /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.x.y sudo ldconfig

Jetson (ARM64) 上的 TensorRT OSS

  1. 安装 Cmake (>=3.13)

    注意

    TensorRT OSS 需要 cmake >= v3.13,而 Jetson/Ubuntu 18.04 上的默认 cmake 是 cmake 3.10.2。

    使用以下命令升级 TensorRT OSS

    复制
    已复制!
                

    sudo apt remove --purge --auto-remove cmake wget https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5.tar.gz tar xvf cmake-3.13.5.tar.gz cd cmake-3.13.5/ ./configure make -j$(nproc) sudo make install sudo ln -s /usr/local/bin/cmake /usr/bin/cmake

  2. 根据您的平台获取 GPU 架构。下表给出了不同 Jetson 平台的 GPU_ARCHS

    Jetson 平台 GPU_ARCHS
    Nano/Tx1 53
    Tx2 62
    AGX Xavier/Xavier NX 72
  3. 构建 TensorRT OSS

    复制
    已复制!
                

    git clone -b 21.03 https://github.com/nvidia/TensorRT cd TensorRT/ git submodule update --init --recursive export TRT_SOURCE=`pwd` cd $TRT_SOURCE mkdir -p build && cd build

    注意

    下面的 -DGPU_ARCHS=72 适用于 Xavier 或 NX,对于其他 Jetson 平台,请将 72 更改为步骤 2 中的 GPU_ARCHS

    复制
    已复制!
                

    /usr/local/bin/cmake .. -DGPU_ARCHS=72 -DTRT_LIB_DIR=/usr/lib/aarch64-linux-gnu/ -DCMAKE_C_COMPILER=/usr/bin/gcc -DTRT_BIN_DIR=`pwd`/out make nvinfer_plugin -j$(nproc)

    构建成功结束后,libnvinfer_plugin.so* 将在 ‘pwd’/out/. 下生成。

  4. 用新生成的 "libnvinfer_plugin.so*" 替换。

    复制
    已复制!
                

    sudo mv /usr/lib/aarch64-linux-gnu/libnvinfer_plugin.so.8.x.y ${HOME}/libnvinfer_plugin.so.8.x.y.bak // backup original libnvinfer_plugin.so.x.y sudo cp `pwd`/out/libnvinfer_plugin.so.8.m.n /usr/lib/aarch64-linux-gnu/libnvinfer_plugin.so.8.x.y sudo ldconfig

标签文件是一个文本文件,其中包含 Segformer 模型训练用于分割的类名称。此处列出的类的顺序必须与模型预测输出的顺序匹配。此顺序源自训练后保存在 results 目录中的 target_class_id_mapping.json 文件。以下是 target_class_id_mapping.json 文件的示例

复制
已复制!
            

{"0": ["foreground"], "1": ["background"]}

以下是相应的 segformer_labels.txt 文件的示例。segformer_labels.txt 中的顺序应与 target_class_id_mapping.json 键的顺序匹配

复制
已复制!
            

foreground background

分割模型通常用作主要推理引擎。它也可以用作辅助推理引擎。从 deepstream_tao_apps 仓库下载 ds-tlt

按照以下步骤将 TensorRT 引擎文件与 ds-tlt 一起使用

  1. 使用 TAO Deploy 生成 TensorRT 引擎。

  2. 引擎文件成功生成后,执行以下操作以使用 DS 6.1 设置 ds-tlt。

要使用示例 ds-tao-segmentation 运行此模型,您必须修改现有的 pgie_citysemsegformer_tao_config.txt 文件此处,以指向此模型。有关所有选项,请参阅下面的配置文件。要了解有关参数的更多信息,请参阅DeepStream 开发指南

从 TAO 5.0.0 开始,.etlt 已弃用。要将 .etlt 直接集成到 DeepStream 应用程序中,您需要在配置文件中使用以下参数。

复制
已复制!
            

tlt-encoded-model=<TAO exported .etlt> tlt-model-key=<Model export key> int8-calib-file=<Calibration cache file>

复制
已复制!
            

[property] gpu-id=0 net-scale-factor=0.01735207357279195 offsets=123.675;116.28;103.53 labelfile-path=../../models/citysemsegformer_vdeployable_v1.0/labels.txt model-engine-file=../../models/citysemsegformer_vdeployable_v1.0/citysemsegformer.onnx_b1_gpu0_fp16.engine # tlt-encoded-model=../../models/citysemsegformer_vdeployable_v1.0/citysemsegformer.etlt # If it is an etlt file # tlt-model-key=tlt_encode # This is needed if etlt file is used. onnx-file=../../models/citysemsegformer_vdeployable_v1.0/citysemsegformer.onnx infer-dims=3;1024;1024 model-color-format=0 batch-size=1 ## 0=FP32, 1=INT8, 2=FP16 mode network-mode=2 ## workspace-size default to 1024 x 1024 MB workspace-size=1048576 interval=0 gie-unique-id=1 cluster-mode=2 ## 0=Detector, 1=Classifier, 2=Semantic Segmentation, 3=Instance Segmentation, 100=Other network-type=100 # Skip nvinfer post-processing, use pgie_pad_buffer_probe_network_type100() instead. ## num-detected-classes= is required to set NvDsInferSegmentationMeta::classes. num-detected-classes=19 ## Allow post-processing to access output tensors. output-tensor-meta=1 ##specify the output tensor order, 0(default value) for CHW and 1 for HWC segmentation-output-order=1

以下是为在 ISBI 数据集上训练的 resnet18 3 通道模型修改的配置文件的示例

复制
已复制!
            

[property] gpu-id=0 net-scale-factor=0.007843 # Since the model input channel is 3, and pre-processing of SegFormer TAO requires BGR format, set the color format to BGR. # 0-RGB, 1-BGR, 2-Gray model-color-format=1 # For grayscale, this should be set to 2 offsets=127.5;127.5;127.5 labelfile-path=/home/nvidia/deepstream_tlt_apps/configs/segformer_tlt/segformer_labels.txt ##Replace following path to your model file # Argument to be used if you are using an tensorrt engine model-engine-file=/home/nvidia/deepstream_tlt_apps/models/segformer/segformer_isbi.engine infer-dims=3;512;512 batch-size=1 ## 0=FP32, 1=INT8, 2=FP16 mode network-mode=2 num-detected-classes=2 interval=0 gie-unique-id=1 ## 0=Detector, 1=Classifier, 2=Semantic Segmentation (sigmoid activation), 3=Instance Segmentation, 100=skip nvinfer postprocessing network-type=100 output-tensor-meta=1 # Set this to 1 when network-type is 100 output-blob-names=argmax_1 # If you had used softmax for segmentation model, it would have been replaced with argmax by TAO for optimization. # Hence, you need to provide argmax_1 segmentation-threshold=0.0 ##specify the output tensor order, 0(default value) for CHW and 1 for HWC segmentation-output-order=1

注意

目前,Segformer 仅支持 DS 配置文件中的 TensorRT Engine 输入。使用 tao deploy 将 .onnx 引擎转换为 .trt

以下是用于在单个图像上进行推理的示例 ds-tao-segmentation 命令

复制
已复制!
            

ds-tao-segmentation -c pgie_config_file -i image_isbi_rgb.jpg

上一篇 部署 UNet 模型到 Deepstream
下一篇 部署 nvOCDR 到 DeepStream
© 版权所有 2024,NVIDIA。 最后更新于 2024 年 10 月 15 日。