Rotate capped training samples across iterations
This commit is contained in:
@@ -7,6 +7,7 @@ import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@@ -49,6 +50,7 @@ def build_sampling(
|
||||
context_positive_repeat: int = 5,
|
||||
context_negative_repeat: int = 6,
|
||||
max_region_share: float = 0.65,
|
||||
sampling_round: int = 0,
|
||||
) -> tuple[list[str], dict[str, Any]]:
|
||||
if assessment.get("status") != "continue_training_loop":
|
||||
raise ValueError("Failure-driven sampling requires a failed assessment")
|
||||
@@ -62,6 +64,8 @@ def build_sampling(
|
||||
raise ValueError("Repeat factors must be positive")
|
||||
if not 0 < max_region_share <= 1:
|
||||
raise ValueError("max_region_share must be in (0, 1]")
|
||||
if sampling_round < 0:
|
||||
raise ValueError("sampling_round must be non-negative")
|
||||
|
||||
samples = {item["sample_slug"]: item for item in manifest["samples"]}
|
||||
gates = assessment["gates"]
|
||||
@@ -172,7 +176,13 @@ def build_sampling(
|
||||
base_paths = base_paths_by_region[region]
|
||||
extra_limit = capped_counts[region] - len(base_paths)
|
||||
image_paths.extend(base_paths)
|
||||
image_paths.extend(extra_paths_by_region[region][:extra_limit])
|
||||
extras = extra_paths_by_region[region]
|
||||
if extras and extra_limit < len(extras):
|
||||
# Rotate the capped repeat window between loop rounds so persistent
|
||||
# failures cannot yield the exact same training list indefinitely.
|
||||
offset = sampling_round % len(extras)
|
||||
extras = extras[offset:] + extras[:offset]
|
||||
image_paths.extend(extras[:extra_limit])
|
||||
repeat_counts = Counter({region: capped_counts[region] for region in capped_counts})
|
||||
if not image_paths:
|
||||
raise ValueError("No train-only tiles selected")
|
||||
@@ -195,6 +205,7 @@ def build_sampling(
|
||||
"context_positive_repeat": context_positive_repeat,
|
||||
"context_negative_repeat": context_negative_repeat,
|
||||
"max_region_share": max_region_share,
|
||||
"sampling_round": sampling_round,
|
||||
"source_train_tile_count": sum(
|
||||
1
|
||||
for tile in summary["tiles"]
|
||||
@@ -223,11 +234,16 @@ def main() -> int:
|
||||
parser.add_argument("--context-positive-repeat", type=int, default=5)
|
||||
parser.add_argument("--context-negative-repeat", type=int, default=6)
|
||||
parser.add_argument("--max-region-share", type=float, default=0.65)
|
||||
parser.add_argument("--sampling-round", type=int)
|
||||
args = parser.parse_args()
|
||||
|
||||
summary = json.loads(args.summary.read_text(encoding="utf-8"))
|
||||
manifest = json.loads(args.corpus_manifest.read_text(encoding="utf-8"))
|
||||
assessment = json.loads(args.assessment.read_text(encoding="utf-8"))
|
||||
sampling_round = args.sampling_round
|
||||
if sampling_round is None:
|
||||
match = re.fullmatch(r"iteration-(\d+)", args.output_dir.parent.name)
|
||||
sampling_round = int(match.group(1)) if match else 0
|
||||
paths, metadata = build_sampling(
|
||||
summary=summary,
|
||||
manifest=manifest,
|
||||
@@ -238,6 +254,7 @@ def main() -> int:
|
||||
context_positive_repeat=args.context_positive_repeat,
|
||||
context_negative_repeat=args.context_negative_repeat,
|
||||
max_region_share=args.max_region_share,
|
||||
sampling_round=sampling_round,
|
||||
)
|
||||
args.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
train_list = args.output_dir / "train-failure-driven.txt"
|
||||
|
||||
@@ -164,6 +164,7 @@ def failure_sampling_command(
|
||||
corpus_manifest: Path,
|
||||
assessment: Path,
|
||||
output_dir: Path,
|
||||
sampling_round: int = 0,
|
||||
) -> list[str]:
|
||||
return [
|
||||
sys.executable,
|
||||
@@ -172,6 +173,7 @@ def failure_sampling_command(
|
||||
"--corpus-manifest", str(corpus_manifest),
|
||||
"--assessment", str(assessment),
|
||||
"--output-dir", str(output_dir),
|
||||
"--sampling-round", str(sampling_round),
|
||||
]
|
||||
|
||||
|
||||
@@ -443,6 +445,7 @@ def main() -> int:
|
||||
corpus_manifest=args.corpus_manifest,
|
||||
assessment=assessment,
|
||||
output_dir=sampling_dir,
|
||||
sampling_round=index,
|
||||
),
|
||||
iteration_dir / "failure-driven-sampling.log",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user