Download split_correct.py from hyeon2525/Ku-Yolo-DataSet: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/datasets/hyeon2525/Ku-Yolo-DataSet/resolve/main/split_correct.py
- Command line
-
hf download hf://datasets/hyeon2525/Ku-Yolo-DataSet/split_correct.py
-
curl -L -o split_correct.py https://huggingface.co/datasets/hyeon2525/Ku-Yolo-DataSet/resolve/main/split_correct.py
3.55 kB
| """ | |
| 데이터셋 분할 - Windows 대소문자 문제 해결 | |
| 각 파일에 고유한 번호를 부여하여 충돌 방지 | |
| """ | |
| import os | |
| import shutil | |
| from pathlib import Path | |
| import random | |
| random.seed(42) | |
| # 1. 모든 이미지-라벨 쌍 찾기 | |
| print("Finding all image-label pairs...") | |
| pairs = [] | |
| for batch_num in range(1, 18): | |
| batch_name = f'batch_{batch_num}' | |
| img_dir = Path(f'data/{batch_name}') | |
| lbl_dir = Path(f'labels/{batch_name}') | |
| if not img_dir.exists(): | |
| continue | |
| # 라벨 파일 기준 | |
| for lbl_file in lbl_dir.glob('*.txt'): | |
| if lbl_file.name == 'classes.txt': | |
| continue | |
| stem = lbl_file.stem | |
| # 이미지 파일 찾기 | |
| img_file_jpg = img_dir / f'{stem}.jpg' | |
| img_file_JPG = img_dir / f'{stem}.JPG' | |
| img_file = None | |
| if img_file_jpg.exists(): | |
| img_file = img_file_jpg | |
| elif img_file_JPG.exists(): | |
| img_file = img_file_JPG | |
| if img_file and img_file.exists(): | |
| pairs.append({ | |
| 'image': str(img_file), | |
| 'label': str(lbl_file), | |
| 'stem': stem, | |
| 'ext': img_file.suffix | |
| }) | |
| print(f"Found {len(pairs)} valid pairs") | |
| # 2. 분할 | |
| random.shuffle(pairs) | |
| total = len(pairs) | |
| train_size = int(total * 0.8) | |
| val_size = int(total * 0.1) | |
| train_pairs = pairs[:train_size] | |
| val_pairs = pairs[train_size:train_size + val_size] | |
| test_pairs = pairs[train_size + val_size:] | |
| print(f"Train: {len(train_pairs)}, Val: {len(val_pairs)}, Test: {len(test_pairs)}") | |
| # 3. 디렉토리 생성 | |
| os.makedirs('dataset/images/train', exist_ok=True) | |
| os.makedirs('dataset/images/val', exist_ok=True) | |
| os.makedirs('dataset/images/test', exist_ok=True) | |
| os.makedirs('dataset/labels/train', exist_ok=True) | |
| os.makedirs('dataset/labels/val', exist_ok=True) | |
| os.makedirs('dataset/labels/test', exist_ok=True) | |
| # 4. 파일 복사 - 고유한 번호 부여 | |
| def copy_files(pairs, split): | |
| print(f"\nCopying {split}...") | |
| for idx, pair in enumerate(pairs): | |
| if (idx + 1) % 200 == 0: | |
| print(f" {idx+1}/{len(pairs)}...") | |
| # 고유한 파일명 생성 (idx를 이용) | |
| # {idx:05d}_{stem}{ext} 형식 | |
| img_dst = f"dataset/images/{split}/{idx:05d}_{pair['stem']}{pair['ext']}" | |
| lbl_dst = f"dataset/labels/{split}/{idx:05d}_{pair['stem']}.txt" | |
| shutil.copy2(pair['image'], img_dst) | |
| shutil.copy2(pair['label'], lbl_dst) | |
| print(f" Done! {len(pairs)} files copied") | |
| # 검증 | |
| actual_count = len(os.listdir(f"dataset/images/{split}")) | |
| print(f" Verified: {actual_count} files in directory") | |
| if actual_count != len(pairs): | |
| print(f" WARNING: Expected {len(pairs)} but found {actual_count}") | |
| copy_files(train_pairs, 'train') | |
| copy_files(val_pairs, 'val') | |
| copy_files(test_pairs, 'test') | |
| # 5. data.yaml | |
| with open('dataset/data.yaml', 'w') as f: | |
| f.write(f"""# TACO Waste Classification Dataset | |
| path: {Path('dataset').absolute()} | |
| train: images/train | |
| val: images/val | |
| test: images/test | |
| nc: 5 | |
| names: ['Plastic', 'Vinyl', 'Can', 'Glass', 'Paper'] | |
| """) | |
| print("\ndata.yaml created") | |
| print("\n=== DONE ===") | |
| print(f"\nFinal verification:") | |
| print(f" Train: {len(os.listdir('dataset/images/train'))} images, {len(os.listdir('dataset/labels/train'))} labels") | |
| print(f" Val: {len(os.listdir('dataset/images/val'))} images, {len(os.listdir('dataset/labels/val'))} labels") | |
| print(f" Test: {len(os.listdir('dataset/images/test'))} images, {len(os.listdir('dataset/labels/test'))} labels") | |