-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapping_fixer.py
More file actions
257 lines (211 loc) · 9.95 KB
/
mapping_fixer.py
File metadata and controls
257 lines (211 loc) · 9.95 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import os
import shutil
from collections import defaultdict
class YOLOClassStandardizer:
def __init__(self, master_classes):
"""
Initialize with a master class list
Args:
master_classes: List of class names in desired order
"""
self.master_classes = master_classes
self.master_mapping = {name: idx for idx, name in enumerate(master_classes)}
def read_classes_file(self, classes_file):
"""Read classes from a classes.txt file"""
with open(classes_file, 'r') as f:
classes = [line.strip() for line in f if line.strip()]
return classes
def remap_annotation_file(self, annotation_file, old_mapping, output_file=None):
"""
Remap class IDs in a single YOLO annotation file
Args:
annotation_file: Path to .txt annotation file
old_mapping: Dict mapping class_name -> old_id
output_file: Output path (if None, overwrites original)
"""
if output_file is None:
output_file = annotation_file
# Create reverse mapping for old IDs
old_id_to_name = {v: k for k, v in old_mapping.items()}
lines = []
line_count = 0
with open(annotation_file, 'r') as f:
for line in f:
line_count += 1
line = line.strip()
# Skip empty lines
if not line:
continue
parts = line.split()
# YOLO format should have exactly 5 parts: class_id x_center y_center width height
if len(parts) >= 5:
try:
# Try to parse the first part as class ID
old_class_id = int(parts[0])
# Validate that other parts are floats (coordinates)
for i in range(1, 5):
float(parts[i])
# Get class name from old ID
if old_class_id in old_id_to_name:
class_name = old_id_to_name[old_class_id]
# Get new ID for this class
if class_name in self.master_mapping:
new_class_id = self.master_mapping[class_name]
parts[0] = str(new_class_id)
lines.append(' '.join(parts))
else:
print(f"⚠️ Warning: Class '{class_name}' not found in master mapping")
else:
print(f"⚠️ Warning: Old class ID {old_class_id} not found in mapping (file: {annotation_file}, line: {line_count})")
except ValueError as e:
print(f"⚠️ Skipping invalid line {line_count} in {annotation_file}: '{line}'")
print(f" Error: {e}")
continue
else:
print(f"⚠️ Skipping malformed line {line_count} in {annotation_file}: '{line}' (expected 5 parts, got {len(parts)})")
# Write remapped file
with open(output_file, 'w') as f:
for line in lines:
f.write(line + '\n')
def standardize_dataset(self, dataset_path, output_path=None, backup=True):
"""
Standardize a single dataset to use the master class mapping
Args:
dataset_path: Path to dataset folder
output_path: Output path (if None, modifies in place)
backup: Whether to create backup of original
"""
if output_path is None:
output_path = dataset_path
# Read current classes
classes_file = os.path.join(dataset_path, 'classes.txt')
if not os.path.exists(classes_file):
print(f"❌ No classes.txt found in {dataset_path}")
return False
current_classes = self.read_classes_file(classes_file)
current_mapping = {name: idx for idx, name in enumerate(current_classes)}
print(f"Standardizing dataset: {dataset_path}")
print(f"Current classes: {current_classes}")
print(f"Output: {output_path}")
# Create backup if requested
if backup and dataset_path == output_path:
backup_path = dataset_path + "_backup"
if os.path.exists(backup_path):
shutil.rmtree(backup_path)
shutil.copytree(dataset_path, backup_path)
print(f"✅ Backup created: {backup_path}")
# Create output directory if different
if output_path != dataset_path:
os.makedirs(output_path, exist_ok=True)
# Process all .txt files (excluding classes.txt)
txt_files = [f for f in os.listdir(dataset_path)
if f.endswith('.txt') and f != 'classes.txt']
remapped_count = 0
for txt_file in txt_files:
input_file = os.path.join(dataset_path, txt_file)
output_file = os.path.join(output_path, txt_file)
# Check if file is empty before processing
if os.path.getsize(input_file) == 0:
print(f"⚠️ Skipping empty file: {txt_file}")
# Still copy empty file to output
if input_file != output_file:
shutil.copy2(input_file, output_file)
continue
self.remap_annotation_file(input_file, current_mapping, output_file)
remapped_count += 1
# Copy other files if output is different directory
if output_path != dataset_path:
for file in os.listdir(dataset_path):
if not file.endswith('.txt'):
src = os.path.join(dataset_path, file)
dst = os.path.join(output_path, file)
if os.path.isfile(src):
shutil.copy2(src, dst)
elif os.path.isdir(src):
shutil.copytree(src, dst, dirs_exist_ok=True)
# Write new classes.txt with master class order
new_classes_file = os.path.join(output_path, 'classes.txt')
with open(new_classes_file, 'w') as f:
for class_name in self.master_classes:
f.write(class_name + '\n')
print(f"✅ Remapped {remapped_count} annotation files")
print(f"✅ Updated classes.txt with {len(self.master_classes)} classes in preferred order")
return True
def standardize_multiple_datasets(self, dataset_paths, output_dir=None, backup=True):
"""
Standardize multiple datasets to use the master class mapping
Args:
dataset_paths: List of dataset folder paths
output_dir: Directory to save standardized datasets (if None, modifies in place)
backup: Whether to create backups
"""
print("Master Class Mapping:")
print("=" * 40)
for idx, class_name in enumerate(self.master_classes):
print(f" {idx}: {class_name}")
print()
# Standardize each dataset
print("Standardizing datasets...")
print("=" * 40)
for i, dataset_path in enumerate(dataset_paths):
if output_dir:
dataset_name = os.path.basename(dataset_path.rstrip('/\\'))
output_path = os.path.join(output_dir, f"standardized_{dataset_name}")
else:
output_path = dataset_path
success = self.standardize_dataset(dataset_path, output_path, backup)
if success:
print(f"✅ Dataset {i+1} standardized successfully")
else:
print(f"❌ Failed to standardize dataset {i+1}")
print()
print("🎉 All datasets standardized!")
print(f"All datasets now use the same class mapping with {len(self.master_classes)} classes")
def main():
"""Example usage"""
# Dataset paths
dataset_paths = [
"batch1",
"batch2"
# Add more dataset paths here
]
# Your preferred class order - this will be the final mapping for all datasets
preferred_class_order = [
"archer",
"goblin",
"dart-goblin",
"barbarian",
"bomber",
"archer-queen",
"princess",
"pekka",
"giant-skeleton",
"prince",
"valkyrie",
"golden-knight",
"spear-goblin",
"goblin-machine",
"knight",
"mega-knight",
"bandit",
"executioner",
"royal-ghost",
"skeleton-king"
# Add your classes in preferred order
]
# Initialize standardizer with your preferred class order
standardizer = YOLOClassStandardizer(master_classes=preferred_class_order)
try:
# Standardize all datasets to use the preferred class order
standardizer.standardize_multiple_datasets(
dataset_paths=dataset_paths,
output_dir=None, # None = modify in place, or provide output directory
backup=True # Create backups before modifying
)
print("\n📋 Final Master Classes (used by all datasets):")
for idx, class_name in enumerate(standardizer.master_classes):
print(f" {idx}: {class_name}")
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
main()