diff --git a/README.md b/README.md index 61bffa96..85cefba2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ [![badge-unittests](https://github.com/xaitk/xaitk-saliency/actions/workflows/ci-unittests.yml/badge.svg)](https://github.com/XAITK/xaitk-saliency/actions/workflows/ci-unittests.yml) [![badge-notebooks](https://github.com/xaitk/xaitk-saliency/actions/workflows/ci-example-notebooks.yml/badge.svg)](https://github.com/XAITK/xaitk-saliency/actions/workflows/ci-example-notebooks.yml) [![codecov](https://codecov.io/gh/XAITK/xaitk-saliency/branch/master/graph/badge.svg?token=VHRNXYCNCG)](https://codecov.io/gh/XAITK/xaitk-saliency) -[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/XAITK/xaitk-saliency.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/XAITK/xaitk-saliency/context:python) # XAITK - Saliency The `xaitk-saliency` package is an open source, Explainable AI (XAI) framework diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 92abc014..0c6f875c 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -17,3 +17,4 @@ Release Notes release_notes/v0.8.2 release_notes/v0.8.3 release_notes/v0.9.0 + release_notes/v0.9.1 diff --git a/docs/release_notes/v0.9.1.rst b/docs/release_notes/v0.9.1.rst new file mode 100644 index 00000000..004c76ab --- /dev/null +++ b/docs/release_notes/v0.9.1.rst @@ -0,0 +1,20 @@ +v0.9.1 +====== + +Fixed a bug where if no detections were found in an image, then the generator would fail. + +Updates / New Features +---------------------- + +Documentation + +* Removed a deprecated badge from the README. + +Implementations + +* Added a check to exit early if no detections were found in `PerturbationOcclusion` + +* Added a check to exit early if no saliency maps were generated in `GenerateObjectDetectorBlackboxSaliency` + +Fixes +----- diff --git a/poetry.lock b/poetry.lock index 40e9f376..a189843d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1711,6 +1711,7 @@ optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, !=3.6.*" files = [ {file = "jsonpointer-2.4-py2.py3-none-any.whl", hash = "sha256:15d51bba20eea3165644553647711d150376234112651b4f1811022aecad7d7a"}, + {file = "jsonpointer-2.4.tar.gz", hash = "sha256:585cee82b70211fa9e6043b7bb89db6e1aa49524340dde8ad6b63206ea689d88"}, ] [[package]] @@ -2997,6 +2998,7 @@ description = "Nvidia JIT LTO Library" optional = true python-versions = ">=3" files = [ + {file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_aarch64.whl", hash = "sha256:004186d5ea6a57758fd6d57052a123c73a4815adf365eb8dd6a85c9eaa7535ff"}, {file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_x86_64.whl", hash = "sha256:d9714f27c1d0f0895cd8915c07a87a1d0029a0aa36acaf9156952ec2a8a12189"}, {file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-win_amd64.whl", hash = "sha256:c3401dc8543b52d3a8158007a0c1ab4e9c768fcbd24153a48c86972102197ddd"}, ] @@ -3800,6 +3802,7 @@ files = [ {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"}, + {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"}, {file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"}, {file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"}, {file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"}, @@ -3807,8 +3810,16 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"}, + {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, {file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, + {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, + {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, + {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, + {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, + {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"}, {file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"}, @@ -3825,6 +3836,7 @@ files = [ {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"}, + {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"}, {file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"}, {file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"}, {file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"}, @@ -3832,6 +3844,7 @@ files = [ {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"}, + {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"}, {file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"}, {file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"}, {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, @@ -4144,6 +4157,7 @@ files = [ {file = "scikit_image-0.21.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8ef5d8d1099317b7b315b530348cbfa68ab8ce32459de3c074d204166951025c"}, {file = "scikit_image-0.21.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:78b1e96c59cab640ca5c5b22c501524cfaf34cbe0cb51ba73bd9a9ede3fb6e1d"}, {file = "scikit_image-0.21.0-cp39-cp39-win_amd64.whl", hash = "sha256:9cffcddd2a5594c0a06de2ae3e1e25d662745a26f94fda31520593669677c010"}, + {file = "scikit_image-0.21.0.tar.gz", hash = "sha256:b33e823c54e6f11873ea390ee49ef832b82b9f70752c8759efd09d5a4e3d87f0"}, ] [package.dependencies] diff --git a/pyproject.toml b/pyproject.toml index 264fb9af..0739bd0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ name = "xaitk_saliency" # REMEMBER: `distutils.version.*Version` types can be used to compare versions # from strings like this. # This package prefers to use the strict numbering standard when possible. -version = "0.9.0" +version = "0.9.1" description = """\ Visual saliency map generation interfaces and baseline implementations \ for explainable AI.""" @@ -25,7 +25,6 @@ classifiers = [ 'Operating System :: MacOS :: MacOS X', 'Operating System :: Unix', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: 3.11', diff --git a/tests/impls/gen_object_detector_blackbox_sal/test_occlusion_based.py b/tests/impls/gen_object_detector_blackbox_sal/test_occlusion_based.py index 8326de14..3e56ea3e 100644 --- a/tests/impls/gen_object_detector_blackbox_sal/test_occlusion_based.py +++ b/tests/impls/gen_object_detector_blackbox_sal/test_occlusion_based.py @@ -12,6 +12,10 @@ from xaitk_saliency.utils.masking import occlude_image_batch +def _perturb(ref_image: np.ndarray) -> np.ndarray: + return np.ones((6, *ref_image.shape[:2]), dtype=bool) + + class TestPerturbationOcclusion: def teardown(self) -> None: @@ -62,70 +66,40 @@ def test_generate_success(self) -> None: Test successfully invoking _generate(). """ - class StubPI (PerturbImage): - """ - Stub perturber that returns masks of ones. - """ - - def perturb(self, ref_image: np.ndarray) -> np.ndarray: - return np.ones((6, *ref_image.shape[:2]), dtype=bool) - - get_config = None # type: ignore - - class StubGen (GenerateDetectorProposalSaliency): - """ - Stub saliency generator that returns zeros with correct shape. - """ - - def generate( - self, - ref_dets: np.ndarray, - pert_dets: np.ndarray, - pert_masks: np.ndarray - ) -> np.ndarray: - return np.zeros((ref_dets.shape[0], *pert_masks.shape[1:]), dtype=np.float16) - - get_config = None # type: ignore - - class StubDetector (DetectImageObjects): - """ - Stub object detector that returns known detections. - """ - - def detect_objects( - self, - img_iter: Iterable[np.ndarray] - ) -> Iterable[Iterable[Tuple[AxisAlignedBoundingBox, Dict[Hashable, float]]]]: - for i, _ in enumerate(img_iter): - # Return different number of detections for each image to - # test padding functinality - yield [( - AxisAlignedBoundingBox((0, 0), (1, 1)), - {'class0': 0.0, 'class1': 0.9} - ) for _ in range(i)] - - get_config = None # type: ignore - - test_pi = StubPI() - test_gen = StubGen() - test_detector = StubDetector() + def detect_objects( + img_iter: Iterable[np.ndarray] + ) -> Iterable[Iterable[Tuple[AxisAlignedBoundingBox, Dict[Hashable, float]]]]: + for i, _ in enumerate(img_iter): + # Return different number of detections for each image to + # test padding functinality + yield [( + AxisAlignedBoundingBox((0, 0), (1, 1)), + {'class0': 0.0, 'class1': 0.9} + ) for _ in range(i)] test_image = np.ones((64, 64, 3), dtype=np.uint8) test_bboxes = np.ones((3, 4)) test_scores = np.ones((3, 2)) + m_perturb = mock.Mock(spec=PerturbImage) + m_perturb.return_value = _perturb(test_image) + m_gen = mock.Mock(spec=GenerateDetectorProposalSaliency) + m_gen.return_value = np.zeros((3, 64, 64)) + m_detector = mock.Mock(spec=DetectImageObjects) + m_detector.detect_objects = detect_objects + # Call with default fill with mock.patch( 'xaitk_saliency.impls.gen_object_detector_blackbox_sal.occlusion_based.occlude_image_batch', wraps=occlude_image_batch ) as m_occ_img: - inst = PerturbationOcclusion(test_pi, test_gen) + inst = PerturbationOcclusion(m_perturb, m_gen) test_result = inst._generate( test_image, test_bboxes, test_scores, - test_detector + m_detector, ) assert test_result.shape == (3, 64, 64) @@ -143,13 +117,13 @@ def detect_objects( 'xaitk_saliency.impls.gen_object_detector_blackbox_sal.occlusion_based.occlude_image_batch', wraps=occlude_image_batch ) as m_occ_img: - inst = PerturbationOcclusion(test_pi, test_gen) + inst = PerturbationOcclusion(m_perturb, m_gen) inst.fill = test_fill test_result = inst._generate( test_image, test_bboxes, test_scores, - test_detector + m_detector, ) assert test_result.shape == (3, 64, 64) @@ -160,3 +134,40 @@ def detect_objects( m_kwargs = m_occ_img.call_args[-1] assert "fill" in m_kwargs assert m_kwargs['fill'] == test_fill + + def test_empty_detections(self) -> None: + """ + Test invoking _generate() with empty detections. + """ + + def detect_objects( + img_iter: Iterable[np.ndarray] + ) -> Iterable[Iterable[Tuple[AxisAlignedBoundingBox, Dict[Hashable, float]]]]: + for i, _ in enumerate(img_iter): + # Return 0 detections for each image + yield [] + + m_detector = mock.Mock(spec=DetectImageObjects) + m_detector.detect_objects = detect_objects + + test_image = np.ones((64, 64, 3), dtype=np.uint8) + + test_bboxes = np.ones((3, 4)) + test_scores = np.ones((3, 2)) + + m_perturb = mock.Mock(spec=PerturbImage) + m_perturb.return_value = _perturb(test_image) + m_gen = mock.Mock(spec=GenerateDetectorProposalSaliency) + m_gen.return_value = np.zeros((3, 64, 64)) + m_detector = mock.Mock(spec=DetectImageObjects) + m_detector.detect_objects = detect_objects + + inst = PerturbationOcclusion(m_perturb, m_gen) + test_result = inst._generate( + test_image, + test_bboxes, + test_scores, + m_detector, + ) + + assert len(test_result) == 0 diff --git a/tests/interfaces/test_gen_object_detector_blackbox_sal.py b/tests/interfaces/test_gen_object_detector_blackbox_sal.py index 1d457a60..e066b017 100644 --- a/tests/interfaces/test_gen_object_detector_blackbox_sal.py +++ b/tests/interfaces/test_gen_object_detector_blackbox_sal.py @@ -301,3 +301,38 @@ def test_call_alias() -> None: None # no objectness passed ) assert test_ret == expected_return + + +def test_return_empty_map() -> None: + """ + Test that an empty array of maps is returned properly + """ + m_impl = mock.Mock(spec=GenerateObjectDetectorBlackboxSaliency) + m_detector = mock.Mock(spec=DetectImageObjects) + + # test reference detections inputs with matching lengths + test_bboxes = np.ones((5, 4), dtype=float) + test_scores = np.ones((5, 3), dtype=float) + + # 2-channel image as just HxW should work + test_image = np.ones((256, 256), dtype=np.uint8) + + expected_return = np.array([]) + m_impl._generate.return_value = expected_return + + test_ret = GenerateObjectDetectorBlackboxSaliency.generate( + m_impl, + test_image, + test_bboxes, + test_scores, + m_detector, + ) + + m_impl._generate.assert_called_with( + test_image, + test_bboxes, + test_scores, + m_detector, + None # no objectness passed + ) + assert len(test_ret) == 0 diff --git a/xaitk_saliency/impls/gen_object_detector_blackbox_sal/occlusion_based.py b/xaitk_saliency/impls/gen_object_detector_blackbox_sal/occlusion_based.py index 4f73e7f1..a807e126 100644 --- a/xaitk_saliency/impls/gen_object_detector_blackbox_sal/occlusion_based.py +++ b/xaitk_saliency/impls/gen_object_detector_blackbox_sal/occlusion_based.py @@ -75,6 +75,9 @@ def _generate( pert_dets_mat = _dets_to_formatted_mat(pert_dets) + if pert_dets_mat.shape[1] == 0: + return np.array([]) + return self._generator( ref_dets_mat, pert_dets_mat, diff --git a/xaitk_saliency/interfaces/gen_object_detector_blackbox_sal.py b/xaitk_saliency/interfaces/gen_object_detector_blackbox_sal.py index 5ac71d50..7f2e2e21 100644 --- a/xaitk_saliency/interfaces/gen_object_detector_blackbox_sal.py +++ b/xaitk_saliency/interfaces/gen_object_detector_blackbox_sal.py @@ -1,3 +1,5 @@ +import logging + import numpy as np import abc from typing import Optional @@ -6,6 +8,7 @@ from smqtk_detection import DetectImageObjects from xaitk_saliency.exceptions import ShapeMismatchError +logger = logging.getLogger(__name__) class GenerateObjectDetectorBlackboxSaliency (Plugfigurable): @@ -144,6 +147,10 @@ def generate( objectness, ) + if len(output) == 0: + logging.info("No detections found for image. Check DetectImageObjects and saliency configuation") + return output + # Check that the saliency heatmaps' shape matches the reference image. if output.shape[1:] != ref_image.shape[:2]: raise ShapeMismatchError(