-
Notifications
You must be signed in to change notification settings - Fork 43
/
BAM.py
333 lines (287 loc) · 14.6 KB
/
BAM.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
import torch
from torch import nn
from torch._C import device
import torch.nn.functional as F
from torch.nn import BatchNorm2d as BatchNorm
import numpy as np
import random
import time
import cv2
import model.resnet as models
import model.vgg as vgg_models
from model.ASPP import ASPP
from model.PPM import PPM
from model.PSPNet import OneModel as PSPNet
from util.util import get_train_val_set
def Weighted_GAP(supp_feat, mask):
supp_feat = supp_feat * mask
feat_h, feat_w = supp_feat.shape[-2:][0], supp_feat.shape[-2:][1]
area = F.avg_pool2d(mask, (supp_feat.size()[2], supp_feat.size()[3])) * feat_h * feat_w + 0.0005
supp_feat = F.avg_pool2d(input=supp_feat, kernel_size=supp_feat.shape[-2:]) * feat_h * feat_w / area
return supp_feat
def get_gram_matrix(fea):
b, c, h, w = fea.shape
fea = fea.reshape(b, c, h*w) # C*N
fea_T = fea.permute(0, 2, 1) # N*C
fea_norm = fea.norm(2, 2, True)
fea_T_norm = fea_T.norm(2, 1, True)
gram = torch.bmm(fea, fea_T)/(torch.bmm(fea_norm, fea_T_norm) + 1e-7) # C*C
return gram
class OneModel(nn.Module):
def __init__(self, args, cls_type=None):
super(OneModel, self).__init__()
self.cls_type = cls_type # 'Base' or 'Novel'
self.layers = args.layers
self.zoom_factor = args.zoom_factor
self.shot = args.shot
self.vgg = args.vgg
self.dataset = args.data_set
self.criterion = nn.CrossEntropyLoss(ignore_index=args.ignore_label)
self.print_freq = args.print_freq/2
self.pretrained = True
self.classes = 2
if self.dataset == 'pascal':
self.base_classes = 15
elif self.dataset == 'coco':
self.base_classes = 60
assert self.layers in [50, 101, 152]
PSPNet_ = PSPNet(args)
backbone_str = 'vgg' if args.vgg else 'resnet'+str(args.layers)
weight_path = 'initmodel/PSPNet/{}/split{}/{}/best.pth'.format(args.data_set, args.split, backbone_str)
new_param = torch.load(weight_path, map_location=torch.device('cpu'))['state_dict']
try:
PSPNet_.load_state_dict(new_param)
except RuntimeError: # 1GPU loads mGPU model
for key in list(new_param.keys()):
new_param[key[7:]] = new_param.pop(key)
PSPNet_.load_state_dict(new_param)
self.layer0, self.layer1, self.layer2, self.layer3, self.layer4 = PSPNet_.layer0, PSPNet_.layer1, PSPNet_.layer2, PSPNet_.layer3, PSPNet_.layer4
# Base Learner
self.learner_base = nn.Sequential(PSPNet_.ppm, PSPNet_.cls)
# Meta Learner
reduce_dim = 256
self.low_fea_id = args.low_fea[-1]
if self.vgg:
fea_dim = 512 + 256
else:
fea_dim = 1024 + 512
self.down_query = nn.Sequential(
nn.Conv2d(fea_dim, reduce_dim, kernel_size=1, padding=0, bias=False),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5))
self.down_supp = nn.Sequential(
nn.Conv2d(fea_dim, reduce_dim, kernel_size=1, padding=0, bias=False),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5))
mask_add_num = 1
self.init_merge = nn.Sequential(
nn.Conv2d(reduce_dim*2 + mask_add_num, reduce_dim, kernel_size=1, padding=0, bias=False),
nn.ReLU(inplace=True))
self.ASPP_meta = ASPP(reduce_dim)
self.res1_meta = nn.Sequential(
nn.Conv2d(reduce_dim*5, reduce_dim, kernel_size=1, padding=0, bias=False),
nn.ReLU(inplace=True))
self.res2_meta = nn.Sequential(
nn.Conv2d(reduce_dim, reduce_dim, kernel_size=3, padding=1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(reduce_dim, reduce_dim, kernel_size=3, padding=1, bias=False),
nn.ReLU(inplace=True))
self.cls_meta = nn.Sequential(
nn.Conv2d(reduce_dim, reduce_dim, kernel_size=3, padding=1, bias=False),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.1),
nn.Conv2d(reduce_dim, self.classes, kernel_size=1))
# Gram and Meta
self.gram_merge = nn.Conv2d(2, 1, kernel_size=1, bias=False)
self.gram_merge.weight = nn.Parameter(torch.tensor([[1.0],[0.0]]).reshape_as(self.gram_merge.weight))
# Learner Ensemble
self.cls_merge = nn.Conv2d(2, 1, kernel_size=1, bias=False)
self.cls_merge.weight = nn.Parameter(torch.tensor([[1.0],[0.0]]).reshape_as(self.cls_merge.weight))
# K-Shot Reweighting
if args.shot > 1:
self.kshot_trans_dim = args.kshot_trans_dim
if self.kshot_trans_dim == 0:
self.kshot_rw = nn.Conv2d(self.shot, self.shot, kernel_size=1, bias=False)
self.kshot_rw.weight = nn.Parameter(torch.ones_like(self.kshot_rw.weight) / args.shot)
else:
self.kshot_rw = nn.Sequential(
nn.Conv2d(self.shot, self.kshot_trans_dim, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(self.kshot_trans_dim, self.shot, kernel_size=1))
self.sigmoid = nn.Sigmoid()
def get_optim(self, model, args, LR):
if args.shot > 1:
optimizer = torch.optim.SGD(
[
{'params': model.down_query.parameters()},
{'params': model.down_supp.parameters()},
{'params': model.init_merge.parameters()},
{'params': model.ASPP_meta.parameters()},
{'params': model.res1_meta.parameters()},
{'params': model.res2_meta.parameters()},
{'params': model.cls_meta.parameters()},
{'params': model.gram_merge.parameters()},
{'params': model.cls_merge.parameters()},
{'params': model.kshot_rw.parameters()},
], lr=LR, momentum=args.momentum, weight_decay=args.weight_decay)
else:
optimizer = torch.optim.SGD(
[
{'params': model.down_query.parameters()},
{'params': model.down_supp.parameters()},
{'params': model.init_merge.parameters()},
{'params': model.ASPP_meta.parameters()},
{'params': model.res1_meta.parameters()},
{'params': model.res2_meta.parameters()},
{'params': model.cls_meta.parameters()},
{'params': model.gram_merge.parameters()},
{'params': model.cls_merge.parameters()},
], lr=LR, momentum=args.momentum, weight_decay=args.weight_decay)
return optimizer
def freeze_modules(self, model):
for param in model.layer0.parameters():
param.requires_grad = False
for param in model.layer1.parameters():
param.requires_grad = False
for param in model.layer2.parameters():
param.requires_grad = False
for param in model.layer3.parameters():
param.requires_grad = False
for param in model.layer4.parameters():
param.requires_grad = False
for param in model.learner_base.parameters():
param.requires_grad = False
# que_img, sup_img, sup_mask, que_mask(meta), que_mask(base), cat_idx(meta)
def forward(self, x, s_x, s_y, y_m, y_b, cat_idx=None):
x_size = x.size()
bs = x_size[0]
h = int((x_size[2] - 1) / 8 * self.zoom_factor + 1)
w = int((x_size[3] - 1) / 8 * self.zoom_factor + 1)
# Query Feature
with torch.no_grad():
query_feat_0 = self.layer0(x)
query_feat_1 = self.layer1(query_feat_0)
query_feat_2 = self.layer2(query_feat_1)
query_feat_3 = self.layer3(query_feat_2)
query_feat_4 = self.layer4(query_feat_3)
if self.vgg:
query_feat_2 = F.interpolate(query_feat_2, size=(query_feat_3.size(2),query_feat_3.size(3)), mode='bilinear', align_corners=True)
query_feat = torch.cat([query_feat_3, query_feat_2], 1)
query_feat = self.down_query(query_feat)
# Support Feature
supp_pro_list = []
final_supp_list = []
mask_list = []
supp_feat_list = []
for i in range(self.shot):
mask = (s_y[:,i,:,:] == 1).float().unsqueeze(1)
mask_list.append(mask)
with torch.no_grad():
supp_feat_0 = self.layer0(s_x[:,i,:,:,:])
supp_feat_1 = self.layer1(supp_feat_0)
supp_feat_2 = self.layer2(supp_feat_1)
supp_feat_3 = self.layer3(supp_feat_2)
mask = F.interpolate(mask, size=(supp_feat_3.size(2), supp_feat_3.size(3)), mode='bilinear', align_corners=True)
supp_feat_4 = self.layer4(supp_feat_3*mask)
final_supp_list.append(supp_feat_4)
if self.vgg:
supp_feat_2 = F.interpolate(supp_feat_2, size=(supp_feat_3.size(2),supp_feat_3.size(3)), mode='bilinear', align_corners=True)
supp_feat = torch.cat([supp_feat_3, supp_feat_2], 1)
supp_feat = self.down_supp(supp_feat)
supp_pro = Weighted_GAP(supp_feat, mask)
supp_pro_list.append(supp_pro)
supp_feat_list.append(eval('supp_feat_' + self.low_fea_id))
# K-Shot Reweighting
que_gram = get_gram_matrix(eval('query_feat_' + self.low_fea_id)) # [bs, C, C] in (0,1)
norm_max = torch.ones_like(que_gram).norm(dim=(1,2))
est_val_list = []
for supp_item in supp_feat_list:
supp_gram = get_gram_matrix(supp_item)
gram_diff = que_gram - supp_gram
est_val_list.append((gram_diff.norm(dim=(1,2))/norm_max).reshape(bs,1,1,1)) # norm2
est_val_total = torch.cat(est_val_list, 1) # [bs, shot, 1, 1]
if self.shot > 1:
val1, idx1 = est_val_total.sort(1)
val2, idx2 = idx1.sort(1)
weight = self.kshot_rw(val1)
weight = weight.gather(1, idx2)
weight_soft = torch.softmax(weight, 1)
else:
weight_soft = torch.ones_like(est_val_total)
est_val = (weight_soft * est_val_total).sum(1,True) # [bs, 1, 1, 1]
# Prior Similarity Mask
corr_query_mask_list = []
cosine_eps = 1e-7
for i, tmp_supp_feat in enumerate(final_supp_list):
resize_size = tmp_supp_feat.size(2)
tmp_mask = F.interpolate(mask_list[i], size=(resize_size, resize_size), mode='bilinear', align_corners=True)
tmp_supp_feat_4 = tmp_supp_feat * tmp_mask
q = query_feat_4
s = tmp_supp_feat_4
bsize, ch_sz, sp_sz, _ = q.size()[:]
tmp_query = q
tmp_query = tmp_query.reshape(bsize, ch_sz, -1)
tmp_query_norm = torch.norm(tmp_query, 2, 1, True)
tmp_supp = s
tmp_supp = tmp_supp.reshape(bsize, ch_sz, -1)
tmp_supp = tmp_supp.permute(0, 2, 1)
tmp_supp_norm = torch.norm(tmp_supp, 2, 2, True)
similarity = torch.bmm(tmp_supp, tmp_query)/(torch.bmm(tmp_supp_norm, tmp_query_norm) + cosine_eps)
similarity = similarity.max(1)[0].reshape(bsize, sp_sz*sp_sz)
similarity = (similarity - similarity.min(1)[0].unsqueeze(1))/(similarity.max(1)[0].unsqueeze(1) - similarity.min(1)[0].unsqueeze(1) + cosine_eps)
corr_query = similarity.reshape(bsize, 1, sp_sz, sp_sz)
corr_query = F.interpolate(corr_query, size=(query_feat_3.size()[2], query_feat_3.size()[3]), mode='bilinear', align_corners=True)
corr_query_mask_list.append(corr_query)
corr_query_mask = torch.cat(corr_query_mask_list, 1)
corr_query_mask = (weight_soft * corr_query_mask).sum(1,True)
# Support Prototype
supp_pro = torch.cat(supp_pro_list, 2) # [bs, 256, shot, 1]
supp_pro = (weight_soft.permute(0,2,1,3) * supp_pro).sum(2,True)
# Tile & Cat
concat_feat = supp_pro.expand_as(query_feat)
merge_feat = torch.cat([query_feat, concat_feat, corr_query_mask], 1) # 256+256+1
merge_feat = self.init_merge(merge_feat)
# Base and Meta
base_out = self.learner_base(query_feat_4)
query_meta = self.ASPP_meta(merge_feat)
query_meta = self.res1_meta(query_meta) # 1080->256
query_meta = self.res2_meta(query_meta) + query_meta
meta_out = self.cls_meta(query_meta)
meta_out_soft = meta_out.softmax(1)
base_out_soft = base_out.softmax(1)
# Classifier Ensemble
meta_map_bg = meta_out_soft[:,0:1,:,:] # [bs, 1, 60, 60]
meta_map_fg = meta_out_soft[:,1:,:,:] # [bs, 1, 60, 60]
if self.training and self.cls_type == 'Base':
c_id_array = torch.arange(self.base_classes+1, device='cuda')
base_map_list = []
for b_id in range(bs):
c_id = cat_idx[0][b_id] + 1
c_mask = (c_id_array!=0)&(c_id_array!=c_id)
base_map_list.append(base_out_soft[b_id,c_mask,:,:].unsqueeze(0).sum(1,True))
base_map = torch.cat(base_map_list,0)
# <alternative implementation>
# gather_id = (cat_idx[0]+1).reshape(bs,1,1,1).expand_as(base_out_soft[:,0:1,:,:]).cuda()
# fg_map = base_out_soft.gather(1,gather_id)
# base_map = base_out_soft[:,1:,:,:].sum(1,True) - fg_map
else:
base_map = base_out_soft[:,1:,:,:].sum(1,True)
est_map = est_val.expand_as(meta_map_fg)
meta_map_bg = self.gram_merge(torch.cat([meta_map_bg,est_map], dim=1))
meta_map_fg = self.gram_merge(torch.cat([meta_map_fg,est_map], dim=1))
merge_map = torch.cat([meta_map_bg, base_map], 1)
merge_bg = self.cls_merge(merge_map) # [bs, 1, 60, 60]
final_out = torch.cat([merge_bg, meta_map_fg], dim=1)
# Output Part
if self.zoom_factor != 1:
meta_out = F.interpolate(meta_out, size=(h, w), mode='bilinear', align_corners=True)
base_out = F.interpolate(base_out, size=(h, w), mode='bilinear', align_corners=True)
final_out = F.interpolate(final_out, size=(h, w), mode='bilinear', align_corners=True)
# Loss
if self.training:
main_loss = self.criterion(final_out, y_m.long())
aux_loss1 = self.criterion(meta_out, y_m.long())
aux_loss2 = self.criterion(base_out, y_b.long())
return final_out.max(1)[1], main_loss, aux_loss1, aux_loss2
else:
return final_out, meta_out, base_out