WeixuanYuan
commited on
Commit
•
7328fd9
1
Parent(s):
692f6ab
Upload VAE_torchV.py
Browse files- model/VAE_torchV.py +171 -0
model/VAE_torchV.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
class ChannelAttention(nn.Module):
|
6 |
+
def __init__(self, in_planes, ratio=16):
|
7 |
+
super(ChannelAttention, self).__init__()
|
8 |
+
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
9 |
+
self.max_pool = nn.AdaptiveMaxPool2d(1)
|
10 |
+
|
11 |
+
self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False)
|
12 |
+
self.relu1 = nn.ReLU()
|
13 |
+
self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False)
|
14 |
+
|
15 |
+
self.sigmoid = nn.Sigmoid()
|
16 |
+
|
17 |
+
def forward(self, x):
|
18 |
+
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
|
19 |
+
max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
|
20 |
+
y = avg_out + max_out
|
21 |
+
y = self.sigmoid(y)
|
22 |
+
|
23 |
+
return x * y.expand_as(x)
|
24 |
+
|
25 |
+
|
26 |
+
class ResCell(nn.Module):
|
27 |
+
def __init__(self, input_channel, output_channel, stride=1):
|
28 |
+
super(ResCell, self).__init__()
|
29 |
+
|
30 |
+
self.stride = stride
|
31 |
+
self.input_channel = input_channel
|
32 |
+
self.output_channel = output_channel
|
33 |
+
|
34 |
+
if self.stride == -1:
|
35 |
+
output_size = ()
|
36 |
+
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
37 |
+
self.skip = nn.Conv2d(self.input_channel, self.output_channel, kernel_size=1, stride=1, padding=0)
|
38 |
+
self.conv1 = nn.ConvTranspose2d(self.input_channel, self.output_channel, kernel_size=5, stride=2, padding=2, output_padding=1)
|
39 |
+
self.conv2 = nn.ConvTranspose2d(self.output_channel, self.output_channel, kernel_size=5, padding=2)
|
40 |
+
|
41 |
+
elif self.stride == 2:
|
42 |
+
self.skip = nn.Conv2d(self.input_channel, self.output_channel, kernel_size=1, stride=2, padding=0)
|
43 |
+
self.conv1 = nn.Conv2d(self.input_channel, self.output_channel, kernel_size=5, stride=self.stride, padding=2)
|
44 |
+
self.conv2 = nn.Conv2d(self.output_channel, self.output_channel, kernel_size=5, padding=2)
|
45 |
+
|
46 |
+
else:
|
47 |
+
self.conv1 = nn.Conv2d(self.input_channel, self.output_channel, kernel_size=5, stride=self.stride, padding=2)
|
48 |
+
self.conv2 = nn.Conv2d(self.output_channel, self.output_channel, kernel_size=5, padding=2)
|
49 |
+
|
50 |
+
self.bn1 = nn.BatchNorm2d(self.output_channel)
|
51 |
+
self.bn2 = nn.BatchNorm2d(self.output_channel)
|
52 |
+
|
53 |
+
# Please replace `CBAM` with the actual module and parameters
|
54 |
+
self.cbam = ChannelAttention(self.output_channel)
|
55 |
+
|
56 |
+
def forward(self, x):
|
57 |
+
if self.stride == -1:
|
58 |
+
upsampled_x = self.upsample(x)
|
59 |
+
skip = self.skip(upsampled_x)
|
60 |
+
x = F.elu(self.bn1(self.conv1(x)))
|
61 |
+
x = self.conv2(x)
|
62 |
+
elif self.stride == 2:
|
63 |
+
skip = self.skip(x)
|
64 |
+
x = F.elu(self.bn1(self.conv1(x)))
|
65 |
+
x = self.conv2(x)
|
66 |
+
else:
|
67 |
+
skip = x
|
68 |
+
x = F.elu(self.bn1(self.conv1(x)))
|
69 |
+
x = self.conv2(x)
|
70 |
+
|
71 |
+
x = self.bn2(x)
|
72 |
+
x = self.cbam(x)
|
73 |
+
x = x + skip
|
74 |
+
x = F.elu(x)
|
75 |
+
|
76 |
+
return x
|
77 |
+
|
78 |
+
|
79 |
+
class ResBlock(nn.Module):
|
80 |
+
def __init__(self, input_channel, output_channel, upsample=False, n_cells=2):
|
81 |
+
super(ResBlock, self).__init__()
|
82 |
+
|
83 |
+
stride = -1 if upsample else 2
|
84 |
+
self.cells = nn.ModuleList([ResCell(input_channel, output_channel, stride=stride)])
|
85 |
+
|
86 |
+
for _ in range(n_cells - 1):
|
87 |
+
self.cells.append(ResCell(input_channel, output_channel, stride=1))
|
88 |
+
|
89 |
+
def forward(self, x):
|
90 |
+
for cell in self.cells:
|
91 |
+
x = cell(x)
|
92 |
+
return x
|
93 |
+
|
94 |
+
|
95 |
+
class Encoder(nn.Module):
|
96 |
+
def __init__(self, input_shape, timbre_dim, N2=0, channel_sizes=None):
|
97 |
+
super(Encoder, self).__init__()
|
98 |
+
|
99 |
+
if channel_sizes is None:
|
100 |
+
channel_sizes = [32, 64, 64, 96, 96, 128, 160, 216]
|
101 |
+
|
102 |
+
self.input_shape = input_shape
|
103 |
+
self.timbre_dim = timbre_dim
|
104 |
+
self.blocks = nn.ModuleList()
|
105 |
+
|
106 |
+
self.blocks.append(ResBlock(input_channel=1, output_channel=channel_sizes[0], upsample=False, n_cells=1))
|
107 |
+
input_channel = channel_sizes[0]
|
108 |
+
|
109 |
+
for c in channel_sizes[1:]:
|
110 |
+
self.blocks.append(ResBlock(input_channel=input_channel, output_channel=c, upsample=False, n_cells=1 + N2))
|
111 |
+
input_channel = c
|
112 |
+
|
113 |
+
self.flatten = nn.Flatten()
|
114 |
+
self.mu_timbre = nn.Linear(self._get_flattened_dim(), timbre_dim)
|
115 |
+
self.sigma_timbre = nn.Linear(self._get_flattened_dim(), timbre_dim)
|
116 |
+
|
117 |
+
def _get_flattened_dim(self):
|
118 |
+
x = torch.zeros((1,) + self.input_shape)
|
119 |
+
for block in self.blocks:
|
120 |
+
x = block(x)
|
121 |
+
x = self.flatten(x)
|
122 |
+
return x.shape[1]
|
123 |
+
|
124 |
+
def reparameterize(self, mu, logvar):
|
125 |
+
std = torch.exp(0.5*logvar)
|
126 |
+
eps = torch.randn_like(std)
|
127 |
+
return mu + eps*std
|
128 |
+
|
129 |
+
def forward(self, x):
|
130 |
+
for block in self.blocks:
|
131 |
+
x = block(x)
|
132 |
+
|
133 |
+
x = self.flatten(x)
|
134 |
+
mu = self.mu_timbre(x)
|
135 |
+
logvar = self.sigma_timbre(x)
|
136 |
+
latent_vector = self.reparameterize(mu, logvar)
|
137 |
+
|
138 |
+
# kl_loss = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp(), dim=1)
|
139 |
+
# kl_loss = torch.mean(kl_loss)
|
140 |
+
|
141 |
+
return mu, logvar, latent_vector
|
142 |
+
|
143 |
+
|
144 |
+
class Decoder(nn.Module):
|
145 |
+
def __init__(self, timbre_dim, N2=0, N3=8, channel_sizes=None):
|
146 |
+
super(Decoder, self).__init__()
|
147 |
+
|
148 |
+
if channel_sizes is None:
|
149 |
+
channel_sizes = [32, 64, 64, 96, 96, 128, 160, 216]
|
150 |
+
|
151 |
+
self.conv_shape = [-1, channel_sizes[-1], 2 ** (9 - N3), 2 ** (8 - N3)]
|
152 |
+
|
153 |
+
self.dense = nn.Linear(timbre_dim, self.conv_shape[1] * self.conv_shape[2] * self.conv_shape[3])
|
154 |
+
self.blocks = nn.ModuleList()
|
155 |
+
|
156 |
+
input_channel = channel_sizes[-1]
|
157 |
+
for c in list(reversed(channel_sizes))[1:]:
|
158 |
+
self.blocks.append(ResBlock(input_channel=input_channel, output_channel=c, upsample=True, n_cells=1 + N2))
|
159 |
+
input_channel = c
|
160 |
+
|
161 |
+
self.decoder_conv = nn.ConvTranspose2d(channel_sizes[0], 1, kernel_size=5, stride=2, padding=2, output_padding=1)
|
162 |
+
|
163 |
+
def forward(self, x):
|
164 |
+
x = F.elu(self.dense(x))
|
165 |
+
x = x.view(-1, self.conv_shape[1], self.conv_shape[2], self.conv_shape[3])
|
166 |
+
for block in self.blocks:
|
167 |
+
x = block(x)
|
168 |
+
|
169 |
+
x = self.decoder_conv(x)
|
170 |
+
x = torch.sigmoid(x)
|
171 |
+
return x
|