Skip to content

Commit

Permalink
V5 (#53)
Browse files Browse the repository at this point in the history
* Compiling

* Fixed bug

* Added to readme

* Added to gitignore

* Added support for loading TensorRT engine file directly

* Minor fixes

* Added log message

* Added command line parser

* Updated changelog

* Added clang-format and reformatted all files

* Changed formatting
  • Loading branch information
cyrusbehr authored Apr 10, 2024
1 parent 24cf385 commit 7c6c22a
Show file tree
Hide file tree
Showing 8 changed files with 875 additions and 652 deletions.
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
ColumnLimit: 140
IndentWidth: 4
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@
build*/
cmake-build-*
.idea/
.vscode/

*.onnx

*.onnx
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: 'v17.0.3' # Use the sha / tag you want to point at
hooks:
- id: clang-format
types_or: [c++, c, cuda]
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ You will need to supply your own onnx model for this sample code or you can down
### Running the Executable
- Navigate to the build directory
- Run the executable and provide the path to your onnx model.
- ex. `./run_inference_benchmark ../models/yolov8n.onnx`
- ex. `./run_inference_benchmark --onnx_model ../models/yolov8n.onnx`
- Note: See sanity check section below for instructions on how to obtain the yolov8n model.
- The first time you run the executable for a given model and options, a TensorRT engine file will be built from your onnx model. This process is fairly slow and can take 5+ minutes for some models (ex. yolo models).
- Alternatively, you can choose to supply your own TensorRT engine file directly:
- ex. `./run_inference_benchmark --trt_model ../models/yolov8n.engine.NVIDIAGeForceRTX3080LaptopGPU.fp16.1.1`
- Note: See V5.0 changelog below for warnings when supply your own TensorRT engine file.

### Sanity Check
- To perform a sanity check, download the `YOLOv8n` model from [here](https://github.com/ultralytics/ultralytics#models).
Expand Down Expand Up @@ -161,6 +164,12 @@ If this project was helpful to you, I would appreciate if you could give it a st

### Changelog

**V5.0**

- `Engine` class has been modified to take a template parameter which specifies the models output data type. The implementation now supports outputs of type `float`, ``__half`, `int8_t`, `int32_t`, `bool`, and `uint8_t`.
- Added support for loading TensorRT engine file directly without needing to compile from onnx model. Howver, it is highly recommended that you use the API provided to build the engine file from the onnx model, instead of loading a TensorRT model directly. If you choose to load a TensorRT model file directly, you must hand-check that the `Options` have been set correctly for your model (for example, if your model has been compiled for FP32 but you try running FP16 inference, it will fail, potentially without a verbose error).
- Added command line parser.

**V4.1**

- Added support for fixed batch size > 1.
Expand Down
98 changes: 98 additions & 0 deletions src/cmd_line_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once
#include "engine.h"
#include <iostream>

struct CommandLineArguments {
std::string onnxModelPath = "";
std::string trtModelPath = "";
};

inline void showHelp(char *argv[]) {
std::cout << "Usage: " << argv[0] << " [OPTIONS]" << std::endl << std::endl;

std::cout << "Options:" << std::endl;
std::cout << "--onnx_model <string> Path to the ONNX model. "
"(Either onnx_model or trt_model must be provided)"
<< std::endl;
std::cout << "--trt_model <string> Path to the TensorRT model. "
"(Either onnx_model or trt_model must be provided)"
<< std::endl;

std::cout << "Example usage:" << std::endl;
std::cout << argv[0] << " --onnx_model model.onnx" << std::endl;
};

inline bool tryGetNextArgument(int argc, char *argv[], int &currentIndex, std::string &value, std::string flag, bool printErrors = true) {
if (currentIndex + 1 >= argc) {
if (printErrors)
std::cout << "Error: No arguments provided for flag '" << flag << "'" << std::endl;
return false;
}

std::string nextArgument = argv[currentIndex + 1];
if (nextArgument.substr(0, 2) == "--") {
if (printErrors)
std::cout << "Error: No arguments provided for flag '" << flag << "'" << std::endl;
return false;
}

value = argv[++currentIndex];
return true;
};

inline bool parseArguments(int argc, char *argv[], CommandLineArguments &arguments) {
if (argc == 1) {
showHelp(argv);
return false;
}

for (int i = 1; i < argc; i++) {
std::string argument = argv[i];

if (argument.substr(0, 2) == "--") {
std::string flag = argument.substr(2);
std::string nextArgument;

if (flag == "onnx_model") {
if (!tryGetNextArgument(argc, argv, i, nextArgument, flag))
return false;

if (!Util::doesFileExist(nextArgument)) {
std::cout << "Error: Unable to find model at path '" << nextArgument << "' for flag '" << flag << "'" << std::endl;
return false;
}

arguments.onnxModelPath = nextArgument;
}

else if (flag == "trt_model") {
if (!tryGetNextArgument(argc, argv, i, nextArgument, flag))
return false;

if (!Util::doesFileExist(nextArgument)) {
std::cout << "Error: Unable to find model at path '" << nextArgument << "' for flag '" << flag << "'" << std::endl;
return false;
}

arguments.trtModelPath = nextArgument;
}

else {
std::cout << "Error: Unknown flag '" << flag << "'" << std::endl;
showHelp(argv);
return false;
}
} else {
std::cout << "Error: Unknown argument '" << argument << "'" << std::endl;
showHelp(argv);
return false;
}
}

if (arguments.onnxModelPath.empty() && arguments.trtModelPath.empty()) {
std::cout << "Error: Must specify either 'onnx_model' or 'trt_model'" << std::endl;
return false;
}

return true;
}
Loading

0 comments on commit 7c6c22a

Please sign in to comment.