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_squeeze_surfaces.py with huggingface_hub
Browse files- test_squeeze_surfaces.py +82 -0
test_squeeze_surfaces.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Tests for squeeze_surfaces.py (offline operating-point sweep). Run: python3 test_squeeze_surfaces.py"""
|
| 3 |
+
import csv
|
| 4 |
+
import tempfile
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from eval_surfaces import detection
|
| 8 |
+
from squeeze_surfaces import at_threshold, best_by_f1, read_rows, sweep_drywall
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def write_rows(path, rows):
|
| 12 |
+
with path.open("w", newline="") as handle:
|
| 13 |
+
writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
|
| 14 |
+
writer.writeheader()
|
| 15 |
+
writer.writerows(rows)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def sample_rows():
|
| 19 |
+
# Three bare-board photos (high drywall fraction) and two look-alikes (low).
|
| 20 |
+
return [
|
| 21 |
+
{"file": "a.jpg", "label": "bare_drywall", "wall_pixels": "1000", "drywall_fraction": "0.9",
|
| 22 |
+
"model_fraction": "0.0", "cue_score": "0.0", "paint_failure_fraction": "0.0", "rust_fraction": "0.0",
|
| 23 |
+
"defect_fraction": "0.0"},
|
| 24 |
+
{"file": "b.jpg", "label": "bare_drywall", "wall_pixels": "1000", "drywall_fraction": "0.6",
|
| 25 |
+
"model_fraction": "0.0", "cue_score": "0.0", "paint_failure_fraction": "0.0", "rust_fraction": "0.0",
|
| 26 |
+
"defect_fraction": "0.0"},
|
| 27 |
+
{"file": "c.jpg", "label": "bare_drywall", "wall_pixels": "1000", "drywall_fraction": "0.3",
|
| 28 |
+
"model_fraction": "0.0", "cue_score": "0.0", "paint_failure_fraction": "0.0", "rust_fraction": "0.0",
|
| 29 |
+
"defect_fraction": "0.0"},
|
| 30 |
+
{"file": "d.jpg", "label": "wallpaper", "wall_pixels": "1000", "drywall_fraction": "0.0",
|
| 31 |
+
"model_fraction": "0.9", "cue_score": "0.0", "paint_failure_fraction": "0.0", "rust_fraction": "0.0",
|
| 32 |
+
"defect_fraction": "0.0"},
|
| 33 |
+
{"file": "e.jpg", "label": "brick", "wall_pixels": "1000", "drywall_fraction": "0.1",
|
| 34 |
+
"model_fraction": "0.0", "cue_score": "0.0", "paint_failure_fraction": "0.0", "rust_fraction": "0.0",
|
| 35 |
+
"defect_fraction": "0.0"},
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_read_rows_parses_scores_as_floats():
|
| 40 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 41 |
+
path = Path(tmp) / "rows.csv"
|
| 42 |
+
write_rows(path, sample_rows())
|
| 43 |
+
rows = read_rows(path)
|
| 44 |
+
assert len(rows) == 5 and isinstance(rows[0]["drywall_fraction"], float)
|
| 45 |
+
assert rows[0]["label"] == "bare_drywall"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_sweep_drywall_matches_the_eval_rule():
|
| 49 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 50 |
+
path = Path(tmp) / "rows.csv"
|
| 51 |
+
write_rows(path, sample_rows())
|
| 52 |
+
rows = read_rows(path)
|
| 53 |
+
curve = sweep_drywall(rows, step=0.1)
|
| 54 |
+
point = at_threshold(curve["curve"], 0.2)
|
| 55 |
+
group = [r for r in rows if r["label"] in {"bare_drywall", "wallpaper", "brick"}]
|
| 56 |
+
expected = detection([float(r["drywall_fraction"]) >= point["threshold"] for r in group],
|
| 57 |
+
[r["label"] == "bare_drywall" for r in group])
|
| 58 |
+
assert point["precision"] == expected["precision"] and point["recall"] == expected["recall"]
|
| 59 |
+
# At 0.2 the 0.3/0.6/0.9 board photos fire; the 0.0/0.1 look-alikes do not.
|
| 60 |
+
assert point["tp"] == 3 and point["fp"] == 0
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_best_by_f1_prefers_the_higher_threshold_on_ties():
|
| 64 |
+
curve = [{"threshold": 0.1, "f1": 0.5, "recall": 0.5, "precision": 0.5},
|
| 65 |
+
{"threshold": 0.2, "f1": 0.5, "recall": 0.5, "precision": 0.5},
|
| 66 |
+
{"threshold": 0.3, "f1": 0.4, "recall": 0.4, "precision": 0.4}]
|
| 67 |
+
assert best_by_f1(curve)["threshold"] == 0.2 # tie -> smaller threshold loses
|
| 68 |
+
assert best_by_f1([{"threshold": 0.1, "f1": None, "recall": None, "precision": None}]) is None
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def test_at_threshold_snaps_to_the_nearest_grid_point():
|
| 72 |
+
curve = [{"threshold": t} for t in (0.0, 0.5, 1.0)]
|
| 73 |
+
assert at_threshold(curve, 0.4)["threshold"] == 0.5
|
| 74 |
+
assert at_threshold(curve, 0.1)["threshold"] == 0.0
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
tests = [(name, fn) for name, fn in sorted(globals().items()) if name.startswith("test_")]
|
| 79 |
+
for name, fn in tests:
|
| 80 |
+
fn()
|
| 81 |
+
print("ok ", name)
|
| 82 |
+
print(f"{len(tests)} tests passed")
|