| 116 | }; |
| 117 | |
| 118 | template <typename T> class Engine { |
| 119 | public: |
| 120 | Engine(const Options &options); |
| 121 | ~Engine(); |
| 122 | |
| 123 | // Build the onnx model into a TensorRT engine file, cache the model to disk |
| 124 | // (to avoid rebuilding in future), and then load the model into memory The |
| 125 | // default implementation will normalize values between [0.f, 1.f] Setting the |
| 126 | // normalize flag to false will leave values between [0.f, 255.f] (some |
| 127 | // converted models may require this). If the model requires values to be |
| 128 | // normalized between [-1.f, 1.f], use the following params: |
| 129 | // subVals = {0.5f, 0.5f, 0.5f}; |
| 130 | // divVals = {0.5f, 0.5f, 0.5f}; |
| 131 | // normalize = true; |
| 132 | bool buildLoadNetwork(std::string onnxModelPath, const std::array<float, 3> &subVals = {0.f, 0.f, 0.f}, |
| 133 | const std::array<float, 3> &divVals = {1.f, 1.f, 1.f}, bool normalize = true); |
| 134 | |
| 135 | // Load a TensorRT engine file from disk into memory |
| 136 | // The default implementation will normalize values between [0.f, 1.f] |
| 137 | // Setting the normalize flag to false will leave values between [0.f, 255.f] |
| 138 | // (some converted models may require this). If the model requires values to |
| 139 | // be normalized between [-1.f, 1.f], use the following params: |
| 140 | // subVals = {0.5f, 0.5f, 0.5f}; |
| 141 | // divVals = {0.5f, 0.5f, 0.5f}; |
| 142 | // normalize = true; |
| 143 | bool loadNetwork(std::string trtModelPath, const std::array<float, 3> &subVals = {0.f, 0.f, 0.f}, |
| 144 | const std::array<float, 3> &divVals = {1.f, 1.f, 1.f}, bool normalize = true); |
| 145 | |
| 146 | // Run inference. |
| 147 | // Input format [input][batch][cv::cuda::GpuMat] |
| 148 | // Output format [batch][output][feature_vector] |
| 149 | bool runInference(const std::vector<std::vector<cv::cuda::GpuMat>> &inputs, std::vector<std::vector<std::vector<T>>> &featureVectors); |
| 150 | |
| 151 | // Utility method for resizing an image while maintaining the aspect ratio by |
| 152 | // adding padding to smaller dimension after scaling While letterbox padding |
| 153 | // normally adds padding to top & bottom, or left & right sides, this |
| 154 | // implementation only adds padding to the right or bottom side This is done |
| 155 | // so that it's easier to convert detected coordinates (ex. YOLO model) back |
| 156 | // to the original reference frame. |
| 157 | static cv::cuda::GpuMat resizeKeepAspectRatioPadRightBottom(const cv::cuda::GpuMat &input, size_t height, size_t width, |
| 158 | const cv::Scalar &bgcolor = cv::Scalar(0, 0, 0)); |
| 159 | |
| 160 | [[nodiscard]] const std::vector<nvinfer1::Dims3> &getInputDims() const { return m_inputDims; }; |
| 161 | [[nodiscard]] const std::vector<nvinfer1::Dims> &getOutputDims() const { return m_outputDims; }; |
| 162 | |
| 163 | // Utility method for transforming triple nested output array into 2D array |
| 164 | // Should be used when the output batch size is 1, but there are multiple |
| 165 | // output feature vectors |
| 166 | static void transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output); |
| 167 | |
| 168 | // Utility method for transforming triple nested output array into single |
| 169 | // array Should be used when the output batch size is 1, and there is only a |
| 170 | // single output feature vector |
| 171 | static void transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<T> &output); |
| 172 | // Convert NHWC to NCHW and apply scaling and mean subtraction |
| 173 | static cv::cuda::GpuMat blobFromGpuMats(const std::vector<cv::cuda::GpuMat> &batchInput, const std::array<float, 3> &subVals, |
| 174 | const std::array<float, 3> &divVals, bool normalize); |
| 175 |
nothing calls this directly
no outgoing calls
no test coverage detected