-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsrresnet.py
177 lines (155 loc) · 7.13 KB
/
srresnet.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
import math
import torch.nn.init as init
from torch.autograd import Variable
class LayerNormalization(nn.Module):
def __init__(self, hidden_size, eps=1e-5):
super(LayerNormalization, self).__init__()
self.eps = eps
self.hidden_size = hidden_size
self.a2 = nn.Parameter(torch.ones(1, hidden_size, 1, 1), requires_grad=True)
self.b2 = nn.Parameter(torch.zeros(1, hidden_size, 1, 1), requires_grad=True)
def forward(self, z):
mu = torch.mean(z, dim=1, keepdim=True)
sigma = torch.std(z, dim=1, keepdim=True)
ln_out = (z - mu.expand_as(z)) / (sigma.expand_as(z) + self.eps)
ln_out = ln_out * self.a2.expand_as(z) + self.b2.expand_as(z)
return ln_out
class _Residual_Block(nn.Module):
def __init__(self, norm):
super(_Residual_Block, self).__init__()
self.conv1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
self.norm = norm
if self.norm == 'BatchNorm':
self.norm1 = nn.BatchNorm2d(64)
self.norm2 = nn.BatchNorm2d(64)
else:
self.norm1 = LayerNormalization(64)
self.norm2 = LayerNormalization(64)
def forward(self, x):
identity_data = x
output = self.conv1(x)
output = self.norm1(output)
output = self.relu(output)
output = self.conv2(output)
output = self.norm2(output)
output = self.relu(output)
output = torch.add(output,identity_data)
return output
class NetG(nn.Module):
def __init__(self, imageSize):
super(NetG, self).__init__()
self.imageSize = imageSize
self.conv_input = nn.Conv2d(in_channels=2, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
self.linear = nn.Linear(128, 512)
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.residual = self.make_layer(_Residual_Block, 6)
self.conv_mid = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn_mid = nn.BatchNorm2d(64)
if imageSize == 128:
self.upscale = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
)
else:
self.upscale = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False),
nn.PixelShuffle(2),
nn.LeakyReLU(0.2, inplace=True),
)
self.conv_output = nn.Conv2d(in_channels=64, out_channels=3, kernel_size=9, stride=1, padding=4, bias=False)
self.tanh = nn.Tanh()
for m in self.modules():
if isinstance(m, nn.Conv2d):
#init.orthogonal(m.weight, math.sqrt(2))
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.zero_()
def make_layer(self, block, num_of_layer):
layers = []
for _ in range(num_of_layer):
layers.append(block(norm="BatchNorm"))
return nn.Sequential(*layers)
def forward(self, x):
out = self.linear(x.view(-1, 128))
out = out.view(-1, 2, 16, 16)
out = self.relu(self.conv_input(out))
residual = out
out = self.residual(out)
out = self.bn_mid(self.conv_mid(out))
out = torch.add(out,residual)
out = self.upscale(out)
out = self.tanh(self.conv_output(out))
return out, 0
class NetD(nn.Module):
def __init__(self, norm, imageSize):
super(NetD, self).__init__()
self.imageSize = imageSize
self.norm = norm
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.sigmoid = nn.Sigmoid()
if imageSize == 128:
self.conv_input = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=2, padding=1, bias=False),
self.relu,
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1, bias=False),
self.relu,
LayerNormalization(64)
)
else:
self.conv_input = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=2, padding=1, bias=False),
self.relu
)
self.ln1 = LayerNormalization(64)
self.ln2 = LayerNormalization(64)
self.residual = self.make_layer(_Residual_Block, 5)
self.conv_mid = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
self.conv_output = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1, bias=False)
self.conv_fc = nn.Conv2d(in_channels=64, out_channels=1, kernel_size=16, stride=1, padding=0, bias=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
#init.orthogonal(m.weight, math.sqrt(2))
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.zero_()
def make_layer(self, block, num_of_layer):
layers = []
for _ in range(num_of_layer):
layers.append(block(self.norm))
return nn.Sequential(*layers)
def forward(self, x):
out = self.conv_input(x)
residual = out
out = self.residual(out)
out = self.ln1(self.conv_mid(out))
out = torch.add(out,residual)
out = self.relu(self.conv_output(out))
out = self.ln2(out)
out_d = self.conv_fc(out)
out_d = self.sigmoid(out_d)
out_d = out_d.mean(0).view(1)
return out_d