Align code with polygon-only training
Browse files
scripts/prepare_polygon_dataset.py
CHANGED
|
@@ -20,7 +20,7 @@ from PIL import Image
|
|
| 20 |
IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".tif", ".tiff"}
|
| 21 |
MASK_SUFFIXES = {".png", ".tif", ".tiff", ".jpg", ".jpeg"}
|
| 22 |
POLYGON_FORMATS = {"coco_polygon", "coco_segmentation", "geojson", "shp", "mask", "binary_mask", "semantic_mask"}
|
| 23 |
-
BBOX_FORMATS = {"
|
| 24 |
|
| 25 |
|
| 26 |
@dataclass
|
|
@@ -39,7 +39,7 @@ class ImportStats:
|
|
| 39 |
def parse_args() -> argparse.Namespace:
|
| 40 |
parser = argparse.ArgumentParser(description=__doc__)
|
| 41 |
parser.add_argument("--manifest", required=True, help="JSONL manifest with standardized sample records.")
|
| 42 |
-
parser.add_argument("--
|
| 43 |
parser.add_argument("--extra-source-root", action="append", default=[], help="Local source roots to scan for mask/polygon datasets.")
|
| 44 |
parser.add_argument("--output-root", required=True)
|
| 45 |
parser.add_argument("--train-ratio", type=float, default=0.8)
|
|
@@ -80,13 +80,13 @@ def local_path_from_record(record: dict[str, Any], key: str) -> Path | None:
|
|
| 80 |
return path if path.exists() else None
|
| 81 |
|
| 82 |
|
| 83 |
-
def
|
| 84 |
-
if
|
| 85 |
return None
|
| 86 |
source = str(record.get("image_path") or "")
|
| 87 |
stem = Path(source).stem.lower()
|
| 88 |
for split in ("train", "val", "test"):
|
| 89 |
-
image_dir =
|
| 90 |
if not image_dir.exists():
|
| 91 |
continue
|
| 92 |
for path in image_dir.iterdir():
|
|
@@ -195,7 +195,7 @@ def main() -> None:
|
|
| 195 |
manifest = Path(args.manifest)
|
| 196 |
output_root = Path(args.output_root)
|
| 197 |
output_root.mkdir(parents=True, exist_ok=True)
|
| 198 |
-
|
| 199 |
stats = ImportStats()
|
| 200 |
accepted: list[dict[str, Any]] = []
|
| 201 |
rejected: list[dict[str, Any]] = []
|
|
@@ -210,7 +210,7 @@ def main() -> None:
|
|
| 210 |
continue
|
| 211 |
quality = float(record.get("quality_score") or 0.0)
|
| 212 |
fmt = str(record.get("annotation_format") or "").lower()
|
| 213 |
-
image_path = local_path_from_record(record, "image_path") or
|
| 214 |
label_path = local_path_from_record(record, "mask_path") or local_path_from_record(record, "annotation_path")
|
| 215 |
|
| 216 |
reject_reason = None
|
|
|
|
| 20 |
IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".tif", ".tiff"}
|
| 21 |
MASK_SUFFIXES = {".png", ".tif", ".tiff", ".jpg", ".jpeg"}
|
| 22 |
POLYGON_FORMATS = {"coco_polygon", "coco_segmentation", "geojson", "shp", "mask", "binary_mask", "semantic_mask"}
|
| 23 |
+
BBOX_FORMATS = {"bbox", "box_txt", "coco_bbox", "voc_bbox"}
|
| 24 |
|
| 25 |
|
| 26 |
@dataclass
|
|
|
|
| 39 |
def parse_args() -> argparse.Namespace:
|
| 40 |
parser = argparse.ArgumentParser(description=__doc__)
|
| 41 |
parser.add_argument("--manifest", required=True, help="JSONL manifest with standardized sample records.")
|
| 42 |
+
parser.add_argument("--bbox-source-root", default=None, help="Optional local bbox dataset mirror for rejection auditing.")
|
| 43 |
parser.add_argument("--extra-source-root", action="append", default=[], help="Local source roots to scan for mask/polygon datasets.")
|
| 44 |
parser.add_argument("--output-root", required=True)
|
| 45 |
parser.add_argument("--train-ratio", type=float, default=0.8)
|
|
|
|
| 80 |
return path if path.exists() else None
|
| 81 |
|
| 82 |
|
| 83 |
+
def find_local_bbox_image(record: dict[str, Any], bbox_root: Path | None) -> Path | None:
|
| 84 |
+
if bbox_root is None:
|
| 85 |
return None
|
| 86 |
source = str(record.get("image_path") or "")
|
| 87 |
stem = Path(source).stem.lower()
|
| 88 |
for split in ("train", "val", "test"):
|
| 89 |
+
image_dir = bbox_root / "images" / split
|
| 90 |
if not image_dir.exists():
|
| 91 |
continue
|
| 92 |
for path in image_dir.iterdir():
|
|
|
|
| 195 |
manifest = Path(args.manifest)
|
| 196 |
output_root = Path(args.output_root)
|
| 197 |
output_root.mkdir(parents=True, exist_ok=True)
|
| 198 |
+
bbox_root = Path(args.bbox_source_root) if args.bbox_source_root else None
|
| 199 |
stats = ImportStats()
|
| 200 |
accepted: list[dict[str, Any]] = []
|
| 201 |
rejected: list[dict[str, Any]] = []
|
|
|
|
| 210 |
continue
|
| 211 |
quality = float(record.get("quality_score") or 0.0)
|
| 212 |
fmt = str(record.get("annotation_format") or "").lower()
|
| 213 |
+
image_path = local_path_from_record(record, "image_path") or find_local_bbox_image(record, bbox_root)
|
| 214 |
label_path = local_path_from_record(record, "mask_path") or local_path_from_record(record, "annotation_path")
|
| 215 |
|
| 216 |
reject_reason = None
|