-
Notifications
You must be signed in to change notification settings - Fork 19
/
model.py
executable file
·178 lines (142 loc) · 4.9 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
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
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, k=3, p=1):
super(ResidualBlock, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=k, padding=p),
nn.BatchNorm2d(out_channels),
nn.PReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=k, padding=p),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
return x + self.net(x)
class UpsampleBLock(nn.Module):
def __init__(self, in_channels, scaleFactor, k=3, p=1):
super(UpsampleBLock, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(in_channels, in_channels * (scaleFactor ** 2), kernel_size=k, padding=p),
nn.PixelShuffle(scaleFactor),
nn.PReLU()
)
def forward(self, x):
return self.net(x)
class Generator(nn.Module):
def __init__(self, n_residual=8):
super(Generator, self).__init__()
self.n_residual = n_residual
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=9, padding=4),
nn.PReLU()
)
for i in range(n_residual):
self.add_module('residual' + str(i+1), ResidualBlock(64, 64))
self.conv2 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.PReLU()
)
self.upsample = nn.Sequential(
UpsampleBLock(64, 2),
UpsampleBLock(64, 2),
nn.Conv2d(64, 3, kernel_size=9, padding=4)
)
def forward(self, x):
#print ('G input size :' + str(x.size()))
y = self.conv1(x)
cache = y.clone()
for i in range(self.n_residual):
y = self.__getattr__('residual' + str(i+1))(y)
y = self.conv2(y)
y = self.upsample(y + cache)
#print ('G output size :' + str(y.size()))
return (torch.tanh(y) + 1.0) / 2.0
class Discriminator(nn.Module):
def __init__(self, l=0.2):
super(Discriminator, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU(l),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(l),
nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU(l),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(l),
nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(l),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(l),
nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(l),
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(512, 1024, kernel_size=1),
nn.LeakyReLU(l),
nn.Conv2d(1024, 1, kernel_size=1)
)
def forward(self, x):
#print ('D input size :' + str(x.size()))
y = self.net(x)
#print ('D output size :' + str(y.size()))
si = torch.sigmoid(y).view(y.size()[0])
#print ('D output : ' + str(si))
return si
class Discriminator_WGAN(nn.Module):
def __init__(self, l=0.2):
super(Discriminator_WGAN, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.LeakyReLU(l),
nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(l),
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(512, 1024, kernel_size=1),
nn.LeakyReLU(l),
nn.Conv2d(1024, 1, kernel_size=1)
)
def forward(self, x):
#print ('D input size :' + str(x.size()))
y = self.net(x)
#print ('D output size :' + str(y.size()))
return y.view(y.size()[0])
def compute_gradient_penalty(D, real_samples, fake_samples):
alpha = torch.randn(real_samples.size(0), 1, 1, 1)
if torch.cuda.is_available():
alpha = alpha.cuda()
interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples)).requires_grad_(True)
d_interpolates = D(interpolates)
fake = torch.ones(d_interpolates.size())
if torch.cuda.is_available():
fake = fake.cuda()
gradients = torch.autograd.grad(
outputs=d_interpolates,
inputs=interpolates,
grad_outputs=fake,
create_graph=True,
retain_graph=True,
only_inputs=True,
)[0]
gradients = gradients.view(gradients.size(0), -1)
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty