-
Notifications
You must be signed in to change notification settings - Fork 3
/
gstnvinfer_impl.cpp
453 lines (395 loc) · 13.7 KB
/
gstnvinfer_impl.cpp
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* 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.
*
*/
#include <vector>
#include <sstream>
#include <cassert>
#include "gstnvinfer_impl.h"
#include "gstnvinfer.h"
#include "gstnvinfer_property_parser.h"
GST_DEBUG_CATEGORY_EXTERN (gst_nvinfer_debug);
#define GST_CAT_DEFAULT gst_nvinfer_debug
using namespace nvdsinfer;
namespace gstnvinfer
{
void
LockGMutex::lock ()
{
assert (!locked);
if (!locked)
g_mutex_lock (&m);
locked = true;
}
void
LockGMutex::unlock ()
{
assert (locked);
if (locked)
g_mutex_unlock (&m);
locked = false;
}
void
LockGMutex::wait (GCond & cond)
{
assert (locked);
if (locked)
g_cond_wait (&cond, &m);
}
DsNvInferImpl::DsNvInferImpl (GstNvInfer * infer)
: m_InitParams (new NvDsInferContextInitParams),
m_GstInfer (infer)
{
NvDsInferContext_ResetInitParams (m_InitParams.get ());
}
DsNvInferImpl::~DsNvInferImpl ()
{
if (m_InitParams) {
delete[]m_InitParams->perClassDetectionParams;
g_strfreev (m_InitParams->outputLayerNames);
g_strfreev (m_InitParams->outputIOFormats);
g_strfreev (m_InitParams->layerDevicePrecisions);
}
}
/** Initialize the ModelLoadThread instance - start the model load thread. */
DsNvInferImpl::ModelLoadThread::ModelLoadThread (DsNvInferImpl & impl)
: m_Impl (impl)
{
m_Thread = std::thread (&DsNvInferImpl::ModelLoadThread::Run, this);
}
/** Destroy the ModelLoadThread instance - stop and join the model load thread. */
DsNvInferImpl::ModelLoadThread::~ModelLoadThread ()
{
if (m_Thread.joinable ()) {
//push stop signal
m_PendingModels.push (ModelItem ("", MODEL_LOAD_STOP));
m_Thread.join ();
}
m_PendingModels.clear ();
}
/** Callable function for the thread. */
void
DsNvInferImpl::ModelLoadThread::Run ()
{
while (true) {
/* Pop a pending model update item. This is a blocking call. */
ModelItem item = m_PendingModels.pop ();
std::string file;
ModelLoadType type;
std::tie (file, type) = item;
// receive thread stop signal
if (type == MODEL_LOAD_STOP)
break;
if (file.empty ())
continue;
// Load the new model
m_Impl.loadModel (file, type);
}
}
NvDsInferStatus
DsNvInferImpl::start ()
{
m_ModelLoadThread.reset (new ModelLoadThread (*this));
return NVDSINFER_SUCCESS;
}
void
DsNvInferImpl::stop ()
{
m_ModelLoadThread.reset ();
m_InferCtx.reset ();
}
/* Queue a new model update. */
bool
DsNvInferImpl::triggerNewModel (const std::string & modelPath,
ModelLoadType loadType)
{
if (!m_ModelLoadThread.get ()) {
return false;
}
m_ModelLoadThread->queueModel (modelPath, loadType);
return true;
}
/* Load the new model - Try to create a new NvDsInferContext instance with new
* parameters and store the instance in m_NextContextReplacement. */
void
DsNvInferImpl::loadModel (const std::string & modelPath, ModelLoadType loadType)
{
NvDsInferContextInitParams curParam;
bool needUpdate = true;
/* Check if a model is loaded but not yet being used for inferencing. */
{
LockGMutex lock (m_GstInfer->process_lock);
curParam = *m_InitParams;
needUpdate = !m_NextContextReplacement;
}
if (!needUpdate) {
notifyLoadModelStatus (ModelStatus {
NVDSINFER_UNKNOWN_ERROR, modelPath, "Trying to update model too frequently"}
);
return;
}
NvDsInferContextInitParamsPtr newParamsPtr (new NvDsInferContextInitParams);
NvDsInferContextInitParams & newParams = *newParamsPtr;
/* Initialize the NvDsInferContextInitParams struct with the new model
* config. */
if (!initNewInferModelParams (newParams, modelPath, loadType, curParam)) {
notifyLoadModelStatus (ModelStatus {
NVDSINFER_CONFIG_FAILED, modelPath,
"Initialization of new model params failed"}
);
return;
}
NvDsInferContextHandle newContext;
/* Create new NvDsInferContext instance. */
NvDsInferStatus err =
createNvDsInferContext (&newContext, newParams, m_GstInfer,
gst_nvinfer_logger);
if (err != NVDSINFER_SUCCESS) {
/* Notify application if the model load failed. */
notifyLoadModelStatus (ModelStatus {
err, modelPath, "Creation new model context failed"}
);
return;
}
NvDsInferContextPtr newCtxPtr (newContext);
assert (newCtxPtr.get ());
/* Check that the input of the newly loaded model parameter is compatible
* with gst-nvinfer instance. */
if (!isNewContextValid (*newCtxPtr, newParams)) {
notifyLoadModelStatus (ModelStatus {
NVDSINFER_CONFIG_FAILED, modelPath,
"New model's settings doesn't match current model"}
);
return;
}
/* Store the new NvDsInferContext instance so that it can be used for
* inferencing after ensuring synchronization with existing buffers
* being processed. */
if (!triggerContextReplace (newCtxPtr, std::move (newParamsPtr), modelPath)) {
notifyLoadModelStatus (ModelStatus {
NVDSINFER_UNKNOWN_ERROR, modelPath, "trigger new model replace failed"}
);
return;
}
}
/* Initialize NvDsInferContextInitParams with a combination of current
* init params and new init params for the new model. */
bool
DsNvInferImpl::initNewInferModelParams (NvDsInferContextInitParams & newParams,
const std::string & newModelPath, ModelLoadType loadType,
const NvDsInferContextInitParams & oldParams)
{
NvDsInferContext_ResetInitParams (&newParams);
assert (!newModelPath.empty ());
switch (loadType) {
case MODEL_LOAD_FROM_CONFIG:
if (!gst_nvinfer_parse_context_params (&newParams, newModelPath.c_str ())) {
GST_WARNING_OBJECT (m_GstInfer,
"[UID %d]: parse new model config file: %s failed.",
m_GstInfer->unique_id, newModelPath.c_str ());
return false;
}
break;
case MODEL_LOAD_FROM_ENGINE:
strncpy (newParams.modelEngineFilePath, newModelPath.c_str (),
sizeof (newParams.modelEngineFilePath));
newParams.networkMode = oldParams.networkMode;
memcpy (newParams.meanImageFilePath, oldParams.meanImageFilePath,
sizeof (newParams.meanImageFilePath));
newParams.networkScaleFactor = oldParams.networkScaleFactor;
newParams.networkInputFormat = oldParams.networkInputFormat;
newParams.numOffsets = oldParams.numOffsets;
memcpy (newParams.offsets, oldParams.offsets, sizeof (newParams.offsets));
break;
default:
GST_WARNING_OBJECT (m_GstInfer,
"[UID %d]: unsupported model load type (:%s), internal error.",
m_GstInfer->unique_id, newModelPath.c_str ());
return false;
}
newParams.maxBatchSize = oldParams.maxBatchSize;
newParams.gpuID = oldParams.gpuID;
newParams.uniqueID = oldParams.uniqueID;
newParams.networkType = oldParams.networkType;
newParams.useDBScan = oldParams.useDBScan;
newParams.classifierThreshold = oldParams.classifierThreshold;
newParams.segmentationThreshold = oldParams.segmentationThreshold;
newParams.copyInputToHostBuffers = oldParams.copyInputToHostBuffers;
newParams.outputBufferPoolSize = oldParams.outputBufferPoolSize;
if (string_empty (newParams.labelsFilePath)
&& !string_empty (oldParams.labelsFilePath)) {
strncpy (newParams.labelsFilePath, oldParams.labelsFilePath,
sizeof (newParams.labelsFilePath));
}
if (oldParams.numDetectedClasses) {
newParams.numDetectedClasses = oldParams.numDetectedClasses;
delete[]newParams.perClassDetectionParams;
newParams.perClassDetectionParams =
new NvDsInferDetectionParams[oldParams.numDetectedClasses];
memcpy (newParams.perClassDetectionParams,
oldParams.perClassDetectionParams,
sizeof (newParams.perClassDetectionParams[0]) *
newParams.numDetectedClasses);
}
return true;
}
/* Check that the input of the newly loaded model is compatible with
* gst-nvinfer instance. */
bool
DsNvInferImpl::isNewContextValid (INvDsInferContext & newCtx,
NvDsInferContextInitParams & newParam)
{
if (newParam.maxBatchSize < m_GstInfer->max_batch_size) {
GST_WARNING_OBJECT (m_GstInfer,
"[UID %d]: New model batch-size[in config] (%d) is smaller then gst-nvinfer's"
" configured batch-size (%d).", m_GstInfer->unique_id,
newParam.maxBatchSize, m_GstInfer->max_batch_size);
return false;
}
NvDsInferNetworkInfo networkInfo;
newCtx.getNetworkInfo (networkInfo);
if (m_GstInfer->network_width != (gint) networkInfo.width ||
m_GstInfer->network_height != (gint) networkInfo.height) {
GST_WARNING_OBJECT (m_GstInfer,
"[UID %d]: New model input resolution (%dx%d) is not compatible with "
"gst-nvinfer's current resolution (%dx%d).", m_GstInfer->unique_id,
networkInfo.width, networkInfo.height, m_GstInfer->network_width,
m_GstInfer->network_height);
return false;
}
return true;
}
/* Notify the app of model load status using GObject signal. */
void
DsNvInferImpl::notifyLoadModelStatus (const ModelStatus & res)
{
assert (!res.cfg_file.empty ());
if (res.status == NVDSINFER_SUCCESS) {
GST_INFO_OBJECT (m_GstInfer, "[UID %d]: Load new model:%s sucessfully",
m_GstInfer->unique_id, res.cfg_file.c_str ());
} else {
GST_ELEMENT_WARNING (m_GstInfer, LIBRARY, SETTINGS,
("[UID %d]: Load new model:%s failed, reason: %s",
m_GstInfer->unique_id, res.cfg_file.c_str (),
(res.message.empty ()? "unknown" : res.message.c_str ())),
(nullptr));
}
g_signal_emit (m_GstInfer, gst_nvinfer_signals[SIGNAL_MODEL_UPDATED], 0,
(int) res.status, res.cfg_file.c_str ());
}
bool
DsNvInferImpl::triggerContextReplace (NvDsInferContextPtr ctx,
NvDsInferContextInitParamsPtr params, const std::string & path)
{
std::string lastConfig;
LockGMutex lock (m_GstInfer->process_lock);
m_NextContextReplacement.reset (new ContextReplacementPtr::element_type (ctx,
std::move (params), path));
return true;
}
DsNvInferImpl::ContextReplacementPtr
DsNvInferImpl::getNextReplacementUnlock ()
{
if (!m_NextContextReplacement.get ()) {
return nullptr;
}
ContextReplacementPtr next;
// get next replacement, meanwhile empty m_NextContextReplacement
next.swap (m_NextContextReplacement);
return next;
}
/* Wait till all the buffers with gst-nvinfer are inferred/post-processed and
* pushed downstream. */
NvDsInferStatus
DsNvInferImpl::flushDataUnlock (LockGMutex & lock)
{
GstNvInferBatch *batch = new GstNvInferBatch;
batch->event_marker = TRUE;
/* Push the event marker batch to ensure all data processed. */
g_queue_push_tail (m_GstInfer->input_queue, batch);
g_cond_broadcast (&m_GstInfer->process_cond);
/* Wait till all the items in the two queues are handled. */
while (!g_queue_is_empty (m_GstInfer->input_queue)) {
lock.wait (m_GstInfer->process_cond);
}
while (!g_queue_is_empty (m_GstInfer->process_queue)) {
lock.wait (m_GstInfer->process_cond);
}
g_cond_broadcast (&m_GstInfer->process_cond);
for (auto & si:*(m_GstInfer->source_info)) {
si.second.object_history_map.clear ();
}
return NVDSINFER_SUCCESS;
}
/* Actually replace the current NvDsInferContext used for inferencing with
* the newly created NvDsInferContext. Also query updated information about
* the model. */
NvDsInferStatus
DsNvInferImpl::resetContextUnlock (NvDsInferContextPtr ctx,
NvDsInferContextInitParamsPtr params, const std::string & path)
{
assert (ctx.get () && params.get () && !path.empty ());
m_InferCtx = ctx;
m_InitParams = std::move (params);
GstNvInfer *nvinfer = m_GstInfer;
g_free (nvinfer->config_file_path);
nvinfer->config_file_path = g_strdup (path.c_str ());
/* Get the network resolution. */
ctx->getNetworkInfo (nvinfer->network_info);
nvinfer->network_width = nvinfer->network_info.width;
nvinfer->network_height = nvinfer->network_info.height;
/* Get information on all the bound layers. */
ctx->fillLayersInfo (*nvinfer->layers_info);
nvinfer->output_layers_info->clear ();
for (auto & layer:*(nvinfer->layers_info)) {
if (!layer.isInput)
nvinfer->output_layers_info->push_back (layer);
}
return NVDSINFER_SUCCESS;
}
/* Check if a new model has been loaded. If yes, ensure synchronization by
* waiting till all the queued buffers are processed and then do the actual
* model replacement. */
NvDsInferStatus
DsNvInferImpl::ensureReplaceNextContext ()
{
NvDsInferStatus err = NVDSINFER_SUCCESS;
LockGMutex lock (m_GstInfer->process_lock);
/* Get any newly loaded model. */
ContextReplacementPtr nextReplacement = getNextReplacementUnlock ();
if (!nextReplacement.get ())
return NVDSINFER_SUCCESS;
NvDsInferContextPtr nextCtx = std::get <0> (*nextReplacement);
NvDsInferContextInitParamsPtr nextParams;
nextParams.swap (std::get <1> (*nextReplacement));
assert (nextParams.get ());
const std::string path = std::get <2> (*nextReplacement);
/* Wait for current processing to finish. */
err = flushDataUnlock (lock);
if (err != NVDSINFER_SUCCESS) {
notifyLoadModelStatus (ModelStatus {
err, path, "Model update failed while flushing data"}
);
return err;
}
/* Replace the model to be used for inferencing. */
err = resetContextUnlock (nextCtx, std::move (nextParams), path);
if (err != NVDSINFER_SUCCESS) {
notifyLoadModelStatus (ModelStatus {
err, path, "Model update failed while replacing the current context"}
);
return err;
}
/* Notify application of successful model load. */
notifyLoadModelStatus (ModelStatus {
NVDSINFER_SUCCESS, path, "New Model updated succefully"}
);
return NVDSINFER_SUCCESS;
}
}