-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet_vae.py
161 lines (132 loc) · 5.64 KB
/
resnet_vae.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
import torch
from torch import nn, optim
import torch.nn.functional as F
class ResizeConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, scale_factor, mode='nearest'):
super().__init__()
self.scale_factor = scale_factor
self.mode = mode
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=1)
def forward(self, x):
x = F.interpolate(x, scale_factor=self.scale_factor, mode=self.mode)
x = self.conv(x)
return x
class BasicBlockEnc(nn.Module):
def __init__(self, in_planes, stride=1):
super().__init__()
planes = in_planes*stride
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
if stride == 1:
self.shortcut = nn.Sequential()
else:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes)
)
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = torch.relu(out)
return out
class BasicBlockDec(nn.Module):
def __init__(self, in_planes, stride=1):
super().__init__()
planes = int(in_planes/stride)
self.conv2 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(in_planes)
# self.bn1 could have been placed here, but that messes up the order of the layers when printing the class
if stride == 1:
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
else:
self.conv1 = ResizeConv2d(in_planes, planes, kernel_size=3, scale_factor=stride)
self.bn1 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential(
ResizeConv2d(in_planes, planes, kernel_size=3, scale_factor=stride),
nn.BatchNorm2d(planes)
)
def forward(self, x):
out = torch.relu(self.bn2(self.conv2(x)))
out = self.bn1(self.conv1(out))
out += self.shortcut(x)
out = torch.relu(out)
return out
class ResNet18Enc(nn.Module):
def __init__(self, num_Blocks=[2,2,2,2], z_dim=10, nc=1):
super().__init__()
self.in_planes = 64
self.z_dim = z_dim
self.conv1 = nn.Conv2d(nc, 64, kernel_size=3, stride=2, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.layer1 = self._make_layer(BasicBlockEnc, 64, num_Blocks[0], stride=1)
self.layer2 = self._make_layer(BasicBlockEnc, 128, num_Blocks[1], stride=2)
self.layer3 = self._make_layer(BasicBlockEnc, 256, num_Blocks[2], stride=2)
self.layer4 = self._make_layer(BasicBlockEnc, 512, num_Blocks[3], stride=2)
self.linear = nn.Linear(512, 2 * z_dim)
def _make_layer(self, BasicBlockEnc, planes, num_Blocks, stride):
strides = [stride] + [1]*(num_Blocks-1)
layers = []
for stride in strides:
layers += [BasicBlockEnc(self.in_planes, stride)]
self.in_planes = planes
return nn.Sequential(*layers)
def forward(self, x):
x = torch.relu(self.bn1(self.conv1(x)))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = F.adaptive_avg_pool2d(x, 1)
x = x.view(x.size(0), -1)
x = self.linear(x)
mu = x[:, :self.z_dim]
logvar = x[:, self.z_dim:]
return mu, logvar
class ResNet18Dec(nn.Module):
def __init__(self, num_Blocks=[2,2,2,2], z_dim=10, nc=1):
super().__init__()
self.in_planes = 512
self.linear = nn.Linear(z_dim, 512)
self.layer4 = self._make_layer(BasicBlockDec, 256, num_Blocks[3], stride=2)
self.layer3 = self._make_layer(BasicBlockDec, 128, num_Blocks[2], stride=2)
self.layer2 = self._make_layer(BasicBlockDec, 64, num_Blocks[1], stride=2)
self.layer1 = self._make_layer(BasicBlockDec, 64, num_Blocks[0], stride=1)
self.conv1 = ResizeConv2d(64, nc, kernel_size=3, scale_factor=1)
def _make_layer(self, BasicBlockDec, planes, num_Blocks, stride):
strides = [stride] + [1]*(num_Blocks-1)
layers = []
for stride in reversed(strides):
layers += [BasicBlockDec(self.in_planes, stride)]
self.in_planes = planes
return nn.Sequential(*layers)
def forward(self, z):
x = self.linear(z)
x = x.view(z.size(0), 512, 1, 1)
x = F.interpolate(x, scale_factor=16)
x = self.layer4(x)
x = self.layer3(x)
x = self.layer2(x)
x = self.layer1(x)
x = torch.sigmoid(self.conv1(x))
x = x.view(x.size(0), 1, 128, 128)
return x
class VAE_mitbih(nn.Module):
def __init__(self, z_dim):
super().__init__()
self.encoder = ResNet18Enc(z_dim=z_dim)
self.decoder = ResNet18Dec(z_dim=z_dim)
def forward(self, x):
mean, logvar = self.encoder(x)
z = self.reparameterize(mean, logvar)
x = self.decoder(z)
return x, mean, logvar
@staticmethod
def reparameterize(mean, logvar):
std = torch.exp(logvar / 2) # in log-space, squareroot is divide by two
epsilon = torch.randn_like(std)
return epsilon * std + mean