-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare_data.py
executable file
·168 lines (133 loc) · 6.02 KB
/
prepare_data.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
This script is used to split the dataset into train, test and dev
More info on its usage is given in the READ.me file
@author: Taras Kucherenko
"""
import sys
import os
import shutil
import pandas
from os import path
sys.path.insert(1, os.path.join(sys.path[0], '..'))
NUM_OF_TEST = 90
FIRST_DATA_ID = 20
LAST_DATA_ID = 1182
AUGMENT = True
def _split_and_format_data(data_dir):
if not os.path.isdir(data_dir):
os.makedirs(data_dir)
_download_datasets(data_dir)
def _download_datasets(data_dir):
_create_dir(data_dir)
# prepare training data (including validation data)
for i in range (FIRST_DATA_ID, LAST_DATA_ID - NUM_OF_TEST):
filename = "audio" + str(i) + ".wav"
original_file_path = path.join("dataset/speech/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "train/inputs/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
filename = "gesture" + str(i) + ".bvh"
original_file_path = path.join("dataset/motion/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "train/labels/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
# prepare test data
for i in range(LAST_DATA_ID - NUM_OF_TEST, LAST_DATA_ID + 1,2):
filename = "audio" + str(i) + ".wav"
original_file_path = path.join("dataset/speech/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "test/inputs/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
filename = "gesture" + str(i) + ".bvh"
original_file_path = path.join("dataset/motion/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "test/labels/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
# prepare dev data (does not affect results of training at all)
for i in range(LAST_DATA_ID - NUM_OF_TEST + 1, LAST_DATA_ID + 1, 2):
filename = "audio" + str(i) + ".wav"
original_file_path = path.join("dataset/speech/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "dev/inputs/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
filename = "gesture" + str(i) + ".bvh"
original_file_path = path.join("dataset/motion/" + filename)
if os.path.exists(original_file_path):
target_file_path = path.join(data_dir + "dev/labels/" + filename)
print(target_file_path)
shutil.copy(original_file_path, target_file_path)
else:
print(original_file_path + " does not exist")
# data augmentation
if AUGMENT:
os.system('./data_processing/add_noisy_data.sh {0} {1} {2} {3}'.format("train", FIRST_DATA_ID, LAST_DATA_ID-NUM_OF_TEST, data_dir))
extracted_dir = path.join(data_dir)
dev_files, train_files, test_files = _format_datasets(extracted_dir)
dev_files.to_csv(path.join(extracted_dir, "gg-dev.csv"), index=False)
train_files.to_csv(path.join(extracted_dir, "gg-train.csv"), index=False)
test_files.to_csv(path.join(extracted_dir, "gg-test.csv"), index=False)
def _create_dir(data_dir):
dir_names = ["train", "test", "dev"]
sub_dir_names = ["inputs", "labels"]
# create ../data_dir/[train, test, dev]/[inputs, labels]
for dir_name in dir_names:
dir_path = path.join(data_dir, dir_name)
print(dir_path)
if not os.path.isdir(dir_path):
os.makedirs(dir_path) # ../data/train
for sub_dir_name in sub_dir_names:
dir_path = path.join(data_dir, dir_name, sub_dir_name)
print(dir_path)
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
def _format_datasets(extracted_dir):
train_files = _files_to_pandas_dataframe(extracted_dir, "train", range(FIRST_DATA_ID, LAST_DATA_ID - NUM_OF_TEST))
test_files = _files_to_pandas_dataframe(extracted_dir, "test", range(LAST_DATA_ID - NUM_OF_TEST, LAST_DATA_ID + 1, 2))
dev_files = _files_to_pandas_dataframe(extracted_dir, "dev", range(LAST_DATA_ID - NUM_OF_TEST+1, LAST_DATA_ID + 1,2))
return dev_files, train_files, test_files
def _files_to_pandas_dataframe(extracted_dir, set_name, idx_range):
files = []
for idx in idx_range:
# original files
try:
input_file = path.abspath(path.join(extracted_dir, set_name, "inputs", "audio" + str(idx) + ".wav"))
except OSError:
continue
try:
label_file = path.abspath(path.join(extracted_dir, set_name, "labels", "gesture" + str(idx) + ".bvh"))
except OSError:
continue
try:
wav_size = path.getsize(input_file)
except OSError:
continue
files.append((input_file, wav_size, label_file))
# noisy files
try:
noisy_input_file = path.abspath(path.join(extracted_dir, set_name, "inputs", "naudio" + str(idx) + ".wav"))
except OSError:
continue
try:
noisy_wav_size = path.getsize(noisy_input_file)
except OSError:
continue
print(str(idx))
files.append((noisy_input_file, noisy_wav_size, label_file))
return pandas.DataFrame(data=files, columns=["wav_filename", "wav_filesize", "bvh_filename"])
if __name__ == "__main__":
_split_and_format_data(sys.argv[1])