NVIDIA DeepStream SDK API Reference

6.4 Release
nvdspreprocess_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __NVDSPREPROCESS_IMPL_H__
24 #define __NVDSPREPROCESS_IMPL_H__
25 
26 #include <stdarg.h>
27 #include <condition_variable>
28 #include <functional>
29 #include <list>
30 #include <memory>
31 #include <mutex>
32 #include <queue>
33 
34 #include <cuda_runtime_api.h>
35 
36 #include "nvdspreprocess_interface.h"
37 
38 #include <stdio.h>
39 #include <assert.h>
40 
43 #define _MAX_CHANNELS 4
44 
46 #define DISABLE_CLASS_COPY(NoCopyClass) \
47  NoCopyClass(const NoCopyClass &) = delete; \
48  void operator=(const NoCopyClass &) = delete
49 
51 #define SIMPLE_MOVE_COPY(Cls) \
52  Cls &operator=(Cls &&o) \
53  { \
54  move_copy(std::move(o)); \
55  return *this; \
56  } \
57  Cls(Cls &&o) { move_copy(std::move(o)); }
58 
60 inline const char *safeStr(const std::string &str)
61 {
62  return str.c_str();
63 }
64 
66 inline bool file_accessible(const char* path)
67 {
68  assert(path);
69  return (access(path, F_OK) != -1);
70 }
71 
73 inline bool file_accessible(const std::string &path)
74 {
75  return (!path.empty()) && file_accessible(path.c_str());
76 }
77 
81 typedef struct
82 {
86  std::string labelsFilePath;
87 
90  std::string meanImageFilePath;
91 
95  std::vector<float> offsets;
96 
98  float pixel_normalization_factor = 1.0;
99 
103 
108 {
109 public:
110  explicit CudaStream(uint flag = cudaStreamDefault, int priority = 0);
111  ~CudaStream();
113  operator cudaStream_t() { return m_Stream; }
115  cudaStream_t& ptr() { return m_Stream; }
118 
119 private:
120  void move_copy(CudaStream&& o)
121  {
122  m_Stream = o.m_Stream;
123  o.m_Stream = nullptr;
124  }
125  DISABLE_CLASS_COPY(CudaStream);
126 
127  cudaStream_t m_Stream = nullptr;
128 };
129 
134 {
135 public:
136  virtual ~CudaBuffer() = default;
138  size_t bytes() const { return m_Size; }
140  template <typename T>
141  T* ptr()
142  {
143  return (T*)m_Buf;
144  }
146  void* ptr() { return m_Buf; }
149 
150 protected:
151  explicit CudaBuffer(size_t s) : m_Size(s) {}
154  {
155  m_Buf = o.m_Buf;
156  o.m_Buf = nullptr;
157  m_Size = o.m_Size;
158  o.m_Size = 0;
159  }
163  void* m_Buf = nullptr;
165  size_t m_Size = 0;
166 };
167 
172 {
173 public:
175  explicit CudaDeviceBuffer(size_t size);
178 };
179 
184 {
185 public:
188  int id = 0);
189  virtual ~NvDsPreProcessTensorImpl() = default;
190 
192  bool setScaleOffsets(float scale, const std::vector<float>& offsets = {});
194  bool setMeanFile(const std::string& file);
203  void*& devBuf);
204 
205 private:
206  NvDsPreProcessStatus readMeanImageFile();
207  DISABLE_CLASS_COPY(NvDsPreProcessTensorImpl);
208 
209 private:
210  int m_UniqueID = 0;
211 
212  /* Network input information. */
213  NvDsPreProcessNetworkSize m_NetworkSize = {0};
214  NvDsPreProcessFormat m_NetworkInputFormat = NvDsPreProcessFormat_RGB;
216 
217  float m_Scale = 1.0f;
218  std::vector<float> m_ChannelMeans; // same as channels
219  std::string m_MeanFile;
220 
221  std::unique_ptr<CudaStream> m_PreProcessStream;
222  std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
223 };
224 
228 extern "C"
231  NvDsPreProcessTensorParams *tensor_params,
232  std::unique_ptr<NvDsPreProcessTensorImpl> & m_Preprocessor, int unique_id);
233 
234 #endif
CudaDeviceBuffer::~CudaDeviceBuffer
~CudaDeviceBuffer()
destructor
CudaBuffer::ptr
T * ptr()
template to return cuda buffer
Definition: nvdspreprocess_impl.h:141
CudaBuffer::CudaBuffer
CudaBuffer(size_t s)
helper move copy functionality
Definition: nvdspreprocess_impl.h:151
CudaBuffer::m_Buf
void * m_Buf
pointer to cuda buffer
Definition: nvdspreprocess_impl.h:163
CustomMeanSubandNormParams
Custom parameters for normalization and mean subtractions.
Definition: nvdspreprocess_impl.h:81
NvDsPreProcessFormat_RGB
@ NvDsPreProcessFormat_RGB
Specifies 24-bit interleaved R-G-B format.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:107
NvDsPreProcessNetworkSize
Holds information about the model network.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:161
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:257
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:66
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:51
NvDsPreProcessStatus
NvDsPreProcessStatus
Enum for the status codes returned by NvDsPreProcessImpl.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:59
CustomMeanSubandNormParams::offsets
std::vector< float > offsets
Holds the per-channel offsets for mean subtraction.
Definition: nvdspreprocess_impl.h:95
NvDsPreProcessTensorImpl
Provides pre-processing functionality like mean subtraction and normalization.
Definition: nvdspreprocess_impl.h:183
CudaStream
Helper class for managing Cuda Streams.
Definition: nvdspreprocess_impl.h:107
CustomMeanSubandNormParams::labelsFilePath
std::string labelsFilePath
Holds the pathname of the labels file containing strings for the class labels.
Definition: nvdspreprocess_impl.h:86
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:133
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:29
CudaStream::~CudaStream
~CudaStream()
NvDsPreProcessTensorImpl::syncStream
NvDsPreProcessStatus syncStream()
synchronize cuda stream
CudaBuffer::move_copy
void move_copy(CudaBuffer &&o)
move_copy cuda buffer
Definition: nvdspreprocess_impl.h:153
CudaBuffer::~CudaBuffer
virtual ~CudaBuffer()=default
NvDsPreProcessTensorImpl::~NvDsPreProcessTensorImpl
virtual ~NvDsPreProcessTensorImpl()=default
CudaBuffer::m_Size
size_t m_Size
buffer size
Definition: nvdspreprocess_impl.h:165
safeStr
const char * safeStr(const std::string &str)
helper check safe string in C
Definition: nvdspreprocess_impl.h:60
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:139
NvDsPreProcessTensorImpl::prepare_tensor
NvDsPreProcessStatus prepare_tensor(NvDsPreProcessBatch *batch, void *&devBuf)
method to prepare tensor using cuda kernels
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:138
CudaDeviceBuffer
CUDA device buffers.
Definition: nvdspreprocess_impl.h:171
CustomMeanSubandNormParams::meanImageFilePath
std::string meanImageFilePath
Holds the pathname of the mean image file (PPM format).
Definition: nvdspreprocess_impl.h:90
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:91
CustomMeanSubandNormParams::networkSize
NvDsPreProcessNetworkSize networkSize
width, height, channels size of Network
Definition: nvdspreprocess_impl.h:101
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:94
CudaBuffer::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(CudaBuffer)
disable class copy
CudaBuffer::ptr
void * ptr()
pointer to cuda buffer
Definition: nvdspreprocess_impl.h:146
NvDsPreProcessFormat
NvDsPreProcessFormat
Defines model color formats.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:104
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:115
CudaStream::CudaStream
CudaStream(uint flag=cudaStreamDefault, int priority=0)