Skip to content

Commit fd09eee

Browse files
🐞 Bugfix: Incorrect Normal score computation (#152)
1 parent 3ed42b5 commit fd09eee

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

model_api/cpp/models/src/anomaly_model.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ std::unique_ptr<ResultBase> AnomalyModel::postprocess(InferenceResult& infResult
8484
anomaly_map.convertTo(anomaly_map, CV_8UC1, 255);
8585

8686
pred_score = normalize(pred_score, imageThreshold);
87+
if (pred_label == labels[0]) { // normal label
88+
pred_score = 1 - pred_score; // Score of normal is 1 - score of anomaly
89+
}
90+
8791
if (!anomaly_map.empty()) {
8892
cv::resize(anomaly_map, anomaly_map, cv::Size{inputImgSize.inputImgWidth, inputImgSize.inputImgHeight});
8993
}
@@ -106,6 +110,10 @@ cv::Mat AnomalyModel::normalize(cv::Mat& tensor, float threshold) {
106110
return normalized;
107111
}
108112

113+
/// @brief Normalize the value to be in the range [0, 1]. Centered around 0.5 and scaled by the normalization scale.
114+
/// @param value Unbounded value to be normalized.
115+
/// @param threshold This is the value that is subtracted from the input value before normalization.
116+
/// @return value between 0 and 1.
109117
double AnomalyModel::normalize(double& value, float threshold) {
110118
double normalized = ((value - threshold) / normalizationScale) + 0.5f;
111119
return std::min(std::max(normalized, 0.), 1.);

model_api/python/openvino/model_api/models/anomaly.py

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]):
7979
# normalize
8080
pred_score = self._normalize(pred_score, self.image_threshold)
8181

82+
if pred_label == self.labels[0]: # normal
83+
pred_score = 1 - pred_score # Score of normal is 1 - score of anomaly
84+
8285
# resize outputs
8386
if anomaly_map is not None:
8487
anomaly_map = cv2.resize(

model_api/python/openvino/model_api/models/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __str__(self) -> str:
4343
pred_mask_min, pred_mask_max = self._compute_min_max(self.pred_mask)
4444
return (
4545
f"anomaly_map min:{anomaly_map_min} max:{anomaly_map_max};"
46-
f"pred_score:{self.pred_score};"
46+
f"pred_score:{np.round(self.pred_score, 1)};"
4747
f"pred_label:{self.pred_label};"
4848
f"pred_mask min:{pred_mask_min} max:{pred_mask_max};"
4949
)

tests/python/accuracy/public_scope.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
{
368368
"image": "coco128/images/train2017/000000000074.jpg",
369369
"reference": [
370-
"anomaly_map min:152 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;"
370+
"anomaly_map min:151 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;"
371371
]
372372
}
373373
]
@@ -379,7 +379,7 @@
379379
{
380380
"image": "coco128/images/train2017/000000000074.jpg",
381381
"reference": [
382-
"anomaly_map min:126 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:0 max:1;"
382+
"anomaly_map min:124 max:225;pred_score:0.9;pred_label:Anomaly;pred_mask min:0 max:1;"
383383
]
384384
}
385385
]

0 commit comments

Comments
 (0)