-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_compilation_tinkering.cpp
More file actions
88 lines (68 loc) · 3.75 KB
/
gpu_compilation_tinkering.cpp
File metadata and controls
88 lines (68 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "CelanturDetection.h"
#include "CelanturSDKInterface.h"
#include "CommonParameters.h"
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <boost/dll.hpp>
const std::filesystem::path exe_path = boost::dll::program_location().parent_path().string();
const std::filesystem::path assets_path = exe_path/".."/"assets";
const std::filesystem::path output_path = exe_path/".."/"output";
const std::filesystem::path gpu_plugin_location = "/usr/local/lib/libTensorRTRuntime.so";
const std::filesystem::path license_file = assets_path/"license";
const std::filesystem::path image_path = assets_path/"image.jpg";
const std::filesystem::path out_image_path = output_path/"gpu_compilation_tinkering.jpg";
const std::filesystem::path model_path = assets_path/"v6-static-fp32.onnx.enc";
const std::filesystem::path model_path_compiled = assets_path/"v6-static-fp16-compiled-full.trt";
/**
The purpose of this example is to show the options one can run SDK using Nvidia GPU inference engine TensorRT.
*/
int main(int argc, char** argv) {
std::filesystem::create_directories(output_path);
// First, compile openvino model if it does not exist
if (!std::filesystem::exists(model_path_compiled)) {
// Preload model to get to the compilation settings
CelanturSDK::ModelCompilerParams compiler_params;
compiler_params.inference_plugin = gpu_plugin_location;
CelanturSDK::ModelCompiler compiler(license_file, compiler_params);
celantur::InferenceEnginePluginCompileSettings settings = compiler.preload_model(model_path);
// TensorRT has some specific settings we can modify; for example, we can set the precision and optimisation level
// In this example, we select the highest optimisation level and FP16 precision for maximum performance
settings["precision"] = celantur::CompilePrecision::FP16;
settings["optimisation_level"] = celantur::OptimisationLevel::Full;
// Now compile the model
std::cout << "Compiling model to " << model_path_compiled << std::endl;
compiler.compile_model(settings, model_path_compiled);
}
// Second, start the processor with the compiled model
celantur::ProcessorParams params;
// Manually point to the CPU inference plugin
params.inference_plugin = gpu_plugin_location;
std::cout << "Looking for license at " << license_file << std::endl;
// OpenCV uses by default BGR, but the Celantur SDK uses RGB so we need to set swapRB to true
params.swapRB = true;
// Start the processor with given parameters and license file
CelanturSDK::Processor processor(params, license_file);
// Get the available inference engine settings and their default values
// For the tensorRT plugin currently there are no additional settings but this may change in the future
celantur::InferenceEnginePluginSettings settings = processor.get_inference_settings(model_path_compiled);
std::cout << "Inference engine parameters:" << std::endl;
for (const std::pair<std::string, std::any>& pair : settings) {
std::cout << pair.first << std::endl;
}
// Load the compiled inference model.
std::cout << "load model from " << model_path << std::endl;
processor.load_inference_model(settings);
// Load some image for processing
std::cout << "loading image from " << image_path << std::endl;
cv::Mat image = cv::imread(image_path);
// Enqueue the image for processing
processor.process(image);
// Get the result
cv::Mat out = processor.get_result();
// Discard the detections. Necessary to free up the memory.
processor.get_detections();
// Save the result
std::cout << "saving result to " << out_image_path << std::endl;
cv::imwrite(out_image_path, out);
return 0;
}