NVIDIA DeepStream SDK API Reference

6.4 Release
sequence_image_process.h
Go to the documentation of this file.
1 
23 #ifndef __NVDS_SEQUENCE_IMAGE_PREPROCESS_H__
24 #define __NVDS_SEQUENCE_IMAGE_PREPROCESS_H__
25 
26 #include "seq_process_common.h"
28 
29 // default values
30 constexpr static uint32_t kDefaultChannel = 3;
31 constexpr static uint32_t kDefaultStride = 1;
32 constexpr static uint32_t kDefaultSubSample = 0;
33 
34 // Buffer manager convert and normalize all the source ROI input images into
35 // network model input datatype and format. It enables temporal batching per
36 // each ROI into sequence, and gather multiple ROI sequence into spacial batches.
37 // It supports `subsample` and `stride` to improve performance for model inference.
39 public:
40  // constructor and destructor
42  NvDsPreProcessAcquirer* allocator, const NvDsPreProcessTensorParams& params, uint32_t depth,
43  uint32_t channel, cudaStream_t stream, uint32_t stride, uint32_t interval)
44  : _allocator(allocator), _tensorParams(params), _seqStride(stride), _subsample(interval)
45  {
46  const auto& shape = params.network_input_shape;
47  DSASSERT(shape.size() == 5 || shape.size() == 4); // NCDHW or NCHW
48  if (shape.size() == 5) {
49  DSASSERT(shape[2] == (int)depth); // NCDHW
50  }
51  _maxBatch = shape[0];
52  _seqSize = depth;
53  _channels = channel;
54  _perBatchSize =
55  std::accumulate(shape.begin() + 1, shape.end(), 1, [](int s, int i) { return s * i; });
56  DSASSERT(_perBatchSize > 0);
57  _cuStream = stream;
58  }
60 
61  // allocator could be set later
62  NvDsPreProcessAcquirer* allocator() const { return _allocator; }
64 
65  // creat new sequence buffers on each new ROI information
66  NvDsPreProcessStatus buildRoiBlocks(const std::vector<NvDsPreProcessUnit>& rois);
67  // locate the output CUDA memory pointer for each coming ROI
68  NvDsPreProcessStatus locateRoiDst(const NvDsPreProcessUnit& roi, void*& dstPatch);
69  // collect all ready results of ROI sequence buffers
71  // pop a ready result
72  bool popReady(ReadyResult& res);
73  // clear before quit
74  void clearAll();
75 
76 private:
77  NvDsPreProcessStatus addRoi(const SourceKey& key, const NvDsPreProcessUnit& unit);
78  uint32_t perBatchBytes() const { return _perBatchSize * sizeof(float); }
79 
80  // HW: height * width
81  uint32_t HWbytes() const { return perBatchBytes() / _seqSize / _channels; }
82 
83  uint32_t SHWbytes() const { return perBatchBytes() / _channels; }
84 
85  bool popOldestReady(ReadyResult& res)
86  {
87  if (_readyPendings.empty()) {
88  return false;
89  }
90  res = _readyPendings.front();
91  _readyPendings.pop();
92  return true;
93  }
94 
95 private:
96  NvDsPreProcessAcquirer* _allocator = nullptr;
97  NvDsPreProcessTensorParams _tensorParams;
98  cudaStream_t _cuStream = nullptr;
99  std::vector<std::unique_ptr<FullBatchBlock>> _accumulateBlocks;
100  SrcDestBufMap _src2dstMap;
101  std::queue<ReadyResult> _readyPendings;
102  uint32_t _maxBatch = 0;
103  uint32_t _seqSize = 0;
104  uint32_t _channels = kDefaultChannel;
105  uint32_t _perBatchSize = 0;
106  uint32_t _seqStride = kDefaultStride;
107  uint32_t _subsample = kDefaultSubSample;
108 };
109 
110 // Preprocess Context for all streams and ROIs. Custom lib symbols is cast into
111 // this context. It connects the buffer manager and input/output cuda processing
112 // kernels. Return the final processed buffer into Gst-nvdspreprocess plugin.
114 public:
115  SequenceImagePreprocess(const CustomInitParams& initParams) { _initParams = initParams; }
117 
118  // derives symbol of initLib
120  // derives symbol of deInitLib
122  // derives symbol of CustomSequenceTensorPreparation
125  NvDsPreProcessAcquirer* allocator);
126 
127  // internal cuda data conversion
129  const NvDsPreProcessUnit& unit, NvDsPreProcessFormat inFormat, void* outPtr);
130 
131 private:
132  NvDsPreProcessStatus setDevice();
133  NvDsPreProcessStatus parseUserConfig();
134 
135  CustomInitParams _initParams;
136  std::unique_ptr<BufferManager> _bufManager;
137  cudaStream_t _cuStream = nullptr;
138  int _gpuId = -1;
139  uint32_t _N = 0;
140  uint32_t _C = kDefaultChannel;
141  uint32_t _S = 0;
142  uint32_t _H = 0;
143  uint32_t _W = 0;
144  Float4Vec _scales = {{1.0, 1.0, 1.0, 1.0}};
145  Float4Vec _means = {{0.0, 0.0, 0.0, 0.0}};
146  uint32_t _subsample = 0;
147  uint32_t _stride = kDefaultStride;
148 };
149 
150 // entrypoint symbols for Gst-nvpreprocess plugin to load
151 // custom lib: libnvds_custom_sequence_preprocess.so
152 extern "C" {
153 // entrypoint for each batched ROI buffers processing
156  CustomTensorParams& tensorParam, NvDsPreProcessAcquirer* allocator);
157 
158 // entrypoint to create and init custom lib context
160 
161 // entrypoint to destroy custom lib context
163 }
164 
165 #endif // __NVDS_SEQUENCE_IMAGE_PREPROCESS_H__
BufferManager::locateRoiDst
NvDsPreProcessStatus locateRoiDst(const NvDsPreProcessUnit &roi, void *&dstPatch)
CustomSequenceTensorPreparation
PROCESS_EXPORT_API NvDsPreProcessStatus CustomSequenceTensorPreparation(CustomCtx *ctx, NvDsPreProcessBatch *batch, NvDsPreProcessCustomBuf *&buf, CustomTensorParams &tensorParam, NvDsPreProcessAcquirer *allocator)
kDefaultStride
constexpr static uint32_t kDefaultStride
Definition: sequence_image_process.h:31
CustomInitParams
Custom Initialization parameter for custom library.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:194
ReadyResult
Definition: seq_process_common.h:149
kDefaultChannel
constexpr static uint32_t kDefaultChannel
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: sequence_image_process.h:30
BufferManager::buildRoiBlocks
NvDsPreProcessStatus buildRoiBlocks(const std::vector< NvDsPreProcessUnit > &rois)
SequenceImagePreprocess::deinit
NvDsPreProcessStatus deinit()
NvDsPreProcessBatch
Holds information about the batch of frames to be inferred.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:257
BufferManager::clearAll
void clearAll()
BufferManager
Definition: sequence_image_process.h:38
BufferManager::popReady
bool popReady(ReadyResult &res)
PROCESS_EXPORT_API
#define PROCESS_EXPORT_API
Definition: seq_process_common.h:100
SequenceImagePreprocess::prepareTensorData
NvDsPreProcessStatus prepareTensorData(NvDsPreProcessBatch *batch, NvDsPreProcessCustomBuf *&buf, CustomTensorParams &tensorParam, NvDsPreProcessAcquirer *allocator)
SequenceImagePreprocess::preprocessData
NvDsPreProcessStatus preprocessData(const NvDsPreProcessUnit &unit, NvDsPreProcessFormat inFormat, void *outPtr)
NvDsPreProcessStatus
NvDsPreProcessStatus
Enum for the status codes returned by NvDsPreProcessImpl.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:59
BufferManager::allocator
NvDsPreProcessAcquirer * allocator() const
Definition: sequence_image_process.h:62
kDefaultSubSample
constexpr static uint32_t kDefaultSubSample
Definition: sequence_image_process.h:32
initLib
PROCESS_EXPORT_API CustomCtx * initLib(CustomInitParams initparams)
BufferManager::setAllocator
void setAllocator(NvDsPreProcessAcquirer *allocator)
Definition: sequence_image_process.h:63
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:29
NvDsPreProcessUnit
A preprocess unit for processing which can be Frame/ROI.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:231
Float4Vec
Definition: sequence_preprocess_kernel.h:31
CustomTensorParams
Tensor params passed to custom library for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:183
sequence_preprocess_kernel.h
SequenceImagePreprocess::init
NvDsPreProcessStatus init()
NvDsPreProcessCustomBuf
Custom Buffer passed to the custom lib for preparing tensor.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:209
BufferManager::~BufferManager
~BufferManager()
Definition: sequence_image_process.h:59
NvDsPreProcessAcquirer
class for acquiring and releasing a buffer from tensor pool by custom lib.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:219
NvDsPreProcessTensorParams::network_input_shape
std::vector< int > network_input_shape
Hold the network shape - interpreted based on network input order For resnet10 : NCHW = infer-batch-s...
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:145
SequenceImagePreprocess
Definition: sequence_image_process.h:113
deInitLib
PROCESS_EXPORT_API void deInitLib(CustomCtx *ctx)
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:139
BufferManager::BufferManager
BufferManager(NvDsPreProcessAcquirer *allocator, const NvDsPreProcessTensorParams &params, uint32_t depth, uint32_t channel, cudaStream_t stream, uint32_t stride, uint32_t interval)
Definition: sequence_image_process.h:41
SourceKey
std::tuple< uint64_t, int64_t, int64_t > SourceKey
Definition: seq_process_common.h:109
seq_process_common.h
SequenceImagePreprocess::~SequenceImagePreprocess
~SequenceImagePreprocess()
Definition: sequence_image_process.h:116
BufferManager::collectReady
NvDsPreProcessStatus collectReady()
SrcDestBufMap
std::map< SourceKey, std::unique_ptr< RoiProcessedBuf > > SrcDestBufMap
Definition: seq_process_common.h:160
DSASSERT
#define DSASSERT
Definition: seq_process_common.h:57
SequenceImagePreprocess::SequenceImagePreprocess
SequenceImagePreprocess(const CustomInitParams &initParams)
Definition: sequence_image_process.h:115
CustomCtx
struct CustomCtx CustomCtx
Context for custom library.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:54
NvDsPreProcessFormat
NvDsPreProcessFormat
Defines model color formats.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:104