-
Notifications
You must be signed in to change notification settings - Fork 2
/
nvdsinfer_func_utils.h
292 lines (245 loc) · 9.18 KB
/
nvdsinfer_func_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA Corporation is strictly prohibited.
*
*/
#ifndef __NVDSINFER_FUNC_UTILS_H__
#define __NVDSINFER_FUNC_UTILS_H__
#include <dlfcn.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <cassert>
#include <condition_variable>
#include <mutex>
#include <sstream>
#include <string>
#include <NvInferRuntime.h>
#include <nvdsinfer.h>
#include <nvdsinfer_context.h>
/* This file provides APIs/macros for some frequently used functionality. */
#define DISABLE_CLASS_COPY(NoCopyClass) \
NoCopyClass(const NoCopyClass&) = delete; \
void operator=(const NoCopyClass&) = delete
#define SIMPLE_MOVE_COPY(Cls) \
Cls& operator=(Cls&& o) { \
move_copy(std::move(o)); \
return *this; \
} \
Cls(Cls&& o) { move_copy(std::move(o)); }
#if defined(NDEBUG)
#define INFER_LOG_FORMAT_(fmt) fmt
#else
#define INFER_LOG_FORMAT_(fmt) "%s:%d " fmt, __FILE__, __LINE__
#endif
#define dsInferError(fmt, ...) \
do { \
dsInferLogPrint__( \
NVDSINFER_LOG_ERROR, INFER_LOG_FORMAT_(fmt), ##__VA_ARGS__); \
} while (0)
#define dsInferWarning(fmt, ...) \
do { \
dsInferLogPrint__( \
NVDSINFER_LOG_WARNING, INFER_LOG_FORMAT_(fmt), ##__VA_ARGS__); \
} while (0)
#define dsInferInfo(fmt, ...) \
do { \
dsInferLogPrint__( \
NVDSINFER_LOG_INFO, INFER_LOG_FORMAT_(fmt), ##__VA_ARGS__); \
} while (0)
#define dsInferDebug(fmt, ...) \
do { \
dsInferLogPrint__( \
NVDSINFER_LOG_DEBUG, INFER_LOG_FORMAT_(fmt), ##__VA_ARGS__); \
} while (0)
#define CHECK_NVINFER_ERROR(err, action, fmt, ...) \
do { \
NvDsInferStatus ifStatus = (err); \
if (ifStatus != NVDSINFER_SUCCESS) { \
auto errStr = NvDsInferStatus2Str(ifStatus); \
dsInferError(fmt ", nvinfer error:%s", ##__VA_ARGS__, errStr); \
action; \
} \
} while (0)
#define RETURN_NVINFER_ERROR(err, fmt, ...) \
CHECK_NVINFER_ERROR(err, return ifStatus, fmt, ##__VA_ARGS__)
#define CHECK_CUDA_ERR_W_ACTION(err, action, fmt, ...) \
do { \
cudaError_t errnum = (err); \
if (errnum != cudaSuccess) { \
dsInferError(fmt ", cuda err_no:%d, err_str:%s", ##__VA_ARGS__, \
(int)errnum, cudaGetErrorName(errnum)); \
action; \
} \
} while (0)
#define CHECK_CUDA_ERR_NO_ACTION(err, fmt, ...) \
CHECK_CUDA_ERR_W_ACTION(err, , fmt, ##__VA_ARGS__)
#define RETURN_CUDA_ERR(err, fmt, ...) \
CHECK_CUDA_ERR_W_ACTION( \
err, return NVDSINFER_CUDA_ERROR, fmt, ##__VA_ARGS__)
#define READ_SYMBOL(lib, func_name) \
lib->symbol<decltype(&func_name)>(#func_name)
namespace nvdsinfer {
extern std::unique_ptr<nvinfer1::ILogger> gTrtLogger;
void dsInferLogPrint__(NvDsInferLogLevel level, const char* fmt, ...);
inline const char* safeStr(const char* str)
{
return !str ? "" : str;
}
inline const char* safeStr(const std::string& str)
{
return str.c_str();
}
inline bool string_empty(const char* str)
{
return !str || strlen(str) == 0;
}
inline bool file_accessible(const char* path)
{
assert(path);
return (access(path, F_OK) != -1);
}
inline bool file_accessible(const std::string& path)
{
return (!path.empty()) && file_accessible(path.c_str());
}
std::string dims2Str(const nvinfer1::Dims& d);
std::string dims2Str(const NvDsInferDims& d);
std::string batchDims2Str(const NvDsInferBatchDims& d);
std::string dataType2Str(const nvinfer1::DataType type);
std::string dataType2Str(const NvDsInferDataType type);
std::string networkMode2Str(const NvDsInferNetworkMode type);
/* Custom unique_ptr subclass with deleter functions for TensorRT objects. */
template <class T>
class UniquePtrWDestroy : public std::unique_ptr<T, void (*)(T*)>
{
public:
UniquePtrWDestroy(T* t = nullptr)
: std::unique_ptr<T, void (*)(T*)>(t, [](T* t) {
if (t)
t->destroy();
}) {}
};
template <class T>
class SharedPtrWDestroy : public std::shared_ptr<T>
{
public:
SharedPtrWDestroy(T* t = nullptr)
: std::shared_ptr<T>(t, [](T* t) {
if (t)
t->destroy();
}) {}
};
class DlLibHandle
{
public:
DlLibHandle(const std::string& path, int mode = RTLD_LAZY);
~DlLibHandle();
bool isValid() const { return m_LibHandle; }
const std::string& getPath() const { return m_LibPath; }
template <typename FuncPtr>
FuncPtr symbol(const char* func)
{
assert(!string_empty(func));
if (!m_LibHandle)
return nullptr;
return (FuncPtr)dlsym(m_LibHandle, func);
}
template <typename FuncPtr>
FuncPtr symbol(const std::string& func)
{
return symbol<FuncPtr>(func.c_str());
}
private:
void* m_LibHandle{nullptr};
const std::string m_LibPath;
};
template <typename Container>
class GuardQueue
{
public:
typedef typename Container::value_type T;
void push(const T& data)
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Queue.push_back(data);
m_Cond.notify_one();
}
T pop()
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Cond.wait(lock, [this]() { return !m_Queue.empty(); });
assert(!m_Queue.empty());
T ret = std::move(*m_Queue.begin());
m_Queue.erase(m_Queue.begin());
return ret;
}
void clear()
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Queue.clear();
}
private:
std::mutex m_Mutex;
std::condition_variable m_Cond;
Container m_Queue;
};
/**
* Get the size of the element from the data type
*/
inline uint32_t
getElementSize(NvDsInferDataType t)
{
switch (t)
{
case INT32:
case FLOAT:
return 4;
case HALF:
return 2;
case INT8:
return 1;
default:
dsInferError(
"Failed to get element size on Unknown datatype:%d", (int)t);
return 0;
}
}
/* Convert between TRT's nvinfer1::Dims representation and DeepStream's
* NvDsInferDimsCHW/NvDsInferDims representation. */
nvinfer1::Dims ds2TrtDims(const NvDsInferDimsCHW& dims);
nvinfer1::Dims ds2TrtDims(const NvDsInferDims& dims);
NvDsInferDims trt2DsDims(const nvinfer1::Dims& dims);
/* Add batch size to provided dims to get full dims as nvinfer1::Dims. */
nvinfer1::Dims CombineDimsBatch(const NvDsInferDims& dims, int batch);
/* Split full dims provided in the form of nvinfer1::Dims into batch size and
* layer dims. */
void SplitFullDims(
const nvinfer1::Dims& fullDims, NvDsInferDims& dims, int& batch);
/* Convert from TRT's nvinfer1::Dims representation to DeepStream's
* NvDsInferBatchDims representation. */
inline void
convertFullDims(const nvinfer1::Dims& fullDims, NvDsInferBatchDims& batchDims)
{
SplitFullDims(fullDims, batchDims.dims, batchDims.batchSize);
}
void normalizeDims(NvDsInferDims& dims);
bool hasWildcard(const nvinfer1::Dims& dims);
bool hasWildcard(const NvDsInferDims& dims);
/* Equality / inequality operators implementation for nvinfer1::Dims */
bool operator<=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
bool operator>(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
bool operator==(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
bool operator!=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
/* Equality / inequality operators implementation for NvDsInferDims */
bool operator<=(const NvDsInferDims& a, const NvDsInferDims& b);
bool operator>(const NvDsInferDims& a, const NvDsInferDims& b);
bool operator==(const NvDsInferDims& a, const NvDsInferDims& b);
bool operator!=(const NvDsInferDims& a, const NvDsInferDims& b);
} // namespace nvdsinfer
#endif