From 2c9e6895707f2ec338c991ac5b0669e3a26b7499 Mon Sep 17 00:00:00 2001 From: yyq19990828 Date: Thu, 21 Aug 2025 17:23:29 +0800 Subject: [PATCH] Improve dataset handling with adaptive folder detection and better test dataset fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add adaptive val/valid folder detection to support both YOLO (val) and COCO (valid) dataset structures - Implement fallback mechanism for test dataset since most datasets don't include test split - Reduce the need for manual dataset modification by automatically handling different folder naming conventions - Add proper error handling with FileNotFoundError for missing validation folders - Convert Chinese comments to English for better internationalization 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- rfdetr/datasets/coco.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/rfdetr/datasets/coco.py b/rfdetr/datasets/coco.py index ef47a4bd9..48309c19d 100644 --- a/rfdetr/datasets/coco.py +++ b/rfdetr/datasets/coco.py @@ -238,12 +238,29 @@ def build_roboflow(image_set, args, resolution): root = Path(args.dataset_dir) assert root.exists(), f'provided Roboflow path {root} does not exist' mode = 'instances' + + # Adaptively detect val/valid folder name + val_folder = None + if (root / "valid").exists(): + val_folder = "valid" + elif (root / "val").exists(): + val_folder = "val" + else: + raise FileNotFoundError(f"Neither 'val' nor 'valid' folder found in {root}") + + # Build path mapping, optimizing test dataset handling PATHS = { "train": (root / "train", root / "train" / "_annotations.coco.json"), - "val": (root / "valid", root / "valid" / "_annotations.coco.json"), - "test": (root / "test", root / "test" / "_annotations.coco.json"), + "val": (root / val_folder, root / val_folder / "_annotations.coco.json"), } + # Handle test dataset: if test doesn't exist, use val dataset instead + if (root / "test").exists() and (root / "test" / "_annotations.coco.json").exists(): + PATHS["test"] = (root / "test", root / "test" / "_annotations.coco.json") + else: + print(f"Warning: test dataset not found, using {val_folder} dataset for testing") + PATHS["test"] = (root / val_folder, root / val_folder / "_annotations.coco.json") + img_folder, ann_file = PATHS[image_set.split("_")[0]] try: