-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_net.py
265 lines (200 loc) · 8.14 KB
/
slice_net.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
import torch
import torch.nn as nn
from torch.nn import functional as F
class SE_module(nn.Module):
def __init__(self, channel, r):
super(SE_module, self).__init__()
self.__avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.__fc = nn.Sequential(
nn.Conv2d(channel, channel//r, 1, bias=False),
nn.ReLU(True),
nn.Conv2d(channel//r, channel, 1, bias=False),
nn.Sigmoid(),
)
def forward(self, x):
y = self.__avg_pool(x)
y = self.__fc(y)
return x * y
class Channel_Attention(nn.Module):
def __init__(self, channel, r):
super(Channel_Attention, self).__init__()
self.__avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.__max_pool = nn.AdaptiveMaxPool2d((1, 1))
self.__fc = nn.Sequential(
nn.Conv2d(channel, channel//r, 1, bias=False),
nn.ReLU(True),
nn.Conv2d(channel//r, channel, 1, bias=False),
)
self.__sigmoid = nn.Sigmoid()
def forward(self, x):
y1 = self.__avg_pool(x)
y1 = self.__fc(y1)
y2 = self.__max_pool(x)
y2 = self.__fc(y2)
y = self.__sigmoid(y1+y2)
return x * y
class Spartial_Attention(nn.Module):
def __init__(self, kernel_size):
super(Spartial_Attention, self).__init__()
assert kernel_size % 2 == 1, "kernel_size = {}".format(kernel_size)
padding = (kernel_size - 1) // 2
self.__layer = nn.Sequential(
nn.Conv2d(2, 1, kernel_size=kernel_size, padding=padding),
nn.Sigmoid(),
)
def forward(self, x):
avg_mask = torch.mean(x, dim=1, keepdim=True)
max_mask, _ = torch.max(x, dim=1, keepdim=True)
mask = torch.cat([avg_mask, max_mask], dim=1)
mask = self.__layer(mask)
return x * mask
class NonLocalBlockND(nn.Module):
def __init__(self,
in_channels,
inter_channels=None,
dimension=2,
sub_sample=True,
bn_layer=True):
super(NonLocalBlockND, self).__init__()
assert dimension in [1, 2, 3]
self.dimension = dimension
self.sub_sample = sub_sample
self.in_channels = in_channels
self.inter_channels = inter_channels
if self.inter_channels is None:
self.inter_channels = in_channels // 2
if self.inter_channels == 0:
self.inter_channels = 1
if dimension == 3:
conv_nd = nn.Conv3d
max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))
bn = nn.BatchNorm3d
elif dimension == 2:
conv_nd = nn.Conv2d
max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))
bn = nn.BatchNorm2d
else:
conv_nd = nn.Conv1d
max_pool_layer = nn.MaxPool1d(kernel_size=(2))
bn = nn.BatchNorm1d
self.g = conv_nd(in_channels=self.in_channels,
out_channels=self.inter_channels,
kernel_size=1,
stride=1,
padding=0)
if bn_layer:
self.W = nn.Sequential(
conv_nd(in_channels=self.inter_channels,
out_channels=self.in_channels,
kernel_size=1,
stride=1,
padding=0), bn(self.in_channels))
nn.init.constant_(self.W[1].weight, 0)
nn.init.constant_(self.W[1].bias, 0)
else:
self.W = conv_nd(in_channels=self.inter_channels,
out_channels=self.in_channels,
kernel_size=1,
stride=1,
padding=0)
nn.init.constant_(self.W.weight, 0)
nn.init.constant_(self.W.bias, 0)
self.theta = conv_nd(in_channels=self.in_channels,
out_channels=self.inter_channels,
kernel_size=1,
stride=1,
padding=0)
self.phi = conv_nd(in_channels=self.in_channels,
out_channels=self.inter_channels,
kernel_size=1,
stride=1,
padding=0)
if sub_sample:
self.g = nn.Sequential(self.g, max_pool_layer)
self.phi = nn.Sequential(self.phi, max_pool_layer)
def forward(self, x):
batch_size = x.size(0)
g_x = self.g(x).view(batch_size, self.inter_channels, -1)#[bs, c, w*h]
g_x = g_x.permute(0, 2, 1)
theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
theta_x = theta_x.permute(0, 2, 1)
phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
f = torch.matmul(theta_x, phi_x)
# print(f.shape)
f_div_C = F.softmax(f, dim=-1)
y = torch.matmul(f_div_C, g_x)
y = y.permute(0, 2, 1).contiguous()
y = y.view(batch_size, self.inter_channels, *x.size()[2:])
W_y = self.W(y)
z = W_y + x
return z
class ClassHead(nn.Module):
def __init__(self, input_channel, num_classes=2, attention=None):
super(ClassHead, self).__init__()
self.input_channel = input_channel
self.num_classes = num_classes
self.attention = attention
self.classifier_conv = nn.Conv2d(
input_channel,
num_classes,
1,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True
)
def forward(self, x):
if self.attention is not None:
x = self.attention(x)
x = self.classifier_conv(x)
return x
class SliceNet(nn.Module):
def __init__(self, resnet, num_classes=2, attention=None):
super(SliceNet, self).__init__()
self.num_classes = num_classes
self.resnet = resnet
self.head = ClassHead(resnet.feature_size, num_classes, attention)
def forward(self, x):
x = self.resnet.conv1(x)
x = self.resnet.bn1(x)
x = self.resnet.relu(x)
x = self.resnet.maxpool(x)
x = self.resnet.layer1(x)
x = self.resnet.layer2(x)
x = self.resnet.layer3(x)
x = self.resnet.layer4(x)
x = self.head(x)
return x
class BitBalanceHardMiningLoss(nn.Module):
def __init__(self):
super(BitBalanceHardMiningLoss, self).__init__()
self.ce = nn.CrossEntropyLoss(reduction='none')
def forward(self, logits, targets):
n, _, _, _ = logits.shape
logits = logits.reshape(n, 2, -1)
targets = targets.reshape(n, -1).to(dtype=int)
ce_loss = self.ce(logits, targets)
# create grad mask
with torch.no_grad():
grad_masks = []
pos_loss = torch.zeros_like(ce_loss).copy_(ce_loss)
pos_loss[targets<1] = 0.
neg_loss = torch.zeros_like(ce_loss).copy_(ce_loss)
neg_loss[targets>1] = 0.
neg_masks = 1 - targets
for i in range(n):
pos_count = torch.nonzero(targets[i, :]).shape[0]
neg_count = torch.nonzero(neg_masks[i, :]).shape[0]
min_count = pos_count if pos_count < neg_count else neg_count
_, pos_indices = torch.topk(pos_loss[i, :], min_count)
_, neg_indices = torch.topk(neg_loss[i, :], min_count)
grad_mask = torch.zeros_like(ce_loss[i, :], dtype=int)
for pos_index in pos_indices:
grad_mask[pos_index] = 1
for neg_index in neg_indices:
grad_mask[neg_index] = 1
grad_masks.append(grad_mask)
grad_masks = torch.stack(grad_masks)
ce_loss = ce_loss[grad_masks]
return ce_loss.mean()