-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitcode.py
More file actions
57 lines (42 loc) · 1.51 KB
/
splitcode.py
File metadata and controls
57 lines (42 loc) · 1.51 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
import os
import shutil
import random
# Paths
SOURCE_DIR = "/home/surya/AgroScan~/data/AgroScan_Balanced"
DEST_DIR = "/home/surya/AgroScan~/data/AgroScan_Split"
shutil.rmtree(DEST_DIR, ignore_errors=True)
# Split ratio
train_ratio = 0.7
val_ratio = 0.15
test_ratio = 0.15
# Create folders
for split in ["train", "val", "test"]:
os.makedirs(os.path.join(DEST_DIR, split), exist_ok=True)
# Process each class
for cls in os.listdir(SOURCE_DIR):
class_path = os.path.join(SOURCE_DIR, cls)
if not os.path.isdir(class_path):
continue
images = [f for f in os.listdir(class_path)
if f.lower().endswith(('.jpg', '.png', '.jpeg', '.webp'))]
random.shuffle(images)
total = len(images)
train_end = int(total * train_ratio)
val_end = int(total * (train_ratio + val_ratio))
train_imgs = images[:train_end]
val_imgs = images[train_end:val_end]
test_imgs = images[val_end:]
# Create class folders
for split in ["train", "val", "test"]:
os.makedirs(os.path.join(DEST_DIR, split, cls), exist_ok=True)
# Copy files
for img in train_imgs:
shutil.copy(os.path.join(class_path, img),
os.path.join(DEST_DIR, "train", cls, img))
for img in val_imgs:
shutil.copy(os.path.join(class_path, img),
os.path.join(DEST_DIR, "val", cls, img))
for img in test_imgs:
shutil.copy(os.path.join(class_path, img),
os.path.join(DEST_DIR, "test", cls, img))
print("✅ Dataset Split Completed")