Files
torchscript-models/glow-tts-jaywalnut/wrap_glow_model.py
T
Brandon Thomas 7a5e0033db Figured it out!
2020-06-03 22:15:08 -04:00

99 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
# From: https://github.com/pytorch/pytorch/issues/20356#issuecomment-545572400
import torch
# Import glow-tts code
import sys
sys.path.append("./glow-tts")
import utils
import models
import commons
from text.symbols import symbols
# Nvidia Apex
#from apex import amp
#from apex.parallel import DistributedDataParallel as DDP
# Data
import numpy as np
from text import text_to_sequence, cmudict
trace_model = False
if sys.argv[1] == 'true':
trace_model = True
elif sys.argv[1] == 'false':
trace_model = False
else:
raise Exception('Scripting or tracing?')
torch.set_grad_enabled(False)
def load(model_checkpoint_path):
model_dir = "./logs/base/"
hps = utils.get_hparams_from_dir(model_dir)
print('Checkpoint path: {}'.format(model_checkpoint_path))
with torch.no_grad():
# NB(bt): This should fix the following:
# RuntimeError: Cannot insert a Tensor that requires grad as a constant.
# Consider making it a parameter or input, or detaching the gradient Tensor:
model = models.FlowGenerator(
len(symbols),
out_channels=hps.data.n_mel_channels,
**hps.model) #.to("cuda")
utils.load_checkpoint(model_checkpoint_path, model)
model.decoder.store_inverse() # do not calcuate jacobians for fast decoding
_ = model.eval()
return (hps, model)
#model_checkpoint_path = '/home/bt/models/glow-tts-jaywalnut/pretrained.pth'
model_checkpoint_path = '/home/bt/models/glow-tts-jaywalnut/G_1007.pth'
hps, glow_model = load(model_checkpoint_path)
output_filename = 'output.torchjit'
cmu_dict = cmudict.CMUDict(hps.data.cmudict_path)
tst_stn = "my name is donald trump and I am the president of the united states, you should respect me"
seq = text_to_sequence(tst_stn, ['english_cleaners'], cmu_dict)
sequence = np.array(seq)[None, :] # NB: None adds a new axis.
print("".join([symbols[c] for c in sequence[0]]))
torched = torch.from_numpy(sequence)
x_tst = torch.autograd.Variable(torched).long()
x_tst_lengths = torch.tensor([x_tst.shape[1]])
print('\n tst_stn', len(tst_stn), tst_stn)
print('\n seq', len(seq), seq)
print('\n sequence', len(sequence), sequence.size, sequence)
print('\n torched', len(torched), torched.shape, torched)
print('\n x_tst', len(x_tst), x_tst)
print('\n x_tst_lengths', len(x_tst_lengths), x_tst_lengths)
# NB: Tracing evaluates the model on input and unrolls and hardcodes branching
# and loops. Scripting allows these to remain by converting the entire program
# to TorchScript. Scripting seems MUCH faster, is also harder get working.
#trace_model = True # TODO: Try True.
if trace_model:
print('Tracing model and saving as: {}'.format(output_filename))
#mel_file = '/home/bt/dev/voder/data/mels/LJ002-0320.mel'
#mel = torch.load(mel_file, map_location=torch.device('cpu'))
# NB: Getting `Tracing failed sanity checks! Graphs differed across invocations!`
inputs = (x_tst, x_tst_lengths)
#traced_script_module = torch.jit.trace(glow_model, inputs, check_trace=True)
traced_script_module = torch.jit.trace(glow_model, inputs, check_trace=False)
traced_script_module.save(output_filename)
else:
print('Saving script model as: {}'.format(output_filename))
container = torch.jit.script(glow_model)
container.save(output_filename)