-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredict_batch_with_ola.py
133 lines (109 loc) · 4.68 KB
/
predict_batch_with_ola.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
import math
import os
import time
import hydra
import torch
import logging
from pathlib import Path
import numpy as np
import torchaudio
from torchaudio.functional import resample
from src.enhance import write
from src.models import modelFactory
from src.model_serializer import SERIALIZE_KEY_MODELS, SERIALIZE_KEY_BEST_STATES, SERIALIZE_KEY_STATE
from src.utils import bold
import soundfile as sf
logger = logging.getLogger(__name__)
def overlap_and_add(chunks, overlap=256, window_len=1024):
W = window_len
win_left_side = np.bartlett(2 * overlap)[:overlap]
win_right_side = np.bartlett(2 * overlap)[overlap:]
window = np.concatenate((win_left_side, np.ones(W - 2 * overlap), win_right_side))
left_window = np.concatenate((np.ones(W - overlap), win_right_side))
right_window = np.concatenate((win_left_side, np.ones(W - overlap)))
n_chunks = len(chunks)
for i in range(n_chunks):
if i == 0:
y = (chunks[i].reshape(-1,) * left_window)
else:
x_chunk = chunks[i].reshape(-1,)
if len(x_chunk) < W or i == n_chunks - 1:
end_pad = W - len(x_chunk)
x_chunk = np.pad(x_chunk, (0, end_pad), 'constant', constant_values=0)
x_ola = x_chunk * right_window
else:
x_ola = x_chunk * window
y = np.pad(y, (0, W - overlap), 'constant', constant_values=0)
x_ola = np.pad(x_ola, (len(y) - len(x_ola), 0), 'constant', constant_values=0)
y += x_ola
return y
SEGMENT_DURATION_SEC = 1
def _load_model(args):
model_name = args.experiment.model
checkpoint_file = Path(args.checkpoint_file)
model = modelFactory.get_model(args)['generator']
package = torch.load(checkpoint_file, 'cpu')
load_best = args.continue_best
if load_best:
logger.info(bold(f'Loading model {model_name} from best state.'))
model.load_state_dict(
package[SERIALIZE_KEY_BEST_STATES][SERIALIZE_KEY_MODELS]['generator'][SERIALIZE_KEY_STATE])
else:
logger.info(bold(f'Loading model {model_name} from last state.'))
model.load_state_dict(package[SERIALIZE_KEY_MODELS]['generator'][SERIALIZE_KEY_STATE])
return model
@hydra.main(config_path="conf", config_name="main_config") # for latest version of hydra=1.0
def main(args):
global __file__
__file__ = hydra.utils.to_absolute_path(__file__)
print(args)
model = _load_model(args)
device = torch.device('cuda')
model.cuda()
folder_path = args.folder_path
for files in os.listdir(folder_path):
filename = os.path.join(folder_path, files)
file_basename = Path(filename).stem
output_dir = args.output
lr_sig, sr = torchaudio.load(str(filename))
if lr_sig.shape[1] > 1:
lr_sig = torch.mean(lr_sig, dim=0, keepdim=True)
if args.experiment.upsample:
lr_sig = resample(lr_sig, sr, args.experiment.hr_sr)
sr = args.experiment.hr_sr
logger.info(f'lr wav shape: {lr_sig.shape}')
segment_duration_samples = sr * SEGMENT_DURATION_SEC
W_hr = 44095 # 44100 samples minus the edge effect samples
W_lr = 11025
overlap_hr = 900 #heuristic value
overlap_lr = overlap_hr // 4
n_chunks = math.ceil(lr_sig.shape[-1] / (W_lr - overlap_lr))
logger.info(f'number of chunks: {n_chunks}')
lr_chunks = []
for i in range(n_chunks):
start = i * (W_lr - overlap_lr)
end = min(start + W_lr, lr_sig.shape[-1])
lr_chunks.append(lr_sig[:, start:end])
pr_chunks = []
model.eval()
pred_start = time.time()
with torch.no_grad():
for i, lr_chunk in enumerate(lr_chunks):
pr_chunk = model(lr_chunk.unsqueeze(0).to(device)).squeeze(0)
#remove edge effect samples (only the 4 final samples are distorted)
pr_chunk = pr_chunk[:, :-5]
pr_chunks.append(pr_chunk.cpu())
pred_duration = time.time() - pred_start
logger.info(f'prediction duration: {pred_duration}')
pr_ola = overlap_and_add(pr_chunks, overlap=overlap_hr, window_len=W_hr)
logger.info(f'pr wav shape: {pr_ola.shape}')
out_filename_ola = os.path.join(output_dir, file_basename + '.wav')
os.makedirs(output_dir, exist_ok=True)
logger.info(f'saving to: {out_filename_ola}, with sample_rate: {args.experiment.hr_sr}')
sf.write(out_filename_ola, pr_ola, args.experiment.hr_sr)
"""
Need to add filename and output to args.
Usage: python predict.py <dset> <experiment> +folder_path=<path to input folder> +output=<path to output dir>
"""
if __name__ == "__main__":
main()