forked from maum-ai/voicefilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
66 lines (52 loc) · 2.13 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
import os
import glob
import torch
import librosa
import argparse
from utils.audio import Audio
from utils.hparams import HParam
from model.model import VoiceFilter
from model.embedder import SpeechEmbedder
def main(args, hp):
model = VoiceFilter(hp).cuda()
chkpt_model = torch.load(args.checkpoint_path)['model']
model.load_state_dict(chkpt_model)
model.eval()
embedder = SpeechEmbedder(hp).cuda()
chkpt_embed = torch.load(args.embedder_path)
embedder.load_state_dict(chkpt_embed)
embedder.eval()
audio = Audio(hp)
dvec_wav, _ = librosa.load(args.reference_file, sr=16000)
dvec_mel = audio.get_mel(dvec_wav)
dvec_mel = torch.from_numpy(dvec_mel).float().cuda()
dvec = embedder(dvec_mel)
dvec = dvec.unsqueeze(0)
mixed_wav, _ = librosa.load(args.mixed_file, sr=16000)
mag, phase = audio.wav2spec(mixed_wav)
mag = torch.from_numpy(mag).float().cuda()
mag = mag.unsqueeze(0)
mask = model(mag, dvec)
est_mag = mag * mask
est_mag = est_mag[0].cpu().detach().numpy()
est_wav = audio.spec2wav(est_mag, phase)
os.makedirs(args.out_dir, exist_ok=True)
out_path = os.path.join(args.out_dir, 'result.wav')
librosa.output.write_wav(out_path, est_wav, sr=16000)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, required=True,
help="yaml file for configuration")
parser.add_argument('-e', '--embedder_path', type=str, required=True,
help="path of embedder model pt file")
parser.add_argument('--checkpoint_path', type=str, default=None,
help="path of checkpoint pt file")
parser.add_argument('-m', '--mixed_file', type=str, required=True,
help='path of mixed wav file')
parser.add_argument('-r', '--reference_file', type=str, required=True,
help='path of reference wav file')
parser.add_argument('-o', '--out_dir', type=str, required=True,
help='directory of output')
args = parser.parse_args()
hp = HParam(args.config)
main(args, hp)