|
|
NVIDIA DeepStream SDK API Reference
|
9.2 Release
|
Go to the documentation of this file.
18 #ifndef SEGMENTERLIB_SEGMENTER_HPP_
19 #define SEGMENTERLIB_SEGMENTER_HPP_
26 #include <opencv2/core.hpp>
27 #include <opencv2/imgcodecs.hpp>
28 #include <opencv2/imgproc.hpp>
31 #include <cuda_runtime_api.h>
32 #include <cuda_runtime.h>
35 #include <NvOnnxParser.h>
36 #include "modules/Segmenter/ISegmenter.hpp"
37 #include "modules/FrameTransform/trtModule.hpp"
38 #include "modules/ReID/ReIDFrameTransformTexture.hpp"
39 #include "modules/ReID/ReIDCropScaler.hpp"
49 typedef std::shared_ptr<std::vector<float>>
TensorPtr;
130 void read(
const YAML::Node &fn)
override;
171 std::string m_trackerConfigPathForPathResolve;
208 float* maskMemFeatures_d,
float* maskMemPosEnc_d,
float* temporalCode_d,
float* objPtr_d,
229 float* m_targetsMaskMemFeatures_d;
230 float* m_targetsMaskMemPosEnc_d;
231 float* m_targetsTemporalCode_d;
232 float* m_targetsObjPtr_d;
246 void updateTensorBank(
247 float* srcBatch,
float* dst,
int& numFilled,
const int maxNumPerTarget,
const int numElementsPerTensor,
248 const TrackerIndex targetInd,
const int indInBatch,
const bool keepFirst=
false
260 Segmenter(
const std::string &configFilePath);
267 Segmenter(
const std::string &configFilePath,
const std::string &modelName);
280 const std::shared_ptr<NvTrackedObjectManager> &targetManager
293 void localize(
const std::map<NvMOTStreamId, NvMOTFrame *>& inputMeta)
override;
299 void update(
const std::map<NvMOTStreamId, NvMOTFrame *>& inputMeta)
override;
379 std::map<NvMOTStreamId, std::vector<std::pair<uint, NvMotDataTypes::Rect2f>>>
m_inputRoisInBatch;
393 void doInference(
const std::map<NvMOTStreamId, NvMOTFrame *>& inputMeta);
434 const std::map<uint32_t, std::shared_ptr<NvTrackedObject>> &targetList,
bool usePrompt,
447 const std::map<uint32_t, std::shared_ptr<NvTrackedObject>> &targetList,
bool usePrompt,
463 bool usePrompt, std::vector<std::shared_ptr<NvTrackedObject>>& targetList,
473 void memAttenInfer(
const std::shared_ptr<NvTrackedObject> target,
const int indInBatch,
const int inputStreamInd);
506 void setMaskDecoderPoints(
const bool usePrompt, std::vector<std::shared_ptr<NvTrackedObject>> targetList);
515 const int indInBatch,
const std::shared_ptr<NvTrackedObject> target,
const NvMOTFrameNum frameNum
525 const int indInBatch,
const std::shared_ptr<NvTrackedObject> target,
const NvMOTFrameNum frameNum
588 const std::shared_ptr<NvTrackedObject> target,
const float confidence,
593 cv::Mat m_imageSumMask;
594 cv::Mat m_imageIDMask;
595 std::map<NvMOTStreamId, std::map<uint, cv::Mat>> m_vizSrcImages;
602 void updatePerTargetMaskInFrame(
const std::shared_ptr<NvTrackedObject> target,
bool initMask);
610 void savePerFrameMask(
612 const std::map<uint32_t, std::shared_ptr<NvTrackedObject>> &targetList
617 #endif // SEGMENTERLIB_SEGMENTER_HPP_
TensorPtr pred_masks_high_res
Predicted high-resolution masks.
Records targets awaiting memory-encoder processing.
std::vector< void * > m_bindingsMaskDecoder
Performs target segmentation and maintains temporal model state.
Segmenter(const std::string &configFilePath)
Construct a Segmenter instance from configuration.
float * m_memory_pos_embed_d
int highResFeaturesEncoder0Size
Image encoder high resolution (0) output feature size (single image).
int highResMaskSize
Mask decoder high resolution output mask size in a batch.
float m_minMemoryUpdateConfidence
Mask confidence to update memory bank.
std::string m_vizDir
Dir to save visualization dumps.
std::vector< float * > getCudaMemBank(const TrackerIndex targetInd)
Get CUDA memory-bank pointers for a target.
std::vector< int > m_inferDims
Network input dimension.
NvMOTFrameNum currentFrameNum
Target age at the latest memory update.
SegmenterConfigParams m_params
Config params for Segmenter module.
@ FOREGROUND
Positive point prompt.
std::vector< void * > m_bindingsMemAtten
int memChannel
Number of channels in memory feature.
void fillMemoryConditionedFeatures(TrackerIndex targetInd, int indInBatch)
Construct memory attention input.
std::map< TrackerIndex, CudaInferenceStatus > m_cudaInferenceStatus
Cuda inference status.
int m_maskMemoryUpdateInterval
Frame interval to update memory bank.
std::vector< MemoryEncoderStatus > statusUncond
Memory encoder status of the unconditional frames.
int m_hiddenDim
Hidden feature dimension.
std::map< TrackerIndex, std::vector< float > > m_targetScores
Target IOU and occlusion scores.
int m_width
Input image patch width.
void extractImagePatchesBatch(const std::map< NvMOTStreamId, NvMOTFrame * > &inputMeta)
Rescale low-level tracker frames into encoder input.
TensorPtr maskmem_pos_enc
Mask memory position encoding.
int highResFeaturesEncoder1Size
Image encoder high resolution (1) output feature size (single image).
void setMaskDecoderSinglePointLabelAsNegative(int pointNumPerTarget, int indInBatch)
Mark a single decoder prompt point as negative.
int maskMemFeatSize
Memory encoder output memory feature size in a batch.
void setMaskDecoderPoints(const bool usePrompt, std::vector< std::shared_ptr< NvTrackedObject >> targetList)
Populate mask-decoder prompt coordinates for a target batch.
TrtModule m_memoryEncoder
Memory encoder module.
float * m_points_d
Input a list of points (prompt) to decoder.
float * m_batchMaskDecSize_d
Mask output for all targets in all streams.
std::shared_ptr< ReIDCropScaler > m_subImageCropScaler
Image crop scaler.
void batchInfer(const std::map< uint32_t, std::shared_ptr< NvTrackedObject >> &targetList, bool usePrompt, int outputLabelIndMax, const NvMOTFrameNum frameNum, const uint inputRoiInd)
Batch inference.
float * m_directly_add_no_mem_embed_d
Non-memory embeddings added to image-encoder output.
NvMotDataTypes::Size m_srcImageSize
Input to low level tracker.
std::vector< TensorPtr > objPtrCond
Object pointers of the conditional frames.
SegmenterCudaMemBank m_cudaMemBank
Cuda memory bank.
TensorPtr pred_masks
Predicted masks.
float m_netScaleFactor
Input normalization factor.
TensorPtr temporal_code
Temporal code.
float * m_obj_ptr_d
Output obj ptrs.
std::vector< MemoryEncoderStatus > statusCond
Memory encoder status of the conditional frames.
void initializeTrtBindings()
Define IO bindings for each TRT engine.
TrtModule m_imageEncoder
Encoder to extract features from each image.
std::vector< void * > m_bindingsMemEncoder
TensorRT bindings.
Manages CUDA-resident temporal features for SAM2 inference.
Stores configuration for segmentation inference.
std::vector< float > m_offsets
List of mean values of color components to be subtracted from input.
void imgEncoderInfer()
Run the image encoder for the current input batch.
SegmenterRunTimeParams m_paramsRt
Runtime parameters.
@ TOP_LEFT
Top-left corner of a bounding-box prompt.
int m_outputLabelNum
Number of decoder output labels for each target.
int m_debugVerboseLevel
Debug message verbose mode level.
int numStatusUncond
Number of unconditional memory entries stored.
float m_minPromptUpdateConfidence
Minimum confidence of detection bbox to use as the prompt for mask decoder.
int m_numConditionalMem
Number of conditional frames to use for memory bank update.
NvMOTFrameNum currentFrameNum
Target age at the latest memory update.
void memAttenInfer(const std::shared_ptr< NvTrackedObject > target, const int indInBatch, const int inputStreamInd)
Run memory attention for one tracked target.
float * m_pointLabels_d
Input a list of point types (single point, bounding box corners) to decoder.
float * m_visionPosEmbed_d
void read(const YAML::Node &fn) override
Load configuration values from a YAML node.
SEGMENTER_TYPES m_segmenterType
Type of segmenter.
std::vector< float > m_outputOccLogit_h
Output occlusion logit on host.
int m_promptType
Prompt type.
std::vector< void * > m_bindingsImgEncoder
cublasHandle_t m_cublasHandle
Cublas handle.
void doInference(const std::map< NvMOTStreamId, NvMOTFrame * > &inputMeta)
Run segmentation inference for targets in the input batch.
~SegmenterCudaMemBank()
Release resources owned by the SegmenterCudaMemBank instance.
TensorPtr maskmem_features
Mask memory features.
std::map< TrackerIndex, std::map< NvMOTFrameNum, SegmentMemoryInfo > > m_nonCondFrameOutputs
Outputs of the non-conditional frames.
Eigen::Vector2f m_scaleInput2Original
Scale from decoder output to original frame size decided by streammux.
TensorPtr maskmem_features
Mask memory features.
int objPtrSize
Mask decoder output obj ptr size in a batch.
void reset(const TrackerIndex targetInd)
Clear retained memory for one target.
TRT module to perform various deep learning inference.
bool isGoodBboxProg(const std::shared_ptr< NvTrackedObject > target, const float confidence, const NvMotDataTypes::Rect2f bboxProg, const float iouThresh=0.5)
Check whether a propagated bounding box is reliable.
float * m_maskmem_features_d
float * m_outputIou_d
Output IOU score.
bool isInitialized() override
Report whether the module is initialized.
Holds per-frame segmentation features retained in memory.
float * m_temporal_code_d
Output mask memory features and pos encoding.
void maskDecoderInfer(bool usePrompt, std::vector< std::shared_ptr< NvTrackedObject >> &targetList, int &outputLabelIndMax, const uint inputStreamInd, const NvMOTFrameNum frameNum)
Run mask-decoder inference for the current target batch.
bool m_isForLocalization
Whether the inference is for localization.
float * m_maskForMem_d
Output mask high resolution.
Provides common loading helpers for tracker configuration parameters.
Eigen::Vector2f m_scaleMask2Src
Scale from mask output to the source image.
int featuresEncoderSize
Image encoder basic resolution output feature size (single image).
TensorPtr obj_ptr
Object pointer.
const SEGMENTER_TYPES getSegmenterType() override
Get the configured segmenter type.
float * m_inputMaskLabel_d
Input mask label, currently unused and set as 0.
float m_minIouPrevFrame4ProgBbox
IOU threshold to use detection bboxes as the prompt for mask decoder.
float * m_outputOccLogit_d
Output occlusion logit; <2 means target is occluded.
std::vector< float > m_outputIou_h
Output IOU score on host.
@ BACKGROUND
Negative point prompt.
float * m_pix_feat_with_mem_d
Attention output features with memory for one target.
float * m_inputBuf_h
Input patches to encoder.
@ BOTTOM_RIGHT
Bottom-right corner of a bounding-box prompt.
bool m_updateMemBankPrompt
Update memory bank using bbox prompt.
TensorPtr temporal_code
Temporal code.
void initialize(const SourceFrameInfo &frameInfo, const uint maxTargetsPerStream, const std::shared_ptr< NvTrackedObjectManager > &targetManager) override
Initialize variables and allocate memories.
void memAttenInferBatchParser(std::vector< std::shared_ptr< NvTrackedObject >> &targetList, const int inputStreamInd)
Assemble and run batched memory-attention input.
Holds the capabilities and requirements reported by the tracker library.
int m_numMaskMem
Number of memory bank masks.
SegmenterConfigParams()
Construct a SegmenterConfigParams instance.
int m_maskPromptUpdateInterval
Frame interval to use detection bboxes as the prompt for mask decoder. (Memory bank is also used exce...
int m_channel
Input image patch channel.
void removeInferStatus()
Remove terminated memory bank.
int pixFeatWithMemSize
Memory attention output feature size in a batch.
bool m_isInit
Segmenter status if initialized.
bool m_useMemBank
Use memory bank.
bool m_saveIDMask
Enables saving encoded target-ID masks.
int m_maxMemVecSize_legacy
Maximum memory vector size of the legacy work spaces.
int m_encoderFeatureDim
Encoder feature dimension.
void queryParams(NvMOTQuery ¶msQuery) override
Populate the capabilities reported to the tracker.
void update(const std::map< NvMOTStreamId, NvMOTFrame * > &inputMeta) override
Update Segmenter after data association.
bool m_outputMask
Output mask to tracker plugin.
std::shared_ptr< std::vector< float > > TensorPtr
int m_height
Input image patch height.
float * m_targetMaskDecSize_h
Output mask basic resolution.
int * m_outputSize_d
Output size.
float * m_featuresEncoder_d
float * m_inputMask_d
Input mask prompt, currently unused and set as 0.
TrtModule m_memoryAttention
Memory attention module.
bool m_useVPICropScaler
Use VPI crop scaler instead of in built crop scaler.
float * m_memory_obj_ptr_d
Defines the interface for target segmentation modules.
void setMaskDecoderPointLabels(int promptType)
Populate mask-decoder labels for points or bounding boxes.
void copyFromCuda(float *src, TensorPtr &dst, int numElements, cudaStream_t &cuStream)
Copy a CUDA buffer into a TensorRT tensor.
std::shared_ptr< NvTrackedObjectManager > m_targetManager
Target manager providing target status to update feature gallery.
float * m_memory_maskmem_features_d
void outputMaskObj(const std::shared_ptr< NvTrackedObject > target, NvMOTMask &mask) override
Output segmentation mask of a target.
void updateTrtBindingsPerStream(const uint inputRoiInd)
Update TRT bindings per stream.
int maxPointNum
Max number of points for each object.
bool isInferStatusEmpty(TrackerIndex targetInd)
Check whether a target has no retained inference state.
int numObjPtrUncond
Number of object pointers stored.
bool skipInfer(std::shared_ptr< NvTrackedObject > target, const NvMOTFrameNum frameNum)
Determine whether segmentation can be skipped for a target.
float * m_targetMaskOriginalSrcSize_d
Mask for each target resized to original streammux size.
float * m_cond_frame_id_diff_d
Memory attention inputs: object pointers, mask memory features, memory, positional embeddings,...
Stores dimensions and batch limits used at segmenter runtime.
void updateInferStatus(const int indInBatch, const std::shared_ptr< NvTrackedObject > target, const NvMOTFrameNum frameNum)
Record a target in the pending inference batch.
void memEncoderInfer(const int batchSizeActual)
Run the memory encoder for the current target batch.
float * m_highResFeaturesEncoder1_d
Image encoder output feature.
float * m_highResFeaturesEncoder0_d
float * m_highResImgPosEmbed1_d
Image encoder output position embedding.
void removeStream(const NvMOTStreamId videoStreamId) override
Remove a stream's Segmenter features.
std::map< TrackerIndex, InferenceStatus > m_inferStatus
Inference status.
Represents a two-dimensional integer size.
TensorPtr maskmem_pos_enc
Mask memory position encoding.
Information about the input source frames for the input frame batch.
Tracks batched CUDA inference work across streams.
void postProcessing(const int indInBatch, const std::shared_ptr< NvTrackedObject > target, const NvMOTFrameNum frameNum)
Convert one inference result into retained target state.
float m_minIouDiff4MemoryUpdate
IOU threshold to update memory bank.
std::vector< TensorPtr > objPtrUncond
Object pointers of the unconditional frames.
float * m_work_2_d
Scratch buffers for memory attention.
bool m_saveFrameViz
Save overlay masks for each target in each frame.
std::map< TrackerIndex, std::map< NvMOTFrameNum, SegmentMemoryInfo > > m_condFrameOutputs
Outputs of the conditional frames.
int m_numObjPtr
Number of object pointers.
float * m_maskmem_pos_enc_d
void singleInfer(const std::map< uint32_t, std::shared_ptr< NvTrackedObject >> &targetList, bool usePrompt, int outputLabelIndMax, const NvMOTFrameNum frameNum, const uint inputRoiInd)
Run segmentation for one target.
void localize(const std::map< NvMOTStreamId, NvMOTFrame * > &inputMeta) override
Localize and segment target in a new frame.
NvMotDataTypes::Size m_originalSrcImageSize
Input to tracker plugin, usually from streammux.
SegmenterCudaMemBank()
Construct a SegmenterCudaMemBank instance from configuration.
Holds the segmentaiton mask of an object.
int m_inputOrder
Input dimension order: 0: NCHW, 1: NHWC.
cudaStream_t m_cuStream
Cuda stream to run all GPU tasks.
int temporalCodeSize
Temporal code size.
void initialize(const SourceFrameInfo &frameInfo, const uint maxTargetsPerStream, const SegmenterRunTimeParams ¶msRt, const SegmenterConfigParams ¶ms, cudaStream_t &cuStream)
Initialize memory bank.
void fillCudaMemoryConditionedFeatures(TrackerIndex targetInd, int indInBatch)
Fill memory conditioned features for cuda memory bank.
int basicMaskSize
Mask decoder basic resolution output mask size in a batch.
virtual ~Segmenter()
Release resources owned by the Segmenter instance.
float * m_batchPixFeatWithMem_d
Attention output features with memory for all targets in all streams.
std::map< NvMOTStreamId, std::vector< std::pair< uint, NvMotDataTypes::Rect2f > > > m_inputRoisInBatch
Input rois to crop.
float m_promptBboxPaddingScale
Padding of the bounding-box prompt as a fraction of the box height.
std::shared_ptr< ReIDFrameTransformTexture > m_subImageCropper2
Image cropper, may share with visual tracker in the future.
TrtModule m_maskDecoder
Decoder taking in prompts (points, bboxes, etc.) to create segmentation mask.
float * m_targetMaskDecSize_d
int maxConcatMemFeatSize
Memory attention input feature max size in a batch.
Tracks pending segmentation inference for one stream.
NvBufSurfaceColorFormat m_colorFormat
Color format of input to segmentation network.
bool m_useCudaMemBank
Use cuda memory bank.
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
void update(float *maskMemFeatures_d, float *maskMemPosEnc_d, float *temporalCode_d, float *objPtr_d, std::shared_ptr< NvTrackedObject > target, int indInBatch, CudaInferenceStatus &inferStatus)
Update memory bank.