Balance positives during precision correction

This commit is contained in:
Jens
2026-07-27 14:22:30 +02:00
parent 9e2dde2b19
commit 100d9e220b
2 changed files with 43 additions and 1 deletions
@@ -35,10 +35,11 @@ def build_sampling(
assessment: dict[str, Any],
positive_repeat: int = 3,
negative_repeat: int = 4,
precision_positive_repeat: int = 1,
) -> tuple[list[str], dict[str, Any]]:
if assessment.get("status") != "continue_training_loop":
raise ValueError("Failure-driven sampling requires a failed assessment")
if positive_repeat < 1 or negative_repeat < 1:
if positive_repeat < 1 or negative_repeat < 1 or precision_positive_repeat < 1:
raise ValueError("Repeat factors must be positive")
samples = {item["sample_slug"]: item for item in manifest["samples"]}
@@ -78,6 +79,10 @@ def build_sampling(
repeat = 1
if tile["label_count"] > 0 and region in weak_recall_regions:
repeat = positive_repeat
elif tile["label_count"] > 0 and region in weak_precision_regions:
# Precision-only correction still needs positive examples to avoid
# shifting the classifier toward background and sacrificing recall.
repeat = precision_positive_repeat
if tile["label_count"] == 0 and (background_failed or region in weak_precision_regions):
repeat = negative_repeat
path = str(Path(tile["image_path"]).resolve())
@@ -97,6 +102,7 @@ def build_sampling(
"background_gate_failed": background_failed,
"positive_repeat": positive_repeat,
"negative_repeat": negative_repeat,
"precision_positive_repeat": precision_positive_repeat,
"source_train_tile_count": sum(
1
for tile in summary["tiles"]
@@ -119,6 +125,7 @@ def main() -> int:
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--positive-repeat", type=int, default=3)
parser.add_argument("--negative-repeat", type=int, default=4)
parser.add_argument("--precision-positive-repeat", type=int, default=1)
args = parser.parse_args()
summary = json.loads(args.summary.read_text(encoding="utf-8"))
@@ -130,6 +137,7 @@ def main() -> int:
assessment=assessment,
positive_repeat=args.positive_repeat,
negative_repeat=args.negative_repeat,
precision_positive_repeat=args.precision_positive_repeat,
)
args.output_dir.mkdir(parents=True, exist_ok=True)
train_list = args.output_dir / "train-failure-driven.txt"