NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/nvdsinferserver/infer_postprocess.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
27 #ifndef __NVDSINFERSERVER_POST_PROCESS_H__
28 #define __NVDSINFERSERVER_POST_PROCESS_H__
29 
30 #include <stdarg.h>
31 #include <condition_variable>
32 #include <functional>
33 #include <list>
34 #include <memory>
35 #include <mutex>
36 #include <queue>
37 
38 #include <cuda_runtime_api.h>
39 
40 #include "infer_batch_buffer.h"
41 #include "infer_common.h"
42 #include "infer_cuda_utils.h"
43 #include "infer_datatypes.h"
44 #include "infer_ibackend.h"
45 #include "infer_iprocess.h"
46 #include "infer_post_datatypes.h"
47 #include "nvdsinfer_custom_impl.h"
48 #include "nvdsinferserver_config.pb.h"
49 
50 namespace ic = nvdsinferserver::config;
51 
52 struct NvDsInferDBScan;
53 
54 namespace nvdsinferserver {
55 
60 public:
61  using TensorAllocator = std::function<SharedSysMem(const std::string& name, size_t bytes)>;
62  using EventAllocator = std::function<SharedCuEvent()>;
63 
64 protected:
66  : BasePostprocessor(type, id){}
67 
68 public:
69  virtual ~Postprocessor() = default;
70  void setDllHandle(const SharedDllHandle& dlHandle) {
71  m_CustomLibHandle = dlHandle;
72  }
73  void setLabelPath(const std::string& path) { m_LabelPath = path; }
75  m_NetworkInfo = info;
76  }
77  void setOutputLayerCount(uint32_t num) { m_OutputLayerCount = num; }
78  void setInputCopy(bool enable) { m_CopyInputToHostBuffers = enable; }
79  const std::vector<std::vector<std::string>>& getLabels() const {
80  return m_Labels;
81  }
83  {
84  m_CpuAllocator = cpuAlloc;
85  m_EventAllocator = event;
86  }
87  bool needInputCopy() const { return m_CopyInputToHostBuffers; }
88 
90  const std::vector<int>& devIds) override;
91 
92 protected:
94  SharedBatchArray& outbuf, SharedCuStream& mainStream) override;
95 
97  SharedBatchArray& outbuf, SharedCuStream& mainStream) override;
98 
101 
102 private:
103  virtual NvDsInferStatus batchParse(
104  std::vector<NvDsInferLayerInfo>& outputLayers,
105  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
106  SharedBatchArray& results) = 0;
107 
108 protected:
109  NvDsInferStatus parseLabelsFile(const std::string& path);
110 
111 private:
112  DISABLE_CLASS_COPY(Postprocessor);
113 
114 protected:
120  uint32_t m_OutputLayerCount = 0;
121  std::string m_LabelPath;
122 
124  std::vector<std::vector<std::string>> m_Labels;
125 
128 };
129 
134 public:
135  DetectPostprocessor(int uid, const ic::DetectionParams &params);
136  ~DetectPostprocessor() override = default;
137 
138  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
139 
140 private:
141  NvDsInferStatus batchParse(
142  std::vector<NvDsInferLayerInfo>& outputLayers,
143  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
144  SharedBatchArray& results) override;
145 
146  bool parseBoundingBox(
147  std::vector<NvDsInferLayerInfo> const& outputLayersInfo,
148  NvDsInferNetworkInfo const& networkInfo,
149  NvDsInferParseDetectionParams const& detectionParams,
150  std::vector<NvDsInferObjectDetectionInfo>& objectList);
151 
152  void clusterAndFillDetectionOutputCV(
153  const std::vector<NvDsInferObjectDetectionInfo>& objectList,
154  std::vector<NvDsInferObject> &outputs);
155  void clusterAndFillDetectionOutputDBSCAN(
156  const std::vector<NvDsInferObjectDetectionInfo>& objectList,
157  std::vector<NvDsInferObject>& outputs);
158  void copyWithoutCluster(
159  const std::vector<NvDsInferObjectDetectionInfo>& objectList,
160  std::vector<NvDsInferObject>& outputs);
161  NvDsInferStatus fillDetectionOutput(
162  const std::vector<NvDsInferLayerInfo>& outputLayers,
163  std::vector<NvDsInferObject> &output);
164  void filterDetectionOutput(
165  NvDsInferParseDetectionParams const &detectionParams,
166  std::vector<NvDsInferObjectDetectionInfo> &objectList);
167  void clusterAndFillDetectionOutputNMS(
168  const std::vector<NvDsInferObjectDetectionInfo>& objectList,
169  uint32_t topk, std::vector<NvDsInferObject>& outputs);
170  std::vector<int> nonMaximumSuppression(
171  const std::vector<std::pair<float, int>>& scoreIndex,
172  const std::vector<NvDsInferParseObjectInfo>& bbox,
173  const float nmsThreshold);
174 
175 private:
176  struct NvDsInferDetectionParams
177  {
179  float threshold;
182  float eps;
185  int minBoxes;
188  int groupThreshold;
192  float minScore;
194  float nmsIOUThreshold;
195  };
196 
197 private:
198  std::shared_ptr<NvDsInferDBScan> m_DBScanHandle;
200  uint32_t m_NumDetectedClasses = 0;
201 
203  std::vector<NvDsInferDetectionParams> m_PerClassDetectionParams;
204  NvDsInferParseDetectionParams m_DetectionParams = {0, {}, {}};
205 
206  NvDsInferParseCustomFunc m_CustomBBoxParseFunc = nullptr;
207  ic::DetectionParams m_DetectConfig;
208 };
209 
214 public:
215  ClassifyPostprocessor(int uid, const ic::ClassificationParams &params);
216  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
217 
218 private:
219  NvDsInferStatus batchParse(
220  std::vector<NvDsInferLayerInfo>& outputLayers,
221  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
222  SharedBatchArray& results) override;
223 
224  NvDsInferStatus fillClassificationOutput(
225  const std::vector<NvDsInferLayerInfo>& outputLayers,
226  InferClassificationOutput& output);
227 
228  bool parseAttributesFromSoftmaxLayers(
229  std::vector<NvDsInferLayerInfo> const& outputLayersInfo,
230  NvDsInferNetworkInfo const& networkInfo, float classifierThreshold,
231  std::vector<NvDsInferAttribute>& attrList, std::string& attrString);
232 
233 private:
234  float m_ClassifierThreshold = 0;
235  NvDsInferClassiferParseCustomFunc m_CustomClassifierParseFunc = nullptr;
236 
237  ic::ClassificationParams m_Config;
238 };
239 
244 public:
245  SegmentPostprocessor(int uid, const ic::SegmentationParams &params);
246  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
247 
248 private:
249  NvDsInferStatus batchParse(
250  std::vector<NvDsInferLayerInfo>& outputLayers,
251  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
252  SharedBatchArray& results) override;
253 
254  NvDsInferStatus fillSegmentationOutput(
255  const std::vector<NvDsInferLayerInfo>& outputLayers,
257 
258  bool parseSemanticSegmentationOutput(
259  std::vector<NvDsInferLayerInfo> const& outputLayersInfo,
260  NvDsInferNetworkInfo const& networkInfo,
261  float segmentationThreshold, unsigned int numClasses,
262  int* classificationMap, float*& classProbabilityMap);
263 
264 private:
265  float m_SegmentationThreshold = 0.0f;
267  m_CustomSemSegmentationParseFunc = nullptr;
268  unsigned int m_NumSegmentationClasses = 0;
269  ic::SegmentationParams m_Config;
270 };
271 
276 public:
277  OtherPostprocessor(int uid, const ic::OtherNetworkParams &params);
278 
279 private:
280  NvDsInferStatus batchParse(
281  std::vector<NvDsInferLayerInfo>& outputLayers,
282  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
283  SharedBatchArray& results) override
284  {
285  return NVDSINFER_SUCCESS;
286  }
287 
288 private:
289  ic::OtherNetworkParams m_Config;
290 };
291 
296 public:
297  TrtIsClassifier(int uid, const ic::TritonClassifyParams& params);
298 
299 private:
300  NvDsInferStatus postHostImpl(
301  SharedBatchArray& inBuf, SharedBatchArray& outbuf,
302  SharedCuStream& mainStream) override;
303  NvDsInferStatus batchParse(
304  std::vector<NvDsInferLayerInfo>& outputLayers,
305  const std::vector<SharedBatchBuf> outputBufs, uint32_t batchSize,
306  SharedBatchArray& results) override
307  {
308  /* should never reach */
309  InferError(
310  "TrtIsClassifer(uid:%d) should not reach here, check error",
311  uniqueId());
313  }
314 
315 private:
316  ic::TritonClassifyParams m_Config;
317 };
318 
319 } // namespace nvdsinferserver
320 
321 #endif
NvDsInferClassiferParseCustomFunc
bool(* NvDsInferClassiferParseCustomFunc)(std::vector< NvDsInferLayerInfo > const &outputLayersInfo, NvDsInferNetworkInfo const &networkInfo, float classifierThreshold, std::vector< NvDsInferAttribute > &attrList, std::string &descString)
Type definition for the custom classifier output parsing function.
Definition: sources/includes/nvdsinfer_custom_impl.h:275
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:69
nvdsinferserver::Postprocessor::setLabelPath
void setLabelPath(const std::string &path)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:73
NvDsInferSemSegmentationParseCustomFunc
bool(* NvDsInferSemSegmentationParseCustomFunc)(std::vector< NvDsInferLayerInfo > const &outputLayersInfo, NvDsInferNetworkInfo const &networkInfo, float segmentationThreshold, unsigned int numClasses, int *classificationMap, float *&classProbabilityMap)
Type definition for the custom semantic segmentation output parsing function.
Definition: sources/includes/nvdsinfer_custom_impl.h:312
nvdsinferserver::SegmentPostprocessor::SegmentPostprocessor
SegmentPostprocessor(int uid, const ic::SegmentationParams &params)
nvdsinferserver::Postprocessor::allocateResource
virtual NvDsInferStatus allocateResource(const std::vector< int > &devIds) override
nvdsinferserver::Postprocessor::Postprocessor
Postprocessor(InferPostprocessType type, int id)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:65
nvdsinferserver::DetectPostprocessor::DetectPostprocessor
DetectPostprocessor(int uid, const ic::DetectionParams &params)
nvdsinferserver::SharedDllHandle
std::shared_ptr< DlLibHandle > SharedDllHandle
Definition: sources/libs/nvdsinferserver/infer_common.h:116
nvdsinferserver::InferPostprocessType
InferPostprocessType
Inference post processing types.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:108
nvdsinferserver::BasePostprocessor
Base post-processor class.
Definition: sources/libs/nvdsinferserver/infer_iprocess.h:167
nvdsinferserver::OtherPostprocessor
Post processor class for tensor output for custom post processing.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:275
nvdsinferserver::Postprocessor::needInputCopy
bool needInputCopy() const
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:87
nvdsinferserver::TrtIsClassifier::TrtIsClassifier
TrtIsClassifier(int uid, const ic::TritonClassifyParams &params)
NvDsInferSegmentationOutput
Holds information parsed from segmentation network output for one frame.
Definition: sources/includes/nvdsinfer_context.h:599
nvdsinferserver::SharedBatchArray
std::shared_ptr< BaseBatchArray > SharedBatchArray
Definition: sources/libs/nvdsinferserver/infer_common.h:80
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: sources/includes/nvdsinfer.h:233
nvdsinferserver::Postprocessor::setDllHandle
void setDllHandle(const SharedDllHandle &dlHandle)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:70
InferError
#define InferError(fmt,...)
Definition: sources/includes/nvdsinferserver/infer_defines.h:51
nvdsinferserver::Postprocessor::m_CpuAllocator
TensorAllocator m_CpuAllocator
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:126
nvdsinferserver::Postprocessor::setInputCopy
void setInputCopy(bool enable)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:78
nvdsinferserver::SharedCuEvent
std::shared_ptr< CudaEvent > SharedCuEvent
Definition: sources/libs/nvdsinferserver/infer_common.h:91
nvdsinferserver::Postprocessor::postHostImpl
NvDsInferStatus postHostImpl(SharedBatchArray &inBuf, SharedBatchArray &outbuf, SharedCuStream &mainStream) override
nvdsinferserver::Postprocessor::m_OutputLayerCount
uint32_t m_OutputLayerCount
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:120
NvDsInferParseDetectionParams
Holds the detection parameters required for parsing objects.
Definition: sources/includes/nvdsinfer_custom_impl.h:184
nvdsinferserver::Postprocessor
A generic post processor class.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:59
nvdsinferserver::DetectPostprocessor
Post processor class for detection output.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:133
nvdsinferserver::Postprocessor::m_NetworkInfo
NvDsInferNetworkInfo m_NetworkInfo
Network input information.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:119
NVDSINFER_UNKNOWN_ERROR
@ NVDSINFER_UNKNOWN_ERROR
Unknown error was encountered.
Definition: sources/includes/nvdsinfer.h:256
nvdsinferserver::Postprocessor::setOutputLayerCount
void setOutputLayerCount(uint32_t num)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:77
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::Postprocessor::~Postprocessor
virtual ~Postprocessor()=default
nvdsinferserver::ClassifyPostprocessor
Post processor class for classification output.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:213
nvdsinferserver::Postprocessor::getLabels
const std::vector< std::vector< std::string > > & getLabels() const
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:79
nvdsinferserver::DetectPostprocessor::allocateResource
NvDsInferStatus allocateResource(const std::vector< int > &devIds) override
infer_batch_buffer.h
Header file of batch buffer related class declarations.
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: sources/libs/nvdsinferserver/infer_common.h:89
nvdsinferserver::Postprocessor::EventAllocator
std::function< SharedCuEvent()> EventAllocator
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:62
nvdsinferserver::SegmentPostprocessor
Post processor class for segmentation output.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:243
NvDsInferNetworkInfo
Holds information about the model network.
Definition: sources/includes/nvdsinfer.h:119
nvdsinferserver::SegmentPostprocessor::allocateResource
NvDsInferStatus allocateResource(const std::vector< int > &devIds) override
nvdsinferserver::Postprocessor::m_EventAllocator
EventAllocator m_EventAllocator
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:127
nvdsinferserver::Postprocessor::parseLabelsFile
NvDsInferStatus parseLabelsFile(const std::string &path)
nvdsinferserver::SharedSysMem
std::shared_ptr< SysMem > SharedSysMem
Definition: sources/libs/nvdsinferserver/infer_common.h:93
nvdsinferserver::Postprocessor::m_CopyInputToHostBuffers
bool m_CopyInputToHostBuffers
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:117
nvdsinferserver::OtherPostprocessor::OtherPostprocessor
OtherPostprocessor(int uid, const ic::OtherNetworkParams &params)
infer_ibackend.h
Inference processing backend interface header file.
nvdsinferserver::BasePostprocessor::uniqueId
int uniqueId() const
Definition: sources/libs/nvdsinferserver/infer_iprocess.h:184
nvdsinferserver::TrtIsClassifier
Post processor class for Triton Classification option.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:295
infer_iprocess.h
Preprocessing and postprocessing interface header file.
nvdsinferserver::ClassifyPostprocessor::ClassifyPostprocessor
ClassifyPostprocessor(int uid, const ic::ClassificationParams &params)
nvdsinferserver::Postprocessor::m_LabelPath
std::string m_LabelPath
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:121
nvdsinferserver::Postprocessor::TensorAllocator
std::function< SharedSysMem(const std::string &name, size_t bytes)> TensorAllocator
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:61
nvdsinferserver::Postprocessor::postCudaImpl
NvDsInferStatus postCudaImpl(SharedBatchArray &inBuf, SharedBatchArray &outbuf, SharedCuStream &mainStream) override
nvdsinferserver::Postprocessor::m_CustomLibHandle
SharedDllHandle m_CustomLibHandle
Custom library implementation.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:116
nvdsinferserver::Postprocessor::setNetworkInfo
void setNetworkInfo(const NvDsInferNetworkInfo &info)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:74
nvdsinferserver::Postprocessor::requestHostOutBufs
SharedBatchArray requestHostOutBufs(const SharedBatchArray &inBuf) override
nvdsinferserver::Postprocessor::m_Labels
std::vector< std::vector< std::string > > m_Labels
Holds the string labels for classes.
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:124
nvdsinferserver::Postprocessor::requestCudaOutBufs
SharedBatchArray requestCudaOutBufs(const SharedBatchArray &inBuf) override
nvdsinferserver::Postprocessor::setAllocator
void setAllocator(TensorAllocator cpuAlloc, EventAllocator event)
Definition: sources/libs/nvdsinferserver/infer_postprocess.h:82
NvDsInferParseCustomFunc
bool(* NvDsInferParseCustomFunc)(std::vector< NvDsInferLayerInfo > const &outputLayersInfo, NvDsInferNetworkInfo const &networkInfo, NvDsInferParseDetectionParams const &detectionParams, std::vector< NvDsInferObjectDetectionInfo > &objectList)
Type definition for the custom bounding box parsing function.
Definition: sources/includes/nvdsinfer_custom_impl.h:215
nvdsinferserver::DetectPostprocessor::~DetectPostprocessor
~DetectPostprocessor() override=default
infer_cuda_utils.h
Header file declaring utility classes for CUDA memory management, CIDA streams and events.
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231
nvdsinferserver::ClassifyPostprocessor::allocateResource
NvDsInferStatus allocateResource(const std::vector< int > &devIds) override