-
Notifications
You must be signed in to change notification settings - Fork 4
/
inference.py
288 lines (251 loc) · 9.88 KB
/
inference.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
import os, glob
import soundfile
import torch
from scipy.signal import decimate
from scipy import interpolate
from torch.utils.data import DataLoader
from argparse import ArgumentParser
from datetime import datetime
from tqdm import tqdm
from datasets.dataset import Sonata32Dataset, SingleSpeaker
from utils.metrics import *
from models.audiounet import AudioUNet
from models.tfilmunet import TFILMUNet
import matplotlib.pyplot as plt
import matplotlib
import torchaudio.transforms as T
def upsample(x_lr, r):
x_lr = x_lr.flatten()
x_hr_len = len(x_lr) * r
x_sp = np.zeros(x_hr_len)
i_lr = np.arange(x_hr_len, step=r)
i_hr = np.arange(x_hr_len)
f = interpolate.splrep(i_lr, x_lr)
x_sp = interpolate.splev(i_hr, f)
return x_sp
def plot_spectrogram(spec, title=None, save_name='test.png', ylabel='frequency (Hz)', aspect='auto',y_max=16000, xmax=None):
fig, axs = plt.subplots(1, 1)
axs.set_title(title)
axs.set_ylabel(ylabel)
axs.set_xlabel('timeframe (-)')
plt.set_cmap('inferno')
im = axs.imshow(librosa.power_to_db(spec), origin='lower', aspect=aspect)
if xmax:
axs.set_xlim((0, xmax))
# axs.set_yscale('log')
axs.set_ylim((0,y_max)) # was 0,1000
# axs.set_yticks([50, 100, 200, 500, 1000, 2000, 5000,10_000,16_000])
# axs.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
clb = plt.colorbar(im, ax=axs)
clb.ax.set_title('dB')
# clb.set_label(label='dB', labelpad=-40, y=1.05, rotation=0)
# fig.colorbar(im, ax=axs, label='dB')
im.set_clim(-40,40)
# fig.colorbar.set_label('Db', rotation=270)
plt.show()
fig.savefig(save_name, bbox_inches='tight')
def plot(waveform,
name,
file_name,
n_fft = 32000,
win_length = 1024,
hop_length = 2048,
low_res = False):
# define transformation
spectrogram = T.Spectrogram(
n_fft=n_fft,
win_length=win_length,
hop_length=hop_length,
center=True,
pad_mode="reflect",
power=2.0,
)
# Perform transformation
spec = spectrogram(waveform)
if low_res:
spec_new = torch.zeros(1,int(spec.shape[1]*3),spec.shape[2])
spec_fill = torch.cat((spec, spec_new),1)
print(f"spec fill min: {torch.min(spec_fill)}")
print(f"spec fill max: {torch.max(spec_fill)}")
plot_spectrogram(spec_fill[0], title=name, save_name=file_name, y_max=4*int(n_fft/2))
else:
# print_stats(spec)
print(f"spec min: {torch.min(spec)}")
print(f"spec max: {torch.max(spec)}")
plot_spectrogram(spec[0], title=name, save_name=file_name, y_max=int(n_fft/2))
def main(opts):
time_stamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
print(f"Current training run {time_stamp} has started!")
# Setup Meter and Device
meter = averageMeter()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if opts.dataset_type == "vctk-single":
test_dataset = SingleSpeaker(root=os.path.join(opts.dataset_root))
elif opts.dataset_type == "piano":
test_dataset = Sonata32Dataset(root='datasets/music_dataset/data/',
target_type='valid',
sr=16000,
scale=8,
dimension=8192,
stride=4096)
try:
test_loader = DataLoader(test_dataset,
batch_size=opts.batch_size,
shuffle=False,
num_workers=opts.num_workers)
except UnboundLocalError:
print("No dataset specified.")
return
# Setup model and pick checkpoint
model = TFILMUNet()
if os.path.exists(opts.checkpoints_root):
checkpoint = max(glob.glob(os.path.join(opts.checkpoints_root, opts.checkpoint)), key=os.path.getctime)
model.load_state_dict(torch.load(checkpoint, map_location=device), strict=True)
else:
raise ValueError(f"Checkpoints directory {opts.checkpoints_root} does not exist")
model = model.to(device)
# Inference
model.eval()
with torch.no_grad():
for test_idx, test_sample in enumerate(tqdm(test_loader)):
# Put img and gt on GPU if available
in_test, gt_test = test_sample[0].to(device), test_sample[2].to(device)
# Forward pass and loss calculation
if opts.method == "our":
out_test = model(in_test)
elif opts.method == "base":
out_test = in_test
# # Update iou meter
meter.update(np.array(gt_test.cpu()), np.array(out_test.cpu()), opts.batch_size)
snr, lsd = meter.get_score()
print("\n---INFERENCE on Checkpoint: {}---\nMODE is set to {}\nSignal to Noise Ratio (SNR): {} \nLog-spectral distance (LSD): {}".format(opts.checkpoint, opts.method, round(float(snr), 4), round(float(lsd), 4)))
def run_examples(opts):
print('Run Inference on example files:')
# Setup model and pick checkpoint
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = TFILMUNet()
if os.path.exists(opts.checkpoints_root):
checkpoint = max(glob.glob(os.path.join(opts.checkpoints_root, opts.checkpoint)), key=os.path.getctime)
model.load_state_dict(torch.load(checkpoint, map_location=device), strict=True)
else:
raise ValueError(f"Checkpoints directory {opts.checkpoints_root} does not exist")
model = model.to(device)
# Inference
model.eval()
os.makedirs('examples', exist_ok=True)
# save wav:
with open(opts.wav_file_list) as f:
for line in f:
filename = os.path.basename(os.path.splitext(line)[0])
print('Evaluating: ' + filename)
os.makedirs('examples/' + filename, exist_ok=True)
with torch.no_grad():
hr, fs = librosa.load(line.strip(), sr=opts.sr)
hr = np.pad(hr, (0, 4096 - (hr.shape[0] % 4096)), 'constant',
constant_values=(0, 0))
lr = decimate(hr, opts.scale)
lr_int = upsample(lr, opts.scale)
# hr_t = torch.as_tensor(hr, device=device)
lr_int_t = torch.unsqueeze(torch.as_tensor(lr_int,dtype=torch.float32, device=device),0)
out = model(torch.unsqueeze(lr_int_t,0))
soundfile.write('examples/' + filename + '/' + filename + '_gt.wav', hr, opts.sr) # high res
soundfile.write('examples/' + filename + '/' + filename + '_low_res.wav', lr, int(opts.sr / opts.scale)) # low res
soundfile.write('examples/' + filename + '/' + filename + '_base.wav', lr_int, opts.sr) # baseline res
soundfile.write('examples/' + filename + '/' + filename + '_super.wav', out[0, 0, :].detach().numpy(), opts.sr) # super res
# plots:
plot(waveform=torch.unsqueeze(torch.as_tensor(lr.copy(),dtype=torch.float32, device=device),0), name="Low Resolution", n_fft=int(int(opts.sr*2) / 4), hop_length=int(1024/4), win_length=int(2048/4), file_name='examples/' + filename + '/spec_low_res_' + filename + '.pdf', low_res=True)
plot(waveform=torch.unsqueeze(torch.as_tensor(hr.copy(),dtype=torch.float32, device=device),0), name="High Resolution",n_fft=int(opts.sr*2), hop_length=1024, win_length=2048, file_name='examples/' + filename + '/spec_high_res_' + filename + '.pdf')
plot(waveform=lr_int_t, name="Baseline",n_fft=int(opts.sr*2), hop_length=1024, win_length=2048, file_name='examples/' + filename + '/spec_baseline_' + filename + '.pdf')
plot(waveform=torch.squeeze(out,0), name="Super Resolution", n_fft=int(opts.sr*2), hop_length=1024, win_length=2048, file_name='examples/' + filename + '/spec_super_res_' + filename + '.pdf')
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument(
"--dataset-type",
type=str,
default="vctk-single"
),
parser.add_argument(
"--dataset-root",
type=str,
default=os.path.join(os.getcwd(), "datasets", "vctk", "vctk-speaker1-val.4.16000.8192.4096.h5")
)
parser.add_argument(
"--full-root",
type=str,
default=os.path.join(os.getcwd(), "datasets", "vctk", "test.h5")
)
parser.add_argument(
"--dataset-split",
type=str,
default="val"
)
parser.add_argument(
"--save-examples",
action="store_true",
help="Save spectrogram plots and wav files"
)
parser.add_argument(
"--wav-file-list",
type=str,
default="assets/save_wav_list.txt"
)
parser.add_argument(
'--scale',
type=int,
default=4,
)
parser.add_argument(
'--dimension',
type=int,
default=8192
)
parser.add_argument(
'--stride',
type=int,
default=4096
)
parser.add_argument(
'--sr',
type=int,
default=16000
),
parser.add_argument(
"--checkpoints-root",
type=str,
default=os.path.join(os.getcwd(), "checkpoints", "runs")
)
parser.add_argument(
"--checkpoint",
type=str,
default="2021_06_23_17_39_44.pth"
)
parser.add_argument(
"--batch-size",
type=int,
default=1
)
parser.add_argument(
"--num-workers",
type=int,
default=1
)
parser.add_argument(
"--method",
type=str,
default="base",
choices=["base", "our"],
help="switch between BASELINE and OUR Method"
)
parser.add_argument(
"--mode",
type=str,
default="inf",
choices=["inf", "vis"],
help="switch between inference and visualization mode"
)
clargs = parser.parse_args()
print(clargs)
if clargs.save_examples:
run_examples(clargs)
else:
main(clargs)