-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_dataset.py
More file actions
115 lines (85 loc) · 3.57 KB
/
Copy pathsplit_dataset.py
File metadata and controls
115 lines (85 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import shutil
import argparse
import random
from pathlib import Path
def get_file_pairs(input_dir):
"""
Find all JPG/JSON file pairs in the input directory.
Returns a list of tuples (jpg_path, json_path).
"""
jpg_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.jpg')]
file_pairs = []
for jpg_file in jpg_files:
base_name = os.path.splitext(jpg_file)[0]
json_file = base_name + '.json'
jpg_path = os.path.join(input_dir, jpg_file)
json_path = os.path.join(input_dir, json_file)
# Check if corresponding JSON file exists
if os.path.exists(json_path):
file_pairs.append((jpg_path, json_path))
else:
print(f"Warning: Found JPG file without corresponding JSON: {jpg_file}")
return file_pairs
def split_dataset(input_dir, output_dir, train_ratio=0.8):
"""
Split the dataset into train and validation sets.
"""
# Create output directories
train_dir = os.path.join(output_dir, 'train')
valid_dir = os.path.join(output_dir, 'valid')
os.makedirs(train_dir, exist_ok=True)
os.makedirs(valid_dir, exist_ok=True)
# Get all file pairs
file_pairs = get_file_pairs(input_dir)
if not file_pairs:
print("No JPG/JSON file pairs found in the input directory.")
return
# Shuffle the file pairs
random.shuffle(file_pairs)
# Calculate split index
split_idx = int(len(file_pairs) * train_ratio)
# Split into train and validation
train_pairs = file_pairs[:split_idx]
valid_pairs = file_pairs[split_idx:]
print(f"Total pairs: {len(file_pairs)}")
print(f"Train pairs: {len(train_pairs)}")
print(f"Validation pairs: {len(valid_pairs)}")
# Copy train files
for jpg_path, json_path in train_pairs:
jpg_filename = os.path.basename(jpg_path)
json_filename = os.path.basename(json_path)
# Copy JPG file
dst_jpg_path = os.path.join(train_dir, jpg_filename)
shutil.copy2(jpg_path, dst_jpg_path)
# Copy JSON file
dst_json_path = os.path.join(train_dir, json_filename)
shutil.copy2(json_path, dst_json_path)
# Copy validation files
for jpg_path, json_path in valid_pairs:
jpg_filename = os.path.basename(jpg_path)
json_filename = os.path.basename(json_path)
# Copy JPG file
dst_jpg_path = os.path.join(valid_dir, jpg_filename)
shutil.copy2(jpg_path, dst_jpg_path)
# Copy JSON file
dst_json_path = os.path.join(valid_dir, json_filename)
shutil.copy2(json_path, dst_json_path)
print(f"Dataset split completed!")
print(f"Train files copied to: {train_dir}")
print(f"Validation files copied to: {valid_dir}")
def main():
parser = argparse.ArgumentParser(description='Split JPG/JSON dataset into train and validation sets.')
parser.add_argument('--input_dir', required=True, help='Input directory containing JPG/JSON file pairs')
parser.add_argument('--output_dir', required=True, help='Output directory to create train/valid subdirectories')
args = parser.parse_args()
# Validate input directory
if not os.path.isdir(args.input_dir):
print(f"Error: Input directory does not exist: {args.input_dir}")
return
# Create output directory if it doesn't exist
os.makedirs(args.output_dir, exist_ok=True)
# Split the dataset
split_dataset(args.input_dir, args.output_dir)
if __name__ == '__main__':
main()