NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/nvdsinferserver/infer_preprocess.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2018-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_PREPROCESS_H__
28 #define __NVDSINFERSERVER_PREPROCESS_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.h>
39 #include <cuda_runtime_api.h>
40 
41 #include "infer_cuda_utils.h"
42 #include "infer_datatypes.h"
43 #include "infer_iprocess.h"
45 #include "infer_utils.h"
46 
47 #include "nvbufsurface.h"
48 #include "nvbufsurftransform.h"
49 
50 namespace nvdsinferserver {
51 
57 public:
59  InferMediaFormat networkFormat, InferDataType dt, int maxBatchSize);
60  virtual ~NetworkPreprocessor() = default;
61 
62  bool setScaleOffsets(float scale, const std::vector<float>& offsets = {});
63  bool setMeanFile(const std::string& file);
65  m_NetworkTensorOrder = order;
66  }
67  void setPoolSize(int size) { m_PoolSize = size; }
68  int poolSize() const { return m_PoolSize; }
69  void setNetworkTensorName(std::string name) { m_TensorName = name; }
70  const BatchSurfaceInfo& getDstSurfaceInfo() const { return m_DstInfo; }
71 
72  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
74 
75 private:
76  SharedBatchBuf requestOutBuffer(SharedBatchBuf& inBuf) override;
77  NvDsInferStatus transformImpl(SharedBatchBuf& src, SharedBatchBuf& dst,
78  SharedCuStream& mainStream) override;
79 
80  NvDsInferStatus cudaTransform(void* srcBuf, const BatchSurfaceInfo& srcInfo,
81  InferMediaFormat srcFormat, InferDataType srcDType, void* dstBuf,
82  const BatchSurfaceInfo& dstInfo, InferDataType dstDType, int batchSize,
83  int devId);
84 
85 protected:
88 
89 private:
90  NvDsInferNetworkInfo m_NetworkInfo = {0};
91  InferTensorOrder m_NetworkTensorOrder = InferTensorOrder::kLinear;
93  InferMediaFormat m_NetworkFormat = InferMediaFormat::kRGB;
94  float m_Scale = 1.0f;
95  std::vector<float> m_ChannelMeans; // same as channels
96  std::string m_MeanFile;
97 
98  std::string m_TensorName;
99 
101  SharedBufPool<UniqCudaTensorBuf> m_BufPool;
102  int m_MaxBatchSize = 1;
103  int m_PoolSize = 1;
104  InferDataType m_DstDataType = InferDataType::kFp32;
105 
106  BatchSurfaceInfo m_DstInfo = {0};
107 
108  std::unique_ptr<CudaStream> m_PreProcessStream;
109  std::unique_ptr<CudaDeviceMem> m_MeanDataBuffer;
110 };
111 
112 class SurfaceBuffer;
113 
119 public:
120  CropSurfaceConverter(int32_t convertPoolSize)
121  : m_ConvertPoolSize(convertPoolSize) {}
122  ~CropSurfaceConverter() override;
123  void setParams(
124  int outW, int outH, InferMediaFormat outFormat, int maxBatchSize);
125  void setMaintainAspectRatio(bool enable) { m_MaintainAspectRatio = enable; }
126  void setSymmetricPadding(bool enable) { m_SymmetricPadding = enable; }
128  m_ComputeHW = compute_hw;
129  }
131  m_TransformParam.transform_filter = filter;
132  }
133 
134 private:
135  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
136  SharedBatchBuf requestOutBuffer(SharedBatchBuf& inBuf);
137  NvDsInferStatus transformImpl(
138  SharedBatchBuf& src, SharedBatchBuf& dst, SharedCuStream& mainStream);
139 
140 private:
141  NvDsInferStatus resizeBatch(SharedBatchBuf& src, SharedBatchBuf& dst);
142 
143 private:
144  int32_t m_ConvertPoolSize = 4;
145 
146  int32_t m_DstWidth = 0;
147  int32_t m_DstHeight = 0;
149  uint32_t m_MaxBatchSize = 0;
150  bool m_MaintainAspectRatio = false;
151  bool m_SymmetricPadding = false;
153 
154  SharedBufPool<std::unique_ptr<SurfaceBuffer>> m_ConvertPool;
155  SharedCuStream m_ConverStream;
156  NvBufSurfTransformParams m_TransformParam = {
158 };
159 
160 } // namespace nvdsinferserver
161 
162 #endif /* __NVDSINFER_CONVERSION_H__ */
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::InferDataType
InferDataType
Datatype of the tensor buffer.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:88
BatchSurfaceInfo
Definition: sources/libs/nvdsinferserver/infer_preprocess_kernel.h:32
nvdsinferserver::InferTensorOrder::kLinear
@ kLinear
NCHW (batch-channels-height-width) tensor order.
nvdsinferserver::InferMediaFormat
InferMediaFormat
Image formats.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:134
nvdsinferserver::NetworkPreprocessor::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(NetworkPreprocessor)
_NvBufSurfaceTransformParams::transform_filter
NvBufSurfTransform_Inter transform_filter
Holds a transform filter.
Definition: sources/includes/nvbufsurftransform.h:203
_NvBufSurfaceTransformParams
Holds transform parameters for a transform call.
Definition: sources/includes/nvbufsurftransform.h:196
nvdsinferserver::NetworkPreprocessor
Preprocessor for scaling and normalization of the input and conversion to network media format.
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:56
nvdsinferserver::SharedBatchBuf
std::shared_ptr< BaseBatchBuffer > SharedBatchBuf
Common buffer interfaces (internal).
Definition: sources/libs/nvdsinferserver/infer_common.h:76
nvdsinferserver::InferTensorOrder
InferTensorOrder
The type of tensor order.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:46
nvdsinferserver::NetworkPreprocessor::syncStream
NvDsInferStatus syncStream()
nvdsinferserver::NetworkPreprocessor::setPoolSize
void setPoolSize(int size)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:67
nvdsinferserver::NetworkPreprocessor::setNetworkTensorOrder
void setNetworkTensorOrder(InferTensorOrder order)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:64
nvdsinferserver::NetworkPreprocessor::getDstSurfaceInfo
const BatchSurfaceInfo & getDstSurfaceInfo() const
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:70
NvBufSurfTransform_Compute
NvBufSurfTransform_Compute
Specifies compute devices used by NvBufSurfTransform.
Definition: sources/includes/nvbufsurftransform.h:46
nvdsinferserver::InferMediaFormat::kRGB
@ kRGB
24-bit interleaved R-G-B
nvdsinferserver::NetworkPreprocessor::~NetworkPreprocessor
virtual ~NetworkPreprocessor()=default
nvdsinferserver::BasePreprocessor
Base preprocessor class.
Definition: sources/libs/nvdsinferserver/infer_iprocess.h:84
nvdsinferserver::CropSurfaceConverter::CropSurfaceConverter
CropSurfaceConverter(int32_t convertPoolSize)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:120
nvdsinferserver::CropSurfaceConverter::setMaintainAspectRatio
void setMaintainAspectRatio(bool enable)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:125
nvdsinferserver::CropSurfaceConverter
Preprocessor for cropping, scaling and padding the inference input to required height,...
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:118
infer_preprocess_kernel.h
nvdsinferserver::CropSurfaceConverter::~CropSurfaceConverter
~CropSurfaceConverter() override
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: sources/libs/nvdsinferserver/infer_common.h:89
NvDsInferNetworkInfo
Holds information about the model network.
Definition: sources/includes/nvdsinfer.h:119
nvdsinferserver::NetworkPreprocessor::setMeanFile
bool setMeanFile(const std::string &file)
nvdsinferserver::InferDataType::kFp32
@ kFp32
nvdsinferserver::CropSurfaceConverter::setScalingFilter
void setScalingFilter(NvBufSurfTransform_Inter filter)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:130
nvdsinferserver::NetworkPreprocessor::poolSize
int poolSize() const
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:68
nvdsinferserver::NetworkPreprocessor::setNetworkTensorName
void setNetworkTensorName(std::string name)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:69
NvBufSurfTransform_None
@ NvBufSurfTransform_None
Specifies no video flip.
Definition: sources/includes/nvbufsurftransform.h:64
NvBufSurfTransformCompute_Default
@ NvBufSurfTransformCompute_Default
Specifies VIC as a compute device for Jetson or dGPU for an x86_64 system.
Definition: sources/includes/nvbufsurftransform.h:50
infer_iprocess.h
Preprocessing and postprocessing interface header file.
nvdsinferserver::CropSurfaceConverter::setSymmetricPadding
void setSymmetricPadding(bool enable)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:126
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
NvBufSurfTransform_Inter
NvBufSurfTransform_Inter
Specifies video interpolation methods.
Definition: sources/includes/nvbufsurftransform.h:85
nvdsinferserver::NetworkPreprocessor::allocateResource
NvDsInferStatus allocateResource(const std::vector< int > &devIds) override
nvdsinferserver::NetworkPreprocessor::readMeanImageFile
NvDsInferStatus readMeanImageFile()
nvdsinferserver::CropSurfaceConverter::setParams
void setParams(int outW, int outH, InferMediaFormat outFormat, int maxBatchSize)
NvBufSurfTransformInter_Default
@ NvBufSurfTransformInter_Default
Specifies GPU-Nearest, VIC-Nearest interpolation.
Definition: sources/includes/nvbufsurftransform.h:100
nvdsinferserver::NetworkPreprocessor::setScaleOffsets
bool setScaleOffsets(float scale, const std::vector< float > &offsets={})
nvdsinferserver::NetworkPreprocessor::NetworkPreprocessor
NetworkPreprocessor(const NvDsInferNetworkInfo &info, InferMediaFormat networkFormat, InferDataType dt, int maxBatchSize)
nvdsinferserver::CropSurfaceConverter::setScalingHW
void setScalingHW(NvBufSurfTransform_Compute compute_hw)
Definition: sources/libs/nvdsinferserver/infer_preprocess.h:127
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