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
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,
362 float m_SegmentationThreshold = 0.0f;
377 const std::vector<NvDsInferLayerInfo>& outputLayers,
383 class BackendContext;
414 void fillLayersInfo(std::vector<NvDsInferLayerInfo> &layersInfo)
override;
416 const std::vector<std::vector<std::string>>& getLabels()
override;
417 void destroy()
override;
427 std::unique_ptr<BackendContext> generateBackendContext(
429 std::unique_ptr<BackendContext> buildModel(
431 bool deserializeEngineAndBackend(
const std::string enginePath,
int dla,
432 std::shared_ptr<TrtEngine>& engine,
433 std::unique_ptr<BackendContext>& backend);
443 static const int INPUT_LAYER_INDEX = 0;
447 uint32_t m_UniqueID = 0;
448 uint32_t m_GpuID = 0;
452 std::unique_ptr<BackendContext> m_BackendContext;
453 std::shared_ptr<DlLibHandle> m_CustomLibHandle;
455 std::unique_ptr<InferPreprocessor> m_Preprocessor;
456 std::unique_ptr<InferPostprocessor> m_Postprocessor;
458 uint32_t m_MaxBatchSize = 0;
463 std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayerInfo;
464 std::vector<NvDsInferBatchDimsLayerInfo> m_OutputLayerInfo;
465 NvDsInferBatchDimsLayerInfo m_InputImageLayerInfo;
467 std::vector<void *> m_BindingBuffers;
468 std::vector<std::unique_ptr<CudaDeviceBuffer>> m_InputDeviceBuffers;
471 std::vector<std::shared_ptr<NvDsInferBatch>> m_Batches;
472 std::mutex m_BatchesMutex;
480 std::unique_ptr<CudaStream> m_InferStream;
481 std::unique_ptr<CudaStream> m_PostprocessStream;
484 std::shared_ptr<CudaEvent> m_InputConsumedEvent;
487 std::shared_ptr<CudaEvent> m_InferCompleteEvent;
491 bool m_Initialized =
false;
492 uint32_t m_AutoIncMem = 1;
493 double m_MaxGPUMem = 99;
494 bool m_DumpIpTensor =
false;
495 std::string m_DumpIpTensorFilePath =
" ";
496 bool m_OverwriteIpTensor =
false;
497 std::string m_OverwriteIpTensorFilePath =
" ";
498 std::ifstream m_OverwriteIpTensorFile;
503 #define printMsg(level, tag_str, fmt, ...) \
505 char* baseName = strrchr((char*)__FILE__, '/'); \
506 baseName = (baseName) ? (baseName + 1) : (char*)__FILE__; \
507 char logMsgBuffer[5 * _MAX_STR_LENGTH + 1]; \
508 snprintf(logMsgBuffer, 5 * _MAX_STR_LENGTH, \
509 tag_str " NvDsInferContextImpl::%s() <%s:%d> [UID = %d]: " fmt, \
510 __func__, baseName, __LINE__, m_UniqueID, ##__VA_ARGS__); \
511 if (m_LoggingFunc) { \
512 m_LoggingFunc(level, logMsgBuffer); \
514 fprintf(stderr, "%s\n", logMsgBuffer); \
518 #define printError(fmt, ...) \
520 printMsg (NVDSINFER_LOG_ERROR, "Error in", fmt, ##__VA_ARGS__); \
523 #define printWarning(fmt, ...) \
525 printMsg (NVDSINFER_LOG_WARNING, "Warning from", fmt, ##__VA_ARGS__); \
528 #define printInfo(fmt, ...) \
530 printMsg (NVDSINFER_LOG_INFO, "Info from", fmt, ##__VA_ARGS__); \
533 #define printDebug(fmt, ...) \
535 printMsg (NVDSINFER_LOG_DEBUG, "DEBUG", fmt, ##__VA_ARGS__); \