13 #ifndef __NVDSINFER_CONTEXT_IMPL_H__
14 #define __NVDSINFER_CONTEXT_IMPL_H__
17 #include <condition_variable>
27 #include <cuda_runtime_api.h>
29 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wclass-memaccess"
34 #include <opencv2/objdetect/objdetect.hpp>
36 #pragma GCC diagnostic pop
38 #include <nvdsinfer_context.h>
39 #include <nvdsinfer_custom_impl.h>
40 #include <nvdsinfer_utils.h>
41 #include <nvdsinfer_logger.h>
60 unsigned int m_BatchSize = 0;
61 std::unique_ptr<CudaEvent> m_OutputCopyDoneEvent =
nullptr;
62 bool m_BuffersWithContext =
true;
73 const NvDsInferBatchDimsLayerInfo& layerInfo,
int id = 0);
80 bool setScaleOffsets(
float scale,
const std::vector<float>& offsets = {});
88 void* devBuf,
CudaStream& mainStream, CudaEvent* waitingEvent);
102 NvDsInferBatchDimsLayerInfo m_NetworkInputLayer;
103 float m_Scale = 1.0f;
104 std::vector<float> m_ChannelMeans;
105 std::string m_MeanFile;
107 std::unique_ptr<CudaStream> m_PreProcessStream;
109 std::shared_ptr<CudaEvent> m_PreProcessCompleteEvent;
110 std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
146 const std::vector<std::vector<std::string>>&
getLabels()
const
169 const std::vector<NvDsInferLayerInfo>& outputLayers,
219 const std::vector<NvDsInferLayerInfo>& outputLayers,
222 bool parseBoundingBox(
223 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
226 std::vector<NvDsInferObjectDetectionInfo>& objectList);
228 std::vector<int> nonMaximumSuppression
229 (std::vector<std::pair<float, int>>& scoreIndex,
230 std::vector<NvDsInferParseObjectInfo>& bbox,
231 const float nmsThreshold);
238 const std::vector<NvDsInferLayerInfo>& outputLayers,
241 std::vector<NvDsInferObjectDetectionInfo> &objectList);
242 void filterTopKOutputs(
const int topK,
243 std::vector<NvDsInferObjectDetectionInfo> &objectList);
247 bool m_UseDBScan = false;
248 std::shared_ptr<NvDsInferDBScan> m_DBScanHandle;
252 uint32_t m_NumDetectedClasses = 0;
259 std::vector<NvDsInferObjectDetectionInfo> m_ObjectList;
262 std::vector<std::vector<cv::Rect>> m_PerClassCvRectList;
265 std::vector<std::vector<NvDsInferObjectDetectionInfo>> m_PerClassObjectList;
283 const std::vector<NvDsInferLayerInfo>& outputLayers,
288 const std::vector<NvDsInferLayerInfo>& outputLayers,
291 std::vector<NvDsInferInstanceMaskInfo> &objectList);
292 void filterTopKOutputs(
const int topK,
293 std::vector<NvDsInferInstanceMaskInfo> &objectList);
299 uint32_t m_NumDetectedClasses = 0;
302 std::vector<NvDsInferDetectionParams> m_PerClassDetectionParams;
306 std::vector<NvDsInferInstanceMaskInfo> m_InstanceMaskList;
308 std::vector<std::vector<NvDsInferInstanceMaskInfo>> m_PerClassInstanceMaskList;
325 const std::vector<NvDsInferLayerInfo>& outputLayers,
329 const std::vector<NvDsInferLayerInfo>& outputLayers,
332 bool parseAttributesFromSoftmaxLayers(
333 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
335 std::vector<NvDsInferAttribute>& attrList, std::string& attrString);
338 float m_ClassifierThreshold = 0.0f;
354 const std::vector<NvDsInferLayerInfo>& outputLayers,
358 const std::vector<NvDsInferLayerInfo>& outputLayers,
361 bool parseSemanticSegmentationOutput(
362 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
364 float segmentationThreshold,
unsigned int numClasses,
365 int* classificationMap,
float*& classProbabilityMap);
368 float m_SegmentationThreshold = 0.0f;
371 uint32_t m_NumSegmentationClasses = 0;
385 const std::vector<NvDsInferLayerInfo>& outputLayers,
391 class BackendContext;
422 void fillLayersInfo(std::vector<NvDsInferLayerInfo> &layersInfo)
override;
424 const std::vector<std::vector<std::string>>& getLabels()
override;
425 void destroy()
override;
435 std::unique_ptr<BackendContext> generateBackendContext(
437 std::unique_ptr<BackendContext> buildModel(
439 bool deserializeEngineAndBackend(
const std::string enginePath,
int dla,
440 std::shared_ptr<TrtEngine>& engine,
441 std::unique_ptr<BackendContext>& backend);
452 static const int INPUT_LAYER_INDEX = 0;
456 uint32_t m_UniqueID = 0;
457 uint32_t m_GpuID = 0;
461 std::unique_ptr<BackendContext> m_BackendContext;
462 std::shared_ptr<DlLibHandle> m_CustomLibHandle;
464 std::unique_ptr<InferPreprocessor> m_Preprocessor;
465 std::unique_ptr<InferPostprocessor> m_Postprocessor;
467 uint32_t m_MaxBatchSize = 0;
472 std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayerInfo;
473 std::vector<NvDsInferBatchDimsLayerInfo> m_OutputLayerInfo;
474 NvDsInferBatchDimsLayerInfo m_InputImageLayerInfo;
476 std::vector<void *> m_BindingBuffers;
477 std::vector<std::unique_ptr<CudaDeviceBuffer>> m_InputDeviceBuffers;
480 std::vector<std::shared_ptr<NvDsInferBatch>> m_Batches;
481 std::mutex m_BatchesMutex;
489 std::unique_ptr<CudaStream> m_InferStream;
490 std::unique_ptr<CudaStream> m_PostprocessStream;
493 std::shared_ptr<CudaEvent> m_InputConsumedEvent;
496 std::shared_ptr<CudaEvent> m_InferCompleteEvent;
500 bool m_Initialized =
false;
501 uint32_t m_AutoIncMem = 1;
502 double m_MaxGPUMem = 99;
503 bool m_DumpIpTensor =
false;
504 std::string m_DumpIpTensorFilePath =
" ";
505 bool m_OverwriteIpTensor =
false;
506 std::string m_OverwriteIpTensorFilePath =
" ";
507 std::ifstream m_OverwriteIpTensorFile;
512 #define printMsg(level, tag_str, fmt, ...) \
514 char* baseName = strrchr((char*)__FILE__, '/'); \
515 baseName = (baseName) ? (baseName + 1) : (char*)__FILE__; \
516 char logMsgBuffer[5 * _MAX_STR_LENGTH + 1]; \
517 snprintf(logMsgBuffer, 5 * _MAX_STR_LENGTH, \
518 tag_str " NvDsInferContextImpl::%s() <%s:%d> [UID = %d]: " fmt, \
519 __func__, baseName, __LINE__, m_UniqueID, ##__VA_ARGS__); \
520 if (m_LoggingFunc) { \
521 m_LoggingFunc(level, logMsgBuffer); \
523 fprintf(stderr, "%s\n", logMsgBuffer); \
527 #define printError(fmt, ...) \
529 printMsg (NVDSINFER_LOG_ERROR, "Error in", fmt, ##__VA_ARGS__); \
532 #define printWarning(fmt, ...) \
534 printMsg (NVDSINFER_LOG_WARNING, "Warning from", fmt, ##__VA_ARGS__); \
537 #define printInfo(fmt, ...) \
539 printMsg (NVDSINFER_LOG_INFO, "Info from", fmt, ##__VA_ARGS__); \
542 #define printDebug(fmt, ...) \
544 printMsg (NVDSINFER_LOG_DEBUG, "DEBUG", fmt, ##__VA_ARGS__); \