forked from maum-ai/univnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
62 lines (51 loc) · 2.29 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
import os
import glob
import tqdm
import torch
import argparse
from scipy.io.wavfile import write
from omegaconf import OmegaConf
from model.generator import Generator
def main(args):
checkpoint = torch.load(args.checkpoint_path)
if args.config is not None:
hp = OmegaConf.load(args.config)
else:
hp = OmegaConf.create(checkpoint['hp_str'])
model = Generator(hp).cuda()
saved_state_dict = checkpoint['model_g']
new_state_dict = {}
for k, v in saved_state_dict.items():
try:
new_state_dict[k] = saved_state_dict['module.' + k]
except:
new_state_dict[k] = v
model.load_state_dict(new_state_dict)
model.eval(inference=True)
with torch.no_grad():
for melpath in tqdm.tqdm(glob.glob(os.path.join(args.input_folder, '*.mel'))):
mel = torch.load(melpath)
if len(mel.shape) == 2:
mel = mel.unsqueeze(0)
mel = mel.cuda()
audio = model.inference(mel)
audio = audio.cpu().detach().numpy()
if args.output_folder is None: # if output folder is not defined, audio samples are saved in input folder
out_path = melpath.replace('.mel', '_reconstructed_epoch%04d.wav' % checkpoint['epoch'])
else:
basename = os.path.basename(melpath)
basename = basename.replace('.mel', '_reconstructed_epoch%04d.wav' % checkpoint['epoch'])
out_path = os.path.join(args.output_folder, basename)
write(out_path, hp.audio.sampling_rate, audio)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default=None,
help="yaml file for config. will use hp_str from checkpoint if not given.")
parser.add_argument('-p', '--checkpoint_path', type=str, required=True,
help="path of checkpoint pt file for evaluation")
parser.add_argument('-i', '--input_folder', type=str, required=True,
help="directory of mel-spectrograms to invert into raw audio.")
parser.add_argument('-o', '--output_folder', type=str, default=None,
help="directory which generated raw audio is saved.")
args = parser.parse_args()
main(args)