-
Notifications
You must be signed in to change notification settings - Fork 0
/
PASS.py
246 lines (219 loc) · 11.1 KB
/
PASS.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
import copy
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
from torch.nn import functional as F
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import StepLR
from torch.autograd import Variable
import os
import sys
import numpy as np
from myNetwork import network
from iCIFAR100 import iCIFAR100
from tqdm import trange,tqdm
class protoAugSSL:
def __init__(self, args, file_name, feature_extractor, task_size, device):
self.file_name = file_name
self.args = args
self.epochs = args.epochs
self.learning_rate = args.learning_rate
self.model = network(args.fg_nc*4, feature_extractor)
self.noise_model = copy.deepcopy(self.model)
self.radius = 0
self.prototype = None
self.class_label = None
self.numclass = args.fg_nc
self.task_size = task_size
self.device = device
self.old_model = None
self.train_transform = transforms.Compose([transforms.RandomCrop((32, 32), padding=4),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(brightness=0.24705882352941178),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761))])
self.test_transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761))])
self.train_dataset = iCIFAR100('./dataset', transform=self.train_transform, download=True)
self.test_dataset = iCIFAR100('./dataset', test_transform=self.test_transform, train=False, download=True)
self.train_loader = None
self.test_loader = None
def map_new_class_index(self, y, order):
return np.array(list(map(lambda x: order.index(x), y)))
def setup_data(self, shuffle, seed):
train_targets = self.train_dataset.targets
test_targets = self.test_dataset.targets
order = [i for i in range(len(np.unique(train_targets)))]
if shuffle:
np.random.seed(seed)
order = np.random.permutation(len(order)).tolist()
else:
order = range(len(order))
self.class_order = order
print(100*'#')
print(self.class_order)
self.train_dataset.targets = self.map_new_class_index(train_targets, self.class_order)
self.test_dataset.targets = self.map_new_class_index(test_targets, self.class_order)
def beforeTrain(self, current_task):
self.model.eval()
if current_task == 0:
classes = [0, self.numclass]
else:
classes = [self.numclass-self.task_size, self.numclass]
self.train_loader, self.test_loader = self._get_train_and_test_dataloader(classes)
if current_task > 0:
self.model.Incremental_learning(4*self.numclass)
self.model.train()
self.model.to(self.device)
self.noise_model.to(self.device)
def _get_train_and_test_dataloader(self, classes):
self.train_dataset.getTrainData(classes)
self.test_dataset.getTestData(classes)
train_loader = DataLoader(dataset=self.train_dataset,
shuffle=True,
batch_size=self.args.batch_size)
test_loader = DataLoader(dataset=self.test_dataset,
shuffle=True,
batch_size=self.args.batch_size)
return train_loader, test_loader
def _get_test_dataloader(self, classes):
self.test_dataset.getTestData_up2now(classes)
test_loader = DataLoader(dataset=self.test_dataset,
shuffle=True,
batch_size=self.args.batch_size)
return test_loader
def train(self, current_task, old_class=0,**kwargs):
opt = torch.optim.Adam(self.model.parameters(), lr=self.learning_rate, weight_decay=2e-4)
scheduler = StepLR(opt, step_size=45, gamma=0.1)
accuracy = 0
for epoch in trange(self.epochs):
scheduler.step()
for step, (indexs, images, target) in enumerate(self.train_loader):
for k in images:
images[k] = images[k].to(self.device)
images, edge, sal = images['img'], images['edge'], images['sal']
# images = images['img']
origin_img = images
target = target.to(self.device)
# self-supervised learning based label augmentation
images = torch.stack([torch.rot90(images, k, (2, 3)) for k in range(4)], 1)
images = images.view(-1, 3, 32, 32)
target = torch.stack([target * 4 + k for k in range(4)], 1).view(-1)
opt.zero_grad()
loss = self._compute_loss(images, target, old_class, edge=edge, sal=sal, oi=origin_img,args=kwargs['args'])
# loss = self._compute_loss(images, target, old_class, oi=origin_img, args=kwargs['args'])
opt.zero_grad()
loss.backward()
opt.step()
if epoch % self.args.print_freq == 0:
accuracy = self._test(self.test_loader)
print('epoch:%d, accuracy:%.5f' % (epoch, accuracy))
self.protoSave(self.model, self.train_loader, current_task)
def _test(self, testloader):
self.model.eval()
correct, total = 0.0, 0.0
for setp, (indexs, imgs, labels) in enumerate(testloader):
imgs, labels = imgs.to(self.device), labels.to(self.device)
with torch.no_grad():
outputs = self.model(imgs)
outputs = outputs[:, ::4] # only compute predictions on original class nodes
predicts = torch.max(outputs, dim=1)[1]
correct += (predicts.cpu() == labels.cpu()).sum()
total += len(labels)
accuracy = correct.item() / total
self.model.train()
return accuracy
def dilation_boundary_loss(self, origin_edgemap, intermediate_x):
dbs_loss = 0
for i in range(len(intermediate_x)):
kernel_size = i*2+3
dilation_kernel=torch.ones(1,1,kernel_size,kernel_size,device=origin_edgemap.device)
if origin_edgemap.ndim==3:
origin_edgemap = origin_edgemap.unsqueeze(1)
dilated_edge_maps = F.conv2d(origin_edgemap, dilation_kernel,padding=kernel_size//2)
dilated_edge_maps = (dilated_edge_maps>0.5).float()
dilated_edge_maps = F.interpolate(dilated_edge_maps,intermediate_x[i].size()[-2:])
numel = dilated_edge_maps.shape[-2] * dilated_edge_maps.shape[-1]
dbs_loss = dbs_loss + F.binary_cross_entropy_with_logits(intermediate_x[i],dilated_edge_maps) / numel
return dbs_loss
def _compute_loss(self, imgs, target, old_class=0, **kwargs):
lms_loss = dbs_loss = 0
output = self.model(imgs)
output, target = output.to(self.device), target.to(self.device)
loss_cls = nn.CrossEntropyLoss()(output/self.args.temp, target)
edge, sal, oi = kwargs['edge'][:, 0], kwargs['sal'][:, 0], kwargs['oi']
oi_sal, oi_edge, inx = self.noise_model.feature.forward_pixel(oi.detach())
numel = sal.shape[-2] * sal.shape[-1]
lms_loss = (torch.sqrt(F.mse_loss(oi_sal, sal)) + torch.sqrt(F.mse_loss(oi_edge, edge))) / numel
args=kwargs['args']
if not args.lm&1:lms_loss=0
dbs_loss = self.dilation_boundary_loss(oi_edge, inx)
if not args.lm&2:dbs_loss=0
if self.old_model is None:
return loss_cls + lms_loss + dbs_loss
else:
feature = self.model.feature(imgs)
feature_old = self.old_model.feature(imgs)
loss_kd = torch.dist(feature, feature_old, 2)
proto_aug = []
proto_aug_label = []
index = list(range(old_class))
for _ in range(self.args.batch_size):
np.random.shuffle(index)
temp = self.prototype[index[0]] + np.random.normal(0, 1, 512) * self.radius
proto_aug.append(temp)
proto_aug_label.append(4*self.class_label[index[0]])
proto_aug = torch.from_numpy(np.float32(np.asarray(proto_aug))).float().to(self.device)
proto_aug_label = torch.from_numpy(np.asarray(proto_aug_label)).to(self.device)
soft_feat_aug = self.model.fc(proto_aug)
loss_protoAug = nn.CrossEntropyLoss()(soft_feat_aug/self.args.temp, proto_aug_label)
return loss_cls + self.args.protoAug_weight*loss_protoAug + self.args.kd_weight*loss_kd \
+ lms_loss + dbs_loss
def afterTrain(self):
path = self.args.save_path + self.file_name + '/'
if not os.path.isdir(path):
os.makedirs(path)
self.numclass += self.task_size
filename = path + '%d_model.pkl' % (self.numclass - self.task_size)
torch.save(self.model, filename)
self.old_model = torch.load(filename)
self.old_model.to(self.device)
self.old_model.eval()
def protoSave(self, model, loader, current_task):
features = []
labels = []
model.eval()
with torch.no_grad():
for i, (indexs, images, target) in enumerate(loader):
if isinstance(images,dict):
images=images['img']
feature = model.feature(images.to(self.device))
if feature.shape[0] == self.args.batch_size:
labels.append(target.numpy())
features.append(feature.cpu().numpy())
labels_set = np.unique(labels)
labels = np.array(labels)
labels = np.reshape(labels, labels.shape[0] * labels.shape[1])
features = np.array(features)
features = np.reshape(features, (features.shape[0] * features.shape[1], features.shape[2]))
feature_dim = features.shape[1]
prototype = []
radius = []
class_label = []
for item in labels_set:
index = np.where(item == labels)[0]
class_label.append(item)
feature_classwise = features[index]
prototype.append(np.mean(feature_classwise, axis=0))
if current_task == 0:
cov = np.cov(feature_classwise.T)
radius.append(np.trace(cov) / feature_dim)
if current_task == 0:
self.radius = np.sqrt(np.mean(radius))
self.prototype = prototype
self.class_label = class_label
print(self.radius)
else:
self.prototype = np.concatenate((prototype, self.prototype), axis=0)
self.class_label = np.concatenate((class_label, self.class_label), axis=0)