-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_prepsplit.py
50 lines (40 loc) · 1.89 KB
/
dataset_prepsplit.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
from sklearn.model_selection import train_test_split
import os
import shutil
def trainTestSplit(augmentedFolderPath, saveDir, testSize : float = 0.25):
'''
Разбивает аугментированные данные из папки [augmentedFolderPath] в train и val в указанную папку \n
augmentedFolderPath - папка откуда брать \n
saveDir - папка куда сохранять \n
testSize - соотношение тестовой выборки \n
'''
print("TTS started!")
files : list[str] = os.listdir(augmentedFolderPath)
imgs = list(filter(lambda it : it.endswith("jpg"), files))
labels = list(filter(lambda it : it.endswith("txt"), files))
print("augmented_imgs_len:", len(imgs))
print("augmented_labels_len", len(labels))
trainDir = os.path.join(saveDir, "train")
valDir = os.path.join(saveDir, "val")
trainImgsDir = os.path.join(trainDir, "images")
trainLabelsDir = os.path.join(trainDir, "labels")
valImgsDir = os.path.join(valDir, "images")
valLabelsDir = os.path.join(valDir, "labels")
try:
os.mkdir(saveDir)
os.mkdir(trainDir)
os.mkdir(valDir)
os.mkdir(trainImgsDir)
os.mkdir(trainLabelsDir)
os.mkdir(valImgsDir)
os.mkdir(valLabelsDir)
except: pass
train_img, val_img, train_labels, val_labels = train_test_split(imgs, labels, test_size=testSize)
for img in train_img: moveTo(img, augmentedFolderPath, trainImgsDir)
for img in val_img: moveTo(img, augmentedFolderPath, valImgsDir)
for label in train_labels: moveTo(label, augmentedFolderPath, trainLabelsDir)
for label in val_labels: moveTo(label, augmentedFolderPath, valLabelsDir)
def moveTo(filename, fromFolder, toFolder):
fromPath = os.path.join(fromFolder, filename)
toPath = os.path.join(toFolder, filename)
shutil.move(fromPath, toPath)