-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISNetFlexLightning.py
444 lines (386 loc) · 17.9 KB
/
ISNetFlexLightning.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import torch
import torch.nn.functional as F
import torch.nn as nn
import ISNetLayersZe as ISNetLayers
import ISNetFunctionsZe as ISNetFunctions
import LRPDenseNetZe as LRPDenseNet
import globalsZe as globals
import pytorch_lightning as pl
import warnings
from collections import OrderedDict
import numpy as np
import copy
from torch.autograd import Variable
import torch.autograd as ag
import random
import warnings
import ISNetFlexTorch
class ISNetFlexLgt(pl.LightningModule):
def __init__(self,model=None,architecture='densenet121',
heat=True,P=0.7,d=0,
optim='SGD',nesterov=False,
LR=1e-3,momentum=0.99,WD=0,dropLr=None,
clip=1,classes=1,dropout=False,
HiddenLayerPenalization=False,dLoss=1,
cut=1,cut2=25,selective=False,selectiveEps=1e-7,Zb=True,e=0.01,
multiple=False,randomLogit=False,
pretrained=False,A=1,B=1,E=1,VGGRemoveLastMaxpool=False,
alternativeForeground='hybrid',
norm='absRoIMean',
val='ISNet',
explainLabels=False):
"""
PyTorch Lightning module based on LRP-Flex. For Stochastic, Original and Selective ISNets.
Args:
model: arbitrary PyTorch DNN to be converted, not restricted to resnet densenet or VGG.
If None, model is as pre-defined backbone, according to architecture parameter
architecture: pre-defined backbone architecture name. Either densenets, resnets or vggs
heat: if True, produces heatmaps and applies ISNets' training scheme (background
relevance minimization)
P: heatmap loss weight, between 0 and 1
d: background loss GWRP exponential decay
optim: optimizer, SGD or Adam
nesterov: if True, uses Nesterov momentum
LR: learning rate
momentum: training momentum
WD: weight decay
dropLr: if not None, list of tuples (epoch,lr) for scheduler
clip: gradient norm clipping value, set to None for no clip
dropout: if True, adds dropout to pre-defined backbone
classes: number of output neurons in pre-defined backbones
dLoss: LDS exponential decay (GWRP)
cut,cut2: C1 and C2 hyper-parameters
selective: if True, explains the softmax-based quantity Eta, instead of a logit
(Selective ISNet)
selectiveEps: epsilon used for stabilizing the relevance pass through softmax (see
Selective ISNet Section in paper)
e: LRP epsilon parameter
Zb: if True, uses LRP-Zb in the first DNN layer
multiple: if True, produces a single heatmap per sample (Faster ISNet). If False,
creates one heatmap per class per sample (original ISNet)
randomLogit: if True, stochastically selects a single logit to be explained (Stochastic
ISNet)
HiddenLayerPenalization: LRP Deep Supervision
pretrained: if True, pre-defined backbone is downloaded pre-trained (ImageNet)
A and B: w1 and w2 parameters, weights for the background and foreground loss
E: parameter of the background loss activation x/(x+E)
VGGRemoveLastMaxpool: if True, removes last maxPool from VGG pre-defined backbone
alternativeForeground='hybrid' adds the loss modification in the paper Faster ISNet
for Background Bias Mitigation on Deep Neural Networks, alternativeForeground='L2'
uses the standard (original) loss
norm: 'absRoIMean' uses standard ISNet loss, 'RoIMean' applies the background loss
normalization step before the absolute value operation
explainLabels: used for explanation only, not for ISNet training. If True,
heatmaps explain the classes indicated by the argument labels in the forward pass
val: loss to use in validation, heatmap, classification or ISNet
"""
super (ISNetFlexLgt,self).__init__()
self.save_hyperparameters()
self.automatic_optimization=False
self.model=ISNetFlexTorch.ISNetFlex(model=model,architecture=architecture,
e=e,classes=classes,dropout=dropout,
HiddenLayerPenalization=HiddenLayerPenalization,
selective=selective,selectiveEps=selectiveEps,
Zb=Zb,multiple=multiple,randomLogit=randomLogit,
pretrained=pretrained,VGGRemoveLastMaxpool=VGGRemoveLastMaxpool,
explainLabels=explainLabels)
self.heat=heat
self.lr=LR
self.optim=optim
self.momentum=momentum
self.WD=WD
self.nesterov=nesterov
self.clip=clip
self.dropLr=dropLr
self.penalizeAll=HiddenLayerPenalization
self.dLoss=dLoss
self.testLoss=False
self.alternativeForeground=alternativeForeground
self.norm=norm
self.val=val
if isinstance(P, dict):
self.P=P[0]
self.increaseP=P
else:
self.P=P
self.increaseP=None
self.d=d
self.cut=cut
self.cut2=cut2
self.tuneCut=False
self.A=A
self.B=B
self.E=E
def forward(self,x,labels=None):
return self.model(x,runLRPFlex=self.heat,labels=labels)
def configure_optimizers(self):
if (self.optim=='Adam'):
from deepspeed.ops.adam import FusedAdam
optimizer=FusedAdam(filter(
lambda p: p.requires_grad,
self.parameters()),
lr=self.lr)
else:
optimizer=torch.optim.SGD(filter(
lambda p: p.requires_grad,
self.parameters()),
lr=self.lr,
momentum=self.momentum,
weight_decay=self.WD,
nesterov=self.nesterov)
if(self.dropLr is not None):
scheduler=torch.optim.lr_scheduler.MultiStepLR(optimizer,milestones=self.dropLr,
verbose=True)
return [optimizer],[scheduler]
else:
return optimizer
def compound_loss(self,out,labels,masks=None,norm=None):
if norm is None:
norm=self.norm
#print('self.model.maximum:',self.model.maximum)
loss=ISNetFlexTorch.CompoundLoss(out,labels,
masks=masks,tuneCut=self.tuneCut,
d=self.d,dLoss=self.dLoss,
cutFlex=self.cut,cut2Flex=self.cut2,
A=self.A,B=self.B,E=self.E,
alternativeForeground=self.alternativeForeground,
norm=norm)
if not self.heat or masks is None:
return loss['classification']
if not self.tuneCut:
return loss['classification'],loss['LRPFlex']
else:
self.keys=list(loss['mapAbsFlex'].keys())
return loss['classification'],loss['LRPFlex'],loss['mapAbsFlex']
def training_step(self,train_batch,batch_idx):
opt=self.optimizers()
opt.zero_grad()
inputs,masks,labels=train_batch
if self.tuneCut:
if(self.current_epoch!=self.cutEpochs):
self.heat=False
out=self.forward(inputs)
loss=self.compound_loss(out,labels=labels)
if(self.current_epoch==self.cutEpochs):
self.heat=True
out=self.forward(inputs)
cLoss,hLoss,mapAbs=self.compound_loss(out,labels=labels,
masks=masks)
#take only values from last tuning epoch
self.updateCut(mapAbs)
#use only for tuning cut value, ignores heatmap loss
loss=cLoss
self.log('train_loss',loss.detach(),
on_epoch=True)
else:
#update dinamic P
if (self.increaseP is not None):
epochs=list(self.increaseP.keys())
epochs.sort()
for epoch in epochs:
if (self.current_epoch>=epoch):
self.P=self.increaseP[epoch]
#data format: channel first
if (self.heat):#ISNet
out=self.forward(inputs)
cLoss,hLoss=self.compound_loss(out,labels=labels,
masks=masks)
loss=(1-self.P)*cLoss+self.P*hLoss
self.log('train_loss', {'Classifier':cLoss.detach(),
'Heatmap':hLoss.detach(),
'Sum':loss.detach()},
on_epoch=True)
else:#Common DenseNet
out=self.forward(inputs)
loss=self.compound_loss(out,labels=labels)
self.log('train_loss',loss,
on_epoch=True)
if(torch.isnan(loss).any()):
raise ValueError('NaN Training Loss')
opt.zero_grad()
self.manual_backward(loss)
if self.clip is not None:
if self.clip!=0:
self.clip_gradients(opt, gradient_clip_val=self.clip,
gradient_clip_algorithm="norm")
opt.step()
def on_train_epoch_start(self, training_step_outputs=None):
#lr step
if self.dropLr:
sch = self.lr_schedulers()
sch.step()
def validation_step(self,val_batch,batch_idx,dataloader_idx=0):
torch.set_grad_enabled(True)
tmp=self.heat
try:
inputs,masks,labels=val_batch
except:
inputs,labels=val_batch
if self.tuneCut:
self.heat=False
if dataloader_idx!=0:
self.heat=False
masks=None
if (self.heat):#ISNet
out=self.forward(inputs)
norm=self.norm
cLoss,hLoss=self.compound_loss(out,labels=labels,masks=masks,norm=norm)
if self.val=='ISNet':
loss=(1-self.P)*cLoss+self.P*hLoss
elif self.val=='heatmap':
loss=hLoss
elif self.val=='classification':
loss=cLoss
else:
raise ValueError('Unrecognized validation loss choice')
self.log('val_loss', {'Classifier':cLoss.detach(),
'Heatmap':hLoss.detach()},
on_epoch=True,on_step=False)
else:#Common DenseNet
logits=self.forward(inputs)
loss=self.compound_loss(logits,labels=labels)
self.heat=tmp
if dataloader_idx==0:
self.log('val_loss_iid', loss.detach(), on_step=True, on_epoch=True)
if dataloader_idx==1:
self.log('val_loss_ood', loss.detach(), on_step=True, on_epoch=True)
if dataloader_idx==2:
self.log('val_loss_cnf', loss.detach(), on_step=True, on_epoch=True)
self.manual_backward(loss)
opt=self.optimizers()
opt.zero_grad()
def test_step(self,test_batch,batch_idx):
#data format: channel first
inputs,labels=test_batch
if self.testLoss:
if self.heat:
out=self.forward(inputs)
cLoss,hLoss=self.compound_loss(out,labels=labels,masks=masks)
return {'pred': logits.detach(), 'labels': labels,
'cLoss': cLoss.detach(), 'hLoss': hLoss.detach()}
else:
out=self.forward(inputs)
cLoss=self.compound_loss(out,labels=labels)
return {'pred': logits.detach(), 'labels': labels,
'cLoss': cLoss.detach(),
'hLoss': torch.zeros(cLoss.shape).type_as(cLoss)}
elif (self.heat):#ISNet
out=self.forward(inputs)
logits=out['output']
heatmaps=out['LRPFlex']['input']
return {'pred': logits.detach(), 'labels': labels,
'images': inputs.cpu().float().detach(),
'heatmaps': heatmaps.cpu().float().detach()}
else:#Common DenseNet
out=self.forward(inputs)
logits=out['output']
return {'pred': logits.detach(), 'labels': labels}
def test_step_end(self, batch_parts):
if(batch_parts['pred'].dim()>2):
logits=batch_parts['pred']
labels=batch_parts['labels']
if (not self.heat):
return {'pred': logits.view(logits.shape[0]*logits.shape[1],logits.shape[-1]),
'labels': labels.view(labels.shape[0]*labels.shape[1],labels.shape[-1])}
elif self.testLoss:
cLoss=batch_parts['cLoss']
hLoss=batch_parts['hLoss']
#print(cLoss.shape)
return {'pred': logits.view(logits.shape[0]*logits.shape[1],logits.shape[-1]),
'labels': labels.view(labels.shape[0]*labels.shape[1],labels.shape[-1]),
'cLoss': cLoss.view(cLoss.shape[0]*cLoss.shape[1],cLoss.shape[-1]),
'hLoss': hLoss.view(hLoss.shape[0]*hLoss.shape[1],hLoss.shape[-1])}
else:
images=batch_parts['images']
heatmaps=batch_parts['heatmaps']
return {'pred': logits.view(logits.shape[0]*logits.shape[1],logits.shape[2]),
'labels': labels.view(labels.shape[0]*labels.shape[1],labels.shape[-1]),
'images': images.view(images.shape[0]*images.shape[1],images.shape[2],
images.shape[3],images.shape[4]),
'heatmaps': heatmaps.view(heatmaps.shape[0]*heatmaps.shape[1],
heatmaps.shape[2],
heatmaps.shape[3],heatmaps.shape[4],
heatmaps.shape[5])}
else:
return batch_parts
def test_epoch_end(self, test_step_outputs):
pred=test_step_outputs[0]['pred']
labels=test_step_outputs[0]['labels']
if self.testLoss:
cLoss=test_step_outputs[0]['cLoss'].unsqueeze(0)
hLoss=test_step_outputs[0]['hLoss'].unsqueeze(0)
elif (self.heat):
images=test_step_outputs[0]['images']
heatmaps=test_step_outputs[0]['heatmaps']
for i,out in enumerate(test_step_outputs,0):
if (i!=0):
pred=torch.cat((pred,out['pred']),dim=0)
labels=torch.cat((labels,out['labels']),dim=0)
if self.testLoss:
cLoss=torch.cat((cLoss,out['cLoss'].unsqueeze(0)),dim=0)
hLoss=torch.cat((hLoss,out['hLoss'].unsqueeze(0)),dim=0)
elif (self.heat):
images=torch.cat((images,out['images']),dim=0)
heatmaps=torch.cat((heatmaps,out['heatmaps']),dim=0)
if self.testLoss:
self.TestResults=pred,labels,cLoss.mean().item(),hLoss.mean().item()
elif (self.heat):
self.TestResults=pred,labels,images,heatmaps
else:
self.TestResults=pred,labels
def returnBackbone(self):
return self.model.returnBackbone()
def initTuneCut(self,epochs):
#train for self.cutEpochs to find cut values, do not use heatmap loss
self.tuneCut=True
self.cutEpochs=epochs-1
def resetCut(self):
self.aggregateE={}
for name in self.keys:
self.aggregateE[name]=[0,0,0]
def updateWelford(self,existingAggregate,newValue):
#https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
(count, mean, M2) = existingAggregate
count += 1
delta = newValue - mean
mean += delta / count
delta2 = newValue - mean
M2 += delta * delta2
return (count, mean, M2)
def updateCut(self,maps):
if not hasattr(self, 'aggregateE'):
self.resetCut()
#print(maps)
for layer in self.keys:
mapAbs=maps[layer]
mapAbsZ=mapAbs[:,:int(mapAbs.shape[1]/2)]
mapAbsE=mapAbs[:,int(mapAbs.shape[1]/2):]
for i,_ in enumerate(mapAbsE,0):#batch iteration
valueE=torch.mean(mapAbsE[i].detach().float()).item()
self.aggregateE[layer]=self.updateWelford(self.aggregateE[layer],valueE)
def finalizeWelford(self,existingAggregate):
# Retrieve the mean, variance and sample variance from an aggregate
(count, mean, M2) = existingAggregate
if count < 2:
return float("nan")
else:
mean, sampleVariance = mean, M2 / (count - 1)
std=sampleVariance**(0.5)
return mean, std
def returnCut(self):
self.tuneCut=False
cut0={}
cut1={}
means={}
stds={}
for layer in self.keys:
means[layer],stds[layer],cut0[layer],cut1[layer]=[],[],[],[]
#order: Z, E, En
mean,std=self.finalizeWelford(self.aggregateE[layer])
means[layer].append(mean)
stds[layer].append(std)
c0=np.maximum(mean/5,mean-3*std)
c1=np.minimum(c0*25,mean+3*std)
cut0[layer].append(c0)
cut1[layer].append(c1)
return cut0,cut1,means,stds