-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresnet.py
229 lines (163 loc) · 6.59 KB
/
resnet.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from torch import nn
from torch.autograd import grad
import torch
from model_utils import *
class conv3x3(nn.Module):
'''
3x3 Conv utility class
'''
def __init__(self, input_dim, output_dim = None, bias = False):
super(conv3x3, self).__init__()
if output_dim == None:
output_dim = input_dim
self.conv = nn.Conv2d(input_dim, output_dim, 3, stride=1, padding=1, bias = bias)
def forward(self, input):
output = self.conv(input)
return output
class ResidualBlockDownSample(nn.Module):
'''
Residual block with downsplampled shortcut, which reduces the spatial size by half.
To be used in Discrimintor.
Args:
input_dim: number of features
size: spatial dimension of the input.
'''
def __init__(self, input_dim, size=48):
super(ResidualBlockDownSample, self).__init__()
half_size = size//2
self.avg_pool1 = nn.AdaptiveAvgPool2d((half_size,half_size))
self.conv_shortcut = nn.Conv2d(input_dim, input_dim, kernel_size = 1)
self.relu1 = nn.LeakyReLU(0.2)
self.relu2 = nn.LeakyReLU(0.2)
self.ln1 = nn.LayerNorm([input_dim, size, size])
self.ln2 = nn.LayerNorm([input_dim, half_size, half_size])
self.conv_1 = conv3x3(input_dim, input_dim, bias = False)
self.conv_2 = conv3x3(input_dim, input_dim, bias = False)
self.avg_pool2 = nn.AdaptiveAvgPool2d((half_size, half_size))
def forward(self, input):
shortcut = self.avg_pool1(input)
shortcut = self.conv_shortcut(shortcut)
output = self.ln1(input)
output = self.relu1(output)
output = self.conv_1(output)
output = self.avg_pool2(output)
output = self.ln2(output)
output = self.relu2(output)
output = self.conv_2(output)
return shortcut + output
class ResidualBlockUpSample(nn.Module):
"""
Residual block with upsampled shortcut, which doubles the spatial size.
To be used in Discrimintor.
Args:
input_dim: number of features
size: spatial dimension of the input.
"""
def __init__(self, input_dim, size):
super(ResidualBlockUpSample, self).__init__()
self.upsample1 = nn.Upsample(scale_factor=2)
self.conv_shortcut = nn.Conv2d(input_dim, input_dim, kernel_size = 1)
self.relu1 = nn.ReLU(True)
self.relu2 = nn.ReLU(True)
self.bn1 = nn.BatchNorm2d(input_dim)
self.bn2 = nn.BatchNorm2d(input_dim)
self.conv_1 = conv3x3(input_dim, input_dim, bias = False)
self.conv_2 = conv3x3(input_dim, input_dim, bias = False)
self.upsample2 = nn.Upsample(scale_factor=2)
def forward(self, input):
shortcut = self.upsample1(input)
shortcut = self.conv_shortcut(shortcut)
output = self.bn1(input)
output = self.relu1(output)
output = self.conv_1(output)
output = self.upsample2(output)
output = self.bn2(output)
output = self.relu2(output)
output = self.conv_2(output)
return shortcut + output
class ResidualBlock(nn.Module):
"""
Standart residual block with layernorm instead of batchnorm.
"""
def __init__(self, input_dim, size):
super(ResidualBlock, self).__init__()
self.conv_shortcut = nn.Conv2d(input_dim, input_dim, kernel_size = 1)
self.relu1 = nn.LeakyReLU(0.2)
self.relu2 = nn.LeakyReLU(0.2)
self.ln1 = nn.LayerNorm([input_dim, size, size])
self.ln2 = nn.LayerNorm([input_dim, size, size])
self.conv_1 = conv3x3(input_dim, input_dim, bias = False)
self.conv_2 = conv3x3(input_dim, input_dim, bias = False)
def forward(self, input):
shortcut = self.conv_shortcut(input)
output = self.ln1(input)
output = self.relu1(output)
output = self.conv_1(output)
output = self.ln2(output)
output = self.relu2(output)
output = self.conv_2(output)
return shortcut + output
class GeneratorResNet(nn.Module):
"""
Resnet Generator Network
Dimension flow:
128 => dim,3,3 => dim,6,6 => dim,12,12 => dim,24,24 => dim,48,48 => 3,48,48
Args:
dim: number of features
"""
def __init__(self, dim=256):
super(GeneratorResNet, self).__init__()
self.dim = dim
self.ln1 = nn.Linear(128, 3*3*self.dim, bias=False)
self.reshape = View((self.dim, 3, 3))
self.bn_ln = nn.BatchNorm2d(self.dim)
self.rb1 = ResidualBlockUpSample(self.dim, size=3)
self.rb2 = ResidualBlockUpSample(self.dim, size=6)
self.rb3 = ResidualBlockUpSample(self.dim, size=12)
self.rb4 = ResidualBlockUpSample(self.dim, size=24)
self.bn = nn.BatchNorm2d(self.dim)
self.conv1 = conv3x3(self.dim, 3)
self.relu = nn.ReLU(True)
self.tanh = nn.Tanh()
def forward(self, input):
output = input
output = self.ln1(output)
output = self.reshape(output)
output = self.bn_ln(output)
output = self.rb1(output)
output = self.rb2(output)
output = self.rb3(output)
output = self.rb4(output)
output = self.bn(output)
output = self.relu(output)
output = self.conv1(output)
output = self.tanh(output)
return output
class DiscriminatorResNet(nn.Module):
"""
Resnet Discriminator Network attached with Geometric block.
Dimension flow:
3,48,48 => dim,48,48 => dim,24,24 => dim,12,12 => dim,6,6 => dim,3,3
Args:
dim: number of features
"""
def __init__(self, dim=256):
super(DiscriminatorResNet, self).__init__()
self.dim = dim
self.conv1 = conv3x3(3, self.dim)
self.rb1 = ResidualBlockDownSample(self.dim, size=48)
self.rb2 = ResidualBlockDownSample(self.dim, size=24)
self.rb3 = ResidualBlockDownSample(self.dim, size=12)
self.rb4 = ResidualBlockDownSample(self.dim, size=6)
self.rb5 = ResidualBlock(self.dim, size=3)
self.gb = GeometricBlock(dim=self.dim, pool=True)
def forward(self, input):
output = input
output = self.conv1(output)
output = self.rb1(output)
output = self.rb2(output)
output = self.rb3(output)
output = self.rb4(output)
output = self.rb5(output)
output = self.gb(output)
return output