big update
This commit is contained in:
0
plots/__init__.py
Normal file
0
plots/__init__.py
Normal file
60
plots/normal_vs_abnormal.py
Normal file
60
plots/normal_vs_abnormal.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import numpy as np
|
||||
import matplotlib as mpl
|
||||
import matplotlib.pyplot as plt
|
||||
import librosa
|
||||
import librosa.display
|
||||
# set LaTeX font
|
||||
# ==============
|
||||
nice_fonts = {
|
||||
# Use LaTeX to write all text
|
||||
"text.usetex": True,
|
||||
"font.family": "serif",
|
||||
# Use 10pt font in plots, to match 10pt font in document
|
||||
"axes.labelsize": 16, # default 10
|
||||
"font.size": 16, # default 10
|
||||
# Make the legend/label fonts a little smaller
|
||||
"legend.fontsize": 14, # default 8
|
||||
"xtick.labelsize": 14, # default 8
|
||||
"ytick.labelsize": 14, # default 8
|
||||
}
|
||||
mpl.rcParams.update(nice_fonts, )
|
||||
mpl.rcParams['axes.unicode_minus'] = False
|
||||
|
||||
# TODO LaTex
|
||||
fan_normal = '../data/mimii/-6_dB_fan/id_04/normal/00000042.wav'
|
||||
fan_anomaly = '../data/mimii/-6_dB_fan/id_04/abnormal/00000048.wav'
|
||||
|
||||
pump_normal = '../data/mimii/-6_dB_pump/id_04/normal/00000042.wav'
|
||||
pump_anomaly = '../data/mimii/-6_dB_pump/id_04/abnormal/00000042.wav'
|
||||
|
||||
slider_normal = '../data/mimii/-6_dB_slider/id_04/normal/00000042.wav'
|
||||
slider_anomaly = '../data/mimii/-6_dB_slider/id_04/abnormal/00000042.wav'
|
||||
|
||||
valve_normal = '../data/mimii/-6_dB_valve/id_04/normal/00000042.wav'
|
||||
valve_anomaly = '../data/mimii/-6_dB_valve/id_04/abnormal/00000042.wav'
|
||||
|
||||
fig = plt.figure(figsize=(17, 5))
|
||||
for i, (p, title) in enumerate(zip([fan_normal, pump_normal, slider_normal, valve_normal, fan_anomaly, pump_anomaly, slider_anomaly, valve_anomaly],
|
||||
['Fan', 'Pump', 'Slider', 'Valve', 'Fan', 'Pump', 'Slider', 'Valve'])):
|
||||
plt.subplot(2, 4, i+1)
|
||||
audio, sr = librosa.load(p, sr=None, mono=True)
|
||||
# n_fft=1024, hop_length=256, n_mels=80, center=False, power=2.0
|
||||
S = librosa.feature.melspectrogram(y=audio, sr=sr, n_fft=1024, hop_length=256, n_mels=80, center=False, power=2.0)
|
||||
S_dB = librosa.power_to_db(S, ref=np.max)
|
||||
librosa.display.specshow(S_dB, x_axis='s' if i == 4 else 'off', hop_length=256,
|
||||
y_axis='mel' if i==4 else 'off', sr=16000, cmap='viridis')
|
||||
if i < 4:
|
||||
plt.title(title)
|
||||
else:
|
||||
plt.title(title + ' malfunction')
|
||||
|
||||
cbar_ax = fig.add_axes([0.94, 0.18, 0.015, 0.7])
|
||||
cmap = mpl.cm.viridis
|
||||
norm = mpl.colors.Normalize(vmin=0, vmax=-80)
|
||||
cb1 = mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap,
|
||||
norm=norm,
|
||||
orientation='vertical',
|
||||
format='%+2.0f dB')
|
||||
plt.tight_layout()
|
||||
fig.subplots_adjust(right=0.93)
|
||||
plt.savefig('normal_vs_abnormal.png')
|
||||
64
plots/playground.py
Normal file
64
plots/playground.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import librosa
|
||||
from matplotlib import pyplot as plt
|
||||
import numpy as np
|
||||
from models.utils import count_parameters
|
||||
from models.ae import SubSpecCAE, AE, GlobalCAE
|
||||
from cfg import ALL_DATASET_PATHS
|
||||
import librosa.display
|
||||
import librosa.feature
|
||||
import seaborn as sns
|
||||
|
||||
purple = (126/255, 87/255, 194/255)
|
||||
params_subspecae = count_parameters(SubSpecCAE())
|
||||
params_ae = count_parameters(AE())
|
||||
params_globalcae = count_parameters(GlobalCAE())
|
||||
print(f'#Parameters SubSpecCAE: {params_subspecae}')
|
||||
print(f'#Parameters AE: {params_ae}')
|
||||
print(f'#SubSpecAe/#AE: {(params_subspecae/params_ae):.2f}')
|
||||
print(f'#GlobalCAE: {params_globalcae}')
|
||||
print(f'#GlobalCAE/#SubSpecCAE: {params_globalcae/params_subspecae}')
|
||||
|
||||
|
||||
fig = plt.figure(figsize=(10, 5))
|
||||
slider_normal = '../data/mimii/-6_dB_slider/id_04/normal/00000042.wav'
|
||||
audio, sr = librosa.load(slider_normal)
|
||||
waveplot = librosa.display.waveplot(audio, sr=sr)
|
||||
plt.tight_layout()
|
||||
plt.savefig('wavplot.png')
|
||||
|
||||
ids = range(0)
|
||||
specs = []
|
||||
i = 0
|
||||
audios = list((ALL_DATASET_PATHS[1] / 'id_00' / 'normal').glob('*.wav'))
|
||||
print(str(audios[80]))
|
||||
audio, sr = librosa.load(str(audios[80]), sr=None)
|
||||
mel_spec = librosa.feature.melspectrogram(audio, sr=sr, n_fft=1024, hop_length=256, n_mels=80, center=False, power=2.0)
|
||||
mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)
|
||||
librosa.display.specshow(mel_spec_db, hop_length=256, x_axis='s', y_axis='mel', cmap='viridis', sr=sr)
|
||||
plt.savefig('notadjusted.png')
|
||||
plt.clf()
|
||||
|
||||
centroids = []
|
||||
for p in audios:
|
||||
audio, sr = librosa.load(str(p), sr=None)
|
||||
spectral_centroids = librosa.feature.spectral_centroid(audio, sr=sr)[0]
|
||||
centroids += spectral_centroids.tolist()
|
||||
|
||||
sns.distplot(centroids, hist=True, kde=True,
|
||||
color=purple,
|
||||
hist_kws={'edgecolor':'black'},
|
||||
kde_kws={'linewidth': 2})
|
||||
plt.xlabel('Occurence counts')
|
||||
plt.ylabel('Density')
|
||||
plt.title('Spectral centroid distribution')
|
||||
plt.tight_layout()
|
||||
plt.savefig('hist.png')
|
||||
|
||||
def get_bands(centroids, n_mels=80):
|
||||
std = np.std(centroids)
|
||||
mu = np.mean(centroids)
|
||||
return int((mu-3*std)/n_mels), int((mu+3*std)/n_mels)
|
||||
|
||||
print(get_bands(centroids))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user