/
model.py
95 lines (69 loc) · 2.12 KB
/
model.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
import torch.nn as nn
class FrontEnd(nn.Module):
''' front end part of discriminator and Q'''
def __init__(self):
super(FrontEnd, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(1, 64, 4, 2, 1),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(128, 1024, 7, bias=False),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.1, inplace=True),
)
def forward(self, x):
output = self.main(x)
return output
class D(nn.Module):
def __init__(self):
super(D, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(1024, 1, 1),
nn.Sigmoid()
)
def forward(self, x):
output = self.main(x).view(-1, 1)
return output
class Q(nn.Module):
def __init__(self):
super(Q, self).__init__()
self.conv = nn.Conv2d(1024, 128, 1, bias=False)
self.bn = nn.BatchNorm2d(128)
self.lReLU = nn.LeakyReLU(0.1, inplace=True)
self.conv_disc = nn.Conv2d(128, 10, 1)
self.conv_mu = nn.Conv2d(128, 2, 1)
self.conv_var = nn.Conv2d(128, 2, 1)
def forward(self, x):
y = self.conv(x)
disc_logits = self.conv_disc(y).squeeze()
mu = self.conv_mu(y).squeeze()
var = self.conv_var(y).squeeze().exp()
return disc_logits, mu, var
class G(nn.Module):
def __init__(self):
super(G, self).__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(74, 1024, 1, 1, bias=False),
nn.BatchNorm2d(1024),
nn.ReLU(True),
nn.ConvTranspose2d(1024, 128, 7, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 1, 4, 2, 1, bias=False),
nn.Sigmoid()
)
def forward(self, x):
output = self.main(x)
return output
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)