-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add code for article How To Run Inference Using TensorRT C++ API
- Loading branch information
1 parent
61e757b
commit dfe1570
Showing
10 changed files
with
1,520 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
set(CMAKE_CXX_STANDARD 14) | ||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) | ||
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH}) | ||
|
||
project(TensorRTSample) | ||
|
||
find_package(Threads) | ||
find_package(CUDA REQUIRED) | ||
find_package(OpenCV REQUIRED) | ||
find_package(TensorRT REQUIRED) | ||
|
||
# TARGETS -------------------------------------------------------------------------------------------------------------- | ||
add_executable(trt_sample trt_sample.cpp) | ||
target_include_directories(trt_sample PUBLIC ${OpenCV_INCLUDE_DIRS} ${CUDA_INCLUDE_DIRS} ${TensorRT_INCLUDE_DIRS}) | ||
target_link_libraries(trt_sample PUBLIC ${OpenCV_LIBS} ${CUDA_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${TensorRT_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# source: | ||
# https://github.com/NVIDIA/tensorrt-laboratory/blob/master/cmake/FindTensorRT.cmake | ||
|
||
# This module defines the following variables: | ||
# | ||
# :: | ||
# | ||
# TensorRT_INCLUDE_DIRS | ||
# TensorRT_LIBRARIES | ||
# TensorRT_FOUND | ||
# | ||
# :: | ||
# | ||
# TensorRT_VERSION_STRING - version (x.y.z) | ||
# TensorRT_VERSION_MAJOR - major version (x) | ||
# TensorRT_VERSION_MINOR - minor version (y) | ||
# TensorRT_VERSION_PATCH - patch version (z) | ||
# | ||
# Hints | ||
# ^^^^^ | ||
# A user may set ``TensorRT_DIR`` to an installation root to tell this module where to look. | ||
# | ||
set(_TensorRT_SEARCHES) | ||
|
||
if(TensorRT_DIR) | ||
set(_TensorRT_SEARCH_ROOT PATHS ${TensorRT_DIR} NO_DEFAULT_PATH) | ||
list(APPEND _TensorRT_SEARCHES _TensorRT_SEARCH_ROOT) | ||
endif() | ||
|
||
# appends some common paths | ||
set(_TensorRT_SEARCH_NORMAL | ||
PATHS "/usr" | ||
) | ||
list(APPEND _TensorRT_SEARCHES _TensorRT_SEARCH_NORMAL) | ||
|
||
# Include dir | ||
foreach(search ${_TensorRT_SEARCHES}) | ||
find_path(TensorRT_INCLUDE_DIR NAMES NvInfer.h ${${search}} PATH_SUFFIXES include) | ||
endforeach() | ||
|
||
if(NOT TensorRT_LIBRARY) | ||
foreach(search ${_TensorRT_SEARCHES}) | ||
find_library(TensorRT_LIBRARY NAMES nvinfer ${${search}} PATH_SUFFIXES lib) | ||
endforeach() | ||
endif() | ||
|
||
if(NOT TensorRT_PARSERS_LIBRARY) | ||
foreach(search ${_TensorRT_SEARCHES}) | ||
find_library(TensorRT_NVPARSERS_LIBRARY NAMES nvparsers ${${search}} PATH_SUFFIXES lib) | ||
endforeach() | ||
endif() | ||
|
||
if(NOT TensorRT_NVONNXPARSER_LIBRARY) | ||
foreach(search ${_TensorRT_SEARCHES}) | ||
find_library(TensorRT_NVONNXPARSER_LIBRARY NAMES nvonnxparser ${${search}} PATH_SUFFIXES lib) | ||
endforeach() | ||
endif() | ||
|
||
mark_as_advanced(TensorRT_INCLUDE_DIR) | ||
|
||
if(TensorRT_INCLUDE_DIR AND EXISTS "${TensorRT_INCLUDE_DIR}/NvInfer.h") | ||
file(STRINGS "${TensorRT_INCLUDE_DIR}/NvInfer.h" TensorRT_MAJOR REGEX "^#define NV_TENSORRT_MAJOR [0-9]+.*$") | ||
file(STRINGS "${TensorRT_INCLUDE_DIR}/NvInfer.h" TensorRT_MINOR REGEX "^#define NV_TENSORRT_MINOR [0-9]+.*$") | ||
file(STRINGS "${TensorRT_INCLUDE_DIR}/NvInfer.h" TensorRT_PATCH REGEX "^#define NV_TENSORRT_PATCH [0-9]+.*$") | ||
|
||
string(REGEX REPLACE "^#define NV_TENSORRT_MAJOR ([0-9]+).*$" "\\1" TensorRT_VERSION_MAJOR "${TensorRT_MAJOR}") | ||
string(REGEX REPLACE "^#define NV_TENSORRT_MINOR ([0-9]+).*$" "\\1" TensorRT_VERSION_MINOR "${TensorRT_MINOR}") | ||
string(REGEX REPLACE "^#define NV_TENSORRT_PATCH ([0-9]+).*$" "\\1" TensorRT_VERSION_PATCH "${TensorRT_PATCH}") | ||
set(TensorRT_VERSION_STRING "${TensorRT_VERSION_MAJOR}.${TensorRT_VERSION_MINOR}.${TensorRT_VERSION_PATCH}") | ||
endif() | ||
|
||
include(FindPackageHandleStandardArgs) | ||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(TensorRT REQUIRED_VARS TensorRT_LIBRARY TensorRT_INCLUDE_DIR VERSION_VAR TensorRT_VERSION_STRING) | ||
|
||
if(TensorRT_FOUND) | ||
set(TensorRT_INCLUDE_DIRS ${TensorRT_INCLUDE_DIR}) | ||
|
||
if(NOT TensorRT_LIBRARIES) | ||
set(TensorRT_LIBRARIES ${TensorRT_LIBRARY} ${TensorRT_NVONNXPARSER_LIBRARY} ${TensorRT_NVPARSERS_LIBRARY}) | ||
endif() | ||
|
||
if(NOT TARGET TensorRT::TensorRT) | ||
add_library(TensorRT::TensorRT UNKNOWN IMPORTED) | ||
set_target_properties(TensorRT::TensorRT PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${TensorRT_INCLUDE_DIRS}") | ||
set_property(TARGET TensorRT::TensorRT APPEND PROPERTY IMPORTED_LOCATION "${TensorRT_LIBRARY}") | ||
endif() | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# How To Run Inference Using TensorRT C++ API | ||
The blog post is here: https://www.learnopencv.com/how-to-run-inference-using-tensorrt-c-api/ | ||
|
||
### To run PyTorch part: | ||
```shell script | ||
python3 -m pip install -r requirements.txt | ||
python3 pytorch_model.py | ||
``` | ||
|
||
### To run TensorRT part: | ||
1. Install [CMake](https://cmake.org/) at least 3.10 version | ||
2. Download and install NVIDIA CUDA 10.0 or later following by official instruction: [link](https://developer.nvidia.com/cuda-10.0-download-archive) | ||
3. Download and extract CuDNN library for your CUDA version (login required): [link](https://developer.nvidia.com/rdp/cudnn-download) | ||
4. Download and extract NVIDIA TensorRT library for your CUDA version (login required): [link](https://developer.nvidia.com/nvidia-tensorrt-6x-download). The minimum required version is 6.0.1.5 | ||
5. Add the path to CUDA, TensorRT, CuDNN to PATH variable (or LD_LIBRARY_PATH) | ||
6. Build or install a pre-built version of OpenCV and OpenCV Contrib. The minimum required version is 4.0.0. | ||
|
||
```shell script | ||
mkdir build | ||
cd build | ||
cmake -DOpenCV_DIR=[path-to-opencv-build] -DTensorRT_DIR=[path-to-tensorrt] .. | ||
make -j8 | ||
trt_sample[.exe] resnet50.onnx turkish_coffee.jpg | ||
``` | ||
|
||
# AI Courses by OpenCV | ||
|
||
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start. | ||
|
||
<a href="https://opencv.org/courses/"> | ||
<p align="center"> | ||
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png"> | ||
</p> | ||
</a> |
Oops, something went wrong.