简体中文 | English
This example uses the YOLO11n-seg model to demonstrate how to perform Instance Segmentation inference using the Command Line Interface (CLI), Python, and C++.
yolo11n-seg.pt,【TestImages】COCO-part.zip
Please download the required yolo11n-seg.pt
model file and test images through the provided link, and save the model file to the models
folder, and place the extracted test images into the images
folder after unzipping.
Important
If you only want to export the ONNX model (with TensorRT plugins) that can be used for inference in this project through the tensorrt_yolo
provided Command Line Interface (CLI) tool trtyolo
, you can install it via PyPI by simply executing the following command:
pip install -U tensorrt_yolo
If you want to experience the same inference speed as C++, please refer to Install-tensorrt_yolo to build the latest version of tensorrt_yolo
yourself.
Use the following command to export the ONNX format with the EfficientRotatedNMS plugin. For detailed trtyolo
CLI export methods, please read Model Export:
trtyolo export -w models/yolo11n-seg.pt -v yolo11 -o models -s
After running the above command, a yolo11n-seg.onnx
file with a batch_size
of 1 will be generated in the models
folder. Next, use the trtexec
tool to convert the ONNX file to a TensorRT engine (fp16):
trtexec --onnx=models/yolo11n-seg.onnx --saveEngine=models/yolo11n-seg.engine --fp16 --staticPlugins=/path/to/your/TensorRT-YOLO/lib/plugin/libcustom_plugins.so --setPluginsToSerialize=/path/to/your/TensorRT-YOLO/lib/plugin/libcustom_plugins.so
Important
The tensorrt_yolo
installed via PyPI only provides the ONNX model (with TensorRT plugins) for inference in this project and does not provide inference capabilities.
If you want to experience the same inference speed as C++, please refer to Install-tensorrt_yolo to build the latest version of tensorrt_yolo
yourself.
Note
The --cudaGraph
command added from version 4.0 can further accelerate the inference process, but this feature only supports static models.
From version 4.3 and later, support for Instance Segmentation inference is added. The command -m 2, --mode 2
is used to select the Instance Segmentation.
-
Use the
trtyolo
command-line tool from thetensorrt_yolo
library for inference. Run the following command to view help information:trtyolo infer --help
-
Run the following command for inference:
trtyolo infer -e models/yolo11n-seg.engine -m 1 -i images -o output -l labels.txt --cudaGraph
The inference results will be saved in the
output
folder, and a visualization result will be generated.
-
Use the
tensorrt_yolo
library to run the example scriptsegment.py
for inference. -
Run the following command for inference:
python segment.py -e models/yolo11n-seg.engine -i images -o output -l labels.txt --cudaGraph
-
Ensure that the project has been compiled according to the
TensorRT-YOLO
Compilation. -
Compile
segment.cpp
into an executable:# Compile using xmake xmake f -P . --tensorrt="/path/to/your/TensorRT" --deploy="/path/to/your/TensorRT-YOLO" xmake -P . -r # Compile using cmake mkdir -p build && cd build cmake -DTENSORRT_PATH="/path/to/your/TensorRT" -DDEPLOY_PATH="/path/to/your/TensorRT-YOLO" .. cmake --build . -j8 --config Release
After compilation, the executable file will be generated in the
bin
folder of the project root directory. -
Run the following command for inference:
cd bin ./segment -e ../models/yolo11n-seg.engine -i ../images -o ../output -l ../labels.txt --cudaGraph
Important
When inferring with an OBB model built using the --fp16
flag, there may be instances of duplicate anchor boxes. This issue is typically caused by a reduction in precision. Therefore, it is not recommended to build OBB models using the --fp16
precision mode.
Through the above methods, you can successfully complete model inference.