-
Notifications
You must be signed in to change notification settings - Fork 20
/
models.py
117 lines (102 loc) · 3.63 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
import torch.nn as nn
class E1(nn.Module):
def __init__(self, sep, size):
super(E1, self).__init__()
self.sep = sep
self.size = size
self.full = nn.Sequential(
nn.Conv2d(3, 32, 4, 2, 1),
nn.InstanceNorm2d(32),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 256, 4, 2, 1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(256, (512 - self.sep), 4, 2, 1),
nn.InstanceNorm2d(512 - self.sep),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d((512 - self.sep), (512 - self.sep), 4, 2, 1),
nn.InstanceNorm2d(512 - self.sep),
nn.LeakyReLU(0.2, inplace=True),
)
def forward(self, net):
net = self.full(net)
net = net.view(-1, (512 - self.sep) * self.size * self.size)
return net
class E2(nn.Module):
def __init__(self, sep, size):
super(E2, self).__init__()
self.sep = sep
self.size = size
self.full = nn.Sequential(
nn.Conv2d(3, 32, 4, 2, 1),
nn.InstanceNorm2d(32),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 128, 4, 2, 1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 128, 4, 2, 1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, self.sep, 4, 2, 1),
nn.InstanceNorm2d(self.sep),
nn.LeakyReLU(0.2),
)
def forward(self, net):
net = self.full(net)
net = net.view(-1, self.sep * self.size * self.size)
return net
class Decoder(nn.Module):
def __init__(self, size):
super(Decoder, self).__init__()
self.size = size
self.main = nn.Sequential(
nn.ConvTranspose2d(512, 512, 4, 2, 1),
nn.InstanceNorm2d(512),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(512, 256, 4, 2, 1),
nn.InstanceNorm2d(256),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(256, 128, 4, 2, 1),
nn.InstanceNorm2d(128),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(128, 64, 4, 2, 1),
nn.InstanceNorm2d(64),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.InstanceNorm2d(32),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(32, 3, 4, 2, 1),
nn.Tanh()
)
def forward(self, net):
net = net.view(-1, 512, self.size, self.size)
net = self.main(net)
return net
class Disc(nn.Module):
def __init__(self, sep, size):
super(Disc, self).__init__()
self.sep = sep
self.size = size
self.classify = nn.Sequential(
nn.Linear((512 - self.sep) * self.size * self.size, 512),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 1),
nn.Sigmoid()
)
def forward(self, net):
net = net.view(-1, (512 - self.sep) * self.size * self.size)
net = self.classify(net)
net = net.view(-1)
return net