Skip to content

Commit 8f2c882

Browse files
author
Galina Zalesskaya
authored
[ENHANCE] Parametrize saliency maps dumping in export (part 2) (#1888)
* Add dump_features for segmentation * Dump_features for the rest of the tasks * Add unit tests * Fix pre-commit issues * Update documentation * Fixes from comments * Add integration cli tests * Remove commented code
1 parent 054f2c1 commit 8f2c882

File tree

26 files changed

+195
-41
lines changed

26 files changed

+195
-41
lines changed

docs/source/guide/get_started/quick_start_guide/cli_commands.rst

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ With the ``--help`` command, you can list additional information, such as its pa
261261
.. code-block::
262262
263263
(otx) ...$ otx export --help
264-
usage: otx export [-h] [--load-weights LOAD_WEIGHTS] [--save-model-to SAVE_MODEL_TO] [template]
264+
usage: otx export [-h] [--load-weights LOAD_WEIGHTS] [--save-model-to SAVE_MODEL_TO] [--work-dir WORK_DIR] [--dump-features] [--half-precision] [template]
265265
266266
positional arguments:
267-
template Enter the path or ID or name of the template file.
267+
template Enter the path or ID or name of the template file.
268268
This can be omitted if you have train-data-roots or run inside a workspace.
269269
270270
optional arguments:
@@ -273,6 +273,9 @@ With the ``--help`` command, you can list additional information, such as its pa
273273
Load model weights from previously saved checkpoint.
274274
--save-model-to SAVE_MODEL_TO
275275
Location where exported model will be stored.
276+
--work-dir WORK_DIR Location where the intermediate output of the export will be stored.
277+
--dump-features Whether to return feature vector and saliency map for explanation purposes.
278+
--half-precision This flag indicated if model is exported in half precision (FP16).
276279
277280
278281
The command below performs exporting to the ``outputs/openvino`` path.
@@ -283,6 +286,12 @@ The command below performs exporting to the ``outputs/openvino`` path.
283286
284287
The command results in ``openvino.xml``, ``openvino.bin`` and ``label_schema.json``
285288

289+
To use the exported model as an input for ``otx explain``, please dump additional outputs with internal information, using ``--dump-features``:
290+
291+
.. code-block::
292+
293+
(otx) ...$ otx export Custom_Object_Detection_Gen3_SSD --load-weights <path/to/trained/weights.pth> --save-model-to outputs/openvino/with_features --dump-features
294+
286295
287296
************
288297
Optimization
@@ -419,7 +428,7 @@ With the ``--help`` command, you can list additional information, such as its pa
419428
Weight of the saliency map when overlaying the saliency map.
420429
421430
422-
The command below will generate saliency maps (heatmaps with read colored areas of focus) of the trained model on the provided dataset and save the resulting images to ``save-explanation-to`` path:
431+
The command below will generate saliency maps (heatmaps with red colored areas of focus) of the trained model on the provided dataset and save the resulting images to ``save-explanation-to`` path:
423432

424433
.. code-block::
425434
@@ -433,6 +442,20 @@ The command below will generate saliency maps (heatmaps with read colored areas
433442

434443
It is possible to pass both PyTorch weights ``.pth`` or OpenVINO™ IR ``openvino.xml`` to ``--load-weights`` option.
435444

445+
By default, the model is exported to the OpenVINO™ IR format without extra feature information needed for the ``explain`` function. To use OpenVINO™ IR model in ``otx explain``, please first export it with ``--dump-features`` parameter:
446+
447+
.. code-block::
448+
449+
(otx) ...$ otx export SSD --load-weights <path/to/trained/weights.pth> \
450+
--save-model-to outputs/openvino/with_features \
451+
--dump-features
452+
(otx) ...$ otx explain SSD --explain-data-roots <path/to/explain/root> \
453+
--load-weights outputs/openvino/with_features \
454+
--save-explanation-to <path/to/output/root> \
455+
--explain-algorithm classwisesaliencymap \
456+
--overlay-weight 0.5
457+
458+
436459
437460
*************
438461
Demonstration

otx/algorithms/action/tasks/inference.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,13 @@ def export(
327327
export_type: ExportType,
328328
output_model: ModelEntity,
329329
precision: ModelPrecision = ModelPrecision.FP32,
330-
dump_features: bool = True,
330+
dump_features: bool = False,
331331
):
332332
"""Export function of OTX Action Task."""
333-
# TODO: add dumping saliency maps and representation vectors according to dump_features flag
334-
dump_features = False
335-
if not dump_features:
336-
logger.warning(
337-
"Ommitting feature dumping is not implemented."
338-
"The saliency maps and representation vector outputs will be dumped in the exported model."
333+
if dump_features:
334+
raise NotImplementedError(
335+
"Feature dumping is not implemented for the anomaly task."
336+
"The saliency maps and representation vector outputs will not be dumped in the exported model."
339337
)
340338

341339
# copied from OTX inference_task.py

otx/algorithms/anomaly/tasks/inference.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def export(
252252
export_type: ExportType,
253253
output_model: ModelEntity,
254254
precision: ModelPrecision = ModelPrecision.FP32,
255-
dump_features: bool = True,
255+
dump_features: bool = False,
256256
) -> None:
257257
"""Export model to OpenVINO IR.
258258
@@ -265,11 +265,10 @@ def export(
265265
Raises:
266266
Exception: If export_type is not ExportType.OPENVINO
267267
"""
268-
# TODO: add dumping saliency maps and representation vectors according to dump_features flag
269-
if not dump_features:
270-
logger.warning(
271-
"Ommitting feature dumping is not implemented."
272-
"The saliency maps and representation vector outputs will be dumped in the exported model."
268+
if dump_features:
269+
raise NotImplementedError(
270+
"Feature dumping is not implemented for the anomaly task."
271+
"The saliency maps and representation vector outputs will not be dumped in the exported model."
273272
)
274273

275274
self.precision[0] = precision

otx/algorithms/classification/tasks/inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def export(
225225
export_type: ExportType,
226226
output_model: ModelEntity,
227227
precision: ModelPrecision = ModelPrecision.FP32,
228-
dump_features: bool = True,
228+
dump_features: bool = False,
229229
):
230230
"""Export function of OTX Classification Task."""
231231

otx/algorithms/common/tasks/training_base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,11 @@ def _initialize(self, options=None): # noqa: C901
336336
options["deploy_cfg"]["dump_features"] = options["dump_features"]
337337
if options["dump_features"]:
338338
output_names = options["deploy_cfg"]["ir_config"]["output_names"]
339-
if "feature_vector" not in output_names and "saliency_map" not in output_names:
340-
options["deploy_cfg"]["ir_config"]["output_names"] += ["feature_vector", "saliency_map"]
339+
if "feature_vector" not in output_names:
340+
options["deploy_cfg"]["ir_config"]["output_names"].append("feature_vector")
341+
if options["deploy_cfg"]["codebase_config"]["task"] != "Segmentation":
342+
if "saliency_map" not in output_names:
343+
options["deploy_cfg"]["ir_config"]["output_names"].append("saliency_map")
341344

342345
self._initialize_post_hook(options)
343346

otx/algorithms/detection/tasks/inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def export(
244244
export_type: ExportType,
245245
output_model: ModelEntity,
246246
precision: ModelPrecision = ModelPrecision.FP32,
247-
dump_features: bool = True,
247+
dump_features: bool = False,
248248
):
249249
"""Export function of OTX Detection Task."""
250250
# copied from OTX inference_task.py

otx/algorithms/segmentation/configs/ocr_lite_hrnet_18/deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_base_ = ["../base/deployments/base_segmentation_dynamic.py"]
44

55
ir_config = dict(
6-
output_names=["output", "feature_vector"],
6+
output_names=["output"],
77
)
88

99
backend_config = dict(

otx/algorithms/segmentation/configs/ocr_lite_hrnet_18_mod2/deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_base_ = ["../base/deployments/base_segmentation_dynamic.py"]
44

55
ir_config = dict(
6-
output_names=["output", "feature_vector"],
6+
output_names=["output"],
77
)
88

99
backend_config = dict(

otx/algorithms/segmentation/configs/ocr_lite_hrnet_s_mod2/deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_base_ = ["../base/deployments/base_segmentation_dynamic.py"]
44

55
ir_config = dict(
6-
output_names=["output", "feature_vector"],
6+
output_names=["output"],
77
)
88

99
backend_config = dict(

otx/algorithms/segmentation/configs/ocr_lite_hrnet_x_mod3/deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_base_ = ["../base/deployments/base_segmentation_dynamic.py"]
44

55
ir_config = dict(
6-
output_names=["output", "feature_vector"],
6+
output_names=["output"],
77
)
88

99
backend_config = dict(

0 commit comments

Comments
 (0)