forked from NVIDIA/TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 3
/
batchedNMSLandmarkConfPlugin.cpp
935 lines (846 loc) · 31.8 KB
/
batchedNMSLandmarkConfPlugin.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "batchedNMSLandmarkConfPlugin.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace nvinfer1;
using nvinfer1::plugin::BatchedNMSLandmarkConfBasePluginCreator;
using nvinfer1::plugin::BatchedNMSLandmarkConfDynamicPlugin;
using nvinfer1::plugin::BatchedNMSLandmarkConfDynamicPluginCreator;
using nvinfer1::plugin::BatchedNMSLandmarkConfPlugin;
using nvinfer1::plugin::BatchedNMSLandmarkConfPluginCreator;
using nvinfer1::plugin::NMSParameters;
#define NVBUG_3321606_WAR 1
namespace
{
const char* NMS_PLUGIN_VERSION{"1"};
const char* NMS_PLUGIN_NAMES[] = {"BatchedNMSLandmarkConf_TRT", "BatchedNMSLandmarkConfDynamic_TRT"};
} // namespace
// namespace nvinfer1 {
// namespace plugin {
// template <>
// void write<NMSParameters>(char *&buffer, const NMSParameters &val)
// {
// auto *param = reinterpret_cast<NMSParameters *>(buffer);
// std::memset(param, 0, sizeof(NMSParameters));
// param->shareLocation = val.shareLocation;
// param->backgroundLabelId = val.backgroundLabelId;
// param->numClasses = val.numClasses;
// param->topK = val.topK;
// param->keepTopK = val.keepTopK;
// param->scoreThreshold = val.scoreThreshold;
// param->iouThreshold = val.iouThreshold;
// param->isNormalized = val.isNormalized;
// buffer += sizeof(NMSParameters);
// }
// } // namespace plugin
// } // namespace nvinfer1
PluginFieldCollection BatchedNMSLandmarkConfBasePluginCreator::mFC{};
std::vector<PluginField> BatchedNMSLandmarkConfBasePluginCreator::mPluginAttributes;
static inline pluginStatus_t checkParams(const NMSParameters& param)
{
// NMS plugin supports maximum thread blocksize of 512 and upto 8 blocks at once.
constexpr int32_t maxTopK{512 * 8};
if (param.topK > maxTopK)
{
gLogError << "Invalid parameter: NMS topK (" << param.topK << ") exceeds limit (" << maxTopK << ")"
<< std::endl;
return STATUS_BAD_PARAM;
}
return STATUS_SUCCESS;
}
BatchedNMSLandmarkConfPlugin::BatchedNMSLandmarkConfPlugin(NMSParameters params)
: param(params)
{
mPluginStatus = checkParams(param);
}
BatchedNMSLandmarkConfPlugin::BatchedNMSLandmarkConfPlugin(const void* data, size_t length)
{
const char *d = reinterpret_cast<const char*>(data), *a = d;
param = read<NMSParameters>(d);
boxesSize = read<int>(d);
scoresSize = read<int>(d);
landmarksSize = read<int>(d);
numPriors = read<int>(d);
mClipBoxes = read<bool>(d);
mPrecision = read<DataType>(d);
mScoreBits = read<int32_t>(d);
mCaffeSemantics = read<bool>(d);
ASSERT(d == a + length);
mPluginStatus = checkParams(param);
}
BatchedNMSLandmarkConfDynamicPlugin::BatchedNMSLandmarkConfDynamicPlugin(NMSParameters params)
: param(params)
{
mPluginStatus = checkParams(param);
}
BatchedNMSLandmarkConfDynamicPlugin::BatchedNMSLandmarkConfDynamicPlugin(const void* data, size_t length)
{
const char *d = reinterpret_cast<const char*>(data), *a = d;
param = read<NMSParameters>(d);
boxesSize = read<int>(d);
scoresSize = read<int>(d);
landmarksSize = read<int>(d);
numPriors = read<int>(d);
mClipBoxes = read<bool>(d);
mPrecision = read<DataType>(d);
mScoreBits = read<int32_t>(d);
mCaffeSemantics = read<bool>(d);
ASSERT(d == a + length);
mPluginStatus = checkParams(param);
}
int BatchedNMSLandmarkConfPlugin::getNbOutputs() const noexcept
{
return 5;
}
int BatchedNMSLandmarkConfDynamicPlugin::getNbOutputs() const noexcept
{
return 5;
}
int BatchedNMSLandmarkConfPlugin::initialize() noexcept
{
return STATUS_SUCCESS;
}
int BatchedNMSLandmarkConfDynamicPlugin::initialize() noexcept
{
return STATUS_SUCCESS;
}
void BatchedNMSLandmarkConfPlugin::terminate() noexcept {}
void BatchedNMSLandmarkConfDynamicPlugin::terminate() noexcept {}
Dims BatchedNMSLandmarkConfPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) noexcept
{
try
{
ASSERT(nbInputDims == 3);
ASSERT(index >= 0 && index < this->getNbOutputs());
ASSERT(inputs[0].nbDims == 3);
ASSERT(inputs[1].nbDims == 2 || (inputs[1].nbDims == 3 && inputs[1].d[2] == 1));
ASSERT(inputs[2].nbDims == 3);
// boxesSize: number of box coordinates for one sample
boxesSize = inputs[0].d[0] * inputs[0].d[1] * inputs[0].d[2];
// scoresSize: number of scores for one sample
scoresSize = inputs[1].d[0] * inputs[1].d[1];
// landmarksSize: number of landmark coordinates for one sample
landmarksSize = inputs[2].d[0] * inputs[2].d[1] * inputs[2].d[2];
// num_detections
if (index == 0)
{
Dims dim0{};
dim0.nbDims = 0;
return dim0;
}
// nmsed_boxes
if (index == 1)
{
return DimsHW(param.keepTopK, 4);
}
// nmsed_landmarks
if (index == 4)
{
return DimsHW(param.keepTopK, 11);
}
// nmsed_scores or nmsed_classes
Dims dim1{};
dim1.nbDims = 1;
dim1.d[0] = param.keepTopK;
return dim1;
}
catch (const std::exception& e)
{
caughtError(e);
}
return Dims{};
}
DimsExprs BatchedNMSLandmarkConfDynamicPlugin::getOutputDimensions(
int outputIndex, const DimsExprs* inputs, int nbInputs, IExprBuilder& exprBuilder) noexcept
{
try
{
ASSERT(nbInputs == 3);
ASSERT(outputIndex >= 0 && outputIndex < this->getNbOutputs());
// Shape of boxes input should be
// Constant shape: [batch_size, num_boxes, num_classes, 4] or [batch_size, num_boxes, 1, 4]
// shareLocation == 0 or 1
// or
// Dynamic shape: some dimension values may be -1
ASSERT(inputs[0].nbDims == 4);
// Shape of scores input should be
// Constant shape: [batch_size, num_boxes, num_classes] or [batch_size, num_boxes,
// num_classes, 1] or Dynamic shape: some dimension values may be -1
ASSERT(inputs[1].nbDims == 3 || inputs[1].nbDims == 4);
// Shape of landmarks input should be
// Constant shape: [batch_size, num_boxes, num_classes, 11] or [batch_size, num_boxes, 1,
// 11]
// shareLocation == 0 or 1
// or
// Dynamic shape: some dimension values may be -1
ASSERT(inputs[2].nbDims == 4);
if (inputs[0].d[0]->isConstant() && inputs[0].d[1]->isConstant() && inputs[0].d[2]->isConstant()
&& inputs[0].d[3]->isConstant())
{
boxesSize = exprBuilder
.operation(DimensionOperation::kPROD,
*exprBuilder.operation(DimensionOperation::kPROD, *inputs[0].d[1], *inputs[0].d[2]),
*inputs[0].d[3])
->getConstantValue();
}
if (inputs[1].d[0]->isConstant() && inputs[1].d[1]->isConstant() && inputs[1].d[2]->isConstant())
{
scoresSize = exprBuilder.operation(DimensionOperation::kPROD, *inputs[1].d[1], *inputs[1].d[2])
->getConstantValue();
}
if (inputs[2].d[0]->isConstant() && inputs[2].d[1]->isConstant() && inputs[2].d[2]->isConstant()
&& inputs[2].d[3]->isConstant())
{
landmarksSize = exprBuilder
.operation(DimensionOperation::kPROD,
*exprBuilder.operation(DimensionOperation::kPROD, *inputs[2].d[1], *inputs[2].d[2]),
*inputs[2].d[3])
->getConstantValue();
}
DimsExprs out_dim;
// num_detections
if (outputIndex == 0)
{
out_dim.nbDims = 2;
out_dim.d[0] = inputs[0].d[0];
out_dim.d[1] = exprBuilder.constant(1);
}
// nmsed_boxes
else if (outputIndex == 1)
{
out_dim.nbDims = 3;
out_dim.d[0] = inputs[0].d[0];
out_dim.d[1] = exprBuilder.constant(param.keepTopK);
out_dim.d[2] = exprBuilder.constant(4);
}
// nmsed_scores
else if (outputIndex == 2)
{
out_dim.nbDims = 2;
out_dim.d[0] = inputs[0].d[0];
out_dim.d[1] = exprBuilder.constant(param.keepTopK);
}
// nmsed_landmarks
else if (outputIndex == 4)
{
out_dim.nbDims = 3;
out_dim.d[0] = inputs[0].d[0];
out_dim.d[1] = exprBuilder.constant(param.keepTopK);
out_dim.d[2] = exprBuilder.constant(11);
}
// nmsed_classes
else
{
out_dim.nbDims = 2;
out_dim.d[0] = inputs[0].d[0];
out_dim.d[1] = exprBuilder.constant(param.keepTopK);
}
return out_dim;
}
catch (const std::exception& e)
{
caughtError(e);
}
return DimsExprs{};
}
size_t BatchedNMSLandmarkConfPlugin::getWorkspaceSize(int maxBatchSize) const noexcept
{
return detectionInferenceLandmarkWorkspaceSize(param.shareLocation, maxBatchSize, boxesSize, scoresSize, landmarksSize,
param.numClasses, numPriors, param.topK, mPrecision, mPrecision);
}
size_t BatchedNMSLandmarkConfDynamicPlugin::getWorkspaceSize(
const PluginTensorDesc* inputs, int nbInputs, const PluginTensorDesc* outputs, int nbOutputs) const noexcept
{
return detectionInferenceLandmarkWorkspaceSize(param.shareLocation, inputs[0].dims.d[0], boxesSize, scoresSize,
landmarksSize, param.numClasses, numPriors, param.topK, mPrecision, mPrecision);
}
int BatchedNMSLandmarkConfPlugin::enqueue(
int32_t batchSize, void const* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
try
{
const void* const locData = inputs[0];
const void* const confData = inputs[1];
const void* const landData = inputs[2];
if (mPluginStatus != STATUS_SUCCESS)
{
return -1;
}
void* keepCount = outputs[0];
void* nmsedBoxes = outputs[1];
void* nmsedScores = outputs[2];
void* nmsedClasses = outputs[3];
void* nmsedLandmarks = outputs[4];
pluginStatus_t status = nmsInferenceLandmarkConfNew(stream, batchSize, boxesSize, scoresSize, landmarksSize,
param.shareLocation, param.backgroundLabelId, numPriors, param.numClasses, param.topK, param.keepTopK,
param.scoreThreshold, param.iouThreshold, mPrecision, locData, mPrecision, confData, landData, keepCount,
nmsedBoxes, nmsedScores, nmsedClasses, nmsedLandmarks, workspace, param.isNormalized, false, mClipBoxes,
mScoreBits, mCaffeSemantics);
return status == STATUS_SUCCESS ? 0 : -1;
}
catch (const std::exception& e)
{
caughtError(e);
}
return -1;
}
int BatchedNMSLandmarkConfDynamicPlugin::enqueue(const PluginTensorDesc* inputDesc, const PluginTensorDesc* outputDesc,
const void* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
try
{
const void* const locData = inputs[0];
const void* const confData = inputs[1];
const void* const landData = inputs[2];
if (mPluginStatus != STATUS_SUCCESS)
{
return -1;
}
void* keepCount = outputs[0];
void* nmsedBoxes = outputs[1];
void* nmsedScores = outputs[2];
void* nmsedClasses = outputs[3];
void* nmsedLandmarks = outputs[4];
pluginStatus_t status = nmsInferenceLandmarkConfNew(stream, inputDesc[0].dims.d[0], boxesSize, scoresSize, landmarksSize,
param.shareLocation, param.backgroundLabelId, numPriors, param.numClasses, param.topK, param.keepTopK,
param.scoreThreshold, param.iouThreshold, mPrecision, locData, mPrecision, confData, landData, keepCount,
nmsedBoxes, nmsedScores, nmsedClasses, nmsedLandmarks, workspace, param.isNormalized, false, mClipBoxes,
mScoreBits, mCaffeSemantics);
return status;
}
catch (const std::exception& e)
{
caughtError(e);
}
return -1;
}
size_t BatchedNMSLandmarkConfPlugin::getSerializationSize() const noexcept
{
// NMSParameters, boxesSize,scoresSize,landmarksSize,numPriors
return sizeof(NMSParameters) + sizeof(int) * 4 + sizeof(bool) * 2 + sizeof(DataType) + sizeof(int32_t);
}
void BatchedNMSLandmarkConfPlugin::serialize(void* buffer) const noexcept
{
char *d = reinterpret_cast<char*>(buffer), *a = d;
write(d, param);
write(d, boxesSize);
write(d, scoresSize);
write(d, landmarksSize);
write(d, numPriors);
write(d, mClipBoxes);
write(d, mPrecision);
write(d, mScoreBits);
write(d, mCaffeSemantics);
ASSERT(d == a + getSerializationSize());
}
size_t BatchedNMSLandmarkConfDynamicPlugin::getSerializationSize() const noexcept
{
// NMSParameters, boxesSize,scoresSize,landmarksSize,numPriors
return sizeof(NMSParameters) + sizeof(int) * 4 + sizeof(bool) * 2 + sizeof(DataType) + sizeof(int32_t);
}
void BatchedNMSLandmarkConfDynamicPlugin::serialize(void* buffer) const noexcept
{
char *d = reinterpret_cast<char*>(buffer), *a = d;
write(d, param);
write(d, boxesSize);
write(d, scoresSize);
write(d, landmarksSize);
write(d, numPriors);
write(d, mClipBoxes);
write(d, mPrecision);
write(d, mScoreBits);
write(d, mCaffeSemantics);
ASSERT(d == a + getSerializationSize());
}
void BatchedNMSLandmarkConfPlugin::configurePlugin(const Dims* inputDims, int nbInputs, const Dims* outputDims,
int nbOutputs, const DataType* inputTypes, const DataType* outputTypes, const bool* inputIsBroadcast,
const bool* outputIsBroadcast, nvinfer1::PluginFormat format, int maxBatchSize) noexcept
{
try
{
ASSERT(nbInputs == 3);
ASSERT(nbOutputs == 5);
ASSERT(inputDims[0].nbDims == 3);
ASSERT(inputDims[1].nbDims == 2 || (inputDims[1].nbDims == 3 && inputDims[1].d[2] == 1));
ASSERT(std::none_of(inputIsBroadcast, inputIsBroadcast + nbInputs, [](bool b) { return b; }));
ASSERT(std::none_of(outputIsBroadcast, outputIsBroadcast + nbInputs, [](bool b) { return b; }));
boxesSize = inputDims[0].d[0] * inputDims[0].d[1] * inputDims[0].d[2];
scoresSize = inputDims[1].d[0] * inputDims[1].d[1];
landmarksSize = inputDims[2].d[0] * inputDims[2].d[1] * inputDims[2].d[2];
// num_boxes
numPriors = inputDims[0].d[0];
const int numLocClasses = param.shareLocation ? 1 : param.numClasses;
// Third dimension of boxes must be either 1 or num_classes
ASSERT(inputDims[0].d[1] == numLocClasses);
ASSERT(inputDims[0].d[2] == 4);
mPrecision = inputTypes[0];
}
catch (const std::exception& e)
{
caughtError(e);
}
}
void BatchedNMSLandmarkConfDynamicPlugin::configurePlugin(
const DynamicPluginTensorDesc* in, int nbInputs, const DynamicPluginTensorDesc* out, int nbOutputs) noexcept
{
try
{
ASSERT(nbInputs == 3);
ASSERT(nbOutputs == 5);
// Shape of boxes input should be
// Constant shape: [batch_size, num_boxes, num_classes, 4] or [batch_size, num_boxes, 1, 4]
// shareLocation == 0 or 1
const int numLocClasses = param.shareLocation ? 1 : param.numClasses;
ASSERT(in[0].desc.dims.nbDims == 4);
ASSERT(in[0].desc.dims.d[2] == numLocClasses);
ASSERT(in[0].desc.dims.d[3] == 4);
// Shape of scores input should be
// Constant shape: [batch_size, num_boxes, num_classes] or [batch_size, num_boxes,
// num_classes, 1]
ASSERT(in[1].desc.dims.nbDims == 3 || (in[1].desc.dims.nbDims == 4 && in[1].desc.dims.d[3] == 1));
boxesSize = in[0].desc.dims.d[1] * in[0].desc.dims.d[2] * in[0].desc.dims.d[3];
scoresSize = in[1].desc.dims.d[1] * in[1].desc.dims.d[2];
landmarksSize = in[2].desc.dims.d[1] * in[2].desc.dims.d[2] * in[2].desc.dims.d[3];
// num_boxes
numPriors = in[0].desc.dims.d[1];
mPrecision = in[0].desc.type;
}
catch (const std::exception& e)
{
caughtError(e);
}
}
bool BatchedNMSLandmarkConfPlugin::supportsFormat(DataType type, PluginFormat format) const noexcept
{
#if NVBUG_3321606_WAR
return ((type == DataType::kFLOAT || type == DataType::kINT32) && format == PluginFormat::kLINEAR);
#else
return ((type == DataType::kHALF || type == DataType::kFLOAT || type == DataType::kINT32)
&& format == PluginFormat::kLINEAR);
#endif // NVBUG_3321606_WAR
}
bool BatchedNMSLandmarkConfDynamicPlugin::supportsFormatCombination(
int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) noexcept
{
ASSERT(nbInputs <= 3 && nbInputs >= 0);
ASSERT(nbOutputs <= 5 && nbOutputs >= 0);
ASSERT(pos < 8 && pos >= 0);
const auto* in = inOut;
const auto* out = inOut + nbInputs;
const bool consistentFloatPrecision = in[0].type == in[pos].type;
switch (pos)
{
case 0:
return (in[0].type == DataType::kHALF || in[0].type == DataType::kFLOAT)
&& in[0].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 1:
return (in[1].type == DataType::kHALF || in[1].type == DataType::kFLOAT)
&& in[1].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 2:
return (in[2].type == DataType::kHALF || in[2].type == DataType::kFLOAT)
&& in[2].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 3: return out[0].type == DataType::kINT32 && out[0].format == PluginFormat::kLINEAR;
case 4:
return (out[1].type == DataType::kHALF || out[1].type == DataType::kFLOAT)
&& out[1].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 5:
return (out[2].type == DataType::kHALF || out[2].type == DataType::kFLOAT)
&& out[2].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 6:
return (out[3].type == DataType::kHALF || out[3].type == DataType::kFLOAT)
&& out[3].format == PluginFormat::kLINEAR && consistentFloatPrecision;
case 7:
return (out[4].type == DataType::kHALF || out[4].type == DataType::kFLOAT)
&& out[4].format == PluginFormat::kLINEAR && consistentFloatPrecision;
}
return false;
}
const char* BatchedNMSLandmarkConfPlugin::getPluginType() const noexcept
{
return NMS_PLUGIN_NAMES[0];
}
const char* BatchedNMSLandmarkConfDynamicPlugin::getPluginType() const noexcept
{
return NMS_PLUGIN_NAMES[1];
}
const char* BatchedNMSLandmarkConfPlugin::getPluginVersion() const noexcept
{
return NMS_PLUGIN_VERSION;
}
const char* BatchedNMSLandmarkConfDynamicPlugin::getPluginVersion() const noexcept
{
return NMS_PLUGIN_VERSION;
}
void BatchedNMSLandmarkConfPlugin::destroy() noexcept
{
delete this;
}
void BatchedNMSLandmarkConfDynamicPlugin::destroy() noexcept
{
delete this;
}
IPluginV2Ext* BatchedNMSLandmarkConfPlugin::clone() const noexcept
{
try
{
auto* plugin = new BatchedNMSLandmarkConfPlugin(param);
plugin->boxesSize = boxesSize;
plugin->scoresSize = scoresSize;
plugin->landmarksSize = landmarksSize;
plugin->numPriors = numPriors;
plugin->setPluginNamespace(mNamespace.c_str());
plugin->setClipParam(mClipBoxes);
plugin->mPrecision = mPrecision;
plugin->setScoreBits(mScoreBits);
plugin->setCaffeSemantics(mCaffeSemantics);
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2DynamicExt* BatchedNMSLandmarkConfDynamicPlugin::clone() const noexcept
{
try
{
auto* plugin = new BatchedNMSLandmarkConfDynamicPlugin(param);
plugin->boxesSize = boxesSize;
plugin->scoresSize = scoresSize;
plugin->landmarksSize = landmarksSize;
plugin->numPriors = numPriors;
plugin->setPluginNamespace(mNamespace.c_str());
plugin->setClipParam(mClipBoxes);
plugin->mPrecision = mPrecision;
plugin->setScoreBits(mScoreBits);
plugin->setCaffeSemantics(mCaffeSemantics);
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}
void BatchedNMSLandmarkConfPlugin::setPluginNamespace(const char* pluginNamespace) noexcept
{
try
{
mNamespace = pluginNamespace;
}
catch (const std::exception& e)
{
caughtError(e);
}
}
const char* BatchedNMSLandmarkConfPlugin::getPluginNamespace() const noexcept
{
return mNamespace.c_str();
}
void BatchedNMSLandmarkConfDynamicPlugin::setPluginNamespace(const char* pluginNamespace) noexcept
{
try
{
mNamespace = pluginNamespace;
}
catch (const std::exception& e)
{
caughtError(e);
}
}
const char* BatchedNMSLandmarkConfDynamicPlugin::getPluginNamespace() const noexcept
{
return mNamespace.c_str();
}
nvinfer1::DataType BatchedNMSLandmarkConfPlugin::getOutputDataType(
int index, const nvinfer1::DataType* inputTypes, int nbInputs) const noexcept
{
if (index == 0)
{
return nvinfer1::DataType::kINT32;
}
return inputTypes[0];
}
nvinfer1::DataType BatchedNMSLandmarkConfDynamicPlugin::getOutputDataType(
int index, const nvinfer1::DataType* inputTypes, int nbInputs) const noexcept
{
if (index == 0)
{
return nvinfer1::DataType::kINT32;
}
return inputTypes[0];
}
void BatchedNMSLandmarkConfPlugin::setClipParam(bool clip) noexcept
{
mClipBoxes = clip;
}
void BatchedNMSLandmarkConfDynamicPlugin::setClipParam(bool clip) noexcept
{
mClipBoxes = clip;
}
void BatchedNMSLandmarkConfPlugin::setScoreBits(int32_t scoreBits) noexcept
{
mScoreBits = scoreBits;
}
void BatchedNMSLandmarkConfDynamicPlugin::setScoreBits(int32_t scoreBits) noexcept
{
mScoreBits = scoreBits;
}
void BatchedNMSLandmarkConfPlugin::setCaffeSemantics(bool caffeSemantics) noexcept
{
mCaffeSemantics = caffeSemantics;
}
void BatchedNMSLandmarkConfDynamicPlugin::setCaffeSemantics(bool caffeSemantics) noexcept
{
mCaffeSemantics = caffeSemantics;
}
bool BatchedNMSLandmarkConfPlugin::isOutputBroadcastAcrossBatch(
int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const noexcept
{
return false;
}
bool BatchedNMSLandmarkConfPlugin::canBroadcastInputAcrossBatch(int inputIndex) const noexcept
{
return false;
}
BatchedNMSLandmarkConfBasePluginCreator::BatchedNMSLandmarkConfBasePluginCreator()
{
mPluginAttributes.clear();
mPluginAttributes.emplace_back(PluginField("shareLocation", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("backgroundLabelId", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("numClasses", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("topK", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("keepTopK", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("scoreThreshold", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("iouThreshold", nullptr, PluginFieldType::kFLOAT32, 1));
mPluginAttributes.emplace_back(PluginField("isNormalized", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("clipBoxes", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("scoreBits", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("caffeSemantics", nullptr, PluginFieldType::kINT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
const char* BatchedNMSLandmarkConfPluginCreator::getPluginName() const noexcept
{
return NMS_PLUGIN_NAMES[0];
}
const char* BatchedNMSLandmarkConfDynamicPluginCreator::getPluginName() const noexcept
{
return NMS_PLUGIN_NAMES[1];
}
const char* BatchedNMSLandmarkConfBasePluginCreator::getPluginVersion() const noexcept
{
return NMS_PLUGIN_VERSION;
}
const PluginFieldCollection* BatchedNMSLandmarkConfBasePluginCreator::getFieldNames() noexcept
{
return &mFC;
}
IPluginV2Ext* BatchedNMSLandmarkConfPluginCreator::createPlugin(
const char* name, const PluginFieldCollection* fc) noexcept
{
try
{
NMSParameters params;
const PluginField* fields = fc->fields;
bool clipBoxes = true;
int32_t scoreBits = 16;
bool caffeSemantics = true;
for (int i = 0; i < fc->nbFields; ++i)
{
const char* attrName = fields[i].name;
if (!strcmp(attrName, "shareLocation"))
{
params.shareLocation = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "backgroundLabelId"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.backgroundLabelId = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "numClasses"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.numClasses = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "topK"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.topK = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "keepTopK"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.keepTopK = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "scoreThreshold"))
{
ASSERT(fields[i].type == PluginFieldType::kFLOAT32);
params.scoreThreshold = *(static_cast<const float*>(fields[i].data));
}
else if (!strcmp(attrName, "iouThreshold"))
{
ASSERT(fields[i].type == PluginFieldType::kFLOAT32);
params.iouThreshold = *(static_cast<const float*>(fields[i].data));
}
else if (!strcmp(attrName, "isNormalized"))
{
params.isNormalized = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "clipBoxes"))
{
clipBoxes = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "scoreBits"))
{
scoreBits = *(static_cast<const int32_t*>(fields[i].data));
}
else if (!strcmp(attrName, "caffeSemantics"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
caffeSemantics = *(static_cast<const bool*>(fields[i].data));
}
}
auto* plugin = new BatchedNMSLandmarkConfPlugin(params);
plugin->setClipParam(clipBoxes);
plugin->setScoreBits(scoreBits);
plugin->setCaffeSemantics(caffeSemantics);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2DynamicExt* BatchedNMSLandmarkConfDynamicPluginCreator::createPlugin(
const char* name, const PluginFieldCollection* fc) noexcept
{
try
{
NMSParameters params;
const PluginField* fields = fc->fields;
bool clipBoxes = true;
int32_t scoreBits = 16;
bool caffeSemantics = true;
for (int i = 0; i < fc->nbFields; ++i)
{
const char* attrName = fields[i].name;
if (!strcmp(attrName, "shareLocation"))
{
params.shareLocation = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "backgroundLabelId"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.backgroundLabelId = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "numClasses"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.numClasses = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "topK"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.topK = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "keepTopK"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
params.keepTopK = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "scoreThreshold"))
{
ASSERT(fields[i].type == PluginFieldType::kFLOAT32);
params.scoreThreshold = *(static_cast<const float*>(fields[i].data));
}
else if (!strcmp(attrName, "iouThreshold"))
{
ASSERT(fields[i].type == PluginFieldType::kFLOAT32);
params.iouThreshold = *(static_cast<const float*>(fields[i].data));
}
else if (!strcmp(attrName, "isNormalized"))
{
params.isNormalized = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "clipBoxes"))
{
clipBoxes = *(static_cast<const bool*>(fields[i].data));
}
else if (!strcmp(attrName, "scoreBits"))
{
scoreBits = *(static_cast<const int32_t*>(fields[i].data));
}
else if (!strcmp(attrName, "caffeSemantics"))
{
ASSERT(fields[i].type == PluginFieldType::kINT32);
caffeSemantics = *(static_cast<const bool*>(fields[i].data));
}
}
auto* plugin = new BatchedNMSLandmarkConfDynamicPlugin(params);
plugin->setClipParam(clipBoxes);
plugin->setScoreBits(scoreBits);
plugin->setCaffeSemantics(caffeSemantics);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2Ext* BatchedNMSLandmarkConfPluginCreator::deserializePlugin(
const char* name, const void* serialData, size_t serialLength) noexcept
{
try
{
// This object will be deleted when the network is destroyed, which will
// call NMS::destroy()
auto* plugin = new BatchedNMSLandmarkConfPlugin(serialData, serialLength);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}
IPluginV2DynamicExt* BatchedNMSLandmarkConfDynamicPluginCreator::deserializePlugin(
const char* name, const void* serialData, size_t serialLength) noexcept
{
try
{
// This object will be deleted when the network is destroyed, which will
// call NMS::destroy()
auto* plugin = new BatchedNMSLandmarkConfDynamicPlugin(serialData, serialLength);
plugin->setPluginNamespace(mNamespace.c_str());
return plugin;
}
catch (const std::exception& e)
{
caughtError(e);
}
return nullptr;
}