-
Notifications
You must be signed in to change notification settings - Fork 3
/
retrain_MNIST.py
343 lines (329 loc) · 18.2 KB
/
retrain_MNIST.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
import torch
from torch import nn
from learner.models import dnns
from learner.learner import Learner
from data_store.datasets import decoy_mnist_both_retrain, decoy_mnist_CE_combined_retrain, decoy_mnist_retrain, decoy_mnist_CE_augmented_retrain
from xil_methods.xil_loss import RRRGradCamLoss, RRRLoss, CDEPLoss, HINTLoss, HINTLoss_IG, RBRLoss, MixLoss1, MixLoss2, MixLoss3, \
MixLoss4, MixLoss5, MixLoss6, MixLoss7, MixLoss8, MixLoss8_ext, MixLoss9, MixLoss11, MixLoss12, MixLoss13, MixLoss14, \
MixLoss15, MixLoss16, MixLoss17, MixLoss18
import util
import explainer
import argparse
import os
from rtpt import RTPT
rtpt = RTPT(name_initials='RW', experiment_name='retrain_MNIST', max_iterations=256)
parser = argparse.ArgumentParser(description='XIL EVAL')
parser.add_argument('-m', '--mode', default='RRR', type=str, choices=['Vanilla','RRR','RRR-G','HINT','CDEP','CE','RBR', 'HINT_IG',\
'Mix1', 'Mix2', 'Mix3', 'Mix4', 'Mix5', 'Mix6', 'Mix7',\
'Mix8', 'Mix8ext', 'Mix9', 'Mix11', 'Mix12', 'Mix13', 'Mix14',\
'Mix15', 'Mix16', 'Mix17', 'Mix18'],
help='Which XIL method to test?')
parser.add_argument('--rrr', default=10, type=int)
parser.add_argument('--rbr', default=100000, type=int)
parser.add_argument('--rrrg', default=1, type=int)
parser.add_argument('--hint', default=100, type=float)
parser.add_argument('--hint_ig', default=50000, type=float)
parser.add_argument('--cdep', default=1000000, type=int)
parser.add_argument('--dataset', default='Mnist', type=str, choices=['Mnist','FMnist'],
help='Which dataset to use?')
parser.add_argument('--run', default=0, type=int,
help='Which seed?')
parser.add_argument('--method', default='ig', type=str, choices=['grad','ig','saliency','input_x_gradient','deep_lift','lrp','guided_backprop'],
help='Which explainer to use?')
parser.add_argument('--elems', default=1, type=int,
help='How many images to retrain?')
args = parser.parse_args()
DEVICE = "cuda"
SEED = [1, 10, 100, 1000, 10000]
SHUFFLE = True
BATCH_SIZE = 256
LEARNING_RATE = 0.001
WEIGHT_DECAY = 0.0001
EPOCHS = 50
SAVE_BEST = True
VERBOSE_AFTER_N_EPOCHS = 2
i = args.run
util.seed_all(SEED[i])
args.reg = None
model = dnns.SimpleConvNet().to(DEVICE)
if args.mode == 'Mix1':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrr},{args.rbr},{args.rrrg}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix2':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrrg},{args.hint}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix3':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrr},{args.cdep}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix4':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrr},{args.rbr}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix5':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rbr},{args.cdep}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix6':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrrg},{args.cdep}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix7':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.cdep},{args.hint}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix8':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrr},{args.hint}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix8ext':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrr},{args.hint_ig}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix9':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rbr},{args.hint}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix11':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rbr},{args.hint_ig}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix12':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.cdep},{args.hint_ig}--seed={SEED[i]}--run={i}'
elif args.mode == 'Mix13':
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.rrrg},{args.hint_ig}--seed={SEED[i]}--run={i}'
else:
if args.mode == 'RRR' or args.mode == 'Mix14':
args.reg = args.rrr
elif args.mode == 'RBR' or args.mode == 'Mix15':
args.reg = args.rbr
elif args.mode == 'RRR-G' or args.mode == 'Mix16':
args.reg = args.rrrg
elif args.mode == 'CDEP' or args.mode == 'Mix17':
args.reg = args.cdep
elif args.mode == 'HINT' or args.mode == 'Mix18':
args.reg = args.hint
MODELNAME = f'Decoy{args.dataset}-CNN-{args.mode}--reg={args.reg}--seed={SEED[i]}--run={i}'
filename = './img_wr_metric/' + str(MODELNAME) + '--' + str(args.method) + '.txt'
file = open(filename, 'r')
lines = file.readlines()
elem = []
count = 0
for line in lines:
if count == 0:
count = count + 1
continue
if count > args.elems:
break
elem.append(int(line.split('\t')[1]))
count = count + 1
if args.dataset == 'Mnist':
train_dataloader, test_dataloader = decoy_mnist_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
if args.mode == 'Vanilla':
args.mode = 'CEL'
loss_fn = nn.CrossEntropyLoss()
elif args.mode == 'RRR':
# args.reg = 10
args.reg = args.rrr
loss_fn = RRRLoss(args.reg)
elif args.mode == 'RBR':
# args.reg = 100000
args.reg = args.rbr
loss_fn = RBRLoss(args.reg)
elif args.mode == 'RRR-G':
# args.reg = 1
args.reg = args.rrrg
loss_fn = RRRGradCamLoss(args.reg)
args.mode = 'RRRGradCAM'
elif args.mode == 'HINT':
train_dataloader, val_dataloader = decoy_mnist_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE, \
hint_expl=True)
# args.reg = 100
args.reg = args.hint
loss_fn = HINTLoss(args.reg, last_conv_specified=True, upsample=True, reduction='mean')
elif args.mode == 'HINT_IG':
train_dataloader, val_dataloader = decoy_mnist_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE, \
hint_expl=True)
# args.reg = 100
args.reg = args.hint_ig
loss_fn = HINTLoss_IG(args.reg, reduction='mean')
elif args.mode == 'CE':
train_dataloader, val_dataloader = decoy_mnist_CE_augmented_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = nn.CrossEntropyLoss()
elif args.mode == 'CDEP':
# args.reg = 1000000
args.reg = args.cdep
loss_fn = CDEPLoss(args.reg)
elif args.mode == 'Mix1':
# Loss function combination of RRR, RBR, and RRRG
loss_fn = MixLoss1(regrate_rrr=args.rrr, regrate_rbr=args.rbr, regrate_rrrg=args.rrrg)
elif args.mode == 'Mix2':
# Loss function combination of RRRG and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss2(regrate_rrrg=args.rrrg, regrate_hint=args.hint)
elif args.mode == 'Mix3':
# Loss function combination of RRR and CDEP
loss_fn = MixLoss3(regrate_rrr=args.rrr, regrate_cdep=args.cdep)
elif args.mode == 'Mix4':
# Loss function combination of RRR and RBR
loss_fn = MixLoss4(regrate_rrr=args.rrr, regrate_rbr=args.rbr)
elif args.mode == 'Mix5':
# Loss function combination of RBR and CDEP
loss_fn = MixLoss5(regrate_rbr=args.rbr, regrate_cdep=args.cdep)
elif args.mode == 'Mix6':
# Loss function combination of RRRG and CDEP
loss_fn = MixLoss6(regrate_rrrg=args.rrrg, regrate_cdep=args.cdep)
elif args.mode == 'Mix7':
# Loss function combination of CDEP and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss7(regrate_cdep=args.cdep, regrate_hint=args.hint)
elif args.mode == 'Mix8':
# Loss function combination of RRR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss8(regrate_rrr=args.rrr, regrate_hint=args.hint)
elif args.mode == 'Mix8ext':
# Loss function combination of RRR and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss8_ext(regrate_rrr=args.rrr, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix9':
# Loss function combination of RBR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss9(regrate_rbr=args.rbr, regrate_hint=args.hint)
elif args.mode == 'Mix11':
# Loss function combination of RBR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss11(regrate_rbr=args.rbr, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix12':
# Loss function combination of CDEP and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss12(regrate_cdep=args.cdep, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix13':
# Loss function combination of RRRG and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
loss_fn = MixLoss13(regrate_rrrg=args.rrrg, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix14':
# Loss function combination of RRR and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rrr
loss_fn = MixLoss14(args.reg)
elif args.mode == 'Mix15':
# Loss function combination of RBR and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rbr
loss_fn = MixLoss15(args.reg)
elif args.mode == 'Mix16':
# Loss function combination of RRRG and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rrrg
loss_fn = MixLoss16(args.reg)
elif args.mode == 'Mix17':
# Loss function combination of CDEP and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.cdep
loss_fn = MixLoss17(args.reg)
elif args.mode == 'Mix18':
# Loss function combination of HINT and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE, hint_expl=True)
args.reg = args.hint
loss_fn = MixLoss18(args.reg)
elif args.dataset == 'FMnist':
train_dataloader, test_dataloader = decoy_mnist_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
if args.mode == 'Vanilla':
args.reg = None
args.mode == 'CEL'
loss_fn = nn.CrossEntropyLoss()
elif args.mode == 'RRR':
args.reg = 10
loss_fn = RRRLoss(args.reg)
elif args.mode == 'RBR':
args.reg = 1000000
loss_fn = RBRLoss(args.reg)
elif args.mode == 'RRR-G':
args.reg = 10
loss_fn = RRRGradCamLoss(args.reg)
args.mode = 'RRRGradCAM'
elif args.mode == 'HINT':
args.reg = 0.00001
loss_fn = HINTLoss(args.reg, last_conv_specified=True, upsample=True)
elif args.mode == 'HINT_IG':
train_dataloader, val_dataloader = decoy_mnist_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE, \
hint_expl=True)
# args.reg = 100
args.reg = args.hint_ig
loss_fn = HINTLoss_IG(args.reg, reduction='mean')
elif args.mode == 'CE':
train_dataloader, val_dataloader = decoy_mnist_CE_augmented_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE,
batch_size=BATCH_SIZE)
args.reg = None
loss_fn = nn.CrossEntropyLoss()
elif args.mode == 'CDEP':
args.reg = 2000000
loss_fn = CDEPLoss(args.reg)
elif args.mode == 'Mix1':
# Loss function combination of RRR, RBR, and RRRG
args.reg = None
loss_fn = MixLoss1(regrate_rrr=args.rrr, regrate_rbr=args.rbr, regrate_rrrg=args.rrrg)
elif args.mode == 'Mix2':
# Loss function combination of RRRG + HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss2(regrate_rrrg=args.rrrg, regrate_hint=args.hint)
elif args.mode == 'Mix3':
# Loss function combination of RRR and CDEP
args.reg = None
loss_fn = MixLoss3(regrate_rrr=args.rrr, regrate_cdep=args.cdep)
elif args.mode == 'Mix4':
# Loss function combination of RRR and RBR
args.reg = None
loss_fn = MixLoss4(regrate_rrr=args.rrr, regrate_rbr=args.rbr)
elif args.mode == 'Mix5':
# Loss function combination of RBR and CDEP
args.reg = None
loss_fn = MixLoss5(regrate_rbr=args.rbr, regrate_cdep=args.cdep)
elif args.mode == 'Mix6':
# Loss function combination of RRRG and CDEP
args.reg = None
loss_fn = MixLoss6(regrate_rrrg=args.rrrg, regrate_cdep=args.cdep)
elif args.mode == 'Mix7':
# Loss function combination of CDEP and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss7(regrate_cdep=args.cdep, regrate_hint=args.hint)
elif args.mode == 'Mix8':
# Loss function combination of RRR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss8(regrate_rrr=args.rrr, regrate_hint=args.hint)
elif args.mode == 'Mix8ext':
# Loss function combination of RRR and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss8_ext(regrate_rrr=args.rrr, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix9':
# Loss function combination of RBR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss9(regrate_rbr=args.rbr, regrate_hint=args.hint)
elif args.mode == 'Mix11':
# Loss function combination of RBR and HINT
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss11(regrate_rbr=args.rbr, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix12':
# Loss function combination of CDEP and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss12(regrate_cdep=args.cdep, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix13':
# Loss function combination of RRRG and HINT_IG
train_dataloader, val_dataloader = decoy_mnist_both_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = None
loss_fn = MixLoss13(regrate_rrrg=args.rrrg, regrate_hint_ig=args.hint_ig)
elif args.mode == 'Mix14':
# Loss function combination of RRR and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rrr
loss_fn = MixLoss14(args.reg)
elif args.mode == 'Mix15':
# Loss function combination of RBR and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rbr
loss_fn = MixLoss15(args.reg)
elif args.mode == 'Mix16':
# Loss function combination of RRRG and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.rrrg
loss_fn = MixLoss16(args.reg)
elif args.mode == 'Mix17':
# Loss function combination of CDEP and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.cdep
loss_fn = MixLoss17(args.reg)
elif args.mode == 'Mix18':
# Loss function combination of HINT and CE
train_dataloader, val_dataloader = decoy_mnist_CE_combined_retrain(elem_num=elem, fmnist=True, train_shuffle=SHUFFLE, device=DEVICE, batch_size=BATCH_SIZE)
args.reg = args.hint
loss_fn = MixLoss18(args.reg)
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
learner = Learner(model, loss_fn, optimizer, DEVICE, MODELNAME, load=True)
learner.fit(train_dataloader, test_dataloader, EPOCHS, save_best=SAVE_BEST, verbose_after_n_epochs=VERBOSE_AFTER_N_EPOCHS)