-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathops_derain.py
More file actions
212 lines (170 loc) · 7.16 KB
/
ops_derain.py
File metadata and controls
212 lines (170 loc) · 7.16 KB
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
import torch
import torch.nn as nn
from copy import deepcopy as copy
Search_Space = {
'base': ['none', 'skip_connect', 'sep_conv_5x3', 'sep_conv_7x5', 'sep_conv_11x5', 'sep_conv_11x7', 'dil_conv_7x5', 'dil_conv_11x5', 'dil_conv_11x7', 'ChannelAttention', 'SpatialAttention']
}
OPS = {
'none': lambda C, stride, affine, expand: Zero(stride, expand),
'skip_connect': lambda C, stride, affine, expand: \
FactorizedExpand(C, C, stride, affine=affine) if expand==True and stride == 1 else Identity() if expand==False and stride == 1 else FactorizedReduce(C, C, stride, affine=affine),
'sep_conv_5x3': lambda C, stride, affine, expand: ConvLs(C, C, 5, 3, stride, 1, affine=affine, expand=expand),
'sep_conv_7x5': lambda C, stride, affine, expand: ConvLs(C, C, 7, 5, stride, 1, affine=affine, expand=expand),
'sep_conv_11x5': lambda C, stride, affine, expand: ConvLs(C, C, 11, 5, stride, 1, affine=affine, expand=expand),
'sep_conv_11x7': lambda C, stride, affine, expand: ConvLs(C, C, 11, 7, stride, 1, affine=affine, expand=expand),
'dil_conv_7x5': lambda C, stride, affine, expand: ConvLs(C, C, 7, 5, stride, 2, affine=affine, expand=expand),
'dil_conv_11x5': lambda C, stride, affine, expand: ConvLs(C, C, 11, 5, stride, 2, affine=affine, expand=expand),
'dil_conv_11x7': lambda C, stride, affine, expand: ConvLs(C, C, 11, 7, stride, 2, affine=affine, expand=expand),
'ChannelAttention': lambda C, stride, affine, expand: ChannelAttention(C, 16),
'SpatialAttention': lambda C, stride, affine, expand: SpatialAttention(7)
}
class ChannelAttention(nn.Module):
def __init__(self, C, ratio=16):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc1 = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False)
self.relu1 = nn.ReLU()
self.fc2 = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
out = avg_out + max_out
out = self.sigmoid(out)
out = out * x + x
return out
class SpatialAttention(nn.Module):
def __init__(self, kernel_size=7):
super(SpatialAttention, self).__init__()
assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
padding = 3 if kernel_size == 7 else 1
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = torch.mean(x, dim=1, keepdim=True)
max_out, _ = torch.max(x, dim=1, keepdim=True)
x = torch.cat([avg_out, max_out], dim=1)
x = self.conv1(x)
out = self.sigmoid(x)
out = out * x + x
return out
class ConvLs(nn.Module):
def __init__(self, in_dim, out_dim, kernel_size_1, kernel_size_2, stride, dilation, affine, expand):
super(ConvLs, self).__init__()
reflect_padding = int(dilation * (kernel_size_1 - 1) / 2)
self.reflection_pad_1 = nn.ReflectionPad2d(reflect_padding)
self.conv2d_1 = nn.Conv2d(in_dim, out_dim, kernel_size_1, stride, dilation=dilation)
self.bn_1 = nn.BatchNorm2d(out_dim, affine=affine)
padding_2 = int((kernel_size_2 - 1) / 2)
self.reflection_pad_2 = nn.ReflectionPad2d(padding_2)
self.conv2d_2 = nn.Conv2d(in_dim, out_dim, kernel_size_2, stride)
self.bn_2 = nn.BatchNorm2d(out_dim, affine=affine)
self.relu = nn.ReLU()
def forward(self, x):
x_r = x
out = self.reflection_pad_1(x)
out = self.conv2d_1(out)
out = self.bn_1(out)
out = out + x_r
out = self.relu(out)
out_2 = self.reflection_pad_2(out)
out_2 = self.conv2d_2(out_2)
out_2 = self.bn_2(out_2)
out_2 = out_2 + x_r
out_2 = self.relu(out_2)
return out_2
class Identity(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x
class Zero(nn.Module):
def __init__(self, stride, expand=False):
super().__init__()
self.expand = expand
if self.expand:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.stride = stride
def forward(self, x):
if self.stride == 1 and not self.expand:
return x * 0.
if self.expand and self.stride ==1:
x = self.up(x)
return x * 0.
# re-sizing by stride
return x[:, :, ::self.stride, ::self.stride] * 0.
class FactorizedExpand(nn.Module):
"""
Reduce feature map size by factorized pointwise(stride=2).
"""
def __init__(self, C_in, C_out, stride=1, affine=True):
super().__init__()
self.stride = stride
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(C_in, C_out // 2, 1, stride=stride, padding=0, bias=False)
self.conv2 = nn.Conv2d(C_in, C_out // 2, 1, stride=stride, padding=0, bias=False)
self.bn = nn.BatchNorm2d(C_out, affine=affine)
def forward(self, x):
x = self.up(x)
x = self.relu(x)
out = torch.cat([self.conv1(x), self.conv2(x)], dim=1)
out = self.bn(out)
return out
class FactorizedReduce(nn.Module):
"""
Reduce feature map size by factorized pointwise(stride=2).
"""
def __init__(self, C_in, C_out, stride=2, affine=True):
super().__init__()
self.stride = stride
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(C_in, C_out // 2, 1, stride=stride, padding=0, bias=False)
self.conv2 = nn.Conv2d(C_in, C_out // 2, 1, stride=stride, padding=0, bias=False)
self.bn = nn.BatchNorm2d(C_out, affine=affine)
def forward(self, x):
x = self.relu(x)
if self.stride == 2:
out = torch.cat([self.conv1(x), self.conv2(x[:, :, 1:, 1:])], dim=1)
else:
out = torch.cat([self.conv1(x), self.conv2(x)], dim=1)
out = self.bn(out)
return out
class StdConv(nn.Module):
""" Standard conv
ReLU - Conv - BN
"""
def __init__(self, C_in, C_out, kernel_size, stride, padding, affine=True):
super().__init__()
self.net = nn.Sequential(
nn.ReLU(),
nn.Conv2d(C_in, C_out, kernel_size, stride, padding, bias=False),
nn.BatchNorm2d(C_out, affine=affine)
)
def forward(self, x):
return self.net(x)
class MixedOp(nn.Module):
""" Mixed operation """
def __init__(self, C, stride, expand, space_name):
super().__init__()
self.PRIMITIVES = copy(Search_Space[space_name])
self._ops = nn.ModuleList()
for primitive in self.PRIMITIVES:
op = OPS[primitive](C, stride, affine=False, expand=expand)
self._ops.append(op)
def forward(self, x, weights):
"""
Args:
x: input
weights: weight for each operation
"""
return sum(w * op(x) for w, op in zip(weights, self._ops))
def drop_path(x, drop_prob):
if drop_prob > 0.:
keep_prob = 1. - drop_prob
mask = x.new_zeros(x.size(0), 1, 1, 1)
mask = mask.bernoulli_(keep_prob)
x.div_(keep_prob)
x.mul_(mask)
return x