-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_tools.py
209 lines (177 loc) · 5.3 KB
/
inference_tools.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
# Tools for ploting STFS, MEL and saving to file
# STFS, Mel dataset and samples. --plot uses this.
#
# Mustafa. B
#
import os
from typing import Optional
import librosa
import torch
import torchaudio
from matplotlib import pyplot as plt
from model_trainer.plotting_utils import save_figure_to_numpy
def plot_spectrogram(spec,
title="Spectrogram",
y_axis_label: Optional[str] = 'freq_bin',
aspect: Optional[str] = 'auto',
xmax: Optional[float] = None,
file_name="default.png"):
"""
Plots STFT and save plot to a file.
:param file_name:
:param spec: librosa STFT mel spec.
:param title: plot title
:param y_axis_label: y axis lable
:param aspect: default aspect ration
:param xmax: left and right respectively
:return: if filename will save and return data
"""
fig, axs = plt.subplots(1, 1)
axs.set_title(title or 'Spectrogram (db)')
axs.set_ylabel(y_axis_label)
axs.set_xlabel('frame')
im = axs.imshow(librosa.power_to_db(spec), origin='lower', aspect=aspect)
if xmax:
axs.set_xlim((0, xmax))
fig.colorbar(im, ax=axs)
plt.show(block=False)
if file_name is not None and len(file_name) > 0:
plt.savefig(file_name)
plt.close()
return
data = save_figure_to_numpy(fig)
plt.close()
return data
def plot_mel_fbank(fbank, title=None):
"""
Plots Filter bank.
:param fbank:
:param title:
:return:
"""
fig, axs = plt.subplots(1, 1)
axs.set_title(title or 'Filter bank')
axs.imshow(fbank, aspect='auto')
axs.set_ylabel('frequency bin')
axs.set_xlabel('mel bin')
plt.show(block=False)
def plot_waveform(waveform,
sample_rate: Optional[int] = 22050,
title="Spectrogram",
xlim=None,
file_name="default_spect.png"):
"""
:param file_name:
:param waveform:
:param sample_rate:
:param title:
:param xlim:
:return:
"""
waveform = waveform.numpy()
num_channels, num_frames = waveform.shape
time_axis = torch.arange(0, num_frames) / sample_rate
figure, axes = plt.subplots(num_channels, 1)
if num_channels == 1:
axes = [axes]
for c in range(num_channels):
axes[c].specgram(waveform[c], Fs=sample_rate)
if num_channels > 1:
axes[c].set_ylabel(f'Channel {c + 1}')
if xlim:
axes[c].set_xlim(xlim)
figure.suptitle(title)
plt.show(block=False)
if len(file_name) > 0:
plt.savefig(file_name)
plt.close()
return
data = save_figure_to_numpy(figure)
plt.close()
return data
def plot_pitch(waveform, pitch, title="Pitch", sample_rate=22050, file_name="default_pitch.png"):
"""
:param title:
:param waveform:
:param pitch:
:param sample_rate:
:param file_name:
:return:
"""
figure, axis = plt.subplots(1, 1)
axis.set_title("Pitch Feature")
axis.grid(True)
end_time = waveform.shape[1] / sample_rate
time_axis = torch.linspace(0, end_time, waveform.shape[1])
axis.plot(time_axis, waveform[0], linewidth=1, color="gray", alpha=0.3)
axis2 = axis.twinx()
time_axis = torch.linspace(0, end_time, pitch.shape[1])
axis2.plot(time_axis, pitch[0], linewidth=2, label="Pitch", color="green")
axis2.legend(loc=0)
plt.show(block=False)
figure.suptitle(title)
plt.show(block=False)
if len(file_name) > 0:
plt.savefig(file_name)
plt.close()
return
data = save_figure_to_numpy(figure)
plt.close()
return data
# from IPython.display import Audio, display
# def play_audio_file(file_path, is_notebook=False, sample_rate=22050):
# """
#
# :param waveform:
# :param is_notebook:
# :param sample_rate:
# :return:
# """
# # waveform = waveform.numpy(
# playsound(file_path)
# def play_audio(waveform, is_notebook=False, sample_rate=22050):
# """
#
# :param waveform:
# :param sample_rate:
# :return:
# """
# waveform = waveform.numpy()
# playsound('/path/to/a/sound/file/you/want/to/play.wav')
#
# num_channels, num_frames = waveform.shape
# if num_channels == 1:
# display(Audio(waveform[0], rate=sample_rate))
# elif num_channels == 2:
# display(Audio((waveform[0], waveform[1]), rate=sample_rate))
# else:
# raise ValueError("Waveform with more than 2 channels are not supported.")
def inspect_file(path):
print("-" * 10)
print("Source:", path)
print("-" * 10)
print(f" - File size: {os.path.getsize(path)} bytes")
print(f" - {torchaudio.info(path)}")
def show_img(img, title=""):
"""
:param img:
:param title:
:return:
"""
plt.imshow(img)
plt.title(title)
thismanager = plt.get_current_fig_manager()
thismanager.window.wm_geometry("+100+100")
plt.show()
def plot_data(data, figsize=(16, 4)):
"""
:param data:
:param figsize:
:return:
"""
fig, axes = plt.subplots(1, len(data), figsize=figsize)
for i in range(len(data)):
axes[i].imshow(data[i],
aspect='auto',
inorigin='bottom',
interpolation='none')