Skip to content

Commit

Permalink
Merge pull request #143 from stephencrowell/dev/fix-failure-masking
Browse files Browse the repository at this point in the history
Fix test_sal_on_coco_dets failure being masked
  • Loading branch information
brianhhu authored Feb 22, 2024
2 parents cd4e8f4 + 756a55f commit 2204e99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/release_notes/pending_release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ Tests
* Fix various floating point equality comparisons to not use exact match.

* Fix random number usage from numpy to use `np.random.default_rng`.

* Fix error being masked in `test_sal_on_coco_dets`
22 changes: 10 additions & 12 deletions tests/utils/test_sal_on_coco_dets.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,16 @@ def test_coco_sal_gen(self, tmpdir: py.path.local) -> None:
output_dir = tmpdir.join('out')

runner = CliRunner()
# TODO: Assign return value to a "result" variable for later
runner.invoke(sal_on_coco_dets_cmd,
[str(dets_file), str(output_dir),
str(config_file), "-v"])
result = runner.invoke(sal_on_coco_dets_cmd,
[str(dets_file), str(output_dir),
str(config_file), "-v"])

# expected created directories for image saliency maps
img_dirs = [output_dir.join(d) for d in ["test_image1", "test_image2"]]
# detection ids that belong to each image
img_dets = [[1, 2, 3], [4, 5]]
img_dets = [[1, 2, 3], [5, 6]]

# TODO: This command is failing in reality but not being caught. This
# test needs to be fixed in the future.
# assert result.exit_code == 0
assert result.exit_code == 0
assert sorted(output_dir.listdir()) == sorted(img_dirs)
for img_dir, det_ids in zip(img_dirs, img_dets):
map_files = [img_dir.join(f"det_{det_id}.jpeg") for det_id in det_ids]
Expand All @@ -81,15 +78,16 @@ def test_coco_sal_gen_img_overlay(self, tmpdir: py.path.local) -> None:
output_dir = tmpdir.join('out')

runner = CliRunner()
runner.invoke(sal_on_coco_dets_cmd,
[str(dets_file), str(output_dir), str(config_file),
"--overlay-image"])
result = runner.invoke(sal_on_coco_dets_cmd,
[str(dets_file), str(output_dir), str(config_file),
"--overlay-image"])

# expected created directories for image saliency maps
img_dirs = [output_dir.join(d) for d in ["test_image1", "test_image2"]]
# detection ids that belong to each image
img_dets = [[1, 2, 3], [4, 5]]
img_dets = [[1, 2, 3], [5, 6]]

assert result.exit_code == 0
assert sorted(output_dir.listdir()) == sorted(img_dirs)
for img_dir, det_ids in zip(img_dirs, img_dets):
map_files = [img_dir.join(f"det_{det_id}.jpeg") for det_id in det_ids]
Expand Down
9 changes: 8 additions & 1 deletion xaitk_saliency/utils/bin/sal_on_coco_dets.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ def sal_on_coco_dets(
# The outputs of pase_coco_dset() are constructed using gid_to_aids, so we
# can assume the order of image and annotation ids in gid_to_aids here
# correspond correctly to that of the generated saliency maps.
img_skip_counter = 0
for img_idx, (img_id, det_ids) in enumerate(dets_dset.gid_to_aids.items()):
# skip if there are no dets for this image
if len(det_ids) == 0:
img_skip_counter += 1
continue # pragma: no cover

img_file = dets_dset.get_image_fpath(img_id)
Expand All @@ -131,9 +133,14 @@ def sal_on_coco_dets(

os.makedirs(sub_dir, exist_ok=True)

sal_skip_counter = 0
for sal_idx, det_id in enumerate(det_ids):
ann = dets_dset.anns[det_id]
if not ('score' in ann or 'prob' in ann):
sal_skip_counter += 1
continue

sal_map = img_sal_maps[img_idx][sal_idx]
sal_map = img_sal_maps[img_idx - img_skip_counter][sal_idx - sal_skip_counter]

fig = plt.figure()
plt.axis('off')
Expand Down

0 comments on commit 2204e99

Please sign in to comment.