-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
195 lines (165 loc) · 6.6 KB
/
models.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
187
188
189
190
191
192
193
194
195
# %%
# Where the VAE, the GAN, and the Diffussion Model are defined.
import numpy
import torch
import torch.nn as nn
import torch.nn.functional as F
print(f"The new Models 802 is imported")
class ConvAutoencoder(nn.Module):
def __init__(self):
super(ConvAutoencoder, self).__init__()
# Encoder
self.encoder = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(True),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(True),
)
# Bottleneck.
# Decoder
self.decoder = nn.Sequential(
nn.ConvTranspose2d(
128, 64, kernel_size=3, stride=2, padding=1, output_padding=1
),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(
64, 32, kernel_size=3, stride=2, padding=1, output_padding=1
),
nn.BatchNorm2d(32),
nn.ReLU(True),
nn.ConvTranspose2d(
32, 3, kernel_size=3, stride=2, padding=1, output_padding=1
),
nn.Sigmoid(), # Use sigmoid if image pixels are normalized between 0 and 1
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
x = F.interpolate(
x, size=(500, 500)
) # Ensure output matches input dimensions exactly
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.conv_layers = nn.Sequential(
# three image channels
nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.25),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
nn.ZeroPad2d((0, 1, 0, 1)),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.25),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.25),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.25),
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.25),
)
self.num_flat_features = self._get_conv_output()
self.fc = nn.Sequential(nn.Flatten(), nn.Linear(self.num_flat_features, 1))
def forward(self, x):
x = self.conv_layers(x)
x = self.fc(x)
return x
def _get_conv_output(self):
input = torch.rand(1, 3, 500, 500) # size of the images
output = self.conv_layers(input)
return int(torch.prod(torch.tensor(output.shape[1:])).item())
class DoubleConv(nn.Module):
"""(convolution => [BN] => ReLU) * 2"""
def __init__(self, in_channels, out_channels, mid_channels=None):
super().__init__()
if not mid_channels:
mid_channels = out_channels
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(mid_channels),
nn.ReLU(inplace=True),
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.double_conv(x)
class UNet(nn.Module):
def __init__(self, n_channels=3):
super(UNet, self).__init__()
self.inc = DoubleConv(n_channels, 64)
self.down1 = DoubleConv(64, 128)
self.down2 = DoubleConv(128, 256)
self.down3 = DoubleConv(256, 512)
self.up1 = DoubleConv(768, 256) # Adjusted channel sizes for concatenation
self.up2 = DoubleConv(384, 128)
self.up3 = DoubleConv(192, 64)
self.outc = nn.Conv2d(64, n_channels, kernel_size=1)
def forward(self, x):
x1 = self.inc(x)
x2 = F.max_pool2d(x1, 2)
x3 = self.down1(x2)
x4 = F.max_pool2d(x3, 2)
x5 = self.down2(x4)
x6 = F.max_pool2d(x5, 2)
x7 = self.down3(x6)
# Ensure dimensions match for skip connections
x = F.interpolate(x7, size=x5.size()[2:], mode="bilinear", align_corners=True)
x = torch.cat([x, x5], dim=1)
x = self.up1(x)
x = F.interpolate(x, size=x3.size()[2:], mode="bilinear", align_corners=True)
x = torch.cat([x, x3], dim=1)
x = self.up2(x)
x = F.interpolate(x, size=x1.size()[2:], mode="bilinear", align_corners=True)
x = torch.cat([x, x1], dim=1)
x = self.up3(x)
logits = self.outc(x)
return torch.sigmoid(logits)
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
# Encoder layers
self.conv1 = nn.Conv2d(3, 64, kernel_size=4, stride=2, padding=1)
self.conv2 = nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1)
self.conv3 = nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1)
self.conv_mu = nn.Conv2d(
256, 128, kernel_size=1
) # Output mu directly from feature maps
self.conv_logvar = nn.Conv2d(256, 128, kernel_size=1) # Output logvar directly
# Decoder layers
self.deconv1 = nn.ConvTranspose2d(128, 128, kernel_size=4, stride=2, padding=1)
self.deconv2 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1)
self.deconv3 = nn.ConvTranspose2d(64, 3, kernel_size=4, stride=2, padding=1)
def reparameterize(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
x = F.relu(self.conv1(x), inplace=True)
x = F.relu(self.conv2(x), inplace=True)
x = F.relu(self.conv3(x), inplace=True)
mu = self.conv_mu(x)
log_var = self.conv_logvar(x)
z = self.reparameterize(mu, log_var)
z = F.relu(self.deconv1(z), inplace=True)
z = F.relu(self.deconv2(z), inplace=True)
z = F.relu(self.deconv3(z), inplace=True)
z = F.interpolate(
z, size=(500, 500), mode="bilinear", align_corners=False
) # Correct size adjustment
z = torch.sigmoid(z) # Normalize to [0,1]
return z, mu, log_var
# %%