13 #ifndef __NVDSINFER_CONTEXT_IMPL_H__
14 #define __NVDSINFER_CONTEXT_IMPL_H__
17 #include <condition_variable>
26 #include <NvCaffeParser.h>
28 #include <cuda_runtime_api.h>
30 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wclass-memaccess"
35 #include <opencv2/objdetect/objdetect.hpp>
37 #pragma GCC diagnostic pop
61 unsigned int m_BatchSize = 0;
62 std::unique_ptr<CudaEvent> m_OutputCopyDoneEvent =
nullptr;
63 bool m_BuffersWithContext =
true;
74 const NvDsInferBatchDimsLayerInfo& layerInfo,
int id = 0);
81 bool setScaleOffsets(
float scale,
const std::vector<float>& offsets = {});
89 void* devBuf,
CudaStream& mainStream, CudaEvent* waitingEvent);
103 NvDsInferBatchDimsLayerInfo m_NetworkInputLayer;
104 float m_Scale = 1.0f;
105 std::vector<float> m_ChannelMeans;
106 std::string m_MeanFile;
108 std::unique_ptr<CudaStream> m_PreProcessStream;
110 std::shared_ptr<CudaEvent> m_PreProcessCompleteEvent;
111 std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
147 const std::vector<std::vector<std::string>>&
getLabels()
const
170 const std::vector<NvDsInferLayerInfo>& outputLayers,
220 const std::vector<NvDsInferLayerInfo>& outputLayers,
223 bool parseBoundingBox(
224 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
227 std::vector<NvDsInferObjectDetectionInfo>& objectList);
229 std::vector<int> nonMaximumSuppression
230 (std::vector<std::pair<float, int>>& scoreIndex,
231 std::vector<NvDsInferParseObjectInfo>& bbox,
232 const float nmsThreshold);
239 const std::vector<NvDsInferLayerInfo>& outputLayers,
242 std::vector<NvDsInferObjectDetectionInfo> &objectList);
243 void filterTopKOutputs(
const int topK,
244 std::vector<NvDsInferObjectDetectionInfo> &objectList);
248 bool m_UseDBScan = false;
249 std::shared_ptr<NvDsInferDBScan> m_DBScanHandle;
253 uint32_t m_NumDetectedClasses = 0;
260 std::vector<NvDsInferObjectDetectionInfo> m_ObjectList;
263 std::vector<std::vector<cv::Rect>> m_PerClassCvRectList;
266 std::vector<std::vector<NvDsInferObjectDetectionInfo>> m_PerClassObjectList;
284 const std::vector<NvDsInferLayerInfo>& outputLayers,
289 const std::vector<NvDsInferLayerInfo>& outputLayers,
292 std::vector<NvDsInferInstanceMaskInfo> &objectList);
293 void filterTopKOutputs(
const int topK,
294 std::vector<NvDsInferInstanceMaskInfo> &objectList);
300 uint32_t m_NumDetectedClasses = 0;
303 std::vector<NvDsInferDetectionParams> m_PerClassDetectionParams;
307 std::vector<NvDsInferInstanceMaskInfo> m_InstanceMaskList;
309 std::vector<std::vector<NvDsInferInstanceMaskInfo>> m_PerClassInstanceMaskList;
326 const std::vector<NvDsInferLayerInfo>& outputLayers,
330 const std::vector<NvDsInferLayerInfo>& outputLayers,
333 bool parseAttributesFromSoftmaxLayers(
334 std::vector<NvDsInferLayerInfo>
const& outputLayersInfo,
336 std::vector<NvDsInferAttribute>& attrList, std::string& attrString);
339 float m_ClassifierThreshold = 0.0f;
355 const std::vector<NvDsInferLayerInfo>& outputLayers,
359 const std::vector<NvDsInferLayerInfo>& outputLayers,
363 float m_SegmentationThreshold = 0.0f;
378 const std::vector<NvDsInferLayerInfo>& outputLayers,
384 class BackendContext;
415 void fillLayersInfo(std::vector<NvDsInferLayerInfo> &layersInfo)
override;
417 const std::vector<std::vector<std::string>>& getLabels()
override;
418 void destroy()
override;
428 std::unique_ptr<BackendContext> generateBackendContext(
430 std::unique_ptr<BackendContext> buildModel(
432 bool deserializeEngineAndBackend(
const std::string enginePath,
int dla,
433 std::shared_ptr<TrtEngine>& engine,
434 std::unique_ptr<BackendContext>& backend);
444 static const int INPUT_LAYER_INDEX = 0;
448 uint32_t m_UniqueID = 0;
449 uint32_t m_GpuID = 0;
453 std::unique_ptr<BackendContext> m_BackendContext;
454 std::shared_ptr<DlLibHandle> m_CustomLibHandle;
456 std::unique_ptr<InferPreprocessor> m_Preprocessor;
457 std::unique_ptr<InferPostprocessor> m_Postprocessor;
459 uint32_t m_MaxBatchSize = 0;
464 std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayerInfo;
465 std::vector<NvDsInferBatchDimsLayerInfo> m_OutputLayerInfo;
466 NvDsInferBatchDimsLayerInfo m_InputImageLayerInfo;
468 std::vector<void *> m_BindingBuffers;
469 std::vector<std::unique_ptr<CudaDeviceBuffer>> m_InputDeviceBuffers;
472 std::vector<std::shared_ptr<NvDsInferBatch>> m_Batches;
473 std::mutex m_BatchesMutex;
481 std::unique_ptr<CudaStream> m_InferStream;
482 std::unique_ptr<CudaStream> m_PostprocessStream;
485 std::shared_ptr<CudaEvent> m_InputConsumedEvent;
488 std::shared_ptr<CudaEvent> m_InferCompleteEvent;
492 bool m_Initialized =
false;
493 uint32_t m_AutoIncMem = 1;
494 double m_MaxGPUMem = 99;
495 bool m_DumpIpTensor =
false;
496 std::string m_DumpIpTensorFilePath =
" ";
497 bool m_OverwriteIpTensor =
false;
498 std::string m_OverwriteIpTensorFilePath =
" ";
499 std::ifstream m_OverwriteIpTensorFile;
504 #define printMsg(level, tag_str, fmt, ...) \
506 char* baseName = strrchr((char*)__FILE__, '/'); \
507 baseName = (baseName) ? (baseName + 1) : (char*)__FILE__; \
508 char logMsgBuffer[5 * _MAX_STR_LENGTH + 1]; \
509 snprintf(logMsgBuffer, 5 * _MAX_STR_LENGTH, \
510 tag_str " NvDsInferContextImpl::%s() <%s:%d> [UID = %d]: " fmt, \
511 __func__, baseName, __LINE__, m_UniqueID, ##__VA_ARGS__); \
512 if (m_LoggingFunc) { \
513 m_LoggingFunc(level, logMsgBuffer); \
515 fprintf(stderr, "%s\n", logMsgBuffer); \
519 #define printError(fmt, ...) \
521 printMsg (NVDSINFER_LOG_ERROR, "Error in", fmt, ##__VA_ARGS__); \
524 #define printWarning(fmt, ...) \
526 printMsg (NVDSINFER_LOG_WARNING, "Warning from", fmt, ##__VA_ARGS__); \
529 #define printInfo(fmt, ...) \
531 printMsg (NVDSINFER_LOG_INFO, "Info from", fmt, ##__VA_ARGS__); \
534 #define printDebug(fmt, ...) \
536 printMsg (NVDSINFER_LOG_DEBUG, "DEBUG", fmt, ##__VA_ARGS__); \