TensorRT 命令行程序trtexec常用用法

安装TensorRT后,进入到/usr/src/tensorrt/bin目录下,可以看到有个trtexec二进制可执行文件,执行 ./trtexec --help可以看到命令行支持的所有参数项:

=== Model Options ===
  --uff=<file>                UFF model
  --onnx=<file>               ONNX model
  --model=<file>              Caffe model (default = no model, random weights used)
  --deploy=<file>             Caffe prototxt file
  --output=<name>[,<name>]*   Output names (it can be specified multiple times); at least one output is required for UFF and Caffe
  --uffInput=<name>,X,Y,Z     Input blob name and its dimensions (X,Y,Z=C,H,W), it can be specified multiple times; at least one is required for UFF models
  --uffNHWC                   Set if inputs are in the NHWC layout instead of NCHW (use X,Y,Z=H,W,C order in --uffInput)

=== Build Options ===
  --maxBatch                  Set max batch size and build an implicit batch engine (default = 1)
  --explicitBatch             Use explicit batch sizes when building the engine (default = implicit)
  --minShapes=spec            Build with dynamic shapes using a profile with the min shapes provided
  --optShapes=spec            Build with dynamic shapes using a profile with the opt shapes provided
  --maxShapes=spec            Build with dynamic shapes using a profile with the max shapes provided
  --minShapesCalib=spec       Calibrate with dynamic shapes using a profile with the min shapes provided
  --optShapesCalib=spec       Calibrate with dynamic shapes using a profile with the opt shapes provided
  --maxShapesCalib=spec       Calibrate with dynamic shapes using a profile with the max shapes provided
                              Note: All three of min, opt and max shapes must be supplied.
                                    However, if only opt shapes is supplied then it will be expanded so
                                    that min shapes and max shapes are set to the same values as opt shapes.
                                    In addition, use of dynamic shapes implies explicit batch.
                                    Input names can be wrapped with escaped single quotes (ex: \'Input:0\').
                              Example input shapes spec: input0:1x3x256x256,input1:1x3x128x128
                              Each input shape is supplied as a key-value pair where key is the input name and
                              value is the dimensions (including the batch dimension) to be used for that input.
                              Each key-value pair has the key and value separated using a colon (:).
                              Multiple input shapes can be provided via comma-separated key-value pairs.
  --inputIOFormats=spec       Type and formats of the input tensors (default = all inputs in fp32:chw)
                              Note: If this option is specified, please make sure that all inputs are in the same order
                                     as network inputs ID.
  --outputIOFormats=spec      Type and formats of the output tensors (default = all outputs in fp32:chw)
                              Note: If this option is specified, please make sure that all outputs are in the same order
                                     as network outputs ID.
                              IO Formats: spec  ::= IOfmt[","spec]
                                          IOfmt ::= type:fmt
                                          type  ::= "fp32"|"fp16"|"int32"|"int8"
                                          fmt   ::= ("chw"|"chw2"|"chw4"|"hwc8"|"chw16"|"chw32")["+"fmt]
  --workspace=N               Set workspace size in megabytes (default = 16)
  --noBuilderCache            Disable timing cache in builder (default is to enable timing cache)
  --nvtxMode=[default|verbose|none] Specify NVTX annotation verbosity
  --minTiming=M               Set the minimum number of iterations used in kernel selection (default = 1)
  --avgTiming=M               Set the number of times averaged in each iteration for kernel selection (default = 8)
  --noTF32                    Disable tf32 precision (default is to enable tf32, in addition to fp32)
  --fp16                      Enable fp16 precision, in addition to fp32 (default = disabled)
  --int8                      Enable int8 precision, in addition to fp32 (default = disabled)
  --best                      Enable all precisions to achieve the best performance (default = disabled)
  --calib=<file>              Read INT8 calibration cache file
  --safe                      Only test the functionality available in safety restricted flows
  --saveEngine=<file>         Save the serialized engine
  --loadEngine=<file>         Load a serialized engine

=== Inference Options ===
  --batch=N                   Set batch size for implicit batch engines (default = 1)
  --shapes=spec               Set input shapes for dynamic shapes inference inputs.
                              Note: Use of dynamic shapes implies explicit batch.
                                    Input names can be wrapped with escaped single quotes (ex: \'Input:0\').
                              Example input shapes spec: input0:1x3x256x256, input1:1x3x128x128
                              Each input shape is supplied as a key-value pair where key is the input name and
                              value is the dimensions (including the batch dimension) to be used for that input.
                              Each key-value pair has the key and value separated using a colon (:).
                              Multiple input shapes can be provided via comma-separated key-value pairs.
  --loadInputs=spec           Load input values from files (default = generate random inputs). Input names can be wrapped with single quotes (ex: 'Input:0')
                              Input values spec ::= Ival[","spec]
                                           Ival ::= name":"file
  --iterations=N              Run at least N inference iterations (default = 10)
  --warmUp=N                  Run for N milliseconds to warmup before measuring performance (default = 200)
  --duration=N                Run performance measurements for at least N seconds wallclock time (default = 3)
  --sleepTime=N               Delay inference start with a gap of N milliseconds between launch and compute (default = 0)
  --streams=N                 Instantiate N engines to use concurrently (default = 1)
  --exposeDMA                 Serialize DMA transfers to and from device. (default = disabled)
  --useSpinWait               Actively synchronize on GPU events. This option may decrease synchronization time but increase CPU usage and power (default = disabled)
  --threads                   Enable multithreading to drive engines with independent threads (default = disabled)
  --useCudaGraph              Use cuda graph to capture engine execution and then launch inference (default = disabled)
  --buildOnly                 Skip inference perf measurement (default = disabled)

=== Build and Inference Batch Options ===
                              When using implicit batch, the max batch size of the engine, if not given,
                              is set to the inference batch size;
                              when using explicit batch, if shapes are specified only for inference, they
                              will be used also as min/opt/max in the build profile; if shapes are
                              specified only for the build, the opt shapes will be used also for inference;
                              if both are specified, they must be compatible; and if explicit batch is
                              enabled but neither is specified, the model must provide complete static
                              dimensions, including batch size, for all inputs

=== Reporting Options ===
  --verbose                   Use verbose logging (default = false)
  --avgRuns=N                 Report performance measurements averaged over N consecutive iterations (default = 10)
  --percentile=P              Report performance for the P percentage (0<=P<=100, 0 representing max perf, and 100 representing min perf; (default = 99%)
  --dumpOutput                Print the output tensor(s) of the last inference iteration (default = disabled)
  --dumpProfile               Print profile information per layer (default = disabled)
  --exportTimes=<file>        Write the timing results in a json file (default = disabled)
  --exportOutput=<file>       Write the output tensors to a json file (default = disabled)
  --exportProfile=<file>      Write the profile information per layer in a json file (default = disabled)

=== System Options ===
  --device=N                  Select cuda device N (default = 0)
  --useDLACore=N              Select DLA core N for layers that support DLA (default = none)
  --allowGPUFallback          When DLA is enabled, allow GPU fallback for unsupported layers (default = disabled)
  --plugins                   Plugin library (.so) to load (can be specified multiple times)

=== Help ===
  --help, -h                  Print this message

   一般来说主要使用命令行程序trtexec来做将pytorch模型导出的onnx文件或者tensorflow生成的uff文件或者caffe的权重文件caffemodel和proto文件解析生成模型engine文件,然后使用模型的engine做推理测试效果。

   以onnx为例:

#解析onnx文件生成engine文件,并做推理,输出结果到rsult.json中
./trtexec --onnx=efficientdet-d0-s.onnx --loadInputs='data':o4_clip-1_raw_data.bin --saveEngine=efficientdet-d0-s.engine  --output='regression','classification','anchors' --exportOutput=result.json

#对onnx来说,output不是必须指定的,但是对推理来说loadInputs是必须指定的
./trtexec --onnx=efficientdet-d0-s.onnx --loadInputs='data':o4_clip-1_raw_data.bin --saveEngine=efficientdet-d0-s.engine  --exportOutput=trtexec-result.json

#直接加载已有的engine文件做推理,输出结果数据到trtexec-result.json中
./trtexec --loadEngine=efficientdet-d0-s.engine  --loadInputs='data':o4_clip-1_raw_data.bin  --exportOutput=trtexec-result.json

这里--onnnx、--saveEngine、--loadEngine好理解不用多说,--output是指定network的输出节点名,有多个输出节点时,节点名字符串之间以逗号分隔,对于onnx文件无需指定,对uff和caffe模型才需要指定,--exportOutput是将模型推理的结果数据(各个输出节点的数据)以json格式保存到指定文件中。

这里需要重点说一下--loadInputs,因为help打印出来的提示信息并没有说的很清楚透彻:

--loadInputs=spec           Load input values from files (default = generate random inputs). Input names can be wrapped with single quotes (ex: 'Input:0')
                              Input values spec ::= Ival[","spec]
                                           Ival ::= name":"file

--loadInputs后面是指定模型的输入数据,数据可以是一个或者多个name:file形式的键值对,中间以逗号分隔,这里的name就是输入节点的名字,例如"input:0"、"data",很显然network只有一个输入节点时,就一个name:file,有多个输入节点时才会有多个键值对需要指定,那么后面的file应该是什么数据呢?help提示中并没有说,我开始以为是输入图片数据所在的图片文件的名字,就以这一的形式指定:

     --loadInputs='data':o4_clip-1.jpg

推理效果很差,于是翻看tensorrt的相关代码,层层往下找到它解析loadInputs参数并读取值的地方,发现它直接从file读取二进制数据,没有做任何其他格式处理,这说明这里的file需要是个纯二进制数据文件,那怎么把输入网络的数据写到这样的文件里呢?一种办法当然是在你的网络里自己增加代码,把输入节点经过预处理的数据以二进制方式写入一个文件里,第二种办法比较方便就是利用numpy的to_file()函数 (不要使用numpy的save()函数,save()保存的是numpy格式的,不是纯裸二进制文件!)来直接把预处理后的数据写入二进制数据文件,例如o4_clip-1_raw_data.bin,然后执行trtexec时指定  

     --loadInputs='data':o4_clip-1_raw_data.bin

这时识别效果才会比较正常了(为何说比较正常而不说完全符合预期结果呢?因为tensorrt解析onnx后生成的engine文件在推理时的精度比直接使用pytorch调用原始的pt模型文件或者使用onnxruntime调用同一模型的onnx来识别的精度要差一些,同一图片里的同一个object的得分明显下降,这个问题去年报给NVIDIA了,但是至今还没有解决),贴个我做过的某个机场识别行李箱的例子:

                                                                   图1

                                                                         图2

可以看到使用onnxruntime调用onnx或者使用pytorch调用pt得到的识别结果的图1中的右上方的行李箱的得分为0.958,而使用tensorrt解析onnx后得到的engine对同一张图进行识别的结果的图2中的同一个箱子的得分只有0.148了!我这还是没加--fp16参数减小模型的效果!后面展示识别结果时加阈值过滤,那么右上方那个箱子就成了没识别出来了!所以这效果相差真挺多的,但是这个问题nvidia一直没有解决。所以做边缘端模型落地有时受限于这些硬件或软件的缺陷或限制造成的识别能力下降真的让人很头疼郁闷抓狂,尤其你的领导或客户根本不懂边缘端模型落地和PC/sever上跑模型那是两个世界的话。

 关于使用numpy的to_file()保存模型的预处理了的输入数据的实际例子,粘贴部分代码如下:



...
compound_coef = 0
force_input_size = None  # set None to use default size
img_path = 'o4_clip-1.png'

use_cuda = True
use_float16 = False
cudnn.fastest = True
cudnn.benchmark = True

obj_list = ['baggage']

input_sizes = [512, 640, 768, 896, 1024, 1280, 1280, 1536]
input_size = input_sizes[compound_coef] if force_input_size is None else force_input_size
ori_imgs, framed_imgs, framed_metas = preprocess(img_path, max_size=input_size)

if use_cuda:
    x = torch.stack([torch.from_numpy(fi).cuda() for fi in framed_imgs], 0)
else:
    x = torch.stack([torch.from_numpy(fi) for fi in framed_imgs], 0)

x = x.to(torch.float32 if not use_float16 else torch.float16).permute(0, 3, 1, 2)

model = EfficientDetBackbone(compound_coef=compound_coef, num_classes=len(obj_list))
model.load_state_dict(torch.load(f'logs/airport/efficientdet-d0_499_79500.pth'))
model.requires_grad_(False)
model.eval()

if use_cuda:
    model = model.cuda()
if use_float16:
    model = model.half()

with torch.no_grad():   
    #features, regression, classification, anchors = model(x)
    regression, classification, anchors = model(x)
    nx = to_numpy(x)
    #np.save("raw_data_numpy.npy",nx)
    nx.tofile("o4_clip-1_raw_data.bin")

<think>嗯,用户想用trtexec把YOLOv5的ONNX模型转成TensorRT引擎。首先我得回忆一下trtexec的基本用法trtexecTensorRT自带的一个命令行工具,用来转换和优化模型。用户可能已经导出了ONNX模型,但需要确认是否导出正确,特别是输入输出节点的名称和形状是否正确。YOLOv5的模型可能有动态输入,比如输入尺寸可以是变化的,这时候在转换的时候需要指定动态形状的参数,比如minShapes、optShapes、maxShapes。 接下来,用户的问题是关于如何使用trtexec的具体命令。我需要回想trtexec常用参数。比如--onnx参数指定输入模型,--saveEngine指定输出的引擎文件。动态输入的话,可能需要使用--minShapes、--optShapes、--maxShapes来设置各个输入的最小、最优和最大形状。比如YOLOv5的输入通常是1x3xHxW,其中H和W可能不同。例如,用户可能希望支持从640x640到1024x1024的动态输入,这时候需要设置对应的参数。 另外,用户可能还需要考虑精度,比如是否使用FP16或者INT8优化。这时候需要添加--fp16或者--int8参数。不过INT8需要校准数据,可能比较复杂,所以可能用户暂时只需要FP16。此外,可能还需要指定工作空间大小,使用--workspace参数,默认是16MB,但有时候需要更大的空间。 然后需要检查是否有其他参数需要注意,比如输出层的名称是否正确,或者是否需要排除某些层。不过YOLOv5的ONNX模型通常已经处理好,所以可能不需要额外处理。另外,trtexec的日志输出可以帮助调试,用户可能需要查看是否有错误信息,比如不支持的算子或者形状问题。 可能用户遇到的问题是转换时出现错误,比如动态形状未正确指定,或者某些层不支持。这时候需要确保TensorRT版本支持所有YOLOv5的算子,或者可能需要使用插件。例如,YOLOv5的Focus层可能在早期版本中被替换为切片操作,但ONNX导出时可能已经处理好了。此外,如果模型有多个输出,需要确认trtexec是否能正确处理。 最后,给出一个示例命令,覆盖这些参数,并解释每个部分的作用。同时提醒用户注意事项,比如TensorRT版本、模型兼容性和动态输入设置。可能还需要引用站内的资料,比如引用1提到的导出ONNX,引用3提到的动态输入参数。</think>以下是使用 `trtexec` 将 YOLOv5 ONNX 模型转换为 TensorRT 引擎的步骤和注意事项: --- ### **步骤说明** 1. **基础命令格式** 输入以下命令,替换路径和参数: ```bash trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine ``` - `--onnx`: 指定输入 ONNX 模型路径 - `--saveEngine`: 指定输出的 TensorRT 引擎文件路径 2. **动态输入设置(若需要)** YOLOv5 通常支持动态输入尺寸,需通过以下参数指定输入范围: ```bash trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine \ --minShapes=input:1x3x640x640 \ --optShapes=input:1x3x640x640 \ --maxShapes=input:1x3x1024x1024 ``` - `input`: 输入层的名称(需与 ONNX 模型对齐) - `minShapes`/`optShapes`/`maxShapes`: 最小/最优/最大输入尺寸[^3] 3. **精度优化(可选)** 添加 `--fp16` 或 `--int8` 启用混合精度优化: ```bash trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine --fp16 ``` 4. **工作空间设置(可选)** 指定 GPU 显存工作空间大小(默认 16MB): ```bash trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine --workspace=4096 ``` --- ### **注意事项** 1. **TensorRT 版本兼容性** 确保 TensorRT 版本与 CUDA、cuDNN 匹配,且支持 ONNX 模型中的算子。 2. **模型输入输出名称** 使用 Netron 工具检查 ONNX 模型的输入/输出层名称,确保与命令中的 `input` 一致。 3. **动态输入问题** 若未正确设置 `minShapes`/`optShapes`/`maxShapes`,转换可能失败或推理时出现维度错误。 --- ### **示例完整命令** ```bash trtexec --onnx=yolov5s.onnx --saveEngine=yolov5s.engine \ --minShapes=input:1x3x640x640 \ --optShapes=input:1x3x640x640 \ --maxShapes=input:1x3x1024x1024 \ --fp16 --workspace=4096 ``` ---
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arnold-FY-Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值