NVIDIA DeepStream SDK API Reference

7.0 Release
nvdspreprocess_impl.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 __NVDSPREPROCESS_IMPL_H__
14 #define __NVDSPREPROCESS_IMPL_H__
15 
16 #include <stdarg.h>
17 #include <condition_variable>
18 #include <functional>
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 
24 #include <cuda_runtime_api.h>
25 
26 #include "nvdspreprocess_interface.h"
27 
28 #include <stdio.h>
29 #include <assert.h>
30 
33 #define _MAX_CHANNELS 4
34 
36 #define DISABLE_CLASS_COPY(NoCopyClass) \
37  NoCopyClass(const NoCopyClass &) = delete; \
38  void operator=(const NoCopyClass &) = delete
39 
41 #define SIMPLE_MOVE_COPY(Cls) \
42  Cls &operator=(Cls &&o) \
43  { \
44  move_copy(std::move(o)); \
45  return *this; \
46  } \
47  Cls(Cls &&o) { move_copy(std::move(o)); }
48 
50 inline const char *safeStr(const std::string &str)
51 {
52  return str.c_str();
53 }
54 
56 inline bool file_accessible(const char* path)
57 {
58  assert(path);
59  return (access(path, F_OK) != -1);
60 }
61 
63 inline bool file_accessible(const std::string &path)
64 {
65  return (!path.empty()) && file_accessible(path.c_str());
66 }
67 
71 typedef struct
72 {
76  std::string labelsFilePath;
77 
80  std::string meanImageFilePath;
81 
85  std::vector<float> offsets;
86 
88  float pixel_normalization_factor = 1.0;
89 
93 
98 {
99 public:
100  explicit CudaStream(uint flag = cudaStreamDefault, int priority = 0);
101  ~CudaStream();
103  operator cudaStream_t() { return m_Stream; }
105  cudaStream_t& ptr() { return m_Stream; }
108 
109 private:
110  void move_copy(CudaStream&& o)
111  {
112  m_Stream = o.m_Stream;
113  o.m_Stream = nullptr;
114  }
115  DISABLE_CLASS_COPY(CudaStream);
116 
117  cudaStream_t m_Stream = nullptr;
118 };
119 
124 {
125 public:
126  virtual ~CudaBuffer() = default;
128  size_t bytes() const { return m_Size; }
130  template <typename T>
131  T* ptr()
132  {
133  return (T*)m_Buf;
134  }
136  void* ptr() { return m_Buf; }
139 
140 protected:
141  explicit CudaBuffer(size_t s) : m_Size(s) {}
144  {
145  m_Buf = o.m_Buf;
146  o.m_Buf = nullptr;
147  m_Size = o.m_Size;
148  o.m_Size = 0;
149  }
153  void* m_Buf = nullptr;
155  size_t m_Size = 0;
156 };
157 
162 {
163 public:
165  explicit CudaDeviceBuffer(size_t size);
168 };
169 
174 {
175 public:
178  int id = 0);
179  virtual ~NvDsPreProcessTensorImpl() = default;
180 
182  bool setScaleOffsets(float scale, const std::vector<float>& offsets = {});
184  bool setMeanFile(const std::string& file);
193  CustomTensorParams &tensorParam, void*& devBuf);
194 
195 private:
196  NvDsPreProcessStatus readMeanImageFile();
197  DISABLE_CLASS_COPY(NvDsPreProcessTensorImpl);
198 
199 private:
200  int m_UniqueID = 0;
201 
202  /* Network input information. */
203  NvDsPreProcessNetworkSize m_NetworkSize = {0};
204  NvDsPreProcessFormat m_NetworkInputFormat = NvDsPreProcessFormat_RGB;
206 
207  float m_Scale = 1.0f;
208  std::vector<float> m_ChannelMeans; // same as channels
209  std::string m_MeanFile;
210 
211  std::unique_ptr<CudaStream> m_PreProcessStream;
212  std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
213 };
214 
218 extern "C"
221  NvDsPreProcessTensorParams *tensor_params,
222  std::unique_ptr<NvDsPreProcessTensorImpl> & m_Preprocessor, int unique_id);
223 
224 #endif
CudaDeviceBuffer::~CudaDeviceBuffer
~CudaDeviceBuffer()
destructor
CudaBuffer::ptr
T * ptr()
template to return cuda buffer
Definition: nvdspreprocess_impl.h:131
CudaBuffer::CudaBuffer
CudaBuffer(size_t s)
helper move copy functionality
Definition: nvdspreprocess_impl.h:141
CudaBuffer::m_Buf
void * m_Buf
pointer to cuda buffer
Definition: nvdspreprocess_impl.h:153
CustomMeanSubandNormParams
Custom parameters for normalization and mean subtractions.
Definition: nvdspreprocess_impl.h:71
NvDsPreProcessFormat_RGB
@ NvDsPreProcessFormat_RGB
Specifies 24-bit interleaved R-G-B format.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:97
NvDsPreProcessNetworkSize
Holds information about the model network.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:151
NvDsPreProcessTensorImpl::setMeanFile
bool setMeanFile(const std::string &file)
method to set mean file
NvDsPreProcessBatch
Holds information about the batch of frames to be inferred.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:247
NvDsPreProcessTensorImpl::allocateResource
NvDsPreProcessStatus allocateResource()
allocate resources for tensor preparation
file_accessible
bool file_accessible(const char *path)
helper check if file accessible in C
Definition: nvdspreprocess_impl.h:56
NvDsPreProcessTensorImpl::setInputOrder
bool setInputOrder(const NvDsPreProcessNetworkInputOrder order)
method to set network input order
SIMPLE_MOVE_COPY
#define SIMPLE_MOVE_COPY(Cls)
helper move function
Definition: nvdspreprocess_impl.h:41
NvDsPreProcessStatus
NvDsPreProcessStatus
Enum for the status codes returned by NvDsPreProcessImpl.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:49
CustomMeanSubandNormParams::offsets
std::vector< float > offsets
Holds the per-channel offsets for mean subtraction.
Definition: nvdspreprocess_impl.h:85
NvDsPreProcessTensorImpl
Provides pre-processing functionality like mean subtraction and normalization.
Definition: nvdspreprocess_impl.h:173
CudaStream
Helper class for managing Cuda Streams.
Definition: nvdspreprocess_impl.h:97
CustomMeanSubandNormParams::labelsFilePath
std::string labelsFilePath
Holds the pathname of the labels file containing strings for the class labels.
Definition: nvdspreprocess_impl.h:76
NvDsPreProcessTensorImpl::NvDsPreProcessTensorImpl
NvDsPreProcessTensorImpl(const NvDsPreProcessNetworkSize &size, NvDsPreProcessFormat format, int id=0)
constructor for tensor preparation implementation
CudaBuffer
Helper base class for managing Cuda allocated buffers.
Definition: nvdspreprocess_impl.h:123
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:34
CudaStream::~CudaStream
~CudaStream()
NvDsPreProcessTensorImpl::syncStream
NvDsPreProcessStatus syncStream()
synchronize cuda stream
CustomTensorParams
Tensor params passed to custom library for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:173
CudaBuffer::move_copy
void move_copy(CudaBuffer &&o)
move_copy cuda buffer
Definition: nvdspreprocess_impl.h:143
CudaBuffer::~CudaBuffer
virtual ~CudaBuffer()=default
NvDsPreProcessTensorImpl::~NvDsPreProcessTensorImpl
virtual ~NvDsPreProcessTensorImpl()=default
CudaBuffer::m_Size
size_t m_Size
buffer size
Definition: nvdspreprocess_impl.h:155
safeStr
const char * safeStr(const std::string &str)
helper check safe string in C
Definition: nvdspreprocess_impl.h:50
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:129
NvDsPreProcessTensorImpl::setScaleOffsets
bool setScaleOffsets(float scale, const std::vector< float > &offsets={})
method to set offsets values
CudaBuffer::bytes
size_t bytes() const
size of cuda buffer in bytes
Definition: nvdspreprocess_impl.h:128
CudaDeviceBuffer
CUDA device buffers.
Definition: nvdspreprocess_impl.h:161
CustomMeanSubandNormParams::meanImageFilePath
std::string meanImageFilePath
Holds the pathname of the mean image file (PPM format).
Definition: nvdspreprocess_impl.h:80
NvDsPreProcessNetworkInputOrder
NvDsPreProcessNetworkInputOrder
Enum for the network input order according to which network shape will be provided to prepare raw ten...
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:81
CustomMeanSubandNormParams::networkSize
NvDsPreProcessNetworkSize networkSize
width, height, channels size of Network
Definition: nvdspreprocess_impl.h:91
CudaDeviceBuffer::CudaDeviceBuffer
CudaDeviceBuffer(size_t size)
constructor
NvDsPreProcessNetworkInputOrder_kNCHW
@ NvDsPreProcessNetworkInputOrder_kNCHW
Specifies NCHW network input order.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:84
CudaBuffer::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(CudaBuffer)
disable class copy
CudaBuffer::ptr
void * ptr()
pointer to cuda buffer
Definition: nvdspreprocess_impl.h:136
NvDsPreProcessTensorImpl::prepare_tensor
NvDsPreProcessStatus prepare_tensor(NvDsPreProcessBatch *batch, CustomTensorParams &tensorParam, void *&devBuf)
method to prepare tensor using cuda kernels
NvDsPreProcessFormat
NvDsPreProcessFormat
Defines model color formats.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:94
normalization_mean_subtraction_impl_initialize
NvDsPreProcessStatus normalization_mean_subtraction_impl_initialize(CustomMeanSubandNormParams *custom_params, NvDsPreProcessTensorParams *tensor_params, std::unique_ptr< NvDsPreProcessTensorImpl > &m_Preprocessor, int unique_id)
Initialize for pixel normalization and mean subtraction.
CudaStream::ptr
cudaStream_t & ptr()
pointer to cuda stream
Definition: nvdspreprocess_impl.h:105
CudaStream::CudaStream
CudaStream(uint flag=cudaStreamDefault, int priority=0)