-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.py
executable file
·361 lines (276 loc) · 11.2 KB
/
module.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from pytorch_memlab import profile, MemReporter
from utils import *
class ResiBlock(nn.Module):
def __init__(self, in_features, filters, shortcut_conv=False):
super(ResiBlock, self).__init__()
self.filter1, self.filter2, self.filter3 = filters
self.in_features = in_features
self.shortcut_conv = shortcut_conv
self.conv_block1 = nn.Sequential(
nn.BatchNorm2d(self.in_features),
nn.LeakyReLU(),
nn.Conv2d(self.in_features, self.filter1, kernel_size=1, bias=False),
)
self.conv_block2 = nn.Sequential(
nn.BatchNorm2d(self.filter1),
nn.LeakyReLU(),
nn.Conv2d(self.filter1, self.filter2, kernel_size=3, padding=(1,1), bias=False),
)
self.conv_block3 = nn.Sequential(
nn.BatchNorm2d(self.filter2),
nn.LeakyReLU(),
nn.Conv2d(self.filter2, self.filter3, kernel_size=1, bias=False),
)
self.shortcut = nn.Conv2d(self.in_features, self.filter3, kernel_size=1, bias=False)
def forward(self, x):
residual = x
x = self.conv_block1(x)
x = self.conv_block2(x)
x = self.conv_block3(x)
if self.shortcut_conv:
shortcut = self.shortcut(residual)
x = torch.add(input=x, other=shortcut)
else:
x = torch.add(input=x, other=residual)
return x
class UpResiBlock(nn.Module):
def __init__(self, in_features, filters, stride=2):
super(UpResiBlock, self).__init__()
self.in_features = in_features
self.filter1, self.filter2, self.filter3 = filters
self.stride = stride
r1 = self.stride * self.stride * self.filter2
r2 = self.stride * self.stride * self.filter3
self.conv_block1 = nn.Sequential(
nn.BatchNorm2d(self.in_features),
nn.LeakyReLU(),
nn.Conv2d(self.in_features, self.filter1, kernel_size=1, bias=False)
)
self.conv_block2 = nn.Sequential(
nn.BatchNorm2d(self.filter1),
nn.LeakyReLU(),
SubPixelConv2D(self.filter1, r1, kernel_size=3, padding=(1,1), upscale=self.stride),
)
self.conv_block3 = nn.Sequential(
nn.BatchNorm2d(self.filter2),
nn.LeakyReLU(),
nn.Conv2d(self.filter2, self.filter3, kernel_size=1, bias=False),
)
self.shortcut = SubPixelConv2D(self.in_features, r2, kernel_size=1, padding=0, upscale=self.stride)
self.dropout = nn.Dropout(p=0.1)
def forward(self, x):
residual = x
x = self.conv_block1(x)
x = self.conv_block2(x)
x = self.conv_block3(x)
shortcut = self.shortcut(residual)
x = torch.add(input=x, other=shortcut)
x = self.dropout(x)
return x
class DownResiBlock(nn.Module):
def __init__(self, in_features, filters, stride=2):
super(DownResiBlock, self).__init__()
self.in_features = in_features
self.filter1, self.filter2, self.filter3 = filters
self.stride = stride
self.conv_block1 = nn.Sequential(
nn.BatchNorm2d(self.in_features),
nn.LeakyReLU(),
nn.Conv2d(self.in_features, self.filter1, kernel_size=1, bias=False),
)
self.conv_block2 = nn.Sequential(
nn.BatchNorm2d(self.filter1),
nn.LeakyReLU(),
nn.Conv2d(self.filter1, self.filter2, kernel_size=3, stride=self.stride, padding=(1,1), bias=False),
)
self.conv_block3 = nn.Sequential(
nn.BatchNorm2d(self.filter2),
nn.LeakyReLU(),
nn.Conv2d(self.filter2, self.filter3, kernel_size=1, bias=False),
)
self.shortcut = nn.Conv2d(self.in_features, self.filter3, kernel_size=1, stride=self.stride, bias=False)
def forward(self, x):
residual = x
x = self.conv_block1(x)
x = self.conv_block2(x)
x = self.conv_block3(x)
shortcut = self.shortcut(residual)
x = torch.add(input=x, other=shortcut)
return x
class SEblock(nn.Module):
def __init__(self, in_features, out_features, ratio=2):
super(SEblock, self).__init__()
self.ratio = ratio
self.in_features = in_features
self.out_features = out_features
self.fc1 = nn.Linear(self.out_features, self.out_features // ratio, bias=False)
self.fc2 = nn.Linear(self.out_features // ratio, self.out_features, bias=False)
self.conv1 = nn.Conv2d(self.in_features, 1, kernel_size=1, bias=False)
def forward(self, l, h):
mean = GlobalAvgPooling(l)
cse = F.relu(self.fc1(mean))
cse = torch.sigmoid(self.fc2(cse))
b, c = cse.shape
_, _, h, w = h.shape
cse_repeat = cse.repeat(1, h*w).view(b,c,h,w)
cse = torch.mul(cse_repeat, h)
sse = torch.sigmoid(self.conv1(l))
sse = sse.repeat(1, c, 1, 1)
sse = torch.mul(sse, h)
x = torch.add(input=cse, other=sse)
return x
class film(nn.Module):
def __init__(self, in_features, filters):
super(film, self).__init__()
self.in_features = in_features
self.filters = filters
self.fc1 = nn.Linear(self.in_features, self.filters)
self.fc2 = nn.Linear(self.in_features, self.filters)
def forward(self, cond, input_tensor):
gamma = self.fc1(cond)
beta = self.fc2(cond)
b, c = gamma.shape
_, _, h, w = input_tensor.shape
gamma_repeat = gamma.repeat(1, h*w).view(b,c,h,w)
beta_repeat = beta.repeat(1, h*w).view(b,c,h,w)
mul = torch.mul(gamma_repeat, input_tensor)
x = torch.add(input=mul, other=beta_repeat)
return x
class film_ResiBlock(nn.Module):
def __init__(self, in_features, filters):
super(film_ResiBlock, self).__init__()
self.in_features = in_features
self.filters = filters
self.conv1 = nn.Conv2d(in_features, filters, kernel_size=1, bias=False)
self.activation = nn.LeakyReLU()
self.conv2 = nn.Conv2d(filters, filters, kernel_size=3, padding=(1,1), bias=2)
self.bn = nn.BatchNorm2d(filters, affine=False)
self.film = film(128, filters)
def forward(self, cond, input_tensor):
a = self.conv1(input_tensor)
a = self.activation(a)
b = self.conv2(a)
b = self.bn(b)
b = self.film(cond, b)
b = self.activation(b)
return torch.add(input=a, other=b)
class SelfAttention(nn.Module):
"""
Reference: https://github.com/heykeetae/Self-Attention-GAN/blob/master/sagan_models.py
"""
""" Self attention Layer"""
def __init__(self,in_dim):
super(SelfAttention,self).__init__()
self.chanel_in = in_dim
self.query_conv = nn.Conv2d(in_channels = in_dim , out_channels = in_dim//8 , kernel_size= 1)
self.key_conv = nn.Conv2d(in_channels = in_dim , out_channels = in_dim//8 , kernel_size= 1)
self.value_conv = nn.Conv2d(in_channels = in_dim , out_channels = in_dim , kernel_size= 1)
self.gamma = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=1)
def forward(self,x):
"""
inputs :
x : input feature maps( B X C X W X H)
returns :
out : self attention value + input feature
attention: B X N X N (N is Width*Height)
"""
m_batchsize,C,width ,height = x.size()
proj_query = self.query_conv(x).view(m_batchsize,-1,width*height).permute(0,2,1) # B X (W*H)xC//8
proj_key = self.key_conv(x).view(m_batchsize,-1,width*height) # B X C//8 x (*W*H)
energy = torch.bmm(proj_query,proj_key) # transpose check
attention = self.softmax(energy) # BX (N) X (N)
proj_value = self.value_conv(x).view(m_batchsize,-1,width*height) # B X C X N
out = torch.bmm(proj_value,attention.permute(0,2,1)) # B,C,N
out = out.view(m_batchsize,C,width,height)
out = self.gamma * out + x
return out
class PixelWiseConcat(nn.Module):
"""
Input:
Lighting direction: (b, c1)
Image: (b, c2, h, w)
Expand lighting direction size to (b, c1, h, w), concat with image.
Output size: (b, c1+c2, h, w)
"""
def __init__(self):
super(PixelWiseConcat, self).__init__()
def forward(self, image, cond):
b, c1 = cond.shape
_, _, h, w = image.shape
repeat_cond = cond.repeat(1, h*w).view(b, c1, h, w)
new_tensor = torch.cat((repeat_cond, image), 1)
return new_tensor
class SubPixelConv2D(nn.Module):
"""
Sub Pixel Convolutional
"""
def __init__(self, in_channels, out_channels, kernel_size, padding, upscale):
super(SubPixelConv2D, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.upscale = upscale
self.conv2d = nn.Conv2d(in_channels=self.in_channels, out_channels=self.out_channels,
kernel_size=self.kernel_size, stride=1,
padding=self.padding, bias=False)
self.subpixel = nn.PixelShuffle(self.upscale)
def forward(self, x):
x = self.conv2d(x)
x = self.subpixel(x)
return x
def GlobalAvgPooling(x):
"""
Input: (batch, channels, height, weight)
Output: mean, (batch, channels)
"""
b, c, h, w = x.shape
x = x.view(b, c, h*w)
mean = torch.mean(x, dim=2)
return mean
"""
Coord Conv reference: https://github.com/mkocabas/CoordConv-pytorch/blob/master/CoordConv.py
"""
class AddCoords(nn.Module):
def __init__(self, with_r=False):
super().__init__()
self.with_r = with_r
def forward(self, input_tensor):
"""
Args:
input_tensor: shape(batch, channel, x_dim, y_dim)
"""
batch_size, _, x_dim, y_dim = input_tensor.size()
xx_channel = torch.arange(x_dim).repeat(1, y_dim, 1)
yy_channel = torch.arange(y_dim).repeat(1, x_dim, 1).transpose(1, 2)
xx_channel = xx_channel.float() / (x_dim - 1)
yy_channel = yy_channel.float() / (y_dim - 1)
xx_channel = xx_channel * 2 - 1
yy_channel = yy_channel * 2 - 1
xx_channel = xx_channel.repeat(batch_size, 1, 1, 1).transpose(2, 3)
yy_channel = yy_channel.repeat(batch_size, 1, 1, 1).transpose(2, 3)
ret = torch.cat([
input_tensor,
xx_channel.type_as(input_tensor),
yy_channel.type_as(input_tensor)], dim=1)
if self.with_r:
rr = torch.sqrt(torch.pow(xx_channel.type_as(input_tensor) - 0.5, 2) + torch.pow(yy_channel.type_as(input_tensor) - 0.5, 2))
ret = torch.cat([ret, rr], dim=1)
return ret
class CoordConv(nn.Module):
def __init__(self, in_channels, out_channels, with_r=False):
super().__init__()
self.addcoords = AddCoords(with_r=with_r)
in_size = in_channels+2
if with_r:
in_size += 1
self.conv = nn.Conv2d(in_size, out_channels, kernel_size=1)
def forward(self, x):
ret = self.addcoords(x)
ret = self.conv(ret)
return ret