-
Notifications
You must be signed in to change notification settings - Fork 10
/
00_train.py
266 lines (215 loc) · 10.6 KB
/
00_train.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
########################################################################
# import default libraries
########################################################################
import os
import sys
import gc
########################################################################
########################################################################
# import additional libraries
########################################################################
import numpy as np
import scipy.stats
# from import
from tqdm import tqdm
try:
from sklearn.externals import joblib
except:
import joblib
# original lib
import common as com
import keras_model
########################################################################
########################################################################
# load parameter.yaml
########################################################################
param = com.yaml_load()
########################################################################
########################################################################
# visualizer
########################################################################
class visualizer(object):
def __init__(self):
import matplotlib.pyplot as plt
self.plt = plt
self.fig = self.plt.figure(figsize=(7, 5))
self.plt.subplots_adjust(wspace=0.3, hspace=0.3)
def loss_plot(self, loss, val_loss):
"""
Plot loss curve.
loss : list [ float ]
training loss time series.
val_loss : list [ float ]
validation loss time series.
return : None
"""
ax = self.fig.add_subplot(1, 1, 1)
ax.cla()
ax.plot(loss)
ax.plot(val_loss)
ax.set_title("Model loss")
ax.set_xlabel("Epoch")
ax.set_ylabel("Loss")
ax.legend(["Train", "Validation"], loc="upper right")
def save_figure(self, name):
"""
Save figure.
name : str
save png file path.
return : None
"""
self.plt.savefig(name)
########################################################################
########################################################################
# get data from the list for file paths
########################################################################
def file_list_to_data(file_list,
msg="calc...",
n_mels=64,
n_frames=5,
n_hop_frames=1,
n_fft=1024,
hop_length=512,
power=2.0):
"""
convert the file_list to a vector array.
file_to_vector_array() is iterated, and the output vector array is concatenated.
file_list : list [ str ]
.wav filename list of dataset
msg : str ( default = "calc..." )
description for tqdm.
this parameter will be input into "desc" param at tqdm.
return : numpy.array( numpy.array( float ) )
data for training (this function is not used for test.)
* dataset.shape = (number of feature vectors, dimensions of feature vectors)
"""
# calculate the number of dimensions
dims = n_mels * n_frames
# iterate file_to_vector_array()
for idx in tqdm(range(len(file_list)), desc=msg):
vectors = com.file_to_vectors(file_list[idx],
n_mels=n_mels,
n_frames=n_frames,
n_fft=n_fft,
hop_length=hop_length,
power=power)
vectors = vectors[: : n_hop_frames, :]
if idx == 0:
data = np.zeros((len(file_list) * vectors.shape[0], dims), float)
data[vectors.shape[0] * idx : vectors.shape[0] * (idx + 1), :] = vectors
return data
########################################################################
########################################################################
# main 00_train.py
########################################################################
if __name__ == "__main__":
# check mode
# "development": mode == True
# "evaluation": mode == False
mode = com.command_line_chk()
if mode is None:
sys.exit(-1)
# make output directory
os.makedirs(param["model_directory"], exist_ok=True)
# initialize the visualizer
visualizer = visualizer()
# load base_directory list
dirs = com.select_dirs(param=param, mode=mode)
# loop of the base directory
for idx, target_dir in enumerate(dirs):
print("\n===========================")
print("[{idx}/{total}] {target_dir}".format(target_dir=target_dir, idx=idx+1, total=len(dirs)))
# set path
machine_type = os.path.split(target_dir)[1]
model_file_path = "{model}/model_{machine_type}.hdf5".format(model=param["model_directory"],
machine_type=machine_type)
if os.path.exists(model_file_path):
com.logger.info("model exists")
continue
history_img = "{model}/history_{machine_type}.png".format(model=param["model_directory"],
machine_type=machine_type)
# pickle file for storing section names
section_names_file_path = "{model}/section_names_{machine_type}.pkl".format(model=param["model_directory"],
machine_type=machine_type)
# pickle file for storing anomaly score distribution
score_distr_file_path = "{model}/score_distr_{machine_type}.pkl".format(model=param["model_directory"],
machine_type=machine_type)
# get section names from wave file names
section_names = com.get_section_names(target_dir, dir_name="train")
unique_section_names = np.unique(section_names)
n_sections = unique_section_names.shape[0]
# make condition dictionary
joblib.dump(unique_section_names, section_names_file_path)
# generate dataset
print("============== DATASET_GENERATOR ==============")
# number of wave files in each section
# required for calculating y_pred for each wave file
n_files_ea_section = []
data = np.empty((0, param["feature"]["n_frames"] * param["feature"]["n_mels"]), float)
for section_idx, section_name in enumerate(unique_section_names):
# get file list for each section
# all values of y_true are zero in training
files, y_true = com.file_list_generator(target_dir=target_dir,
section_name=section_name,
dir_name="train",
mode=mode)
n_files_ea_section.append(len(files))
data_ea_section = file_list_to_data(files,
msg="generate train_dataset",
n_mels=param["feature"]["n_mels"],
n_frames=param["feature"]["n_frames"],
n_hop_frames=param["feature"]["n_hop_frames"],
n_fft=param["feature"]["n_fft"],
hop_length=param["feature"]["hop_length"],
power=param["feature"]["power"])
data = np.append(data, data_ea_section, axis=0)
# number of all files
n_all_files = sum(n_files_ea_section)
# number of vectors for each wave file
n_vectors_ea_file = int(data.shape[0] / n_all_files)
# make one-hot vector for conditioning
condition = np.zeros((data.shape[0], n_sections), float)
start_idx = 0
for section_idx in range(n_sections):
n_vectors = n_vectors_ea_file * n_files_ea_section[section_idx]
condition[start_idx : start_idx + n_vectors, section_idx : section_idx + 1] = 1
start_idx += n_vectors
# 1D vector to 2D image
data = data.reshape(data.shape[0], param["feature"]["n_frames"], param["feature"]["n_mels"], 1)
# train model
print("============== MODEL TRAINING ==============")
model = keras_model.get_model(param["feature"]["n_frames"],
param["feature"]["n_mels"],
n_sections,
param["fit"]["lr"])
model.summary()
history = model.fit(x=data,
y=condition,
epochs=param["fit"]["epochs"],
batch_size=param["fit"]["batch_size"],
shuffle=param["fit"]["shuffle"],
validation_split=param["fit"]["validation_split"],
verbose=param["fit"]["verbose"])
# calculate y_pred for fitting anomaly score distribution
y_pred = []
start_idx = 0
for section_idx in range(n_sections):
for file_idx in range(n_files_ea_section[section_idx]):
p = model.predict(data[start_idx : start_idx + n_vectors_ea_file, : , :, :])[:, section_idx : section_idx + 1]
y_pred.append(np.mean(np.log(np.maximum(1.0 - p, sys.float_info.epsilon)
- np.log(np.maximum(p, sys.float_info.epsilon)))))
start_idx += n_vectors_ea_file
# fit anomaly score distribution
shape_hat, loc_hat, scale_hat = scipy.stats.gamma.fit(y_pred)
gamma_params = [shape_hat, loc_hat, scale_hat]
joblib.dump(gamma_params, score_distr_file_path)
visualizer.loss_plot(history.history["loss"], history.history["val_loss"])
visualizer.save_figure(history_img)
model.save(model_file_path)
com.logger.info("save_model -> {}".format(model_file_path))
print("============== END TRAINING ==============")
del data
del condition
del model
keras_model.clear_session()
gc.collect()