NVIDIA DeepStream SDK API Reference

7.0 Release
sequence_image_process.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef __NVDS_SEQUENCE_IMAGE_PREPROCESS_H__
14 #define __NVDS_SEQUENCE_IMAGE_PREPROCESS_H__
15 
16 #include "seq_process_common.h"
18 
19 // default values
20 constexpr static uint32_t kDefaultChannel = 3;
21 constexpr static uint32_t kDefaultStride = 1;
22 constexpr static uint32_t kDefaultSubSample = 0;
23 
24 // Buffer manager convert and normalize all the source ROI input images into
25 // network model input datatype and format. It enables temporal batching per
26 // each ROI into sequence, and gather multiple ROI sequence into spacial batches.
27 // It supports `subsample` and `stride` to improve performance for model inference.
29 public:
30  // constructor and destructor
32  NvDsPreProcessAcquirer* allocator, const NvDsPreProcessTensorParams& params, uint32_t depth,
33  uint32_t channel, cudaStream_t stream, uint32_t stride, uint32_t interval)
34  : _allocator(allocator), _tensorParams(params), _seqStride(stride), _subsample(interval)
35  {
36  const auto& shape = params.network_input_shape;
37  DSASSERT(shape.size() == 5 || shape.size() == 4); // NCDHW or NCHW
38  if (shape.size() == 5) {
39  DSASSERT(shape[2] == (int)depth); // NCDHW
40  }
41  _maxBatch = shape[0];
42  _seqSize = depth;
43  _channels = channel;
44  _perBatchSize =
45  std::accumulate(shape.begin() + 1, shape.end(), 1, [](int s, int i) { return s * i; });
46  DSASSERT(_perBatchSize > 0);
47  _cuStream = stream;
48  }
50 
51  // allocator could be set later
52  NvDsPreProcessAcquirer* allocator() const { return _allocator; }
54 
55  // creat new sequence buffers on each new ROI information
56  NvDsPreProcessStatus buildRoiBlocks(const std::vector<NvDsPreProcessUnit>& rois);
57  // locate the output CUDA memory pointer for each coming ROI
58  NvDsPreProcessStatus locateRoiDst(const NvDsPreProcessUnit& roi, void*& dstPatch);
59  // collect all ready results of ROI sequence buffers
61  // pop a ready result
62  bool popReady(ReadyResult& res);
63  // clear before quit
64  void clearAll();
65 
66 private:
67  NvDsPreProcessStatus addRoi(const SourceKey& key, const NvDsPreProcessUnit& unit);
68  uint32_t perBatchBytes() const { return _perBatchSize * sizeof(float); }
69 
70  // HW: height * width
71  uint32_t HWbytes() const { return perBatchBytes() / _seqSize / _channels; }
72 
73  uint32_t SHWbytes() const { return perBatchBytes() / _channels; }
74 
75  bool popOldestReady(ReadyResult& res)
76  {
77  if (_readyPendings.empty()) {
78  return false;
79  }
80  res = _readyPendings.front();
81  _readyPendings.pop();
82  return true;
83  }
84 
85 private:
86  NvDsPreProcessAcquirer* _allocator = nullptr;
87  NvDsPreProcessTensorParams _tensorParams;
88  cudaStream_t _cuStream = nullptr;
89  std::vector<std::unique_ptr<FullBatchBlock>> _accumulateBlocks;
90  SrcDestBufMap _src2dstMap;
91  std::queue<ReadyResult> _readyPendings;
92  uint32_t _maxBatch = 0;
93  uint32_t _seqSize = 0;
94  uint32_t _channels = kDefaultChannel;
95  uint32_t _perBatchSize = 0;
96  uint32_t _seqStride = kDefaultStride;
97  uint32_t _subsample = kDefaultSubSample;
98 };
99 
100 // Preprocess Context for all streams and ROIs. Custom lib symbols is cast into
101 // this context. It connects the buffer manager and input/output cuda processing
102 // kernels. Return the final processed buffer into Gst-nvdspreprocess plugin.
104 public:
105  SequenceImagePreprocess(const CustomInitParams& initParams) { _initParams = initParams; }
107 
108  // derives symbol of initLib
110  // derives symbol of deInitLib
112  // derives symbol of CustomSequenceTensorPreparation
115  NvDsPreProcessAcquirer* allocator);
116 
117  // internal cuda data conversion
119  const NvDsPreProcessUnit& unit, NvDsPreProcessFormat inFormat, void* outPtr);
120 
121 private:
122  NvDsPreProcessStatus setDevice();
123  NvDsPreProcessStatus parseUserConfig();
124 
125  CustomInitParams _initParams;
126  std::unique_ptr<BufferManager> _bufManager;
127  cudaStream_t _cuStream = nullptr;
128  int _gpuId = -1;
129  uint32_t _N = 0;
130  uint32_t _C = kDefaultChannel;
131  uint32_t _S = 0;
132  uint32_t _H = 0;
133  uint32_t _W = 0;
134  Float4Vec _scales = {{1.0, 1.0, 1.0, 1.0}};
135  Float4Vec _means = {{0.0, 0.0, 0.0, 0.0}};
136  uint32_t _subsample = 0;
137  uint32_t _stride = kDefaultStride;
138 };
139 
140 // entrypoint symbols for Gst-nvpreprocess plugin to load
141 // custom lib: libnvds_custom_sequence_preprocess.so
142 extern "C" {
143 // entrypoint for each batched ROI buffers processing
146  CustomTensorParams& tensorParam, NvDsPreProcessAcquirer* allocator);
147 
148 // entrypoint to create and init custom lib context
150 
151 // entrypoint to destroy custom lib context
153 }
154 
155 #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:21
CustomInitParams
Custom Initialization parameter for custom library.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:184
ReadyResult
Definition: seq_process_common.h:139
kDefaultChannel
constexpr static uint32_t kDefaultChannel
Definition: sequence_image_process.h:20
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:247
BufferManager::clearAll
void clearAll()
BufferManager
Definition: sequence_image_process.h:28
BufferManager::popReady
bool popReady(ReadyResult &res)
PROCESS_EXPORT_API
#define PROCESS_EXPORT_API
Definition: seq_process_common.h:90
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:49
BufferManager::allocator
NvDsPreProcessAcquirer * allocator() const
Definition: sequence_image_process.h:52
kDefaultSubSample
constexpr static uint32_t kDefaultSubSample
Definition: sequence_image_process.h:22
initLib
PROCESS_EXPORT_API CustomCtx * initLib(CustomInitParams initparams)
BufferManager::setAllocator
void setAllocator(NvDsPreProcessAcquirer *allocator)
Definition: sequence_image_process.h:53
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:34
NvDsPreProcessUnit
A preprocess unit for processing which can be Frame/ROI.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:221
Float4Vec
Definition: sequence_preprocess_kernel.h:21
CustomTensorParams
Tensor params passed to custom library for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:173
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:199
BufferManager::~BufferManager
~BufferManager()
Definition: sequence_image_process.h:49
NvDsPreProcessAcquirer
class for acquiring and releasing a buffer from tensor pool by custom lib.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:209
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:135
SequenceImagePreprocess
Definition: sequence_image_process.h:103
deInitLib
PROCESS_EXPORT_API void deInitLib(CustomCtx *ctx)
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:129
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:31
SourceKey
std::tuple< uint64_t, int64_t, int64_t > SourceKey
Definition: seq_process_common.h:99
seq_process_common.h
SequenceImagePreprocess::~SequenceImagePreprocess
~SequenceImagePreprocess()
Definition: sequence_image_process.h:106
BufferManager::collectReady
NvDsPreProcessStatus collectReady()
SrcDestBufMap
std::map< SourceKey, std::unique_ptr< RoiProcessedBuf > > SrcDestBufMap
Definition: seq_process_common.h:150
DSASSERT
#define DSASSERT
Definition: seq_process_common.h:47
SequenceImagePreprocess::SequenceImagePreprocess
SequenceImagePreprocess(const CustomInitParams &initParams)
Definition: sequence_image_process.h:105
CustomCtx
struct CustomCtx CustomCtx
Context for custom library.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:44
NvDsPreProcessFormat
NvDsPreProcessFormat
Defines model color formats.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:94