18 #ifndef __NVDSINFER_CONTEXT_IMPL_H__
19 #define __NVDSINFER_CONTEXT_IMPL_H__
22 #include <condition_variable>
32 #include <cuda_runtime_api.h>
34 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wclass-memaccess"
39 #include <opencv2/objdetect/objdetect.hpp>
41 #pragma GCC diagnostic pop
43 #include <nvdsinfer_context.h>
44 #include <nvdsinfer_custom_impl.h>
45 #include <nvdsinfer_utils.h>
46 #include <nvdsinfer_logger.h>
60 std::vector<void*> m_DeviceBuffers;
61 std::vector<std::unique_ptr<CudaHostBuffer>> m_HostBuffers;
63 std::vector<std::unique_ptr<CudaDeviceBuffer>> m_OutputDeviceBuffers;
65 unsigned int m_BatchSize = 0;
66 std::unique_ptr<CudaEvent> m_OutputCopyDoneEvent =
nullptr;
67 bool m_BuffersWithContext =
true;
74 class InferPreprocessor
78 const NvDsInferBatchDimsLayerInfo& layerInfo,
int id = 0);
85 bool setScaleOffsets(
float scale,
const std::vector<float>& offsets = {});
93 void* devBuf,
CudaStream& mainStream, CudaEvent* waitingEvent);
107 NvDsInferBatchDimsLayerInfo m_NetworkInputLayer;
108 float m_Scale = 1.0f;
109 std::vector<float> m_ChannelMeans;
110 std::string m_MeanFile;
112 std::unique_ptr<CudaStream> m_PreProcessStream;
114 std::shared_ptr<CudaEvent> m_PreProcessCompleteEvent;
115 std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
121 class InferPostprocessor
151 const std::vector<std::vector<std::string>>&
getLabels()
const
174 const std::vector<NvDsInferLayerInfo>& outputLayers,
208 std::vector<std::vector<std::string>>
m_Labels;
212 class DetectPostprocessor :
public InferPostprocessor
224 const std::vector<NvDsInferLayerInfo>& outputLayers,
227 bool parseBoundingBox(
228 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
231 std::vector<NvDsInferObjectDetectionInfo>& objectList);
233 std::vector<int> nonMaximumSuppression
234 (std::vector<std::pair<float, int>>& scoreIndex,
235 std::vector<NvDsInferParseObjectInfo>& bbox,
236 const float nmsThreshold);
243 const std::vector<NvDsInferLayerInfo>& outputLayers,
246 std::vector<NvDsInferObjectDetectionInfo> &objectList);
247 void filterTopKOutputs(
const int topK,
248 std::vector<NvDsInferObjectDetectionInfo> &objectList);
252 bool m_UseDBScan = false;
253 std::shared_ptr<NvDsInferDBScan> m_DBScanHandle;
257 uint32_t m_NumDetectedClasses = 0;
264 std::vector<NvDsInferObjectDetectionInfo> m_ObjectList;
267 std::vector<std::vector<cv::Rect>> m_PerClassCvRectList;
270 std::vector<std::vector<NvDsInferObjectDetectionInfo>> m_PerClassObjectList;
276 class InstanceSegmentPostprocessor :
public InferPostprocessor
288 const std::vector<NvDsInferLayerInfo>& outputLayers,
293 const std::vector<NvDsInferLayerInfo>& outputLayers,
296 std::vector<NvDsInferInstanceMaskInfo> &objectList);
297 void filterTopKOutputs(
const int topK,
298 std::vector<NvDsInferInstanceMaskInfo> &objectList);
304 uint32_t m_NumDetectedClasses = 0;
307 std::vector<NvDsInferDetectionParams> m_PerClassDetectionParams;
311 std::vector<NvDsInferInstanceMaskInfo> m_InstanceMaskList;
313 std::vector<std::vector<NvDsInferInstanceMaskInfo>> m_PerClassInstanceMaskList;
319 class ClassifyPostprocessor :
public InferPostprocessor
330 const std::vector<NvDsInferLayerInfo>& outputLayers,
334 const std::vector<NvDsInferLayerInfo>& outputLayers,
337 bool parseAttributesFromSoftmaxLayers(
338 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
340 std::vector<NvDsInferAttribute>& attrList, std::string& attrString);
343 float m_ClassifierThreshold = 0.0f;
348 class SegmentPostprocessor :
public InferPostprocessor
359 const std::vector<NvDsInferLayerInfo>& outputLayers,
363 const std::vector<NvDsInferLayerInfo>& outputLayers,
366 bool parseSemanticSegmentationOutput(
367 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
369 float segmentationThreshold,
unsigned int numClasses,
370 int* classificationMap,
float*& classProbabilityMap);
373 float m_SegmentationThreshold = 0.0f;
376 uint32_t m_NumSegmentationClasses = 0;
379 class OtherPostprocessor :
public InferPostprocessor
390 const std::vector<NvDsInferLayerInfo>& outputLayers,
396 class BackendContext;
401 class NvDsInferContextImpl :
public INvDsInferContext
427 void fillLayersInfo(std::vector<NvDsInferLayerInfo> &layersInfo)
override;
429 const std::vector<std::vector<std::string>>& getLabels()
override;
430 void destroy()
override;
440 std::unique_ptr<BackendContext> generateBackendContext(
442 std::unique_ptr<BackendContext> buildModel(
444 bool deserializeEngineAndBackend(
const std::string enginePath,
int dla,
445 std::shared_ptr<TrtEngine>& engine,
446 std::unique_ptr<BackendContext>& backend);
457 static const int INPUT_LAYER_INDEX = 0;
461 uint32_t m_UniqueID = 0;
462 uint32_t m_GpuID = 0;
466 std::unique_ptr<BackendContext> m_BackendContext;
467 std::shared_ptr<DlLibHandle> m_CustomLibHandle;
469 std::unique_ptr<InferPreprocessor> m_Preprocessor;
470 std::unique_ptr<InferPostprocessor> m_Postprocessor;
472 uint32_t m_MaxBatchSize = 0;
477 std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayerInfo;
478 std::vector<NvDsInferBatchDimsLayerInfo> m_OutputLayerInfo;
479 NvDsInferBatchDimsLayerInfo m_InputImageLayerInfo;
481 std::vector<void *> m_BindingBuffers;
482 std::vector<std::unique_ptr<CudaDeviceBuffer>> m_InputDeviceBuffers;
485 std::vector<std::shared_ptr<NvDsInferBatch>> m_Batches;
486 std::mutex m_BatchesMutex;
491 GuardQueue<std::list<NvDsInferBatch*>> m_FreeBatchQueue;
492 GuardQueue<std::list<NvDsInferBatch*>> m_ProcessBatchQueue;
494 std::unique_ptr<CudaStream> m_InferStream;
495 std::unique_ptr<CudaStream> m_PostprocessStream;
498 std::shared_ptr<CudaEvent> m_InputConsumedEvent;
501 std::shared_ptr<CudaEvent> m_InferCompleteEvent;
505 bool m_Initialized =
false;
506 uint32_t m_AutoIncMem = 1;
507 double m_MaxGPUMem = 99;
508 bool m_DumpIpTensor =
false;
509 std::string m_DumpIpTensorFilePath =
" ";
510 bool m_OverwriteIpTensor =
false;
511 std::string m_OverwriteIpTensorFilePath =
" ";
512 std::ifstream m_OverwriteIpTensorFile;
517 #define printMsg(level, tag_str, fmt, ...) \
519 char* baseName = strrchr((char*)__FILE__, '/'); \
520 baseName = (baseName) ? (baseName + 1) : (char*)__FILE__; \
521 char logMsgBuffer[5 * _MAX_STR_LENGTH + 1]; \
522 snprintf(logMsgBuffer, 5 * _MAX_STR_LENGTH, \
523 tag_str " NvDsInferContextImpl::%s() <%s:%d> [UID = %d]: " fmt, \
524 __func__, baseName, __LINE__, m_UniqueID, ##__VA_ARGS__); \
525 if (m_LoggingFunc) { \
526 m_LoggingFunc(level, logMsgBuffer); \
528 fprintf(stderr, "%s\n", logMsgBuffer); \
532 #define printError(fmt, ...) \
534 printMsg (NVDSINFER_LOG_ERROR, "Error in", fmt, ##__VA_ARGS__); \
537 #define printWarning(fmt, ...) \
539 printMsg (NVDSINFER_LOG_WARNING, "Warning from", fmt, ##__VA_ARGS__); \
542 #define printInfo(fmt, ...) \
544 printMsg (NVDSINFER_LOG_INFO, "Info from", fmt, ##__VA_ARGS__); \
547 #define printDebug(fmt, ...) \
549 printMsg (NVDSINFER_LOG_DEBUG, "DEBUG", fmt, ##__VA_ARGS__); \