forked from GMvandeVen/continual-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
227 lines (178 loc) · 10.6 KB
/
encoder.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
import torch
from torch.nn import functional as F
from linear_nets import MLP,fc_layer
from exemplars import ExemplarHandler
from continual_learner import ContinualLearner
from replayer import Replayer
import utils
class Classifier(ContinualLearner, Replayer, ExemplarHandler):
'''Model for classifying images, "enriched" as "ContinualLearner"-, Replayer- and ExemplarHandler-object.'''
def __init__(self, image_size, image_channels, classes,
fc_layers=3, fc_units=1000, fc_drop=0, fc_bn=True, fc_nl="relu", gated=False,
bias=True, excitability=False, excit_buffer=False, binaryCE=False, binaryCE_distill=False):
# configurations
super().__init__()
self.classes = classes
self.label = "Classifier"
self.fc_layers = fc_layers
# settings for training
self.binaryCE = binaryCE
self.binaryCE_distill = binaryCE_distill
# check whether there is at least 1 fc-layer
if fc_layers<1:
raise ValueError("The classifier needs to have at least 1 fully-connected layer.")
######------SPECIFY MODEL------######
# flatten image to 2D-tensor
self.flatten = utils.Flatten()
# fully connected hidden layers
self.fcE = MLP(input_size=image_channels*image_size**2, output_size=fc_units, layers=fc_layers-1,
hid_size=fc_units, drop=fc_drop, batch_norm=fc_bn, nl=fc_nl, bias=bias,
excitability=excitability, excit_buffer=excit_buffer, gated=gated)
mlp_output_size = fc_units if fc_layers>1 else image_channels*image_size**2
# classifier
self.classifier = fc_layer(mlp_output_size, classes, excit_buffer=True, nl='none', drop=fc_drop)
def list_init_layers(self):
'''Return list of modules whose parameters could be initialized differently (i.e., conv- or fc-layers).'''
list = []
list += self.fcE.list_init_layers()
list += self.classifier.list_init_layers()
return list
@property
def name(self):
return "{}_c{}".format(self.fcE.name, self.classes)
def forward(self, x):
final_features = self.fcE(self.flatten(x))
return self.classifier(final_features)
def feature_extractor(self, images):
return self.fcE(self.flatten(images))
def train_a_batch(self, x, y, scores=None, x_=None, y_=None, scores_=None, rnt=0.5, active_classes=None, task=1):
'''Train model for one batch ([x],[y]), possibly supplemented with replayed data ([x_],[y_/scores_]).
[x] <tensor> batch of inputs (could be None, in which case only 'replayed' data is used)
[y] <tensor> batch of corresponding labels
[scores] None or <tensor> 2Dtensor:[batch]x[classes] predicted "scores"/"logits" for [x]
NOTE: only to be used for "BCE with distill" (only when scenario=="class")
[x_] None or (<list> of) <tensor> batch of replayed inputs
[y_] None or (<list> of) <tensor> batch of corresponding "replayed" labels
[scores_] None or (<list> of) <tensor> 2Dtensor:[batch]x[classes] predicted "scores"/"logits" for [x_]
[rnt] <number> in [0,1], relative importance of new task
[active_classes] None or (<list> of) <list> with "active" classes
[task] <int>, for setting task-specific mask'''
# Set model to training-mode
self.train()
# Reset optimizer
self.optimizer.zero_grad()
##--(1)-- CURRENT DATA --##
if x is not None:
# If requested, apply correct task-specific mask
if self.mask_dict is not None:
self.apply_XdGmask(task=task)
# Run model
y_hat = self(x)
# -if needed, remove predictions for classes not in current task
if active_classes is not None:
class_entries = active_classes[-1] if type(active_classes[0])==list else active_classes
y_hat = y_hat[:, class_entries]
# Calculate prediction loss
if self.binaryCE:
# -binary prediction loss
binary_targets = utils.to_one_hot(y.cpu(), y_hat.size(1)).to(y.device)
if self.binaryCE_distill and (scores is not None):
classes_per_task = int(y_hat.size(1) / task)
binary_targets = binary_targets[:, -(classes_per_task):]
binary_targets = torch.cat([torch.sigmoid(scores / self.KD_temp), binary_targets], dim=1)
predL = None if y is None else F.binary_cross_entropy_with_logits(
input=y_hat, target=binary_targets, reduction='none'
).sum(dim=1).mean() #--> sum over classes, then average over batch
else:
# -multiclass prediction loss
predL = None if y is None else F.cross_entropy(input=y_hat, target=y, reduction='elementwise_mean')
# Weigh losses
loss_cur = predL
# Calculate training-precision
precision = None if y is None else (y == y_hat.max(1)[1]).sum().item() / x.size(0)
# If XdG is combined with replay, backward-pass needs to be done before new task-mask is applied
if (self.mask_dict is not None) and (x_ is not None):
weighted_current_loss = rnt*loss_cur
weighted_current_loss.backward()
else:
precision = predL = None
# -> it's possible there is only "replay" [i.e., for offline with incremental task learning]
##--(2)-- REPLAYED DATA --##
if x_ is not None:
# In the Task-IL scenario, [y_] or [scores_] is a list and [x_] needs to be evaluated on each of them
# (in case of 'exact' or 'exemplar' replay, [x_] is also a list!
TaskIL = (type(y_)==list) if (y_ is not None) else (type(scores_)==list)
if not TaskIL:
y_ = [y_]
scores_ = [scores_]
active_classes = [active_classes] if (active_classes is not None) else None
n_replays = len(y_) if (y_ is not None) else len(scores_)
# Prepare lists to store losses for each replay
loss_replay = [None]*n_replays
predL_r = [None]*n_replays
distilL_r = [None]*n_replays
# Run model (if [x_] is not a list with separate replay per task and there is no task-specific mask)
if (not type(x_)==list) and (self.mask_dict is None):
y_hat_all = self(x_)
# Loop to evalute predictions on replay according to each previous task
for replay_id in range(n_replays):
# -if [x_] is a list with separate replay per task, evaluate model on this task's replay
if (type(x_)==list) or (self.mask_dict is not None):
x_temp_ = x_[replay_id] if type(x_)==list else x_
if self.mask_dict is not None:
self.apply_XdGmask(task=replay_id+1)
y_hat_all = self(x_temp_)
# -if needed (e.g., Task-IL or Class-IL scenario), remove predictions for classes not in replayed task
y_hat = y_hat_all if (active_classes is None) else y_hat_all[:, active_classes[replay_id]]
# Calculate losses
if (y_ is not None) and (y_[replay_id] is not None):
if self.binaryCE:
binary_targets_ = utils.to_one_hot(y_[replay_id].cpu(), y_hat.size(1)).to(y_[replay_id].device)
predL_r[replay_id] = F.binary_cross_entropy_with_logits(
input=y_hat, target=binary_targets_, reduction='none'
).sum(dim=1).mean() #--> sum over classes, then average over batch
else:
predL_r[replay_id] = F.cross_entropy(y_hat, y_[replay_id], reduction='elementwise_mean')
if (scores_ is not None) and (scores_[replay_id] is not None):
# n_classes_to_consider = scores.size(1) #--> with this version, no zeroes are added to [scores]!
n_classes_to_consider = y_hat.size(1) #--> zeros will be added to [scores] to make it this size!
kd_fn = utils.loss_fn_kd_binary if self.binaryCE else utils.loss_fn_kd
distilL_r[replay_id] = kd_fn(scores=y_hat[:, :n_classes_to_consider],
target_scores=scores_[replay_id], T=self.KD_temp)
# Weigh losses
if self.replay_targets=="hard":
loss_replay[replay_id] = predL_r[replay_id]
elif self.replay_targets=="soft":
loss_replay[replay_id] = distilL_r[replay_id]
# If task-specific mask, backward pass needs to be performed before next task-mask is applied
if self.mask_dict is not None:
weighted_replay_loss_this_task = (1 - rnt) * loss_replay[replay_id] / n_replays
weighted_replay_loss_this_task.backward()
# Calculate total loss
loss_replay = None if (x_ is None) else sum(loss_replay)/n_replays
loss_total = loss_replay if (x is None) else (loss_cur if x_ is None else rnt*loss_cur+(1-rnt)*loss_replay)
##--(3)-- ALLOCATION LOSSES --##
# Add SI-loss (Zenke et al., 2017)
surrogate_loss = self.surrogate_loss()
if self.si_c>0:
loss_total += self.si_c * surrogate_loss
# Add EWC-loss
ewc_loss = self.ewc_loss()
if self.ewc_lambda>0:
loss_total += self.ewc_lambda * ewc_loss
# Backpropagate errors (if not yet done)
if (self.mask_dict is None) or (x_ is None):
loss_total.backward()
# Take optimization-step
self.optimizer.step()
# Return the dictionary with different training-loss split in categories
return {
'loss_total': loss_total.item(),
'loss_current': loss_cur.item() if x is not None else 0,
'loss_replay': loss_replay.item() if (loss_replay is not None) and (x is not None) else 0,
'pred': predL.item() if predL is not None else 0,
'pred_r': sum(predL_r).item()/n_replays if (x_ is not None and predL_r[0] is not None) else 0,
'distil_r': sum(distilL_r).item()/n_replays if (x_ is not None and distilL_r[0] is not None) else 0,
'ewc': ewc_loss.item(), 'si_loss': surrogate_loss.item(),
'precision': precision if precision is not None else 0.,
}