Add leak-free regional YOLO dataset builder
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "build_regional_yolo_dataset.py"
|
||||
SPEC = importlib.util.spec_from_file_location("build_regional_yolo_dataset", SCRIPT)
|
||||
assert SPEC and SPEC.loader
|
||||
module = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(module)
|
||||
|
||||
|
||||
def test_regional_dataset_balances_contexts_and_keeps_validation_closed() -> None:
|
||||
manifest = {"samples": [
|
||||
{"sample_slug": "f-train", "region": "flanders", "split": "train", "context": "ribbon"},
|
||||
{"sample_slug": "f-val", "region": "flanders", "split": "val", "context": "mixed"},
|
||||
{"sample_slug": "w-train", "region": "wallonia", "split": "train", "context": "ribbon"},
|
||||
]}
|
||||
summary = {"tiles": [
|
||||
{"sample_slug": "f-train", "split": "train", "image_path": "/f-pos.png", "kept": True},
|
||||
{"sample_slug": "f-train", "split": "train", "image_path": "/f-neg.png", "is_negative": True},
|
||||
{"sample_slug": "f-val", "split": "val", "image_path": "/f-val.png"},
|
||||
{"sample_slug": "w-train", "split": "train", "image_path": "/w.png"},
|
||||
]}
|
||||
train, val, evidence = module.build(
|
||||
summary=summary, manifest=manifest, region="flanders",
|
||||
priority_contexts={"ribbon"}, priority_repeat=3, negative_repeat=2,
|
||||
)
|
||||
assert train == ["/f-pos.png"] * 3 + ["/f-neg.png"] * 2
|
||||
assert val == ["/f-val.png"]
|
||||
assert evidence["negative_train_entry_count"] == 2
|
||||
assert evidence["protected_samples_in_training"] == []
|
||||
|
||||
|
||||
def test_regional_dataset_rejects_protected_tiles() -> None:
|
||||
manifest = {"samples": [
|
||||
{"sample_slug": "f-cal", "region": "flanders", "split": "calibration", "context": "ribbon"},
|
||||
]}
|
||||
summary = {"tiles": [{"sample_slug": "f-cal", "split": "calibration", "image_path": "/cal.png"}]}
|
||||
with pytest.raises(ValueError, match="protected"):
|
||||
module.build(
|
||||
summary=summary, manifest=manifest, region="flanders",
|
||||
priority_contexts=set(), priority_repeat=1, negative_repeat=1,
|
||||
)
|
||||
Reference in New Issue
Block a user