-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_categories.py
55 lines (50 loc) · 1.87 KB
/
split_categories.py
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
import os
import shutil
import random
items = os.listdir(os.path.abspath(os.path.join(os.pardir, "images")))
train, test, validate = 0, 0, 0
shutil.rmtree("Train", ignore_errors=True)
shutil.rmtree("Test", ignore_errors=True)
shutil.rmtree("Validate", ignore_errors=True)
os.makedirs("Train", exist_ok=True)
os.makedirs("Test", exist_ok=True)
os.makedirs("Validate", exist_ok=True)
for item in items:
if item == "README.md":
continue
os.makedirs(os.path.join("Train", item), exist_ok=True)
os.makedirs(os.path.join("Test", item), exist_ok=True)
os.makedirs(os.path.join("Validate", item), exist_ok=True)
for item in items:
if item == "README.md":
continue
images = os.listdir(os.path.abspath(os.path.join(os.pardir, "images", item)))
random.shuffle(images)
num_images = len(images)
num_train = int(0.7 * num_images)
num_test = int(0.2 * num_images)
num_validate = int(0.1 * num_images)
train_images = images[:num_train]
test_images = images[num_train : num_train + num_test]
validate_images = images[num_train + num_test :]
for image in train_images:
shutil.copy(
os.path.abspath(os.path.join(os.pardir, "images", item, image)),
os.path.abspath(os.path.join("Train", item, image)),
)
train += 1
for image in test_images:
shutil.copy(
os.path.abspath(os.path.join(os.pardir, "images", item, image)),
os.path.abspath(os.path.join("Test", item, image)),
)
test += 1
for image in validate_images:
shutil.copy(
os.path.abspath(os.path.join(os.pardir, "images", item, image)),
os.path.abspath(os.path.join("Validate", item, image)),
)
validate += 1
print("Number of Images in Train: ", train)
print("Number of Images in Test: ", test)
print("Number of Images in Validate: ", validate)