NVIDIA DeepStream SDK API Reference

6.4 Release
infer_preprocess.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2018-2022 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 
22 #ifndef __NVDSINFERSERVER_PREPROCESS_H__
23 #define __NVDSINFERSERVER_PREPROCESS_H__
24 
25 #include <stdarg.h>
26 #include <condition_variable>
27 #include <functional>
28 #include <list>
29 #include <memory>
30 #include <mutex>
31 #include <queue>
32 
33 #include <cuda.h>
34 #include <cuda_runtime_api.h>
35 
36 #include "infer_cuda_utils.h"
37 #include "infer_datatypes.h"
38 #include "infer_iprocess.h"
40 #include "infer_utils.h"
41 
42 #include "nvbufsurface.h"
43 #include "nvbufsurftransform.h"
44 
45 namespace nvdsinferserver {
46 
52 public:
54  InferMediaFormat networkFormat, InferDataType dt, int maxBatchSize);
55  virtual ~NetworkPreprocessor() = default;
56 
57  bool setScaleOffsets(float scale, const std::vector<float>& offsets = {});
58  bool setMeanFile(const std::string& file);
60  m_NetworkTensorOrder = order;
61  }
62  void setPoolSize(int size) { m_PoolSize = size; }
63  int poolSize() const { return m_PoolSize; }
64  void setNetworkTensorName(std::string name) { m_TensorName = name; }
65  const BatchSurfaceInfo& getDstSurfaceInfo() const { return m_DstInfo; }
66 
67  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
69 
70 private:
71  SharedBatchBuf requestOutBuffer(SharedBatchBuf& inBuf) override;
72  NvDsInferStatus transformImpl(SharedBatchBuf& src, SharedBatchBuf& dst,
73  SharedCuStream& mainStream) override;
74 
75  NvDsInferStatus cudaTransform(void* srcBuf, const BatchSurfaceInfo& srcInfo,
76  InferMediaFormat srcFormat, InferDataType srcDType, void* dstBuf,
77  const BatchSurfaceInfo& dstInfo, InferDataType dstDType, int batchSize,
78  int devId);
79 
80 protected:
83 
84 private:
85  NvDsInferNetworkInfo m_NetworkInfo = {0};
86  InferTensorOrder m_NetworkTensorOrder = InferTensorOrder::kLinear;
88  InferMediaFormat m_NetworkFormat = InferMediaFormat::kRGB;
89  float m_Scale = 1.0f;
90  std::vector<float> m_ChannelMeans; // same as channels
91  std::string m_MeanFile;
92 
93  std::string m_TensorName;
94 
96  SharedBufPool<UniqCudaTensorBuf> m_BufPool;
97  int m_MaxBatchSize = 1;
98  int m_PoolSize = 1;
99  InferDataType m_DstDataType = InferDataType::kFp32;
100 
101  BatchSurfaceInfo m_DstInfo = {0};
102 
103  std::unique_ptr<CudaStream> m_PreProcessStream;
104  std::unique_ptr<CudaDeviceMem> m_MeanDataBuffer;
105 };
106 
107 class SurfaceBuffer;
108 
114 public:
115  CropSurfaceConverter(int32_t convertPoolSize)
116  : m_ConvertPoolSize(convertPoolSize) {}
117  ~CropSurfaceConverter() override;
118  void setParams(
119  int outW, int outH, InferMediaFormat outFormat, int maxBatchSize);
120  void setMaintainAspectRatio(bool enable) { m_MaintainAspectRatio = enable; }
121  void setSymmetricPadding(bool enable) { m_SymmetricPadding = enable; }
123  m_ComputeHW = compute_hw;
124  }
126  m_TransformParam.transform_filter = filter;
127  }
128 
129 private:
130  NvDsInferStatus allocateResource(const std::vector<int>& devIds) override;
131  SharedBatchBuf requestOutBuffer(SharedBatchBuf& inBuf);
132  NvDsInferStatus transformImpl(
133  SharedBatchBuf& src, SharedBatchBuf& dst, SharedCuStream& mainStream);
134 
135 private:
136  NvDsInferStatus resizeBatch(SharedBatchBuf& src, SharedBatchBuf& dst);
137 
138 private:
139  int32_t m_ConvertPoolSize = 4;
140 
141  int32_t m_DstWidth = 0;
142  int32_t m_DstHeight = 0;
144  uint32_t m_MaxBatchSize = 0;
145  bool m_MaintainAspectRatio = false;
146  bool m_SymmetricPadding = false;
148 
149  SharedBufPool<std::unique_ptr<SurfaceBuffer>> m_ConvertPool;
150  SharedCuStream m_ConverStream;
151  NvBufSurfTransformParams m_TransformParam = {
153 };
154 
155 } // namespace nvdsinferserver
156 
157 #endif /* __NVDSINFER_CONVERSION_H__ */
nvdsinferserver
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: infer_custom_process.h:28
nvdsinferserver::InferDataType
InferDataType
Datatype of the tensor buffer.
Definition: infer_datatypes.h:83
BatchSurfaceInfo
Definition: infer_preprocess_kernel.h:27
nvdsinferserver::InferTensorOrder::kLinear
@ kLinear
NCHW (batch-channels-height-width) tensor order.
nvdsinferserver::SharedBatchBuf
std::shared_ptr< BaseBatchBuffer > SharedBatchBuf
Common buffer interfaces (internal).
Definition: infer_common.h:71
nvdsinferserver::InferMediaFormat
InferMediaFormat
Image formats.
Definition: infer_datatypes.h:129
nvdsinferserver::NetworkPreprocessor::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(NetworkPreprocessor)
_NvBufSurfaceTransformParams::transform_filter
NvBufSurfTransform_Inter transform_filter
Holds a transform filter.
Definition: nvbufsurftransform.h:192
_NvBufSurfaceTransformParams
Holds transform parameters for a transform call.
Definition: nvbufsurftransform.h:185
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::NetworkPreprocessor
Preprocessor for scaling and normalization of the input and conversion to network media format.
Definition: infer_preprocess.h:51
nvdsinferserver::InferTensorOrder
InferTensorOrder
The type of tensor order.
Definition: infer_datatypes.h:41
nvdsinferserver::NetworkPreprocessor::syncStream
NvDsInferStatus syncStream()
nvdsinferserver::NetworkPreprocessor::setPoolSize
void setPoolSize(int size)
Definition: infer_preprocess.h:62
nvdsinferserver::NetworkPreprocessor::setNetworkTensorOrder
void setNetworkTensorOrder(InferTensorOrder order)
Definition: infer_preprocess.h:59
nvdsinferserver::NetworkPreprocessor::getDstSurfaceInfo
const BatchSurfaceInfo & getDstSurfaceInfo() const
Definition: infer_preprocess.h:65
infer_preprocess_kernel.h
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
NvBufSurfTransform_Compute
NvBufSurfTransform_Compute
Specifies compute devices used by NvBufSurfTransform.
Definition: nvbufsurftransform.h:40
nvdsinferserver::InferMediaFormat::kRGB
@ kRGB
24-bit interleaved R-G-B
nvdsinferserver::NetworkPreprocessor::~NetworkPreprocessor
virtual ~NetworkPreprocessor()=default
nvdsinferserver::BasePreprocessor
Base preprocessor class.
Definition: infer_iprocess.h:79
nvdsinferserver::CropSurfaceConverter::CropSurfaceConverter
CropSurfaceConverter(int32_t convertPoolSize)
Definition: infer_preprocess.h:115
nvdsinferserver::CropSurfaceConverter::setMaintainAspectRatio
void setMaintainAspectRatio(bool enable)
Definition: infer_preprocess.h:120
nvdsinferserver::CropSurfaceConverter
Preprocessor for cropping, scaling and padding the inference input to required height,...
Definition: infer_preprocess.h:113
nvdsinferserver::CropSurfaceConverter::~CropSurfaceConverter
~CropSurfaceConverter() override
NvDsInferNetworkInfo
Holds information about the model network.
Definition: nvdsinfer.h:110
nvdsinferserver::NetworkPreprocessor::setMeanFile
bool setMeanFile(const std::string &file)
infer_cuda_utils.h
Header file declaring utility classes for CUDA memory management, CIDA streams and events.
nvdsinferserver::InferDataType::kFp32
@ kFp32
nvdsinferserver::CropSurfaceConverter::setScalingFilter
void setScalingFilter(NvBufSurfTransform_Inter filter)
Definition: infer_preprocess.h:125
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: infer_common.h:84
nvdsinferserver::NetworkPreprocessor::poolSize
int poolSize() const
Definition: infer_preprocess.h:63
nvdsinferserver::NetworkPreprocessor::setNetworkTensorName
void setNetworkTensorName(std::string name)
Definition: infer_preprocess.h:64
NvBufSurfTransform_None
@ NvBufSurfTransform_None
Specifies no video flip.
Definition: nvbufsurftransform.h:58
NvBufSurfTransformCompute_Default
@ NvBufSurfTransformCompute_Default
Specifies VIC as a compute device for Jetson or dGPU for an x86_64 system.
Definition: nvbufsurftransform.h:44
nvdsinferserver::CropSurfaceConverter::setSymmetricPadding
void setSymmetricPadding(bool enable)
Definition: infer_preprocess.h:121
NvBufSurfTransform_Inter
NvBufSurfTransform_Inter
Specifies video interpolation methods.
Definition: nvbufsurftransform.h:79
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)
infer_iprocess.h
Preprocessing and postprocessing interface header file.
NvBufSurfTransformInter_Default
@ NvBufSurfTransformInter_Default
Specifies GPU-Nearest, VIC-Nearest interpolation.
Definition: nvbufsurftransform.h:94
nvdsinferserver::NetworkPreprocessor::setScaleOffsets
bool setScaleOffsets(float scale, const std::vector< float > &offsets={})
nvbufsurftransform.h
nvdsinferserver::NetworkPreprocessor::NetworkPreprocessor
NetworkPreprocessor(const NvDsInferNetworkInfo &info, InferMediaFormat networkFormat, InferDataType dt, int maxBatchSize)
nvbufsurface.h
nvdsinferserver::CropSurfaceConverter::setScalingHW
void setScalingHW(NvBufSurfTransform_Compute compute_hw)
Definition: infer_preprocess.h:122
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218