@@ -54,18 +54,34 @@ class MultiClassNMS3OpConverter : public OpConverter {
54
54
PADDLE_GET_CONST (float , op_desc.GetAttr (" nms_threshold" ));
55
55
int keep_top_k = PADDLE_GET_CONST (int , op_desc.GetAttr (" keep_top_k" ));
56
56
bool normalized = PADDLE_GET_CONST (bool , op_desc.GetAttr (" normalized" ));
57
- int num_classes = scores_tensor->getDimensions ().d [0 ];
57
+ int class_index = engine_->with_dynamic_shape () ? 1 : 0 ;
58
+ int num_classes = scores_tensor->getDimensions ().d [class_index];
58
59
59
60
auto bboxes_dims = bboxes_tensor->getDimensions ();
60
- nvinfer1::Dims3 bboxes_expand_dims (bboxes_dims.d [0 ], 1 , bboxes_dims.d [1 ]);
61
- auto * bboxes_expand_layer =
62
- TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *bboxes_tensor);
63
- bboxes_expand_layer->setReshapeDimensions (bboxes_expand_dims);
64
-
65
- nvinfer1::Permutation permutation{1 , 0 };
66
- auto * scores_transpose_layer =
67
- TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *scores_tensor);
68
- scores_transpose_layer->setFirstTranspose (permutation);
61
+ nvinfer1::IShuffleLayer* bboxes_expand_layer = nullptr ;
62
+ nvinfer1::IShuffleLayer* scores_transpose_layer = nullptr ;
63
+ if (engine_->with_dynamic_shape ()) {
64
+ nvinfer1::Dims4 bboxes_expand_dims (
65
+ bboxes_dims.d [0 ], bboxes_dims.d [1 ], 1 , bboxes_dims.d [2 ]);
66
+ bboxes_expand_layer =
67
+ TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *bboxes_tensor);
68
+ bboxes_expand_layer->setReshapeDimensions (bboxes_expand_dims);
69
+
70
+ nvinfer1::Permutation permutation{0 , 2 , 1 };
71
+ scores_transpose_layer =
72
+ TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *scores_tensor);
73
+ scores_transpose_layer->setFirstTranspose (permutation);
74
+ } else {
75
+ nvinfer1::Dims3 bboxes_expand_dims (bboxes_dims.d [0 ], 1 , bboxes_dims.d [1 ]);
76
+ bboxes_expand_layer =
77
+ TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *bboxes_tensor);
78
+ bboxes_expand_layer->setReshapeDimensions (bboxes_expand_dims);
79
+
80
+ nvinfer1::Permutation permutation{1 , 0 };
81
+ scores_transpose_layer =
82
+ TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *scores_tensor);
83
+ scores_transpose_layer->setFirstTranspose (permutation);
84
+ }
69
85
70
86
std::vector<nvinfer1::ITensor*> batch_nms_inputs;
71
87
batch_nms_inputs.push_back (bboxes_expand_layer->getOutput (0 ));
@@ -101,35 +117,50 @@ class MultiClassNMS3OpConverter : public OpConverter {
101
117
fields.size () * sizeof (nvinfer1::PluginField)));
102
118
plugin_collections->nbFields = static_cast <int >(fields.size ());
103
119
plugin_collections->fields = fields.data ();
104
-
105
- auto creator = GetPluginRegistry ()->getPluginCreator (" BatchedNMS_TRT" , " 1" );
120
+ std::string nms_plugin_name = " BatchedNMS_TRT" ;
121
+ if (engine_->with_dynamic_shape ()) {
122
+ nms_plugin_name = " BatchedNMSDynamic_TRT" ;
123
+ }
124
+ auto creator =
125
+ GetPluginRegistry ()->getPluginCreator (nms_plugin_name.c_str (), " 1" );
106
126
auto batch_nms_plugin =
107
- creator->createPlugin (" BatchNMSPlugin " , plugin_collections);
127
+ creator->createPlugin (nms_plugin_name. c_str () , plugin_collections);
108
128
free (plugin_collections);
109
129
110
130
auto batch_nms_layer = engine_->network ()->addPluginV2 (
111
131
batch_nms_inputs.data (), batch_nms_inputs.size (), *batch_nms_plugin);
132
+ // static shape: [keep_topk, 4], [keep_topk], [keep_topk]
133
+ // dynamic shape: [bs, keep_topk, 4], [bs, keep_topk], [bs, keep_topk]
112
134
auto nmsed_boxes = batch_nms_layer->getOutput (1 );
113
135
auto nmsed_scores = batch_nms_layer->getOutput (2 );
114
136
auto nmsed_classes = batch_nms_layer->getOutput (3 );
115
137
116
138
auto nmsed_scores_transpose_layer =
117
139
TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *nmsed_scores);
118
- nmsed_scores_transpose_layer->setReshapeDimensions (
119
- nvinfer1::Dims2 (keep_top_k, 1 ));
120
140
auto nmsed_classes_reshape_layer =
121
141
TRT_ENGINE_ADD_LAYER (engine_, Shuffle, *nmsed_classes);
122
- nmsed_classes_reshape_layer->setReshapeDimensions (
123
- nvinfer1::Dims2 (keep_top_k, 1 ));
124
-
142
+ if (engine_->with_dynamic_shape ()) {
143
+ nmsed_scores_transpose_layer->setReshapeDimensions (
144
+ nvinfer1::Dims3 (bboxes_dims.d [0 ], keep_top_k, 1 ));
145
+
146
+ nmsed_classes_reshape_layer->setReshapeDimensions (
147
+ nvinfer1::Dims3 (bboxes_dims.d [0 ], keep_top_k, 1 ));
148
+ } else {
149
+ nmsed_scores_transpose_layer->setReshapeDimensions (
150
+ nvinfer1::Dims2 (keep_top_k, 1 ));
151
+
152
+ nmsed_classes_reshape_layer->setReshapeDimensions (
153
+ nvinfer1::Dims2 (keep_top_k, 1 ));
154
+ }
125
155
std::vector<nvinfer1::ITensor*> concat_inputs;
126
156
concat_inputs.push_back (nmsed_classes_reshape_layer->getOutput (0 ));
127
157
concat_inputs.push_back (nmsed_scores_transpose_layer->getOutput (0 ));
128
158
concat_inputs.push_back (nmsed_boxes);
129
159
130
160
auto nms_concat_layer = TRT_ENGINE_ADD_LAYER (
131
161
engine_, Concatenation, concat_inputs.data (), concat_inputs.size ());
132
- nms_concat_layer->setAxis (1 );
162
+ int axis_index = engine_->with_dynamic_shape () ? 1 : 0 ;
163
+ nms_concat_layer->setAxis (axis_index + 1 );
133
164
134
165
// add fake index as output to be consistent with the outputs of
135
166
// multiclass_nms3
0 commit comments