From 7c1c62df56b1045accd9937fe47ea11a44d806fe Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Tue, 12 Dec 2023 16:59:46 +0100 Subject: [PATCH 1/4] return anomaly maps in range 0-255 --- model_api/cpp/models/src/anomaly_model.cpp | 1 + model_api/python/openvino/model_api/models/anomaly.py | 2 ++ tests/python/accuracy/public_scope.json | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/model_api/cpp/models/src/anomaly_model.cpp b/model_api/cpp/models/src/anomaly_model.cpp index 36edbdf5..351a50a2 100644 --- a/model_api/cpp/models/src/anomaly_model.cpp +++ b/model_api/cpp/models/src/anomaly_model.cpp @@ -81,6 +81,7 @@ std::unique_ptr AnomalyModel::postprocess(InferenceResult& infResult pred_mask.convertTo(pred_mask, CV_8UC1, 1 / 255.); cv::resize(pred_mask, pred_mask, cv::Size{inputImgSize.inputImgWidth, inputImgSize.inputImgHeight}); anomaly_map = normalize(anomaly_map, pixelThreshold); + anomaly_map.convertTo(anomaly_map, CV_8UC1, 255); pred_score = normalize(pred_score, imageThreshold); if (!anomaly_map.empty()) { diff --git a/model_api/python/openvino/model_api/models/anomaly.py b/model_api/python/openvino/model_api/models/anomaly.py index 0cdd100e..00cc299b 100644 --- a/model_api/python/openvino/model_api/models/anomaly.py +++ b/model_api/python/openvino/model_api/models/anomaly.py @@ -70,6 +70,8 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]): assert anomaly_map is not None pred_mask = (anomaly_map >= self.pixel_threshold).astype(np.uint8) anomaly_map = self._normalize(anomaly_map, self.pixel_threshold) + anomaly_map *= 255 + anomaly_map = anomaly_map.astype(np.uint8) pred_mask = cv2.resize( pred_mask, (meta["original_shape"][1], meta["original_shape"][0]) ) diff --git a/tests/python/accuracy/public_scope.json b/tests/python/accuracy/public_scope.json index c8be9c3d..d6c14c23 100644 --- a/tests/python/accuracy/public_scope.json +++ b/tests/python/accuracy/public_scope.json @@ -367,7 +367,7 @@ { "image": "coco128/images/train2017/000000000074.jpg", "reference": [ - "anomaly_map min:0.594 max:1.0;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;" + "anomaly_map min:151 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;" ] } ] @@ -379,7 +379,7 @@ { "image": "coco128/images/train2017/000000000074.jpg", "reference": [ - "anomaly_map min:0.495 max:1.0;pred_score:1.0;pred_label:Anomaly;pred_mask min:0 max:1;" + "anomaly_map min:126 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:0 max:1;" ] } ] From 0012783a32d155c3f4ec051d9939da307e408bc0 Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Wed, 13 Dec 2023 09:33:27 +0100 Subject: [PATCH 2/4] Remove printing precision --- model_api/python/openvino/model_api/models/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_api/python/openvino/model_api/models/utils.py b/model_api/python/openvino/model_api/models/utils.py index e6fbadac..b6263304 100644 --- a/model_api/python/openvino/model_api/models/utils.py +++ b/model_api/python/openvino/model_api/models/utils.py @@ -42,7 +42,7 @@ def __str__(self) -> str: anomaly_map_min, anomaly_map_max = self._compute_min_max(self.anomaly_map) pred_mask_min, pred_mask_max = self._compute_min_max(self.pred_mask) return ( - f"anomaly_map min:{anomaly_map_min:.3f} max:{anomaly_map_max};" + f"anomaly_map min:{anomaly_map_min} max:{anomaly_map_max};" f"pred_score:{self.pred_score};" f"pred_label:{self.pred_label};" f"pred_mask min:{pred_mask_min} max:{pred_mask_max};" From 9190fd5bfbfa7d5fbae2816063096e398a5e7141 Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Wed, 13 Dec 2023 10:10:41 +0100 Subject: [PATCH 3/4] Remove printing precision in c++ --- model_api/cpp/models/include/models/results.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_api/cpp/models/include/models/results.h b/model_api/cpp/models/include/models/results.h index e71e5762..d7962db3 100644 --- a/model_api/cpp/models/include/models/results.h +++ b/model_api/cpp/models/include/models/results.h @@ -67,7 +67,7 @@ struct AnomalyResult : public ResultBase cv::minMaxLoc(prediction.anomaly_map, &min_anomaly_map, &max_anomaly_map); double min_pred_mask, max_pred_mask; cv::minMaxLoc(prediction.pred_mask, &min_pred_mask, &max_pred_mask); - os << "anomaly_map min:"<< std::fixed << std::setprecision(3) << min_anomaly_map << " max:" << std::fixed << std::setprecision(1) << max_anomaly_map << ";"; + os << "anomaly_map min:" << min_anomaly_map << " max:" << max_anomaly_map << ";"; os << "pred_score:" << prediction.pred_score << ";"; os << "pred_label:" << prediction.pred_label << ";"; os << std::fixed << std::setprecision(0) << "pred_mask min:" << min_pred_mask << " max:" << max_pred_mask << ";"; From 776a2b9e75a4474624e86b0f7bb9158ac92f88de Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Wed, 13 Dec 2023 11:03:52 +0100 Subject: [PATCH 4/4] Fix precision difference between python and c++ --- model_api/cpp/models/include/models/results.h | 2 +- model_api/python/openvino/model_api/models/anomaly.py | 2 +- tests/python/accuracy/public_scope.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/model_api/cpp/models/include/models/results.h b/model_api/cpp/models/include/models/results.h index d7962db3..39958f1e 100644 --- a/model_api/cpp/models/include/models/results.h +++ b/model_api/cpp/models/include/models/results.h @@ -68,7 +68,7 @@ struct AnomalyResult : public ResultBase double min_pred_mask, max_pred_mask; cv::minMaxLoc(prediction.pred_mask, &min_pred_mask, &max_pred_mask); os << "anomaly_map min:" << min_anomaly_map << " max:" << max_anomaly_map << ";"; - os << "pred_score:" << prediction.pred_score << ";"; + os << "pred_score:" << std::fixed << std::setprecision(1) << prediction.pred_score << ";"; os << "pred_label:" << prediction.pred_label << ";"; os << std::fixed << std::setprecision(0) << "pred_mask min:" << min_pred_mask << " max:" << max_pred_mask << ";"; diff --git a/model_api/python/openvino/model_api/models/anomaly.py b/model_api/python/openvino/model_api/models/anomaly.py index 00cc299b..3a3bffb1 100644 --- a/model_api/python/openvino/model_api/models/anomaly.py +++ b/model_api/python/openvino/model_api/models/anomaly.py @@ -71,7 +71,7 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]): pred_mask = (anomaly_map >= self.pixel_threshold).astype(np.uint8) anomaly_map = self._normalize(anomaly_map, self.pixel_threshold) anomaly_map *= 255 - anomaly_map = anomaly_map.astype(np.uint8) + anomaly_map = np.round(anomaly_map).astype(np.uint8) pred_mask = cv2.resize( pred_mask, (meta["original_shape"][1], meta["original_shape"][0]) ) diff --git a/tests/python/accuracy/public_scope.json b/tests/python/accuracy/public_scope.json index d6c14c23..f61fbcd9 100644 --- a/tests/python/accuracy/public_scope.json +++ b/tests/python/accuracy/public_scope.json @@ -367,7 +367,7 @@ { "image": "coco128/images/train2017/000000000074.jpg", "reference": [ - "anomaly_map min:151 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;" + "anomaly_map min:152 max:255;pred_score:1.0;pred_label:Anomaly;pred_mask min:1 max:1;" ] } ]