-
Notifications
You must be signed in to change notification settings - Fork 5
/
onnx_export.py
162 lines (138 loc) · 5.84 KB
/
onnx_export.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import argparse
import json
import os
from typing import List
import numpy as np
import torch
import yaml
from torch import nn, Tensor, LongTensor
from fregan import Generator
from modules.fastspeech2 import MelSpectrogramDecoder, PitchAndDurationPredictor, FeatureEmbedder, VocoderGenerator
from text import phoneme_to_id, accent_to_id
from utils.model import Config, get_model, get_vocoder
from torch.onnx.symbolic_registry import _onnx_stable_opsets
OPSET = _onnx_stable_opsets[-1]
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = torch.device("cpu")
class Variance(nn.Module):
def __init__(self, config: Config, variance_model: PitchAndDurationPredictor, pitch_mean: float, pitch_std: float):
super(Variance, self).__init__()
self.sampling_rate = config["preprocess"]["audio"]["sampling_rate"]
self.hop_length = config["preprocess"]["stft"]["hop_length"]
self.variance_model = variance_model
self.pitch_mean = pitch_mean
self.pitch_std = pitch_std
def forward(
self,
phonemes: Tensor,
accents: Tensor,
speakers: Tensor,
):
pitches, log_durations = self.variance_model(phonemes, accents, speakers)
pitches = torch.log(pitches * self.pitch_std + self.pitch_mean)
durations = torch.clamp((torch.exp(log_durations) - 1) / (self.sampling_rate / self.hop_length), min=0.01)
return pitches, durations
class Embedder(nn.Module):
def __init__(self, config: Config, embedder_model: FeatureEmbedder, pitch_mean: float, pitch_std: float):
super(Embedder, self).__init__()
self.sampling_rate = config["preprocess"]["audio"]["sampling_rate"]
self.hop_length = config["preprocess"]["stft"]["hop_length"]
self.embedder_model = embedder_model
self.pitch_mean = pitch_mean
self.pitch_std = pitch_std
def forward(
self,
phonemes: Tensor,
pitches: Tensor,
speakers: Tensor,
):
pitches = (torch.exp(pitches) - self.pitch_mean) / self.pitch_std
feature_embedded = self.embedder_model(phonemes, pitches, speakers)
return feature_embedded
class Decoder(nn.Module):
def __init__(self, config: Config, decoder: MelSpectrogramDecoder, vocoder: VocoderGenerator):
super(Decoder, self).__init__()
self.max_wav_value = config["preprocess"]["audio"]["max_wav_value"]
self.decoder = decoder
self.vocoder_type = config["model"]["vocoder_type"]
self.vocoder = vocoder
def forward(self, length_regulated_tensor: Tensor) -> Tensor:
_, postnet_outputs = self.decoder(length_regulated_tensor)
if self.vocoder_type == "melgan":
wavs = self.vocoder.inference(postnet_outputs[0].transpose(0, 1).unsqueeze(0)).unsqueeze(0)
else:
wavs = self.vocoder(postnet_outputs[0].transpose(0, 1).unsqueeze(0)).squeeze(1)
return wavs
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--restore_step", type=int, default=0)
parser.add_argument("--speaker_num", type=int, default=10)
parser.add_argument(
"-c", "--config", type=str, required=True, help="path to config yaml"
)
args = parser.parse_args()
config: Config = yaml.load(
open(args.config, "r"), Loader=yaml.FullLoader
)
variance_model, embedder_model, decoder_model, _ = get_model(args.restore_step, config, device, args.speaker_num, False)
fregan_model = get_vocoder(device, config["model"]["vocoder_type"])
with open(
os.path.join(config["preprocess"]["path"]["preprocessed_path"], "stats.json")
) as f:
stats = json.load(f)
pitch_data: List[float] = stats["pitch"]
pitch_mean, pitch_std = pitch_data[2], pitch_data[3]
variance_model = Variance(config, variance_model, pitch_mean, pitch_std)
embedder_model = Embedder(config, embedder_model, pitch_mean, pitch_std)
decoder_model = Decoder(config, decoder_model, fregan_model)
decoder_model.eval()
decoder_model.requires_grad_ = False
phonemes = torch.from_numpy(np.array([[phoneme_to_id[p] for p in "k o N n i ch i w a".split(" ")]])).to(dtype=torch.int64, device=device)
accents = torch.from_numpy(np.array([[accent_to_id[a] for a in "_ [ _ _ _ _ _ _ #".split(" ")]])).to(dtype=torch.int64, device=device)
speakers = torch.from_numpy(np.array([0])).to(dtype=torch.int64, device=device)
torch.onnx.export(
variance_model,
(
phonemes, accents, speakers
),
"variance_model.onnx",
input_names=["phonemes", "accents", "speakers"],
output_names=["pitches", "durations"],
dynamic_axes={
"phonemes": {1: "inLength"},
"accents": {1: "inLength"},
"pitches": {1: "outLength"},
"durations": {1: "outLength"}
},
opset_version=OPSET,
)
pitches = torch.from_numpy(np.array([[5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5]])).to(dtype=torch.float, device=device)
embedber_input = (phonemes, pitches, speakers)
torch.onnx.export(
embedder_model,
embedber_input,
"embedder_model.onnx",
input_names=["phonemes", "pitches", "speakers"],
output_names=["feature_embedded"],
dynamic_axes={
"phonemes": {1: "inLength"},
"pitches": {1: "inLength"},
"feature_embedded": {1: "outLength"},
},
opset_version=OPSET,
)
embedded_tensor = embedder_model(*embedber_input)
torch.onnx.export(
decoder_model,
(
embedded_tensor,
),
"decoder_model.onnx",
input_names=["length_regulated_tensor"],
output_names=["wav"],
dynamic_axes={
"length_regulated_tensor": {1: "length"},
"wav": {1: "outLength"},
},
opset_version=OPSET,
)