strange-bench / code /tests /test_export_sam3_masks.py
sidgairo18's picture
Generation code: generic documentation
3726258 verified
Raw History Blame Contribute Delete
3.37 kB
"""CPU tests of the mask export (no GPU, no SAM 3).
"""
import json
import numpy as np
import pytest
from strangebench.masks import export_sam3_masks as E
def test_rle_round_trip_and_box_convention():
m = np.zeros((40, 60), dtype=np.uint8)
m[5:15, 10:30] = 255
rle = E.rle_from_mask(m)
assert rle["size"] == [40, 60] and isinstance(rle["counts"], str)
assert E.rle_bbox(rle) == [10.0, 5.0, 20.0, 10.0]
assert E.rle_bbox(E.rle_from_mask(np.zeros((4, 4), bool))) is None
def test_box_rle_covers_the_box():
rle = E.box_rle([10, 5, 20, 10], 40, 60)
assert E.rle_bbox(rle) == pytest.approx([10.0, 5.0, 20.0, 10.0], abs=1.0)
def test_match_raw_ann_picks_same_class_best_box():
raw = [{"category_name": "dog", "bbox": [0, 0, 10, 10]}, {"category_name": "cat", "bbox": [50, 50, 10, 10]},
{"category_name": "cat", "bbox": [0, 0, 10, 10]}]
assert E.match_raw_ann(raw, "cat", [1, 1, 10, 10])["bbox"] == [0, 0, 10, 10]
assert E.match_raw_ann(raw, "bird", [0, 0, 5, 5]) is None
def _tree(root, tier="tier2_1typical"):
(root / tier / "images").mkdir(parents=True)
d = {"images": [{"id": 1, "scene_id": "s1", "file_name": "s1.png", "height": 40, "width": 60}],
"categories": [{"id": 1, "name": "dog"}, {"id": 2, "name": "cat"}],
"annotations": [{"id": 7, "image_id": 1, "category_id": 1, "bbox": [10, 5, 20, 10], "area": 200, "iscrowd": 0},
{"id": 8, "image_id": 1, "category_id": 2, "bbox": [40, 20, 10, 10], "area": 100, "iscrowd": 1}]}
(root / tier / "annotations.json").write_text(json.dumps(d))
return d
def test_attach_writes_new_tree_and_keeps_boxes(tmp_path):
tree, out = tmp_path / "shipped", tmp_path / "out"
shipped = _tree(tree)
m = np.zeros((40, 60), dtype=np.uint8)
m[5:15, 10:30] = 255
rle = E.rle_from_mask(m)
E.write_json_atomic(out / "fragments" / "tier2_1typical" / "s1.json", {"scene_id": "s1", "anns": [
{"ann_id": 7, "category": "dog", "bbox_shipped": [10, 5, 20, 10], "segmentation": rle,
"mask_bbox": E.rle_bbox(rle), "box_iou": 1.0}]})
E.main(["attach", "--out-root", str(out), "--variant", f"balanced={tree}"])
got = json.load(open(out / "balanced" / "tier2_1typical" / "annotations.json"))
a7, a8 = got["annotations"]
assert a7["segmentation_source"] == "sam3" and a7["segmentation"] == rle
assert a8["segmentation_source"] == "box"
assert [a["bbox"] for a in got["annotations"]] == [a["bbox"] for a in shipped["annotations"]]
assert json.load(open(tree / "tier2_1typical" / "annotations.json")) == shipped
assert (out / "mask_report.csv").exists()
def test_attach_falls_back_to_box_and_refuses_missing(tmp_path):
tree, out = tmp_path / "shipped", tmp_path / "out"
_tree(tree)
with pytest.raises(SystemExit):
E.main(["attach", "--out-root", str(out), "--variant", f"balanced={tree}"])
E.write_json_atomic(out / "fragments" / "tier2_1typical" / "s1.json", {"scene_id": "s1", "anns": [
{"ann_id": 7, "category": "dog", "bbox_shipped": [10, 5, 20, 10], "segmentation": None, "box_iou": 0.0}]})
E.main(["attach", "--out-root", str(out), "--variant", f"balanced={tree}"])
got = json.load(open(out / "balanced" / "tier2_1typical" / "annotations.json"))
assert got["annotations"][0]["segmentation_source"] == "box"