Image Segmentation
PyTorch
English
painting-vision-robotics-kit
semantic-segmentation
robotics
edge-ai
construction-ai
autonomous-painting
wall-painting-robot
paint-coverage-estimation
building-facade
drywall
skirting-detection
window-detection
lidar
depth-validation
segformer
deeplabv3
Eval Results (legacy)
Upload test_validation.py with huggingface_hub
Browse files- test_validation.py +206 -0
test_validation.py
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Tests for validation.py (numpy only). Run: python3 test_validation.py"""
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from label_schema import CLASSES, IGNORE, MATERIAL_CLASSES
|
| 6 |
+
from validation import COMPLETION_THRESHOLDS, ScoreAccumulator, choose_completion_threshold, split_coverage
|
| 7 |
+
|
| 8 |
+
C = {name: i for i, name in enumerate(CLASSES)}
|
| 9 |
+
M = {name: i for i, name in enumerate(MATERIAL_CLASSES)}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def scene():
|
| 13 |
+
"""64x64 wall: left half unpainted, right half painted, a window, a switch, skirting."""
|
| 14 |
+
labels = np.full((64, 64), C["wall_unpainted"], np.uint8)
|
| 15 |
+
labels[:, 32:] = C["wall_painted"]
|
| 16 |
+
labels[:6] = C["other"]
|
| 17 |
+
labels[56:] = C["skirting"]
|
| 18 |
+
labels[10:20, 10:24] = C["window"]
|
| 19 |
+
labels[30:35, 40:44] = C["switch_outlet"]
|
| 20 |
+
material = np.full((64, 64), IGNORE, np.uint8)
|
| 21 |
+
wall = np.isin(labels, [1, 2])
|
| 22 |
+
material[wall] = M["drywall"]
|
| 23 |
+
return labels, material
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_perfect_prediction_scores_present_classes_and_flags_the_rest():
|
| 27 |
+
labels, material = scene()
|
| 28 |
+
acc = ScoreAccumulator()
|
| 29 |
+
acc.add(labels, labels, material, material, confidence=np.ones(labels.shape))
|
| 30 |
+
r = acc.result()
|
| 31 |
+
for name in ("other", "wall_unpainted", "wall_painted", "skirting", "window", "switch_outlet"):
|
| 32 |
+
assert r["per_class"][name]["status"] == "scored" and r["per_class"][name]["iou"] == 1.0, name
|
| 33 |
+
for name in ("wall_uncertain", "ac_unit", "door", "wall_obstacle"):
|
| 34 |
+
entry = r["per_class"][name]
|
| 35 |
+
assert entry["status"] == "no_ground_truth" and entry["iou"] is None and entry["false_positive_px"] == 0
|
| 36 |
+
assert set(r["classes_without_ground_truth"]) == {"wall_uncertain", "ac_unit", "door", "wall_obstacle"}
|
| 37 |
+
assert r["mean_iou"] == 1.0 and r["mean_iou_gt_classes"] == 1.0 and r["pixel_accuracy"] == 1.0
|
| 38 |
+
assert r["material"]["wallpaper"]["status"] == "no_ground_truth"
|
| 39 |
+
assert r["ece_semantic"] == 0.0 and r["painted_coverage_mae"] == 0.0
|
| 40 |
+
assert r["wall_boundary_f1_3px"] == 1.0 and r["bottom_edge_mae_px"] == 0.0
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_false_positives_on_absent_class_are_counted_not_hidden():
|
| 44 |
+
labels, material = scene()
|
| 45 |
+
pred = labels.copy()
|
| 46 |
+
pred[40:50, 5:15] = C["door"] # 100 px of door that is not there
|
| 47 |
+
acc = ScoreAccumulator()
|
| 48 |
+
acc.add(pred, labels, material, material)
|
| 49 |
+
door = acc.result()["per_class"]["door"]
|
| 50 |
+
assert door["status"] == "no_ground_truth" and door["false_positive_px"] == 100
|
| 51 |
+
assert door["iou"] == 0.0 and door["image_false_alarm_rate"] == 1.0
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_small_fixture_detection_is_per_image():
|
| 55 |
+
labels, material = scene()
|
| 56 |
+
pred = labels.copy()
|
| 57 |
+
pred[30:35, 40:44] = C["wall_painted"] # switch missed entirely
|
| 58 |
+
acc = ScoreAccumulator()
|
| 59 |
+
acc.add(pred, labels, material, material)
|
| 60 |
+
acc.add(labels, labels, material, material) # found in the second frame
|
| 61 |
+
switch = acc.result()["per_class"]["switch_outlet"]
|
| 62 |
+
assert switch["images_with_gt"] == 2 and switch["image_detection_recall"] == 0.5
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_false_complete_and_coverage_bias():
|
| 66 |
+
labels, material = scene()
|
| 67 |
+
pred = labels.copy()
|
| 68 |
+
pred[pred == C["wall_unpainted"]] = C["wall_painted"] # declares the half-painted wall finished
|
| 69 |
+
acc = ScoreAccumulator()
|
| 70 |
+
acc.add(pred, labels, material, material)
|
| 71 |
+
r = acc.result()
|
| 72 |
+
assert r["false_complete_rate"] == 1.0 and r["false_complete_eligible_images"] == 1
|
| 73 |
+
assert r["painted_coverage_bias"] > 0.4
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_wallpaper_image_detection_and_material_scores():
|
| 77 |
+
labels, material = scene()
|
| 78 |
+
papered = material.copy()
|
| 79 |
+
papered[material == M["drywall"]] = M["wallpaper"]
|
| 80 |
+
acc = ScoreAccumulator()
|
| 81 |
+
acc.add(labels, labels, papered, papered) # wallpaper found
|
| 82 |
+
acc.add(labels, labels, material, papered) # wallpaper called drywall
|
| 83 |
+
acc.add(labels, labels, material, material) # plain drywall, correctly not wallpaper
|
| 84 |
+
r = acc.result()
|
| 85 |
+
assert r["wallpaper_image_recall"] == 0.5 and r["wallpaper_image_precision"] == 1.0
|
| 86 |
+
assert r["material"]["wallpaper"]["status"] == "scored"
|
| 87 |
+
assert 0.0 < r["wallpaper_recall"] < 1.0
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_calibration_error_when_overconfident():
|
| 91 |
+
labels, material = scene()
|
| 92 |
+
pred = labels.copy()
|
| 93 |
+
pred[labels == C["wall_painted"]] = C["wall_unpainted"] # half the wall wrong
|
| 94 |
+
acc = ScoreAccumulator()
|
| 95 |
+
acc.add(pred, labels, material, material, confidence=np.full(labels.shape, 0.99))
|
| 96 |
+
assert acc.result()["ece_semantic"] > 0.3
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_ignore_pixels_do_not_count():
|
| 100 |
+
labels, material = scene()
|
| 101 |
+
target = labels.copy()
|
| 102 |
+
target[:, :8] = IGNORE
|
| 103 |
+
pred = labels.copy()
|
| 104 |
+
pred[:, :8] = C["door"]
|
| 105 |
+
acc = ScoreAccumulator()
|
| 106 |
+
acc.add(pred, target, material, material)
|
| 107 |
+
assert acc.result()["per_class"]["door"]["false_positive_px"] == 0
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_groups_are_scored_separately():
|
| 111 |
+
labels, material = scene()
|
| 112 |
+
wrong = np.zeros_like(labels)
|
| 113 |
+
acc = ScoreAccumulator()
|
| 114 |
+
acc.add(labels, labels, material, material, groups=["source:synthetic"])
|
| 115 |
+
acc.add(wrong, labels, material, material, groups=["source:real"])
|
| 116 |
+
groups = acc.result()["by_group"]
|
| 117 |
+
assert groups["source:synthetic"]["mean_iou"] == 1.0 and groups["source:synthetic"]["images"] == 1
|
| 118 |
+
assert groups["source:real"]["mean_iou"] < 0.2
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_split_coverage_lists_missing():
|
| 122 |
+
presence = np.zeros((3, len(CLASSES)), bool)
|
| 123 |
+
presence[:, [0, 2, 8]] = True
|
| 124 |
+
material = np.zeros((3, len(MATERIAL_CLASSES)), bool)
|
| 125 |
+
material[:, 0] = True
|
| 126 |
+
report = split_coverage(presence, material)
|
| 127 |
+
assert "wall_unpainted" in report["classes_missing"] and "window" not in report["classes_missing"]
|
| 128 |
+
assert report["materials_missing"] == ["drywall", "wallpaper"]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def test_completion_threshold_suppresses_confidently_wrong_false_completes():
|
| 132 |
+
# Two unfinished walls the argmax calls fully painted: one at P(painted)=0.65
|
| 133 |
+
# on the unpainted half (hesitant), one at 0.99 (confidently wrong). Plus a
|
| 134 |
+
# truly finished wall painted at P=0.92.
|
| 135 |
+
acc = ScoreAccumulator()
|
| 136 |
+
unfinished, material = scene()
|
| 137 |
+
finished = unfinished.copy()
|
| 138 |
+
finished[finished == C["wall_unpainted"]] = C["wall_painted"]
|
| 139 |
+
pred = finished.copy()
|
| 140 |
+
for p_unpainted_half in (0.65, 0.99):
|
| 141 |
+
prob = np.where(unfinished == C["wall_unpainted"], p_unpainted_half, 0.99)
|
| 142 |
+
acc.add(pred, unfinished, material, material, painted_probability=prob)
|
| 143 |
+
acc.add(pred, finished, material, material, painted_probability=np.full(finished.shape, 0.92))
|
| 144 |
+
r = acc.result()
|
| 145 |
+
assert r["false_complete_rate"] == 1.0 # argmax: both unfinished walls "done"
|
| 146 |
+
points = {p["threshold"]: p for p in r["completion_operating_points"]}
|
| 147 |
+
assert set(points) == set(COMPLETION_THRESHOLDS)
|
| 148 |
+
assert points[0.5]["false_complete_rate"] == 1.0
|
| 149 |
+
assert points[0.7]["false_complete_rate"] == 0.5 and points[0.7]["complete_recall"] == 1.0
|
| 150 |
+
assert points[0.95]["complete_recall"] == 0.0 # too strict: the finished wall is refused too
|
| 151 |
+
assert points[0.95]["false_complete_rate"] == 0.5 # ...while the confidently wrong wall still passes
|
| 152 |
+
# Nothing separates the confidently wrong wall from the finished one, so no threshold qualifies.
|
| 153 |
+
assert r["completion_threshold"]["threshold"] is None and "no threshold" in r["completion_threshold"]["reason"]
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_choose_completion_threshold_takes_lowest_qualifying():
|
| 157 |
+
points = [{"threshold": t, "false_complete_rate": rate, "complete_recall": recall}
|
| 158 |
+
for t, rate, recall in ((0.5, 0.4, 1.0), (0.6, 0.0, 0.0), (0.7, 0.04, 0.9), (0.9, 0.0, 0.5))]
|
| 159 |
+
chosen = choose_completion_threshold(points)
|
| 160 |
+
assert chosen["threshold"] == 0.7 and chosen["complete_recall"] == 0.9 # 0.6 signs nothing off
|
| 161 |
+
none_eligible = [{"threshold": 0.5, "false_complete_rate": None, "complete_recall": 1.0}]
|
| 162 |
+
assert "no unfinished walls" in choose_completion_threshold(none_eligible)["reason"]
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def test_completion_points_absent_without_probabilities():
|
| 166 |
+
labels, material = scene()
|
| 167 |
+
acc = ScoreAccumulator()
|
| 168 |
+
acc.add(labels, labels, material, material)
|
| 169 |
+
assert "completion_operating_points" not in acc.result()
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def test_condition_scores_defects_per_pixel_and_per_image():
|
| 173 |
+
labels, material = scene()
|
| 174 |
+
wall = np.isin(labels, [1, 2])
|
| 175 |
+
cond = np.where(wall, 0, IGNORE).astype(np.uint8)
|
| 176 |
+
cond[40:50, 36:46] = 1 # a peeling patch
|
| 177 |
+
cond[40:50, 50:54] = 2 # a rust streak
|
| 178 |
+
acc = ScoreAccumulator()
|
| 179 |
+
acc.add(labels, labels, material, material, condition_pred=cond, condition_target=cond)
|
| 180 |
+
clean = np.where(wall, 0, IGNORE).astype(np.uint8)
|
| 181 |
+
false_rust = clean.copy()
|
| 182 |
+
false_rust[20:30, 40:50] = 2 # rust reported on a sound wall
|
| 183 |
+
acc.add(labels, labels, material, material, condition_pred=false_rust, condition_target=clean)
|
| 184 |
+
r = acc.result()
|
| 185 |
+
assert r["iou_paint_failure"] == 1.0 and r["paint_failure_image_recall"] == 1.0
|
| 186 |
+
assert r["rust_image_recall"] == 1.0 and r["rust_image_false_alarm_rate"] == 1.0
|
| 187 |
+
assert r["rust_precision"] == round(40 / 140, 5) and r["condition"]["sound"]["status"] == "scored"
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_condition_absent_without_masks():
|
| 191 |
+
labels, material = scene()
|
| 192 |
+
acc = ScoreAccumulator()
|
| 193 |
+
acc.add(labels, labels, material, material)
|
| 194 |
+
assert "condition_mean_iou" not in acc.result()
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def main():
|
| 198 |
+
tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")]
|
| 199 |
+
for test in tests:
|
| 200 |
+
test()
|
| 201 |
+
print(f"ok {test.__name__}")
|
| 202 |
+
print(f"{len(tests)} tests passed")
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
if __name__ == "__main__":
|
| 206 |
+
main()
|