-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.py
executable file
·186 lines (142 loc) · 6.32 KB
/
util.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import numpy as np
import torch
def flatten(v):
"""
Flatten a list of lists/tuples
"""
return [x for y in v for x in y]
def rescale(x):
"""
Rescale a tensor to 0-1
"""
return (x - x.min()) / (x.max() - x.min())
def find_max_epoch(path):
"""
Find maximum epoch/iteration in path, formatted ${n_iter}.pkl
E.g. 100000.pkl
Parameters:
path (str): checkpoint path
Returns:
maximum iteration, -1 if there is no (valid) checkpoint
"""
files = os.listdir(path)
epoch = -1
for f in files:
if len(f) <= 4:
continue
if f[-4:] == '.pkl':
try:
epoch = max(epoch, int(f[:-4]))
except:
continue
return epoch
def print_size(net):
"""
Print the number of parameters of a network
"""
if net is not None and isinstance(net, torch.nn.Module):
module_parameters = filter(lambda p: p.requires_grad, net.parameters())
params = sum([np.prod(p.size()) for p in module_parameters])
print("{} Parameters: {:.6f}M".format(
net.__class__.__name__, params / 1e6), flush=True)
# Utilities for diffusion models
def std_normal(size):
"""
Generate the standard Gaussian variable of a certain size
"""
return torch.normal(0, 1, size=size).cuda()
def calc_diffusion_step_embedding(diffusion_steps, diffusion_step_embed_dim_in):
"""
Embed a diffusion step $t$ into a higher dimensional space
E.g. the embedding vector in the 128-dimensional space is
[sin(t * 10^(0*4/63)), ... , sin(t * 10^(63*4/63)), cos(t * 10^(0*4/63)), ... , cos(t * 10^(63*4/63))]
Parameters:
diffusion_steps (torch.long tensor, shape=(batchsize, 1)):
diffusion steps for batch data
diffusion_step_embed_dim_in (int, default=128):
dimensionality of the embedding space for discrete diffusion steps
Returns:
the embedding vectors (torch.tensor, shape=(batchsize, diffusion_step_embed_dim_in)):
"""
assert diffusion_step_embed_dim_in % 2 == 0
half_dim = diffusion_step_embed_dim_in // 2
_embed = np.log(10000) / (half_dim - 1)
_embed = torch.exp(torch.arange(half_dim) * -_embed).cuda()
_embed = diffusion_steps * _embed
diffusion_step_embed = torch.cat((torch.sin(_embed),
torch.cos(_embed)), 1)
return diffusion_step_embed
def calc_diffusion_hyperparams(T, beta_0, beta_T):
"""
Compute diffusion process hyperparameters
Parameters:
T (int): number of diffusion steps
beta_0 and beta_T (float): beta schedule start/end value,
where any beta_t in the middle is linearly interpolated
Returns:
a dictionary of diffusion hyperparameters including:
T (int), Beta/Alpha/Alpha_bar/Sigma (torch.tensor on cpu, shape=(T, ))
These cpu tensors are changed to cuda tensors on each individual gpu
"""
Beta = torch.linspace(beta_0, beta_T, T)
Alpha = 1 - Beta
Alpha_bar = Alpha + 0
Beta_tilde = Beta + 0
for t in range(1, T):
Alpha_bar[t] *= Alpha_bar[t-1] # \bar{\alpha}_t = \prod_{s=1}^t \alpha_s
Beta_tilde[t] *= (1-Alpha_bar[t-1]) / (1-Alpha_bar[t]) # \tilde{\beta}_t = \beta_t * (1-\bar{\alpha}_{t-1}) / (1-\bar{\alpha}_t)
Sigma = torch.sqrt(Beta_tilde) # \sigma_t^2 = \tilde{\beta}_t
_dh = {}
_dh["T"], _dh["Beta"], _dh["Alpha"], _dh["Alpha_bar"], _dh["Sigma"] = T, Beta, Alpha, Alpha_bar, Sigma
diffusion_hyperparams = _dh
return diffusion_hyperparams
def sampling(net, size, diffusion_hyperparams):
"""
Perform the complete sampling step according to p(x_0|x_T) = \prod_{t=1}^T p_{\theta}(x_{t-1}|x_t)
Parameters:
net (torch network): the wavenet model
size (tuple): size of tensor to be generated,
usually is (number of audios to generate, channels=1, length of audio)
diffusion_hyperparams (dict): dictionary of diffusion hyperparameters returned by calc_diffusion_hyperparams
note, the tensors need to be cuda tensors
Returns:
the generated audio(s) in torch.tensor, shape=size
"""
_dh = diffusion_hyperparams
T, Alpha, Alpha_bar, Sigma = _dh["T"], _dh["Alpha"], _dh["Alpha_bar"], _dh["Sigma"]
assert len(Alpha) == T
assert len(Alpha_bar) == T
assert len(Sigma) == T
assert len(size) == 3
print('begin sampling, total number of reverse steps = %s' % T)
x = std_normal(size)
with torch.no_grad():
for t in range(T-1, -1, -1):
diffusion_steps = (t * torch.ones((size[0], 1))).cuda() # use the corresponding reverse step
epsilon_theta = net((x, diffusion_steps,)) # predict \epsilon according to \epsilon_\theta
x = (x - (1-Alpha[t])/torch.sqrt(1-Alpha_bar[t]) * epsilon_theta) / torch.sqrt(Alpha[t]) # update x_{t-1} to \mu_\theta(x_t)
if t > 0:
x = x + Sigma[t] * std_normal(size) # add the variance term to x_{t-1}
return x
def training_loss(net, loss_fn, X, diffusion_hyperparams):
"""
Compute the training loss of epsilon and epsilon_theta
Parameters:
net (torch network): the wavenet model
loss_fn (torch loss function): the loss function, default is nn.MSELoss()
X (torch.tensor): training data, shape=(batchsize, 1, length of audio)
diffusion_hyperparams (dict): dictionary of diffusion hyperparameters returned by calc_diffusion_hyperparams
note, the tensors need to be cuda tensors
Returns:
training loss
"""
_dh = diffusion_hyperparams
T, Alpha_bar = _dh["T"], _dh["Alpha_bar"]
audio = X
B, C, L = audio.shape # B is batchsize, C=1, L is audio length
diffusion_steps = torch.randint(T, size=(B,1,1)).cuda() # randomly sample diffusion steps from 1~T
z = std_normal(audio.shape)
transformed_X = torch.sqrt(Alpha_bar[diffusion_steps]) * audio + torch.sqrt(1-Alpha_bar[diffusion_steps]) * z # compute x_t from q(x_t|x_0)
epsilon_theta = net((transformed_X, diffusion_steps.view(B,1),)) # predict \epsilon according to \epsilon_\theta
return loss_fn(epsilon_theta, z)