Skip to content

Commit a49d58f

Browse files
authored
Merge pull request #2659 from openvinotoolkit/mergeback/140to150
2 parents 9909d17 + 2a936f0 commit a49d58f

File tree

18 files changed

+152
-72
lines changed

18 files changed

+152
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ All notable changes to this project will be documented in this file.
4444
- Update ModelAPI configuration(<https://github.com/openvinotoolkit/training_extensions/pull/2564>)
4545
- Add Anomaly modelAPI changes (<https://github.com/openvinotoolkit/training_extensions/pull/2563>)
4646
- Update Image numpy access (<https://github.com/openvinotoolkit/training_extensions/pull/2586>)
47+
- Make max_num_detections configurable (<https://github.com/openvinotoolkit/training_extensions/pull/2647>)
4748

4849
### Bug fixes
4950

src/otx/algorithms/common/configs/training_base.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
"""Base Configuration of OTX Common Algorithms."""
22

3-
# Copyright (C) 2022 Intel Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing,
12-
# software distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions
15-
# and limitations under the License.
3+
# Copyright (C) 2022-2023 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
165

176
from sys import maxsize
187

@@ -241,6 +230,16 @@ class BasePostprocessing(ParameterGroup):
241230
affects_outcome_of=ModelLifecycle.INFERENCE,
242231
)
243232

233+
max_num_detections = configurable_integer(
234+
header="Maximum number of detection per image",
235+
description="Extra detection outputs will be discared in non-maximum suppression process. "
236+
"Defaults to 0, which means per-model default value.",
237+
default_value=0,
238+
min_value=0,
239+
max_value=10000,
240+
affects_outcome_of=ModelLifecycle.INFERENCE,
241+
)
242+
244243
use_ellipse_shapes = configurable_boolean(
245244
default_value=False,
246245
header="Use ellipse shapes",

src/otx/algorithms/detection/adapters/mmdet/configurer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def configure_model(self, cfg, data_classes, model_classes, ir_options, **kwargs
3737
"""Configuration for model config."""
3838
super().configure_model(cfg, data_classes, model_classes, ir_options, **kwargs)
3939
self.configure_regularization(cfg)
40+
self.configure_max_num_detections(cfg, kwargs.get("max_num_detections", 0))
41+
42+
def configure_max_num_detections(self, cfg, max_num_detections):
43+
"""Patch config for maximum number of detections."""
44+
if max_num_detections > 0:
45+
logger.info(f"Model max_num_detections: {max_num_detections}")
46+
test_cfg = cfg.model.test_cfg
47+
test_cfg.max_per_img = max_num_detections
48+
test_cfg.nms_pre = max_num_detections * 10
49+
# Special cases for 2-stage detectors (e.g. MaskRCNN)
50+
if hasattr(test_cfg, "rpn"):
51+
test_cfg.rpn.nms_pre = max_num_detections * 20
52+
test_cfg.rpn.max_per_img = max_num_detections * 10
53+
if hasattr(test_cfg, "rcnn"):
54+
test_cfg.rcnn.max_per_img = max_num_detections
55+
train_cfg = cfg.model.train_cfg
56+
if hasattr(train_cfg, "rpn_proposal"):
57+
train_cfg.rpn_proposal.nms_pre = max_num_detections * 20
58+
train_cfg.rpn_proposal.max_per_img = max_num_detections * 10
4059

4160
def configure_regularization(self, cfg): # noqa: C901
4261
"""Patch regularization parameters."""

src/otx/algorithms/detection/adapters/mmdet/task.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def configure(self, training=True, ir_options=None, train_dataset=None, export=F
177177
model_classes,
178178
self._input_size,
179179
train_dataset=train_dataset,
180+
max_num_detections=self.max_num_detections,
180181
)
181182
if should_cluster_anchors(self._recipe_cfg):
182183
if train_dataset is not None:
@@ -485,6 +486,12 @@ def _export_model(
485486
assert len(self._precision) == 1
486487
export_options["precision"] = str(self._precision[0])
487488
export_options["type"] = str(export_format)
489+
if self.max_num_detections > 0:
490+
logger.info(f"Export max_num_detections: {self.max_num_detections}")
491+
post_proc_cfg = export_options["deploy_cfg"]["codebase_config"]["post_processing"]
492+
post_proc_cfg["max_output_boxes_per_class"] = self.max_num_detections
493+
post_proc_cfg["keep_top_k"] = self.max_num_detections
494+
post_proc_cfg["pre_top_k"] = self.max_num_detections * 10
488495

489496
export_options["deploy_cfg"]["dump_features"] = dump_features
490497
if dump_features:

src/otx/algorithms/detection/adapters/openvino/task.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
"""Openvino Task of Detection."""
22

3-
# Copyright (C) 2021 Intel Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing,
12-
# software distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions
15-
# and limitations under the License.
3+
# Copyright (C) 2021-2023 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
165

176
import copy
187
import io

src/otx/algorithms/detection/configs/base/configuration.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
"""Configuration file of OTX Detection."""
22

3-
# Copyright (C) 2022 Intel Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing,
12-
# software distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions
15-
# and limitations under the License.
3+
# Copyright (C) 2022-2023 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
165

176
from attr import attrs
187

src/otx/algorithms/detection/configs/detection/configuration.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ postprocessing:
294294
value: 0.01
295295
visible_in_ui: true
296296
warning: null
297+
max_num_detections:
298+
affects_outcome_of: INFERENCE
299+
default_value: 0
300+
description:
301+
Extra detection outputs will be discared in non-maximum suppression process.
302+
Defaults to 0, which means per-model default values.
303+
editable: true
304+
header: Maximum number of detections per image
305+
max_value: 10000
306+
min_value: 0
307+
type: INTEGER
308+
ui_rules:
309+
action: DISABLE_EDITING
310+
operator: AND
311+
rules: []
312+
type: UI_RULES
313+
value: 0
314+
visible_in_ui: true
315+
warning: null
297316
use_ellipse_shapes:
298317
affects_outcome_of: INFERENCE
299318
default_value: false

src/otx/algorithms/detection/configs/instance_segmentation/configuration.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ postprocessing:
294294
value: 0.01
295295
visible_in_ui: true
296296
warning: null
297+
max_num_detections:
298+
affects_outcome_of: INFERENCE
299+
default_value: 0
300+
description:
301+
Extra detection outputs will be discared in non-maximum suppression process.
302+
Defaults to 0, which means per-model default values.
303+
editable: true
304+
header: Maximum number of detections per image
305+
max_value: 10000
306+
min_value: 0
307+
type: INTEGER
308+
ui_rules:
309+
action: DISABLE_EDITING
310+
operator: AND
311+
rules: []
312+
type: UI_RULES
313+
value: 0
314+
visible_in_ui: true
315+
warning: null
297316
use_ellipse_shapes:
298317
affects_outcome_of: INFERENCE
299318
default_value: false

src/otx/algorithms/detection/configs/instance_segmentation/convnext_maskrcnn/model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@
115115
nms=dict(type="nms", iou_threshold=0.7),
116116
min_bbox_size=0,
117117
),
118-
rcnn=dict(
119-
score_thr=0.05, nms=dict(type="nms", iou_threshold=0.5, max_num=100), max_per_img=100, mask_thr_binary=0.5
120-
),
118+
rcnn=dict(score_thr=0.05, nms=dict(type="nms", iou_threshold=0.5), max_per_img=100, mask_thr_binary=0.5),
121119
),
122120
)
123121

src/otx/algorithms/detection/configs/instance_segmentation/resnet50_maskrcnn/model.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
"""Model configuration of Resnet50-MaskRCNN model for Instance-Seg Task."""
22

3-
# Copyright (C) 2022 Intel Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing,
12-
# software distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions
15-
# and limitations under the License.
3+
# Copyright (C) 2022-2023 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
165

176
# pylint: disable=invalid-name
187

@@ -149,7 +138,7 @@
149138
),
150139
rcnn=dict(
151140
score_thr=0.05,
152-
nms=dict(type="nms", iou_threshold=0.5, max_num=100),
141+
nms=dict(type="nms", iou_threshold=0.5),
153142
max_per_img=100,
154143
mask_thr_binary=0.5,
155144
),

0 commit comments

Comments
 (0)