-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfer.py
55 lines (40 loc) · 1.62 KB
/
infer.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
import sys
sys.path.append('utils')
sys.path.append('model')
import os
import torch
from tqdm import tqdm
import utils.dsp
from utils.config_parser import Config
from utils.torch_utils import set_device, load_weights
import preprocess
import dataprocess
from model.models import Generator
def main():
config = Config()
print("Processing text for \'%s\'." % (config.text_file))
data = preprocess.preprocess(config.text_file, 'infer', config)
dataloader = dataprocess.load_infer(data, config)
G = Generator(config)
G.load_state_dict(load_weights(config.checkpoint_file))
G = set_device(G, config.device, config.use_cpu)
G.eval()
print("Generating spectrogram with \'%s\'." % (config.checkpoint_file))
spec = []
y_prev = torch.zeros(1, config.prev_length, config.fft_size//2 + 1)
for x in tqdm(dataloader, leave=False, ascii=True):
x, y_prev = set_device((x, y_prev), config.device, config.use_cpu)
y_gen = G(x, y_prev)
y_gen = y_gen.squeeze(1)
y_prev = y_gen[:, -config.prev_length:,:]
spec.append(y_gen.data)
print ("Generating audio with Griffin-Lim algorithm.")
spec = torch.cat(spec, dim=1).transpose(1, 2) # T x D -> D x T
wave = utils.dsp.inv_spectrogram(spec, config)
if not os.path.exists('./samples'):
os.makedirs('./samples')
savename = './samples/' + os.path.basename(config.checkpoint_file).replace('.pt', '_') + os.path.basename(config.text_file).replace('.txt', '.wav')
utils.dsp.save(savename, wave, config.sample_rate)
print("Audio saved to \'%s\'." % (savename))
if __name__ == "__main__":
main()