-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
98 lines (85 loc) · 3.51 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
import torch
import torch.nn as nn
# Generator model
class Generator(nn.Module):
def __init__(self, in_channels=3, out_channels=3, features=[64, 128, 256, 512]):
super(Generator, self).__init__()
self.encoder = nn.ModuleList()
self.decoder = nn.ModuleList()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
# Encoder
for feature in features:
self.encoder.append(self.double_conv(in_channels, feature))
in_channels = feature
# Bottleneck
self.bottleneck = self.double_conv(features[-1], features[-1] * 2)
# Decoder
for feature in reversed(features):
self.decoder.append(
nn.ConvTranspose2d(feature * 2, feature, kernel_size=2, stride=2)
)
self.decoder.append(self.double_conv(feature * 2, feature))
# Final layer
self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)
def forward(self, x):
skip_connections = []
for layer in self.encoder:
x = layer(x)
skip_connections.append(x)
x = self.pool(x)
x = self.bottleneck(x)
skip_connections = skip_connections[::-1]
for idx in range(0, len(self.decoder), 2):
x = self.decoder[idx](x)
skip_connection = skip_connections[idx // 2]
if x.shape != skip_connection.shape:
x = torch.nn.functional.interpolate(x, size=skip_connection.shape[2:])
x = torch.cat((skip_connection, x), dim=1)
x = self.decoder[idx + 1](x)
return self.final_conv(x)
@staticmethod
def double_conv(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
# Discriminator model
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1), # -> (32, 128, 128)
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1), # -> (64, 64, 64)
nn.BatchNorm2d(64, momentum=0.82),
nn.ZeroPad2d((0, 1, 0, 1)),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1), # -> (128, 32, 32)
nn.BatchNorm2d(128, momentum=0.82),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1), # -> (256, 16, 16)
nn.BatchNorm2d(256, momentum=0.8),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1), # -> (512, 8, 8)
nn.BatchNorm2d(512, momentum=0.8),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(512, 1024, kernel_size=3, stride=2, padding=1), # -> (1024, 4, 4)
nn.BatchNorm2d(1024, momentum=0.8),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Flatten(),
nn.Linear(1024*5*5,1),
nn.Sigmoid()
)
def forward(self, img):
validity = self.model(img)
return validity