-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_split.py
49 lines (37 loc) · 1.59 KB
/
test_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
import os
from shutil import copyfile
from sklearn.utils import shuffle
######################################################
# Specify the source folder containing subfolders named after genus, species and class id
# Use birdCLEF_sort_data.py in order to sort wav files accordingly
train_path = 'dataset/train/src/'
# Specify target folder for validation split
test_path = 'dataset/test/'
######################################################
# get classes from subfolders
classes = [c for c in sorted(os.listdir(train_path))]
# get files for classes
for c in classes:
# shuffle files
files = shuffle([train_path + c + "/" + f for f in os.listdir(train_path + c)], random_state=1337)
# choose amount of files for validation split from each class
# we want at least 1 sample per class (2 if sample count os between 12 and 20)
# we take 10% of the samples if sample count > 20
if len(files) <= 12:
num_test_files = 1
elif len(files) > 12 and len(files) <= 20:
num_test_files = 2
else:
num_test_files = int(len(files) * 0.1)
test_files = files[:num_test_files]
print c, len(files), len(test_files)
# copy test files for validation to target folder
for tf in test_files:
# copy test file
new_path = tf.replace(train_path, test_path).rsplit("/", 1)[0]
if not os.path.exists(new_path):
os.makedirs(new_path)
copyfile(tf, tf.replace(train_path, test_path))
# remove test file from train
# Note: You might want to test the script first before deleting any files :)
os.remove(tf)