-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_validate_split.py
91 lines (66 loc) · 2.02 KB
/
train_validate_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from os import getcwd
from os.path import join
from genericpath import isdir
from sys import argv
'''
argv[1] = percentage of images used for training between 0.0 - 1.0
argv[2] = images path
argv[3] = labels path
'''
class DirNotFoundError(Exception):
pass
path = getcwd()
def createDirs() -> None:
from os import mkdir
global path
path = join(path, "train_val_split")
cnt = 1
while isdir(path):
path = path[:-1] + str(cnt)
cnt += 1
mkdir(path)
imagesPath: str = join(path, "images")
labelsPath: str = join(path, "labels")
mkdir(imagesPath)
mkdir(labelsPath)
mkdir(join(imagesPath, "train"))
mkdir(join(imagesPath, "val"))
mkdir(join(labelsPath, "train"))
mkdir(join(labelsPath, "val"))
def trainValSplit() -> None:
from os import listdir
from shutil import copy2
trainingSize: float
if argv[1].replace(".", "").isnumeric():
trainingSize = float(argv[1])
else:
raise ValueError("argv[1] must be a float between 0.0 and 1.0")
if((trainingSize > 1.0) or (trainingSize < 0.0)):
raise ValueError("argv[1] must be a float between 0.0 and 1.0")
images: list = listdir(argv[2])
trainingSize: int = round(len(images) * trainingSize)
if (isdir(argv[2]) == False):
raise DirNotFoundError(f"The directory {argv[2]} does not exists")
cnt = 0
for file in images:
if cnt < trainingSize:
copy2(join(argv[2], file), join(path, join("images", "train")))
else:
copy2(join(argv[2], file), join(path, join("images", "val")))
cnt += 1
if (isdir(argv[3]) == False):
raise DirNotFoundError(f"The directory {argv[3]} does not exists")
cnt = 0
for file in listdir(argv[3]):
if cnt < trainingSize:
copy2(join(argv[3], file), join(path, join("labels", "train")))
else:
copy2(join(argv[3], file), join(path, join("labels", "val")))
cnt += 1
if __name__ == "__main__":
ARGS_SIZE = 4
SIZE = len(argv)
if SIZE != ARGS_SIZE:
raise ValueError(f"Expected {ARGS_SIZE}, but got {SIZE}")
createDirs()
trainValSplit()