-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_split.py
67 lines (60 loc) · 2.43 KB
/
image_split.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
56
57
58
59
60
61
62
63
64
65
66
67
import shutil
import os
import splitfolders
import csv
from csv_ann_type import csv_folder
def files_move(files, dir_from, dir_to):
for name in files:
shutil.move(dir_from + name, dir_to + name)
print(f"Moved {len(files)} files from directory: {dir_from} to: {dir_to}.")
train_ratio = 0.6
val_ratio = 0.2
test_ratio = 0.2
input_folder = "images/"
output_folder = "train_val_test"
test_im_group_F = "test_group_F/"
im_group_F = input_folder + "group_F/"
type_F = csv_folder + "type_F.csv"
test_split_group_F = output_folder + "/test/group_F/"
train_split_group_F = output_folder + "/train/group_F/"
val_split_group_F = output_folder + "/val/group_F/"
if __name__ == "__main__":
# Delete folder if it already exists.
if os.path.isdir(output_folder):
shutil.rmtree(output_folder)
# Create folder if it doesn't exist.
if not os.path.isdir(test_im_group_F):
os.makedirs(test_im_group_F)
# Getting the number of vectors of the whole file.
with open(type_F, "r", newline="") as f:
length = len(
list(csv.reader(f, delimiter=";", quotechar='"', quoting=csv.QUOTE_MINIMAL))
)
# Before splitting, transfer photos from group_F which will only be in the test set.
length = int(test_ratio * length) # Number of test files.
test_files = [
f for f in os.listdir(im_group_F) if f[-8:-5] != "mod" and int(f[:-5]) < length
]
files_move(test_files, im_group_F, test_im_group_F)
# The division of data into three sets: training, validation and test.
splitfolders.ratio(
input_folder,
output=output_folder,
seed=43,
ratio=(train_ratio, val_ratio, test_ratio),
)
# After splitting, transfer and dividing photos from group_F test set to train and val.
files = [f for f in os.listdir(test_split_group_F)]
split_val = int(len(files) * val_ratio / train_ratio)
train_files = files[split_val:]
val_files = files[:split_val]
files_move(train_files, test_split_group_F, train_split_group_F)
files_move(val_files, test_split_group_F, val_split_group_F)
# Copy photos back to original directory.
for name in test_files:
shutil.copy(test_im_group_F + name, im_group_F + name)
# Transfer not modified photos to the test set.
files_move(test_files, test_im_group_F, test_split_group_F)
# Delete folder temporary exists.
if os.path.isdir(test_im_group_F):
shutil.rmtree(test_im_group_F)