-
Notifications
You must be signed in to change notification settings - Fork 1
/
loaddata.py
179 lines (138 loc) · 8.7 KB
/
loaddata.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
168
169
170
171
172
173
174
175
176
177
178
179
import math
import os
from os import listdir
from os.path import join
import numpy as np
from scipy.io import wavfile
from mfcc import get_mfcc_features
from preprocessing import remove_pauses, normalize_signal
from utils import recreate_file
def load_templates(sample_rate, signal, num_frames=25, num_mfcc=13, use_deltas=True):
x = []
samples_without_pauses = remove_pauses(sample_rate, normalize_signal(signal))
mfcc_features = get_mfcc_features(sample_rate, samples_without_pauses, num_mfcc, use_deltas)
templates = np.split(mfcc_features[:mfcc_features.shape[0] // num_frames * num_frames],
mfcc_features.shape[0] // num_frames)
for template in templates:
x.append(template)
return np.array(x)
def load_templates_with_out(sample_rate, signal, out, num_frames=25, num_mfcc=13, use_deltas=True):
x = []
y = []
samples_without_pauses = remove_pauses(sample_rate, normalize_signal(signal))
mfcc_features = get_mfcc_features(sample_rate, samples_without_pauses, num_mfcc, use_deltas)
templates = np.split(mfcc_features[:mfcc_features.shape[0] // num_frames * num_frames],
mfcc_features.shape[0] // num_frames)
for template in templates:
x.append(template)
y.append(out)
return x, y
# Загрузить шаблоны из файлов и присвоить им значение эталонного выхода out
def load_templates_from_files_with_out(files, out, num_templates=math.inf, num_frames=25, num_mfcc=13,
use_deltas=True):
x = []
y = []
for file in files:
(sample_rate, signal) = wavfile.read(file)
x_file, y_file = load_templates_with_out(sample_rate, signal, out, num_frames, num_mfcc, use_deltas)
x.extend(x_file)
y.extend(y_file)
if len(y) > num_templates:
break
if num_templates == math.inf:
return x, y
else:
if len(y) < num_templates:
raise RuntimeError(
'num_templates ({}) is too big: use value less than {}\nDirectory: {}'.format(num_templates, len(y),
files))
return x[:num_templates], y[:num_templates]
def load_speakers_data_authentication(num_frames=25, num_mfcc=13, use_deltas=True, num_registered_male=5,
num_registered_female=5, num_unregistered_male=5, num_unregistered_female=5,
num_train_templates=6, num_test_templates=4):
male_speakers = sorted(
[join("speakers", "russian", "male", d) for d in listdir(join("speakers", "russian", "male"))])
female_speakers = sorted(
[join("speakers", "russian", "female", d) for d in listdir(join("speakers", "russian", "female"))])
x_train, y_train, x_test, y_test = [], [], [], []
for speaker in male_speakers[:num_registered_male]:
files_registered = sorted([join(speaker, f) for f in listdir(speaker)])
x, y = load_templates_from_files_with_out(files=files_registered, out=1,
num_templates=num_train_templates + num_test_templates,
num_frames=num_frames, num_mfcc=num_mfcc, use_deltas=use_deltas)
x_train.extend(x[:num_train_templates])
y_train.extend(y[:num_train_templates])
x_test.extend(x[num_train_templates:num_train_templates + num_test_templates])
y_test.extend(y[num_train_templates:num_train_templates + num_test_templates])
for speaker in male_speakers[num_registered_male:num_registered_male + num_unregistered_male]:
files_unregistered = sorted([join(speaker, f) for f in listdir(speaker)])
x, y = load_templates_from_files_with_out(files=files_unregistered, out=0,
num_templates=num_train_templates + num_test_templates,
num_frames=num_frames, num_mfcc=num_mfcc, use_deltas=use_deltas)
x_train.extend(x[:num_train_templates])
y_train.extend(y[:num_train_templates])
x_test.extend(x[num_train_templates:num_train_templates + num_test_templates])
y_test.extend(y[num_train_templates:num_train_templates + num_test_templates])
for speaker in female_speakers[:num_registered_female]:
files_registered = sorted([join(speaker, f) for f in listdir(speaker)])
x, y = load_templates_from_files_with_out(files=files_registered, out=1,
num_templates=num_train_templates + num_test_templates,
num_frames=num_frames, num_mfcc=num_mfcc, use_deltas=use_deltas)
x_train.extend(x[:num_train_templates])
y_train.extend(y[:num_train_templates])
x_test.extend(x[num_train_templates:num_train_templates + num_test_templates])
y_test.extend(y[num_train_templates:num_train_templates + num_test_templates])
for speaker in female_speakers[num_registered_female:num_registered_female + num_unregistered_female]:
files_unregistered = sorted([join(speaker, f) for f in listdir(speaker)])
x, y = load_templates_from_files_with_out(files=files_unregistered, out=0,
num_templates=num_train_templates + num_test_templates,
num_frames=num_frames, num_mfcc=num_mfcc, use_deltas=use_deltas)
x_train.extend(x[:num_train_templates])
y_train.extend(y[:num_train_templates])
x_test.extend(x[num_train_templates:num_train_templates + num_test_templates])
y_test.extend(y[num_train_templates:num_train_templates + num_test_templates])
return (np.array(x_train), np.array(y_train)), (np.array(x_test), np.array(y_test))
def load_speakers_data_identification(num_frames=25, num_mfcc=13, use_deltas=True, num_speakers=50, num_female=9,
num_train_templates=6, num_test_templates=4):
num_male = num_speakers - num_female
speakers = sorted([join("speakers", "russian", "male", d) for d in listdir(join("speakers", "russian", "male"))])[
:num_male]
speakers.extend(
sorted([join("speakers", "russian", "female", d) for d in listdir(join("speakers", "russian", "female"))])[
:num_female])
return load_templates_from_directories(directories=speakers, num_frames=num_frames, num_mfcc=num_mfcc,
use_deltas=use_deltas, num_train_templates=num_train_templates,
num_test_templates=num_test_templates)
def load_templates_from_directories(directories, num_frames=25, num_mfcc=13, use_deltas=True, num_train_templates=10,
num_test_templates=2):
x_train, y_train, x_test, y_test = [], [], [], []
for i, speaker_directory in enumerate(directories):
speaker_files = sorted([join(speaker_directory, f) for f in listdir(speaker_directory)])
half_len = len(speaker_files) // 2
x, y = load_templates_from_files_with_out(files=speaker_files[:half_len], out=i,
num_templates=num_train_templates, num_frames=num_frames,
num_mfcc=num_mfcc, use_deltas=use_deltas)
x_train.extend(x[:num_train_templates])
y_train.extend(y[:num_train_templates])
x, y = load_templates_from_files_with_out(files=speaker_files[half_len:], out=i,
num_templates=num_test_templates, num_frames=num_frames,
num_mfcc=num_mfcc, use_deltas=use_deltas)
x_test.extend(x[:num_test_templates])
y_test.extend(y[:num_test_templates])
return (np.array(x_train), np.array(y_train)), (np.array(x_test), np.array(y_test))
def save_data_to_files(x_train, x_train_path, y_train, y_train_path, x_test=None, x_test_path=None, y_test=None,
y_test_path=None):
recreate_file(x_train_path)
np.save(x_train_path, x_train)
recreate_file(y_train_path)
np.save(y_train_path, y_train)
if x_test is not None:
recreate_file(x_test_path)
np.save(x_test_path, x_test)
if y_test is not None:
recreate_file(y_test_path)
np.save(y_test_path, y_test)
def load_data_from_files(x_train_path, y_train_path, x_test_path=None, y_test_path=None):
return (np.load(x_train_path), np.load(y_train_path)), \
(np.load(x_test_path) if x_test_path is not None else None,
np.load(y_test_path) if y_test_path is not None else None)