-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
351 lines (292 loc) · 12.4 KB
/
common.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
@file common.py
@brief Commonly used script
@author Toshiki Nakamura, Yuki Nikaido, and Yohei Kawaguchi (Hitachi Ltd.)
Copyright (C) 2020 Hitachi, Ltd. All right reserved.
"""
########################################################################
# import python-library
########################################################################
# default
import csv
import glob
import argparse
import itertools
import re
import sys
import os
# additional
import numpy
import librosa
import librosa.core
import librosa.feature
import yaml
from tqdm import tqdm
########################################################################
########################################################################
# setup STD I/O
########################################################################
"""
Standard output is logged in "baseline.log".
"""
import logging
logging.basicConfig(level=logging.DEBUG, filename="baseline.log")
logger = logging.getLogger(' ')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
########################################################################
########################################################################
# version
########################################################################
__versions__ = "1.0.0"
########################################################################
########################################################################
# argparse
########################################################################
def command_line_chk():
parser = argparse.ArgumentParser(description='Without option argument, it will not run properly.')
parser.add_argument('-v', '--version', action='store_true', help="show application version")
parser.add_argument('-c', '--config', type=str, default = "baseline.yml", help="specify yml config")
args = parser.parse_args()
if args.version:
print("===============================")
print("HLS4ML TOYADMOS ANOMALY DETECTION\nversion {}".format(__versions__))
print("===============================\n")
return args
########################################################################
########################################################################
# load parameter.yaml
########################################################################
def yaml_load(config):
with open(config) as stream:
param = yaml.safe_load(stream)
return param
########################################################################
########################################################################
# file I/O
########################################################################
# wav file Input
def file_load(wav_name, mono=False):
"""
load .wav file.
wav_name : str
target .wav file
sampling_rate : int
audio file sampling_rate
mono : boolean
When load a multi channels file and this param True, the returned data will be merged for mono data
return : numpy.array( float )
"""
try:
return librosa.load(wav_name, sr=None, mono=mono)
except:
logger.error("file_broken or not exists!! : {}".format(wav_name))
def save_csv(save_file_path,
save_data):
with open(save_file_path, "w", newline="") as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerows(save_data)
def save_dat(data, filename):
numpy.savetxt(filename, data, delimiter=' ', newline='\n', fmt='%g')
########################################################################
########################################################################
# feature extractor
########################################################################
def file_to_vector_array(file_name,
n_mels=64,
frames=5,
n_fft=1024,
hop_length=512,
power=2.0,
downsample=True):
"""
convert file_name to a vector array.
file_name : str
target .wav file
return : numpy.array( numpy.array( float ) )
vector array
* dataset.shape = (dataset_size, feature_vector_length)
"""
# 01 calculate the number of dimensions
dims = n_mels * frames
# 02 generate melspectrogram using librosa
y, sr = file_load(file_name)
mel_spectrogram = librosa.feature.melspectrogram(y=y,
sr=sr,
n_fft=n_fft,
hop_length=hop_length,
n_mels=n_mels,
power=power)
mel_spectrogram = mel_spectrogram[:,50:250]
# 03 convert melspectrogram to log mel energy
log_mel_spectrogram = 20.0 / power * numpy.log10(mel_spectrogram + sys.float_info.epsilon)
#03.5 trim to 50 to 250
# 04 calculate total vector size
vector_array_size = len(log_mel_spectrogram[0, :]) - frames + 1
# 05 skip too short clips
if vector_array_size < 1:
return numpy.empty((0, dims))
# 06 generate feature vectors by concatenating multiframes
#downsample mel spectrogram
if downsample:
n_mels = 32
frames = 4
vector_array = numpy.zeros((vector_array_size, n_mels*frames))
for t in range(frames):
new_vec = log_mel_spectrogram[:, t: t + vector_array_size].T
vector_array[:, n_mels * t: n_mels * (t + 1)] = new_vec[:,::4]
return vector_array
else:
vector_array = numpy.zeros((vector_array_size, dims))
for t in range(frames):
vector_array[:, n_mels * t: n_mels * (t + 1)] = log_mel_spectrogram[:, t: t + vector_array_size].T
return vector_array
# load dataset
def select_dirs(param):
"""
param : dict
baseline.yaml data
return :
dirs : list [ str ]
load base directory list of dev_data
"""
logger.info("load_directory <- development")
dir_path = os.path.abspath("{base}/*".format(base=param["dev_directory"]))
dirs = sorted(glob.glob(dir_path))
return dirs
########################################################################
def get_machine_id_list_for_test(target_dir,
dir_name="test",
ext="wav"):
"""
target_dir : str
base directory path of "dev_data" or "eval_data"
test_dir_name : str (default="test")
directory containing test data
ext : str (default="wav)
file extension of audio files
return :
machine_id_list : list [ str ]
list of machine IDs extracted from the names of test files
"""
# create test files
dir_path = os.path.abspath("{dir}/{dir_name}/*.{ext}".format(dir=target_dir, dir_name=dir_name, ext=ext))
file_paths = sorted(glob.glob(dir_path))
# extract id
machine_id_list = sorted(list(set(itertools.chain.from_iterable(
[re.findall('id_[0-9][0-9]', ext_id) for ext_id in file_paths]))))
return machine_id_list
def test_file_list_generator(target_dir,
id_name,
dir_name="test",
prefix_normal="normal",
prefix_anomaly="anomaly",
ext="wav"):
"""
target_dir : str
base directory path of the dev_data or eval_data
id_name : str
id of wav file in <<test_dir_name>> directory
dir_name : str (default="test")
directory containing test data
prefix_normal : str (default="normal")
normal directory name
prefix_anomaly : str (default="anomaly")
anomaly directory name
ext : str (default="wav")
file extension of audio files
return :
if the mode is "development":
test_files : list [ str ]
file list for test
test_labels : list [ boolean ]
label info. list for test
* normal/anomaly = 0/1
if the mode is "evaluation":
test_files : list [ str ]
file list for test
"""
logger.info("target_dir : {}".format(target_dir+"_"+id_name))
# development
normal_files = sorted(
glob.glob("{dir}/{dir_name}/{prefix_normal}_{id_name}*.{ext}".format(dir=target_dir,
dir_name=dir_name,
prefix_normal=prefix_normal,
id_name=id_name,
ext=ext)))
normal_labels = numpy.zeros(len(normal_files))
anomaly_files = sorted(
glob.glob("{dir}/{dir_name}/{prefix_anomaly}_{id_name}*.{ext}".format(dir=target_dir,
dir_name=dir_name,
prefix_anomaly=prefix_anomaly,
id_name=id_name,
ext=ext)))
anomaly_labels = numpy.ones(len(anomaly_files))
files = numpy.concatenate((normal_files, anomaly_files), axis=0)
labels = numpy.concatenate((normal_labels, anomaly_labels), axis=0)
logger.info("test_file num : {num}".format(num=len(files)))
if len(files) == 0:
logger.exception("no_wav_file!!")
print("\n========================================")
return files, labels
########################################################################
def list_to_vector_array(file_list,
msg="calc...",
n_mels=64,
frames=5,
n_fft=1024,
hop_length=512,
power=2.0,
downsample=False):
"""
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 ) )
vector array 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 * frames
# iterate file_to_vector_array()
for idx in tqdm(range(len(file_list)), desc=msg):
vector_array = file_to_vector_array(file_list[idx],
n_mels=n_mels,
frames=frames,
n_fft=n_fft,
hop_length=hop_length,
power=power,
downsample=downsample)
if idx == 0:
dataset = numpy.zeros((vector_array.shape[0] * len(file_list), dims), float)
dataset[vector_array.shape[0] * idx: vector_array.shape[0] * (idx + 1), :] = vector_array
return dataset
def file_list_generator(target_dir,
dir_name="train",
ext="wav"):
"""
target_dir : str
base directory path of the dev_data or eval_data
dir_name : str (default="train")
directory name containing training data
ext : str (default="wav")
file extension of audio files
return :
train_files : list [ str ]
file list for training
"""
logger.info("target_dir : {}".format(target_dir))
# generate training list
training_list_path = os.path.abspath("{dir}/{dir_name}/*.{ext}".format(dir=target_dir, dir_name=dir_name, ext=ext))
files = sorted(glob.glob(training_list_path))
if len(files) == 0:
logger.exception("no_wav_file!!")
logger.info("train_file num : {num}".format(num=len(files)))
return files
########################################################################