Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTE][Release][XAI] Detection fix two stage bbox_head error #1414

Merged
merged 13 commits into from
Dec 8, 2022
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __pycache__
env
.env
.tox
results/

data/*
.coverage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
samples_per_gpu=10,
workers_per_gpu=4,
train=dict(
type=dataset_type,
ann_file="data/coco/annotations/instances_train2017.json",
img_prefix="data/coco/train2017",
pipeline=train_pipeline,
type=dataset_type,
ann_file="data/coco/annotations/instances_train2017.json",
img_prefix="data/coco/train2017",
pipeline=train_pipeline,
),
val=dict(
type=dataset_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,13 @@ def _add_predictions_to_dataset(self, prediction_results, dataset, confidence_th
if saliency_map is not None:
if saliency_map.ndim == 2:
# Single saliency map per image, support e.g. EigenCAM use case
actmap = get_actmap(
saliency_map, (dataset_item.width, dataset_item.height)
)
actmap = get_actmap(saliency_map, (dataset_item.width, dataset_item.height))
saliency_media = ResultMediaEntity(
name="Saliency Map",
type="saliency_map",
annotation_scene=dataset_item.annotation_scene,
numpy=actmap,
roi=dataset_item.roi
roi=dataset_item.roi,
)
dataset_item.append_metadata_item(saliency_media, model=self._task_environment.model)
elif saliency_map.ndim == 3:
Expand All @@ -326,22 +324,22 @@ def _add_predictions_to_dataset(self, prediction_results, dataset, confidence_th
num_saliency_maps = saliency_map.shape[0]
if num_saliency_maps == len(labels) + 1:
# Include the background as the last category
labels.append(LabelEntity('background', Domain.DETECTION))
labels.append(LabelEntity("background", Domain.DETECTION))
for class_id, class_wise_saliency_map in enumerate(saliency_map):
actmap = get_actmap(
class_wise_saliency_map, (dataset_item.width, dataset_item.height)
)
actmap = get_actmap(class_wise_saliency_map, (dataset_item.width, dataset_item.height))
class_name_str = labels[class_id].name
saliency_media = ResultMediaEntity(
name=class_name_str,
type="saliency_map",
annotation_scene=dataset_item.annotation_scene,
numpy=actmap, roi=dataset_item.roi
numpy=actmap,
roi=dataset_item.roi,
)
dataset_item.append_metadata_item(saliency_media, model=self._task_environment.model)
else:
raise RuntimeError(f'Single saliency map has to be 2 or 3-dimensional, '
f'but got {saliency_map.ndim} dims')
raise RuntimeError(
f"Single saliency map has to be 2 or 3-dimensional, but got {saliency_map.ndim} dims"
)

def _patch_data_pipeline(self):
base_dir = os.path.abspath(os.path.dirname(self.template_file_path))
Expand Down Expand Up @@ -380,9 +378,7 @@ def patch_data_pipeline(cfg):
pipeline_step.type = "LoadAnnotationFromOTEDataset"
pipeline_step.domain = domain
pipeline_step.min_size = cfg.pop("min_size", -1)
if (subset == "train" and
pipeline_step.type == "Collect" and
cfg.type not in ["ImageTilingDataset"]):
if subset == "train" and pipeline_step.type == "Collect" and cfg.type not in ["ImageTilingDataset"]:
pipeline_step = BaseTask._get_meta_keys(pipeline_step)
patch_color_conversion(cfg.pipeline)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def evaluate(self, results, metric="accuracy", metric_options=None, logger=None)

# compute top-k metrics from mmcls and align them in [0,1] range
eval_results = super().evaluate(results, metrics, metric_options, logger)
for k in metric_options['topk']:
eval_results[f'accuracy_top-{k}'] /= 100
assert 0 <= eval_results[f'accuracy_top-{k}'] <= 1
for k in metric_options["topk"]:
eval_results[f"accuracy_top-{k}"] /= 100
assert 0 <= eval_results[f"accuracy_top-{k}"] <= 1

# Add Evaluation Accuracy score per Class - it can be used only for multi-class dataset.
if self.class_acc:
Expand Down
38 changes: 19 additions & 19 deletions external/model-preparation-algorithm/tests/test_xai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from mmcls.models import build_classifier
from mmdet.models import build_detector

from mpa.det.stage import DetectionStage # noqa
from mpa.modules.hooks.auxiliary_hooks import ReciproCAMHook, DetSaliencyMapHook
from mpa.det.stage import DetectionStage


torch.manual_seed(0)
Expand Down Expand Up @@ -37,31 +37,31 @@ def get_classification_model():
@staticmethod
def get_detection_model():
model_cfg = dict(
type='CustomSingleStageDetector',
backbone=dict(type='mobilenetv2_w1',
out_indices=(4, 5),
frozen_stages=-1,
norm_eval=False,
pretrained=True),
type="CustomSingleStageDetector",
backbone=dict(
type="mobilenetv2_w1", out_indices=(4, 5), frozen_stages=-1, norm_eval=False, pretrained=True
),
bbox_head=dict(
type='CustomSSDHead',
type="CustomSSDHead",
in_channels=(96, 320),
num_classes=20,
anchor_generator=dict(
type='SSDAnchorGeneratorClustered',
type="SSDAnchorGeneratorClustered",
reclustering_anchors=True,
strides=[16, 32],
widths=[np.array([70.93408016, 132.06659281, 189.56180207, 349.90057837]),
np.array([272.31733885, 448.52200666, 740.63350023, 530.78990182,
790.99297377])],
heights=[np.array([93.83759764, 235.21261441, 432.6029996, 250.08979657]),
np.array([672.8829653, 474.84783528, 420.18291446, 741.02592293,
766.45636125])]),
widths=[
np.array([70.93408016, 132.06659281, 189.56180207, 349.90057837]),
np.array([272.31733885, 448.52200666, 740.63350023, 530.78990182, 790.99297377]),
],
heights=[
np.array([93.83759764, 235.21261441, 432.6029996, 250.08979657]),
np.array([672.8829653, 474.84783528, 420.18291446, 741.02592293, 766.45636125]),
],
),
bbox_coder=dict(
type='DeltaXYWHBBoxCoder',
target_means=[.0, .0, .0, .0],
target_stds=[0.1, 0.1, 0.2, 0.2]),
)
type="DeltaXYWHBBoxCoder", target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[0.1, 0.1, 0.2, 0.2]
),
),
)
model = build_detector(model_cfg)
return model.eval()
Expand Down
5 changes: 3 additions & 2 deletions ote_sdk/ote_sdk/utils/tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# SPDX-License-Identifier: Apache-2.0
#

import copy
from itertools import product
from typing import Any, List, Tuple, Union

Expand Down Expand Up @@ -79,7 +80,7 @@ def predict(self, image: np.ndarray):
detections = np.append(detections, output, axis=0)
# cache full image feature vector and saliency map at 0 index
if i == 0:
features = feats
features = copy.deepcopy(feats)

if np.prod(detections.shape):
dets, keep = multiclass_nms(detections, max_num=self.max_number)
Expand Down Expand Up @@ -128,7 +129,7 @@ def predict_tile(
if "feature_vector" in raw_predictions or "saliency_map" in raw_predictions:
features = [
raw_predictions["feature_vector"].reshape(-1),
raw_predictions["saliency_map"],
raw_predictions["saliency_map"][0],
]
return features, output

Expand Down