-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDiffusion_Denoising.py
More file actions
154 lines (132 loc) · 5.53 KB
/
Diffusion_Denoising.py
File metadata and controls
154 lines (132 loc) · 5.53 KB
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
import math
import torch
from torch import nn
import torch.nn.functional as F
class DiffusionEmbedding(nn.Module):
def __init__(self, dim, proj_dim, max_steps=500):
super().__init__()
self.register_buffer(
"embedding", self._build_embedding(dim, max_steps), persistent=False
)
self.projection1 = nn.Linear(dim * 2, proj_dim)
self.projection2 = nn.Linear(proj_dim, proj_dim)
def forward(self, diffusion_step):
x = self.embedding[diffusion_step]
x = self.projection1(x)
x = F.silu(x)
x = self.projection2(x)
x = F.silu(x)
return x # [batch_size, proj_dim]
def _build_embedding(self, dim, max_steps):
steps = torch.arange(max_steps).unsqueeze(1) # [T,1]
dims = torch.arange(dim).unsqueeze(0) # [1,dim]
table = steps * 10.0 ** (dims * 4.0 / dim) # [T,dim]
table = torch.cat(
[torch.sin(table), torch.cos(table)], dim=1) # [T,2*dim]
return table
class ResidualBlock(nn.Module):
def __init__(self, hidden_size, residual_channels, dilation):
super().__init__()
self.dilated_conv = nn.Conv1d(
residual_channels,
2 * residual_channels,
3,
padding=dilation,
dilation=dilation,
padding_mode="circular",
)
self.diffusion_projection = nn.Linear(hidden_size, residual_channels)
self.conditioner_projection = nn.Conv1d(
1, 2 * residual_channels, 1, padding=2, padding_mode="circular"
)
self.output_projection = nn.Conv1d(
residual_channels, 2 * residual_channels, 1)
nn.init.kaiming_normal_(self.conditioner_projection.weight)
nn.init.kaiming_normal_(self.output_projection.weight)
def forward(self, x, conditioner, diffusion_step):
diffusion_step = self.diffusion_projection(
diffusion_step).unsqueeze(-1)
conditioner = self.conditioner_projection(conditioner)
y = x + diffusion_step
y = self.dilated_conv(y) + conditioner
gate, filter = torch.chunk(y, 2, dim=1)
y = torch.sigmoid(gate) * torch.tanh(filter)
y = self.output_projection(y)
y = F.leaky_relu(y, 0.4)
residual, skip = torch.chunk(y, 2, dim=1)
return (x + residual) / math.sqrt(2.0), skip
class CondUpsampler(nn.Module):
def __init__(self, cond_length, target_dim):
super().__init__()
self.linear1 = nn.Linear(cond_length, target_dim // 2)
self.linear2 = nn.Linear(target_dim // 2, target_dim)
def forward(self, x):
x = self.linear1(x)
x = F.leaky_relu(x, 0.4)
x = self.linear2(x)
x = F.leaky_relu(x, 0.4)
return x
class EpsilonTheta(nn.Module):
def __init__(
self,
target_dim,
cond_length,
time_emb_dim=16,
residual_layers=8,
residual_channels=8,
dilation_cycle_length=2,
residual_hidden=64,
):
"""Denoising Network
Args:
target_dim (int): Target dimension 1
cond_length (int): Condition length 100
time_emb_dim (int, optional): Time embedding. Defaults to 16.
residual_layers (int, optional): Number of residual layers. Defaults to 8.
residual_channels (int, optional): Residual channels. Defaults to 8.
dilation_cycle_length (int, optional): Dilation cycle length. Defaults to 2.
residual_hidden (int, optional): Residual hidden size. Defaults to 64.
"""
super().__init__()
self.input_projection = nn.Conv1d(
1, residual_channels, 1, padding=2, padding_mode="circular"
) # 1D convolution shape [batch_size, residual_channels, target_dim]
self.diffusion_embedding = DiffusionEmbedding(
time_emb_dim, proj_dim=residual_hidden
) # Time embedding shape [batch_size, proj_dim]
self.cond_upsampler = CondUpsampler(
target_dim=target_dim, cond_length=cond_length
) # Condition upsampling shape [batch_size, target_dim]
self.residual_layers = nn.ModuleList(
[
ResidualBlock(
residual_channels=residual_channels,
dilation=2 ** (i % dilation_cycle_length),
hidden_size=residual_hidden,
)
for i in range(residual_layers)
]
)
self.skip_projection = nn.Conv1d(
residual_channels, residual_channels, 3) # Skip connection
self.output_projection = nn.Conv1d(residual_channels, 1, 3) # Output
# Kaiming initialization
nn.init.kaiming_normal_(self.input_projection.weight)
nn.init.kaiming_normal_(self.skip_projection.weight)
# Initialize output weights to 0
nn.init.zeros_(self.output_projection.weight)
def forward(self, inputs, time, cond):
x = self.input_projection(inputs) # [B,8,T]
x = F.leaky_relu(x, 0.4) # [B,8,T]
diffusion_step = self.diffusion_embedding(time) # [B,64]
cond_up = self.cond_upsampler(cond) # [B,1,T]
skip = []
for layer in self.residual_layers:
x, skip_connection = layer(x, cond_up, diffusion_step)
skip.append(skip_connection)
x = torch.sum(torch.stack(skip), dim=0) / \
math.sqrt(len(self.residual_layers)) # [B,8,T]
x = self.skip_projection(x) # [B,8,T]
x = F.leaky_relu(x, 0.4)
x = self.output_projection(x) # [B,1,T]
return x # [B,1,T]