NVIDIA DeepStream SDK API Reference

6.4 Release
nvdsinfer_func_utils.h
Go to the documentation of this file.
1 
12 #ifndef __NVDSINFER_FUNC_UTILS_H__
13 #define __NVDSINFER_FUNC_UTILS_H__
14 
15 #include <dlfcn.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <cassert>
20 #include <condition_variable>
21 #include <mutex>
22 #include <sstream>
23 #include <string>
24 #include <unordered_set>
25 
26 #include <NvInfer.h>
27 #include <NvInferRuntime.h>
28 #include <nvdsinfer.h>
29 #include <nvdsinfer_context.h>
30 #include <nvdsinfer_logger.h>
31 
32 /* This file provides APIs/macros for some frequently used functionality. */
33 
34 #define DISABLE_CLASS_COPY(NoCopyClass) \
35  NoCopyClass(const NoCopyClass&) = delete; \
36  void operator=(const NoCopyClass&) = delete
37 
38 #define SIMPLE_MOVE_COPY(Cls) \
39  Cls& operator=(Cls&& o) { \
40  move_copy(std::move(o)); \
41  return *this; \
42  } \
43  Cls(Cls&& o) { move_copy(std::move(o)); }
44 
45 #define CHECK_NVINFER_ERROR(err, action, fmt, ...) \
46  do { \
47  NvDsInferStatus ifStatus = (err); \
48  if (ifStatus != NVDSINFER_SUCCESS) { \
49  auto errStr = NvDsInferStatus2Str(ifStatus); \
50  dsInferError(fmt ", nvinfer error:%s", ##__VA_ARGS__, errStr); \
51  action; \
52  } \
53  } while (0)
54 
55 #define RETURN_NVINFER_ERROR(err, fmt, ...) \
56  CHECK_NVINFER_ERROR(err, return ifStatus, fmt, ##__VA_ARGS__)
57 
58 #define CHECK_CUDA_ERR_W_ACTION(err, action, fmt, ...) \
59  do { \
60  cudaError_t errnum = (err); \
61  if (errnum != cudaSuccess) { \
62  dsInferError(fmt ", cuda err_no:%d, err_str:%s", ##__VA_ARGS__, \
63  (int)errnum, cudaGetErrorName(errnum)); \
64  action; \
65  } \
66  } while (0)
67 
68 #define CHECK_CUDA_ERR_NO_ACTION(err, fmt, ...) \
69  CHECK_CUDA_ERR_W_ACTION(err, , fmt, ##__VA_ARGS__)
70 
71 #define RETURN_CUDA_ERR(err, fmt, ...) \
72  CHECK_CUDA_ERR_W_ACTION( \
73  err, return NVDSINFER_CUDA_ERROR, fmt, ##__VA_ARGS__)
74 
75 #define READ_SYMBOL(lib, func_name) \
76  lib->symbol<decltype(&func_name)>(#func_name)
77 
78 namespace nvdsinfer {
79 
80 inline const char* safeStr(const char* str)
81 {
82  return !str ? "" : str;
83 }
84 
85 inline const char* safeStr(const std::string& str)
86 {
87  return str.c_str();
88 }
89 
90 inline bool string_empty(const char* str)
91 {
92  return !str || strlen(str) == 0;
93 }
94 
95 inline bool file_accessible(const char* path)
96 {
97  assert(path);
98  return (access(path, F_OK) != -1);
99 }
100 
101 inline bool file_accessible(const std::string& path)
102 {
103  return (!path.empty()) && file_accessible(path.c_str());
104 }
105 
106 std::string dims2Str(const nvinfer1::Dims& d);
107 std::string dims2Str(const NvDsInferDims& d);
108 std::string batchDims2Str(const NvDsInferBatchDims& d);
109 
110 std::string dataType2Str(const nvinfer1::DataType type);
111 std::string dataType2Str(const NvDsInferDataType type);
112 std::string networkMode2Str(const NvDsInferNetworkMode type);
113 
114 /* Custom unique_ptr subclass with deleter functions for TensorRT objects. */
115 template <class T>
116 class UniquePtrWDestroy : public std::unique_ptr<T, void (*)(T*)>
117 {
118 public:
119  UniquePtrWDestroy(T* t = nullptr)
120  : std::unique_ptr<T, void (*)(T*)>(t, [](T* t) {
121  if (t)
122  t->destroy();
123  }) {}
124 };
125 
126 template <class T>
127 class SharedPtrWDestroy : public std::shared_ptr<T>
128 {
129 public:
130  SharedPtrWDestroy(T* t = nullptr)
131  : std::shared_ptr<T>(t, [](T* t) {
132  if (t)
133  t->destroy();
134  }) {}
135 };
136 
138 {
139 public:
140  DlLibHandle(const std::string& path, int mode = RTLD_LAZY);
141  ~DlLibHandle();
142 
143  bool isValid() const { return m_LibHandle; }
144  const std::string& getPath() const { return m_LibPath; }
145 
146  template <typename FuncPtr>
147  FuncPtr symbol(const char* func)
148  {
149  assert(!string_empty(func));
150  if (!m_LibHandle)
151  return nullptr;
152  return (FuncPtr)dlsym(m_LibHandle, func);
153  }
154 
155  template <typename FuncPtr>
156  FuncPtr symbol(const std::string& func)
157  {
158  return symbol<FuncPtr>(func.c_str());
159  }
160 
161 private:
162  void* m_LibHandle{nullptr};
163  const std::string m_LibPath;
164 };
165 
166 
167 template <typename Container>
169 {
170 public:
171  typedef typename Container::value_type T;
172  void push(const T& data)
173  {
174  std::unique_lock<std::mutex> lock(m_Mutex);
175  m_Queue.push_back(data);
176  m_Cond.notify_one();
177  }
178  T pop()
179  {
180  std::unique_lock<std::mutex> lock(m_Mutex);
181  m_Cond.wait(lock, [this]() { return !m_Queue.empty(); });
182  assert(!m_Queue.empty());
183  T ret = std::move(*m_Queue.begin());
184  m_Queue.erase(m_Queue.begin());
185  return ret;
186  }
187  bool isEmpty()
188  {
189  std::unique_lock<std::mutex> lock(m_Mutex);
190  return m_Queue.empty();
191  }
192  void clear()
193  {
194  std::unique_lock<std::mutex> lock(m_Mutex);
195  m_Queue.clear();
196  }
197 
198 private:
199  std::mutex m_Mutex;
200  std::condition_variable m_Cond;
201  Container m_Queue;
202 };
203 
207 inline uint32_t
209 {
210  switch (t)
211  {
212  case INT32:
213  case FLOAT:
214  return 4;
215  case HALF:
216  return 2;
217  case INT8:
218  return 1;
219  default:
220  dsInferError(
221  "Failed to get element size on Unknown datatype:%d", (int)t);
222  return 0;
223  }
224 }
225 
226 /* Convert between TRT's nvinfer1::Dims representation and DeepStream's
227  * NvDsInferDimsCHW/NvDsInferDims representation. */
228 nvinfer1::Dims ds2TrtDims(const NvDsInferDimsCHW& dims);
229 nvinfer1::Dims ds2TrtDims(const NvDsInferDims& dims);
230 NvDsInferDims trt2DsDims(const nvinfer1::Dims& dims);
231 
232 /* Add batch size to provided dims to get full dims as nvinfer1::Dims. */
233 nvinfer1::Dims CombineDimsBatch(const NvDsInferDims& dims, int batch);
234 /* Split full dims provided in the form of nvinfer1::Dims into batch size and
235  * layer dims. */
236 void SplitFullDims(
237  const nvinfer1::Dims& fullDims, NvDsInferDims& dims, int& batch);
238 
239 /* Convert from TRT's nvinfer1::Dims representation to DeepStream's
240  * NvDsInferBatchDims representation. */
241 inline void
242 convertFullDims(const nvinfer1::Dims& fullDims, NvDsInferBatchDims& batchDims)
243 {
244  SplitFullDims(fullDims, batchDims.dims, batchDims.batchSize);
245 }
246 
247 void normalizeDims(NvDsInferDims& dims);
248 
249 bool hasWildcard(const nvinfer1::Dims& dims);
250 bool hasWildcard(const NvDsInferDims& dims);
251 
252 /* Equality / inequality operators implementation for nvinfer1::Dims */
253 bool operator<=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
254 bool operator>(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
255 bool operator==(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
256 bool operator!=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
257 
258 /* Equality / inequality operators implementation for NvDsInferDims */
259 bool operator<=(const NvDsInferDims& a, const NvDsInferDims& b);
260 bool operator>(const NvDsInferDims& a, const NvDsInferDims& b);
261 bool operator==(const NvDsInferDims& a, const NvDsInferDims& b);
262 bool operator!=(const NvDsInferDims& a, const NvDsInferDims& b);
263 
264 
265 bool isValidOutputFormat(const std::string& fmt);
266 bool isValidOutputDataType(const std::string& dataType);
267 nvinfer1::DataType str2DataType(const std::string& dataType);
268 uint32_t str2TensorFormat(const std::string& fmt);
269 
270 struct BuildParams;
271 bool validateIOTensorNames(const BuildParams& params,
272  const nvinfer1::INetworkDefinition& network);
273 bool isValidDeviceType(const std::string& fmt);
274 bool isValidPrecisionType(const std::string& dataType);
275 nvinfer1::DataType str2PrecisionType(const std::string& dataType);
276 nvinfer1::DeviceType str2DeviceType(const std::string& deviceType);
277 
278 } // namespace nvdsinfer
279 
280 #endif
nvdsinfer::SharedPtrWDestroy::SharedPtrWDestroy
SharedPtrWDestroy(T *t=nullptr)
Definition: nvdsinfer_func_utils.h:130
nvdsinfer::DlLibHandle::getPath
const std::string & getPath() const
Definition: nvdsinfer_func_utils.h:144
nvdsinfer::GuardQueue
Definition: nvdsinfer_func_utils.h:168
nvdsinfer::isValidPrecisionType
bool isValidPrecisionType(const std::string &dataType)
nvdsinfer::ds2TrtDims
nvinfer1::Dims ds2TrtDims(const NvDsInferDimsCHW &dims)
nvdsinfer::isValidOutputFormat
bool isValidOutputFormat(const std::string &fmt)
nvdsinfer::GuardQueue::pop
T pop()
Definition: nvdsinfer_func_utils.h:178
nvdsinfer::GuardQueue::push
void push(const T &data)
Definition: nvdsinfer_func_utils.h:172
nvdsinfer::dims2Str
std::string dims2Str(const nvinfer1::Dims &d)
ds3d::DataType
DataType
Definition: idatatype.h:77
nvdsinfer::normalizeDims
void normalizeDims(NvDsInferDims &dims)
nvdsinfer::DlLibHandle::DlLibHandle
DlLibHandle(const std::string &path, int mode=RTLD_LAZY)
nvdsinfer::CombineDimsBatch
nvinfer1::Dims CombineDimsBatch(const NvDsInferDims &dims, int batch)
nvdsinfer::operator>
bool operator>(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::str2PrecisionType
nvinfer1::DataType str2PrecisionType(const std::string &dataType)
NvDsInferDims
Holds the dimensions of a layer.
Definition: nvdsinfer.h:46
nvdsinfer::networkMode2Str
std::string networkMode2Str(const NvDsInferNetworkMode type)
dsInferError
#define dsInferError(fmt,...)
Definition: nvdsinfer_logger.h:28
nvdsinfer::str2DataType
nvinfer1::DataType str2DataType(const std::string &dataType)
NvDsInferDataType
NvDsInferDataType
Specifies the data type of a layer.
Definition: nvdsinfer.h:72
NvDsInferNetworkMode
NvDsInferNetworkMode
Defines internal data formats used by the inference engine.
Definition: nvdsinfer_context.h:115
nvdsinfer
Copyright (c) 2019-2021, NVIDIA CORPORATION.
Definition: nvdsinfer_model_builder.h:41
nvdsinfer::GuardQueue::T
Container::value_type T
Definition: nvdsinfer_func_utils.h:171
nvdsinfer::operator!=
bool operator!=(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::str2TensorFormat
uint32_t str2TensorFormat(const std::string &fmt)
FLOAT
@ FLOAT
Specifies FP32 format.
Definition: nvdsinfer.h:75
nvdsinfer::hasWildcard
bool hasWildcard(const nvinfer1::Dims &dims)
nvdsinfer::GuardQueue::clear
void clear()
Definition: nvdsinfer_func_utils.h:192
nvdsinfer::SharedPtrWDestroy
Definition: nvdsinfer_func_utils.h:127
HALF
@ HALF
Specifies FP16 format.
Definition: nvdsinfer.h:77
INT32
@ INT32
Specifies INT32 format.
Definition: nvdsinfer.h:81
nvdsinfer_context.h
Copyright (c) 2018-2020, NVIDIA CORPORATION.
INT8
@ INT8
Specifies INT8 format.
Definition: nvdsinfer.h:79
nvdsinfer::convertFullDims
void convertFullDims(const nvinfer1::Dims &fullDims, NvDsInferBatchDims &batchDims)
Definition: nvdsinfer_func_utils.h:242
nvdsinfer::UniquePtrWDestroy::UniquePtrWDestroy
UniquePtrWDestroy(T *t=nullptr)
Definition: nvdsinfer_func_utils.h:119
NvDsInferDimsCHW
Holds the dimensions of a three-dimensional layer.
Definition: nvdsinfer.h:59
nvdsinfer::UniquePtrWDestroy
Definition: nvdsinfer_func_utils.h:116
nvdsinfer_logger.h
nvdsinfer::operator==
bool operator==(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::DlLibHandle
Definition: nvdsinfer_func_utils.h:137
nvdsinfer::operator<=
bool operator<=(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::dataType2Str
std::string dataType2Str(const nvinfer1::DataType type)
nvdsinfer::DlLibHandle::symbol
FuncPtr symbol(const std::string &func)
Definition: nvdsinfer_func_utils.h:156
nvdsinfer::file_accessible
bool file_accessible(const char *path)
Definition: nvdsinfer_func_utils.h:95
nvdsinfer::DlLibHandle::~DlLibHandle
~DlLibHandle()
nvdsinfer::DlLibHandle::symbol
FuncPtr symbol(const char *func)
Definition: nvdsinfer_func_utils.h:147
nvdsinfer.h
nvdsinfer::str2DeviceType
nvinfer1::DeviceType str2DeviceType(const std::string &deviceType)
nvdsinfer::string_empty
bool string_empty(const char *str)
Definition: nvdsinfer_func_utils.h:90
nvdsinfer::safeStr
const char * safeStr(const char *str)
Definition: nvdsinfer_func_utils.h:80
nvdsinfer::trt2DsDims
NvDsInferDims trt2DsDims(const nvinfer1::Dims &dims)
nvdsinfer::SplitFullDims
void SplitFullDims(const nvinfer1::Dims &fullDims, NvDsInferDims &dims, int &batch)
nvdsinfer::GuardQueue::isEmpty
bool isEmpty()
Definition: nvdsinfer_func_utils.h:187
nvdsinfer::getElementSize
uint32_t getElementSize(NvDsInferDataType t)
Get the size of the element from the data type.
Definition: nvdsinfer_func_utils.h:208
nvdsinfer::batchDims2Str
std::string batchDims2Str(const NvDsInferBatchDims &d)
nvdsinfer::validateIOTensorNames
bool validateIOTensorNames(const BuildParams &params, const nvinfer1::INetworkDefinition &network)
INFER_EXPORT_API::fullDims
InferDims fullDims(int batchSize, const InferDims &in)
Extend the dimensions to include batch size.
nvdsinfer::isValidDeviceType
bool isValidDeviceType(const std::string &fmt)
nvdsinfer::DlLibHandle::isValid
bool isValid() const
Definition: nvdsinfer_func_utils.h:143
nvdsinfer::isValidOutputDataType
bool isValidOutputDataType(const std::string &dataType)