Hand-Datasets-Raw / FreiHAND /shared-v1.1 /script /test_segment_eq255.py
orz2333's picture
Migrate HOT3D, OakInk-v1, OakInk-v2 and FreiHAND to shared-v1.1 (#4)
10ed3aa
Raw History Blame Contribute Delete
5.78 kB
import copy
from contextlib import closing
import json
from pathlib import Path
import tempfile
import unittest
import numpy as np
from segment_eq255 import (check_metadata, check_scope, check_totals, decode_frames, encode_frames, label_row,
new_metadata, scope_sequences, selected, LABEL)
def fixture():
return {"dataset_id": "FreiHAND", "split": "train", "num_frames": 1,
"components": {"mano": {"frozen": True}, "segment": {
"schema_id": "segment/foreground_intensity/v1", "entity_id": "hand_right",
"value_range": [0, 255], "normalization_scale": 1 / 255, "foreground_category": "hand",
"valid_ref": "annotation/segment/mask_valid.pt", "provenance": {"kind": "format_conversion"},
"storage": {"pixel_format": "gray", "codec": "ffv1", "source_lossy": True}}}}
class TestSelection(unittest.TestCase):
def test_every_possible_value(self):
a = np.arange(256, dtype=np.uint8).reshape(16, 16)
b = selected(a)
self.assertEqual(b.dtype, np.uint8)
self.assertEqual(int(np.count_nonzero(b)), 1)
self.assertEqual(b[-1, -1], 255)
self.assertEqual(a[0, 1], 1)
def test_widened_native_input(self):
a = np.array([[0, 1, 127, 248, 254, 255]], dtype=np.uint16)
np.testing.assert_array_equal(selected(a), [[0, 0, 0, 0, 0, 255]])
def test_reject_scaling_or_float(self):
for a in (np.array([[65535]], dtype=np.uint16), np.array([[1.0]], dtype=np.float32)):
with self.assertRaises(ValueError):
selected(a)
def test_no_morphological_changes(self):
a = np.array([[255, 254, 255], [255, 0, 255], [0, 255, 0]], dtype=np.uint8)
np.testing.assert_array_equal(selected(a) == 255, a == 255)
def test_zero_is_not_mapped_to_background(self):
self.assertEqual(label_row(2, False), {"frame_index": 2, "labels": []})
self.assertEqual(label_row(3, True)["labels"], [LABEL])
self.assertEqual(LABEL["entity_id"], "hand_right")
self.assertIsNone(LABEL["source_instance_id"])
def test_metadata_only_segment(self):
a = fixture()
old = copy.deepcopy(a)
b = new_metadata(a)
self.assertEqual(a, old)
check_metadata(a, b)
self.assertEqual(b["components"]["segment"]["schema_id"], "segment/uint16_labels/v1")
def test_other_annotation_mutation_rejected(self):
a = fixture()
b = new_metadata(a)
b["components"]["mano"]["frozen"] = False
with self.assertRaises(ValueError):
check_metadata(a, b)
def test_validity_reference_mutation_rejected(self):
a = fixture()
b = new_metadata(a)
b["components"]["segment"]["valid_ref"] = "changed.pt"
with self.assertRaises(ValueError):
check_metadata(a, b)
def test_eval_conversion_rejected(self):
a = fixture()
a["split"] = "test"
a["components"]["segment"]["schema_id"] = "segment/uint16_labels/v1"
with self.assertRaises(ValueError):
new_metadata(a)
def test_decoder_and_encoder_declarations_checked(self):
a = fixture()
for key, value in (("decoder", "scale gray8 to gray16le"),
("encoder_parameters", {"level": 3, "threads": 1, "input_pixel_format": "gray16le"})):
b = new_metadata(a)
b["components"]["segment"]["storage"][key] = value
with self.assertRaises(ValueError):
check_metadata(a, b)
def test_exact_pilot_and_full_scope(self):
self.assertEqual(scope_sequences("pilot"), ["evaluation_00000000", "training_00000000", "training_00126976"])
self.assertEqual(len(scope_sequences("all_33_shards")), 33)
with self.assertRaises(ValueError):
scope_sequences("invented")
def test_partial_full_claim_rejected(self):
with self.assertRaises(ValueError):
check_scope(Path("/not-used"), {"scope": "all_33_shards", "sequences": ["training_00000000"]})
def test_duplicate_or_missing_eval_rejected(self):
for seqs in (["training_00000000", "training_00126976"], scope_sequences("pilot") + ["training_00000000"]):
with self.assertRaises(ValueError):
check_scope(Path("/not-used"), {"scope": "pilot", "sequences": seqs})
def test_summary_recomputed(self):
views = [{"frames": 5, "pixels_checked": 500, "empty_after_selection_frames": [2]}]
good = {"training_frames": 5, "pixels_checked": 500, "empty_frames": 1}
check_totals(good, views)
for key in good:
bad = good | {key: good[key] + 1}
with self.assertRaises(ValueError):
check_totals(bad, views)
def test_lossless_gray8_video_roundtrip(self):
a = np.arange(256, dtype=np.uint8).reshape(16, 16)
frames = [selected(a), np.zeros_like(a), np.full_like(a, 255)]
with tempfile.TemporaryDirectory() as d:
path = Path(d) / "mask.mkv"
encode_frames(path, iter(frames), 16, 16)
with closing(decode_frames(path, "gray", 16, 16, 3)) as stream:
actual = list(stream)
for x, y in zip(frames, actual, strict=True):
self.assertEqual(y.dtype, np.uint16)
np.testing.assert_array_equal(x, y)
with self.assertRaises(ValueError):
with closing(decode_frames(path, "gray", 16, 16, 2)) as stream:
list(stream)
with self.assertRaises(ValueError):
with closing(decode_frames(path, "gray", 16, 16, 4)) as stream:
list(stream)
if __name__ == "__main__":
unittest.main()