-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatmapModule.py
789 lines (719 loc) · 31.1 KB
/
HeatmapModule.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#
# Copyright (c) University of Luxembourg 2019-2020.
# Created by Hazem FAHMY, hazem.fahmy@uni.lu, SNT, 2019.
# Modified by Mojtaba Bagherzadeh, m.bagherzadeh@uottawa.ca, University of Ottawa, 2019.
#
from imports import time, os, math, torch, np, pd, Variable, Image, setupTransformer, normalize, join, exists, isfile, \
basename, isdir, dirname, entropy, pairwise_distances
import dataSupplier as DS
from matplotlib import pyplot
def computeDistanceSheets(layerX, caseFile):
print("Calculating HM distances ....")
calcAndSaveHeatmapDistances(layerX, caseFile["filesPath"], "HMDistance.xlsx", caseFile["metric"])
print("Calculating of HM distances is completed")
def saveHeatmaps(caseFile, prefix):
dnn = caseFile["DNN"]
datasetName = caseFile["datasetName"]
retrainData = caseFile["testDataNpy"]
csvPath = caseFile["testCSV"]
if prefix == "Train":
retrainData = caseFile["trainDataNpy"]
csvPath = caseFile["trainCSV"]
elif prefix == "Test":
retrainData = caseFile["testDataNpy"]
csvPath = caseFile["testCSV"]
elif prefix == "I":
retrainData = caseFile["improveDataNpy"]
csvPath = caseFile["improveCSV"]
start = time.time()
imgListX = []
if datasetName != 'FLD':
imgClasses = retrainData.dataset.classes
imageList = pd.read_csv(csvPath, names=["image", "result", "expected", "predicted"].append(imgClasses))
for index, row in imageList.iterrows():
if row["result"] == "Wrong":
imgListX.append(row["image"])
print("The test result file contains " + str(len(imageList)) + " records")
print("Saving heatmaps for " + str(len(imgListX)) + " images")
csvPath = imgListX
calcTestImagesHeatmap(caseFile, retrainData, csvPath, dnn, prefix + "_")
end = time.time()
print("Total time consumption of operation Saving Heatmaps is " + str((end - start) / 60.0) + " minutes.")
def generateActivations(inputImage, model, dataSetName: str, outputPath: str, visulize: bool):
with torch.no_grad():
image = Image.open(inputImage)
transformer = setupTransformer(dataSetName)
imageTensor = transformer(image).float()
imageTensor = imageTensor.unsqueeze_(0)
imageTensor.detach()
if torch.cuda.is_available():
torch.cuda.empty_cache()
imageTensor, model = imageTensor.cuda(), model.cuda()
### enable instrumenation of model
for i in range(0, len(model.features)):
model.features[i].register_forward_hook(forward_hook)
for i in range(0, len(model.classifier)):
model.classifier[i].register_forward_hook(forward_hook)
inputToModel = Variable(imageTensor)
model.forward(inputToModel)
k = 0
sizee = len(model.features) + len(model.classifier)
layersHM = [0] * sizee
for i in range(0, len(model.features)):
layersHM[k] = model.features[i].Y.detach()
layersHM[k].detach()
k = k + 1
for i in range(0, len(model.classifier)):
layersHM[k] = model.classifier[i].Y.detach()
layersHM[k].detach()
k = k + 1
if visulize:
visualizeHeatMap(inputImage, layersHM, outputPath)
del model
del imageTensor
return layersHM
def safeHM(HMFile, layer, inputImage, model, dataSetName, outputPath, visulize, area, npyPath, imgExt, FLD):
if exists(HMFile):
if not torch.cuda.is_available():
HM = torch.load(HMFile, map_location='cpu')
else:
HMtemp = torch.load(HMFile, map_location='cuda:0')
HM = HMtemp.cuda()
else:
#print("not found")
HMtot, _ = generateHeatMap(inputImage, model, dataSetName, outputPath, visulize, area, npyPath, imgExt, FLD)
HM = HMtot[layer]
# HM = HM.cpu()
# print(HM)
if not isdir(dirname(HMFile)):
os.makedirs(dirname(HMFile))
torch.save(HM, HMFile)
return HM
def IEEmapper(area, npyPath, filePath, FLD):
print(npyPath, filePath)
rightbrow = [2, 3]
leftbrow = [0, 1]
mouth = [23, 24, 25, 26]
righteye = [16, 17, 18, 19, 20, 21, 22]
lefteye = [9, 10, 11, 12, 13, 14, 15]
noseridge = [4, 5]
nose = [6, 7, 8]
KParray = [0, 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]
if area == "rightbrow":
KParray = rightbrow
elif area == "leftbrow":
KParray = leftbrow
elif area == "righteye":
KParray = righteye
elif area == "lefteye":
KParray = lefteye
elif area == "nose":
KParray = nose
elif area == "noseridge":
KParray = noseridge
elif area == "mouth":
KParray = mouth
# makeFolder(outputPath)
dataset = np.load(npyPath, allow_pickle=True)
dataset = dataset.item()
x_data = dataset["data"]
y_data = dataset["label"]
x_data = x_data.astype(np.float32)
x_data = x_data / 255.
x_data = x_data[:, np.newaxis]
# print(inputImage)
imgSource = basename(dirname(filePath))
trainImage = str(basename(filePath).split(".")[0])
# print(trainImage)
if imgSource == "TrainingSet":
fileName = int(trainImage)
if imgSource == "ImprovementSet":
fileName = int(trainImage.split("I")[1])
elif imgSource == "TestSet":
fileName = int(trainImage)
if int(FLD) == 1:
fileName = str(fileName - 23041)
if int(FLD) == 2:
fileName = str(fileName - 16013)
if int(FLD) == 3:
fileName = str(fileName + 1)
else:
fileName = int()
# print(trainImage)
print(trainImage)
inputs = x_data[int(fileName) - 1]
return KParray, inputs, dataset, y_data[int(fileName) - 1]
def generateHeatMap(inputImage, model, dataSetName, outputPath, visulize, area, npyPath, imgExt, FLD):
model = model.eval()
if dataSetName == "FLD":
# imageName = "I" + str(counter) + ".pt"
# savePath = join(outputPath, imageName)
# if not isfile(savePath):
KParray, inputs, dataset, labels_gt = IEEmapper(area, npyPath, inputImage, FLD)
transformer = setupTransformer(dataSetName)
inputs = transformer(inputs)
if torch.cuda.is_available():
inputs = Variable(inputs.cuda())
model = model.cuda()
else:
inputs = Variable(inputs)
model = ieeRegister(model)
predict = model(inputs.unsqueeze(0).float())
predict_cpu = predict.cpu()
predict_cpu = predict_cpu.detach().numpy()
predict_xy1 = DS.transfer_target(predict_cpu, n_points=DS.n_points)
labels_msk = np.ones(labels_gt.shape)
labels_msk[labels_gt <= 1e-5] = 0
predict_xy = np.multiply(predict_xy1, labels_msk)
avg, sum_diff = calculate_pixel_distance(labels_gt, predict_xy)
label = 0
worst_label = 0
worst_KP = 0
for KP in sum_diff[0]:
if KParray.count(label) > 0:
if KP > worst_KP:
worst_KP = KP
worst_label = label
label += 1
# print(worst_label)
# KPindex = int(row["worst_KP"][2::])
predict_cpu = ieeBackKP(predict_cpu, worst_label)
# predict_cpu = HeatmapModule.ieeBackParts(predict_cpu, area)
tAF = torch.from_numpy(predict_cpu[0]).type(torch.FloatTensor)
if torch.cuda.is_available():
tAF = Variable(tAF).cuda()
else:
tAF = Variable(tAF).cpu()
model.relprop(tAF)
layersHM = returnHeatmap(model, False, True)
# layerIndex = int(layer.replace("Layer", ""))
# torch.save(heatmaps[layerIndex], savePath)
else:
with torch.no_grad():
image = Image.open(inputImage)
transformer = setupTransformer(dataSetName)
imageTensor = transformer(image).float()
imageTensor = imageTensor.unsqueeze_(0)
imageTensor.detach()
if torch.cuda.is_available():
# torch.cuda.empty_cache()
imageTensor = imageTensor.cuda()
# model = model.cuda()
else:
imageTensor = imageTensor.cpu()
# model = model.cpu()
### enable instrumenation of model
for i in range(0, len(model.features)):
model.features[i].register_forward_hook(forward_hook)
for i in range(0, len(model.classifier)):
model.classifier[i].register_forward_hook(forward_hook)
inputToModel = Variable(imageTensor)
outputFromModel = model.forward(inputToModel)
outputFromModel = outputFromModel.detach().cpu().numpy()
# if torch.cuda.is_available():
# inputToModel = inputToModel.cuda()
best = outputFromModel.argmax()
# outputProb = outputFromModel[0][best]
#print(outputFromModel)
en = np.exp(outputFromModel) / np.sum(np.exp(outputFromModel))
en = en[0]
en /= sum(en)
entropy_ = 0
for cl in en:
if cl != 0:
entropy_ = entropy_+(-1*cl * math.log2(cl))
en = np.exp(outputFromModel) / np.sum(np.exp(outputFromModel))
en = en[0]
entropy_ = entropy(en, base=10) ##fixme base=num_classes
#print(outputFromModel)
#normalizedOutput = (outputFromModel - outputFromModel.min()) / (outputFromModel.max() - outputFromModel.min())
#print(normalizedOutput)
#logOutput = np.log(normalizedOutput)
#print(logOutput)
#entropy = -1 * np.sum(normalizedOutput*logOutput)
#print(entropy)
## the following code only calculate the postive relevance. why an image is classified to a certain class.
# this is why the relevance of output features except the highest one is set to zero.
for i in range(0, best):
outputFromModel[0][i] = 0
for i in range(best + 1, len(outputFromModel[0])):
outputFromModel[0][i] = 0
tAF = torch.from_numpy(outputFromModel[0]).type(torch.FloatTensor)
# tAF = Variable(tAF).cpu()
if torch.cuda.is_available():
tAF = Variable(tAF).cuda()
else:
tAF = Variable(tAF).cpu()
model.relprop(tAF)
layersHM = returnHeatmap(model, True, True)
if visulize:
visualizeHeatMap(inputImage, layersHM, outputPath)
del model
del imageTensor
del tAF
return layersHM, entropy_
def generateHeatMapOfLayer(inputImage, model, dataSetName: str, layerIndex: int):
with torch.no_grad():
image = Image.open(inputImage)
transformer = setupTransformer(dataSetName)
imageTensor = transformer(image).float()
imageTensor = imageTensor.unsqueeze_(0)
imageTensor.detach()
if torch.cuda.is_available():
torch.cuda.empty_cache()
imageTensor, model = imageTensor.cuda(), model.cuda()
### enable instrumenation of model
for i in range(0, len(model.features)):
model.features[i].register_forward_hook(forward_hook)
for i in range(0, len(model.classifier)):
model.classifier[i].register_forward_hook(forward_hook)
inputToModel = Variable(imageTensor)
outputFromModel = model.forward(inputToModel)
outputFromModel = outputFromModel.detach().cpu().numpy()
if torch.cuda.is_available():
inputToModel = inputToModel.cuda()
best = outputFromModel.argmax()
outputProb = outputFromModel[0][best]
## the following code only calculate the postive relevance. why an image is classified to a certain class.
# this is why the relevance of output features except the highest one is set to zero.
for i in range(0, best):
outputFromModel[0][i] = 0
for i in range(best + 1, len(outputFromModel[0])):
outputFromModel[0][i] = 0
tAF = torch.from_numpy(outputFromModel[0]).type(torch.FloatTensor)
# tAF = Variable(tAF).cpu()
if torch.cuda.is_available():
tAF = Variable(tAF).cuda()
else:
tAF = Variable(tAF).cpu()
model.relprop(tAF)
if layerIndex < len(model.features):
heatmap = model.features[layerIndex].HM.detach()
else:
heatmap = model.classifier[layerIndex - len(model.features)].HM.detach()
del model
del imageTensor
del tAF
return heatmap
def forward_hook(self, input, output):
# print("forward hook..")
self.X = input[0]
self.Y = output
def ieeRegister(model):
model.conv2d_1.register_forward_hook(forward_hook)
model.conv2d_2.register_forward_hook(forward_hook)
model.maxpool_1.register_forward_hook(forward_hook)
model.conv2d_3.register_forward_hook(forward_hook)
model.conv2d_4.register_forward_hook(forward_hook)
model.maxpool_2.register_forward_hook(forward_hook)
model.conv2d_5.register_forward_hook(forward_hook)
model.conv2d_6.register_forward_hook(forward_hook)
model.conv2d_trans_1.register_forward_hook(forward_hook)
model.conv2d_trans_2.register_forward_hook(forward_hook)
return model
def ieeBackKP(predict_cpu, KPindex):
for i in range(0, len(predict_cpu)):
for j in range(0, len(predict_cpu[i])):
if j != KPindex:
predict_cpu[i][j] = 0
return predict_cpu
def ieeBackParts(predict_cpu, area):
rightbrow = [2, 3]
leftbrow = [0, 1]
mouth = [23, 24, 25, 26]
righteye = [16, 17, 18, 19, 20, 21, 22]
lefteye = [9, 10, 11, 12, 13, 14, 15]
noseridge = [4, 5]
nose = [6, 7, 8]
for i in range(0, len(predict_cpu)):
for j in range(0, len(predict_cpu[i])):
if (area == "mouth"):
if (mouth.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "nose"):
if (nose.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "righteye"):
if (righteye.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "lefteye"):
if (lefteye.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "rightbrow"):
if (rightbrow.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "leftbrow"):
if (leftbrow.count(j) < 1):
predict_cpu[i][j] = 0
elif (area == "noseridge"):
if (noseridge.count(j) < 1):
predict_cpu[i][j] = 0
return predict_cpu
def returnActivations(model):
heatmaps = [0] * 10
heatmaps[0] = model.conv2d_1.Y.detach()
print(heatmaps[0])
heatmaps[1] = model.conv2d_2.Y.detach()
heatmaps[2] = model.maxpool_1.Y.detach()
heatmaps[3] = model.conv2d_3.Y.detach()
heatmaps[4] = model.conv2d_4.Y.detach()
heatmaps[5] = model.maxpool_2.Y.detach()
heatmaps[6] = model.conv2d_5.Y.detach()
heatmaps[7] = model.conv2d_6.Y.detach()
heatmaps[8] = model.conv2d_trans_1.Y.detach()
heatmaps[9] = model.conv2d_trans_2.Y.detach()
# heatmaps.size() = [3*128*96]
print(heatmaps[0].size)
heatmapVis = heatmaps[0][0].data.cpu().numpy()
# heatmapVis = heatmapVis.astype('uint8')
print(heatmapVis.shape)
# heatmapVis = np.absolute(heatmapVis)
# heatmapVis = 1000000 * heatmapVis
# img1 = Image.open("/users/hazem.fahmy/Documents/HPC/IEE/DataSets/TrainingSet/23039.png")
img2 = Image.fromarray(heatmapVis, "1")
# img3 = Image.new('L', (img1.width + img2.width, img2.height))
# img3.paste(img1, (0, 0))
# img3.paste(img2, (img1.width, 0))
img2.save("/users/hazem.fahmy/Documents/HPC/IEE/lefteye/_negetive_heatmap.JPEG")
# heatmapVis = 250 * (heatmapVis-heatmapVis.min()) / (heatmapVis.max()-heatmapVis.min())
return heatmaps
def returnHeatmap(model, Alex, HM):
if not Alex:
heatmaps = [0] * 10
if HM:
heatmaps[0] = model.conv2d_1.HM.detach()
heatmaps[1] = model.conv2d_2.HM.detach()
heatmaps[2] = model.maxpool_1.HM.detach()
heatmaps[3] = model.conv2d_3.HM.detach()
heatmaps[4] = model.conv2d_4.HM.detach()
heatmaps[5] = model.maxpool_2.HM.detach()
heatmaps[6] = model.conv2d_5.HM.detach()
heatmaps[7] = model.conv2d_6.HM.detach()
heatmaps[8] = model.conv2d_trans_1.HM.detach()
heatmaps[9] = model.conv2d_trans_2.HM.detach()
else:
heatmaps[0] = model.conv2d_1.Y.detach()
heatmaps[1] = model.conv2d_2.Y.detach()
heatmaps[2] = model.maxpool_1.Y.detach()
heatmaps[3] = model.conv2d_3.Y.detach()
heatmaps[4] = model.conv2d_4.Y.detach()
heatmaps[5] = model.maxpool_2.Y.detach()
heatmaps[6] = model.conv2d_5.Y.detach()
heatmaps[7] = model.conv2d_6.Y.detach()
heatmaps[8] = model.conv2d_trans_1.Y.detach()
heatmaps[9] = model.conv2d_trans_2.Y.detach()
else:
k = 0
sizee = len(model.features) + len(model.classifier)
heatmaps = [0] * sizee
for i in range(0, len(model.features)):
if HM:
heatmaps[k] = model.features[i].HM.detach()
else:
heatmaps[k] = model.features[i].Y.detach()
k += 1
for i in range(0, len(model.classifier)):
if HM:
heatmaps[k] = model.classifier[i].HM.detach()
else:
heatmaps[k] = model.classifier[i].Y.detach()
k += 1
return heatmaps
def calcTestImagesHeatmap(caseFile, npyPath, csvPath, model, imgSource):
outputPath = join(caseFile["filesPath"], "Heatmaps")
DataSetsPath = join(caseFile["outputPath"], "DataSets")
datasetName = caseFile["datasetName"]
area = caseFile["faceSubset"]
layers = caseFile["layers"]
FLD = caseFile["FLD"]
imgExt = caseFile["imgExt"]
if not exists(outputPath):
os.mkdir(outputPath)
if (datasetName == "FLD"):
imageList = pd.read_csv(csvPath)
dataset = np.load(npyPath, allow_pickle=True)
dataset = dataset.item()
x_data = dataset["data"]
x_data = x_data.astype(np.float32)
x_data = x_data / 255.
x_data = x_data[:, np.newaxis]
cnt1 = 0
cnt2 = 0
for index, row in imageList.iterrows():
cnt2 = 0
if row["result"] == "Wrong":
if row["worst_component"] == area:
cnt1 += 1
fileName = basename(str(row["image"]))
imageName = imgSource + fileName.split(".")[0] + ".pt"
saveFlag = False
for layerX in layers:
DirX = join(outputPath, layerX)
if not exists(DirX):
os.mkdir(DirX)
savePath = join(DirX, imageName)
if not isfile(savePath):
saveFlag = True
if saveFlag:
if imgSource == "Train_":
filePath = join(DataSetsPath, "TrainingSet", fileName)
else:
filePath = join(DataSetsPath, "TestSet", fileName)
HMtot, _ = generateHeatMap(filePath, model, datasetName, "", False, area, npyPath,
imgExt, FLD)
for layerX in layers:
savePath = join(outputPath, layerX, imageName)
if not isfile(savePath):
layerIndex = int(layerX.replace("Layer", ""))
torch.save(HMtot[layerIndex], savePath)
if cnt2 % 1000 == 0:
print("Generated {} Heatmaps".format(cnt1), end="\r")
else:
##this part need to be modified to save heatmaps by Layer
cnt1 = 0
imageList = csvPath
print("Total heatmaps to collect", len(imageList))
#print(imageList[0], model, datasetName, outputPath)
#print()
for imagePath in imageList:
imageFileName = basename(imagePath)
imageClass = imagePath.split(os.sep)[len(imagePath.split(os.sep)) - 2]
cnt1 = cnt1 + 1
# print("Checked {} images".format(cnt1), end="\r")
if imgSource == "Train_":
inputImage = join(DataSetsPath, "TrainingSet", imageClass, imageFileName)
elif imgSource == "Test_":
inputImage = join(DataSetsPath, "TestSet", imageClass, imageFileName)
else:
inputImage = join(DataSetsPath, "ImprovementSet", "ImprovementSet", imageClass, imageFileName)
imageName = imgSource + imageFileName.split(".")[0] + "_" + str(imageClass) + ".pt"
saveFlag = False
for layerX in layers:
DirX = join(outputPath, layerX)
if not exists(DirX):
os.mkdir(DirX)
savePath = join(DirX, imageName)
if not isfile(savePath):
saveFlag = True
if saveFlag:
HMtot, _ = generateHeatMap(inputImage, model, datasetName, "", False, area, npyPath, imgExt, FLD)
for layerX in layers:
layerIndex = int(layerX.replace("Layer", ""))
savePath = join(outputPath, layerX, imageName)
torch.save(HMtot[layerIndex], savePath)
# heatmaps = generateHeatMap(inputImage, model, datasetName, outputPath, False, imgExt)
# heatmaps = generateActivations(inputImage, model, datasetName, outputPath, True)
if cnt1 % 1000 == 0:
print("Generated {} Heatmaps".format(cnt1), end="\r")
def calcRetrainImagesHeatmap(imagesList, reTrainset, datasetName, model, layer, area, inputDir):
heatmaps = {}
x = 0
if (datasetName == "FLD"):
for idx, (inputsX, labels) in enumerate(reTrainset):
for inputs in inputsX:
fileName = imagesList[x]
inputs = Variable(inputs)
model = ieeRegister(model)
predict = model(inputs.unsqueeze(0))
predict_cpu = predict.cpu()
predict_cpu = predict_cpu.detach().numpy()
predict_cpu = ieeBackParts(predict_cpu, area)
tAF = torch.from_numpy(predict_cpu[0]).type(torch.FloatTensor)
tAF = Variable(tAF).cpu()
model.relprop(tAF)
heatmaps[fileName] = returnHeatmap(layer, model)
x = x + 1
else:
with torch.no_grad():
cnt1 = 0
for image in reTrainset.dataset.imgs:
imagePath = image[0]
imageFileName = basename(imagePath).split(".")[0]
imageClass = imagePath.split(os.sep)[len(imagePath.split(os.sep)) - 2]
img = imageClass + "_" + imageFileName
cnt1 = cnt1 + 1
print("Generating hetamp of " + imagePath)
if cnt1 % 10 == 0:
print("Image checked: " + str(cnt1) + "/" + str(len(reTrainset.dataset.imgs)))
layerIndex = int(layer.replace("Layer", ""))
heatmaps[img], _ = generateHeatMapOfLayer(imagePath, model, datasetName,
inputDir + "/" + imageClass + "/" + imageFileName,
layerIndex)
return heatmaps
def visualizeHeatMap(image, heatmap, outputImagePath, negetiveHeatMap=False):
heatmap = heatmap[0:12]
for i in range(0, len(heatmap)):
print(heatmap[i].shape)
heatmapVis = heatmap[i][0].data.cpu().numpy()
# heatmapVis = np.absolute(heatmapVis)
# heatmapVis = 1e9 * (np.absolute(heatmapVis))
# heatmapVis = 1e9 * heatmapVis
heatmapVis = 800 * (heatmapVis - heatmapVis.min()) / (heatmapVis.max() - heatmapVis.min())
heatmapVis = heatmapVis.astype('uint8')
if not exists(join(outputImagePath, "Visualize")):
os.makedirs(join(outputImagePath, "Visualize"))
img1 = Image.open(image)
img1.save(join(outputImagePath, "Visualize", basename(image)))
# img2 = Image.fromarray(heatmapVis, 'RGB')
square = int(math.sqrt(len(heatmapVis)))
# plot all 64 maps in an 8x8 squares
ix = 1
for _ in range(square):
for _ in range(square):
# specify subplot and turn of axis
ax = pyplot.subplot(square, square, ix)
ax.set_xticks([])
ax.set_yticks([])
# plot filter channel in grayscale
pyplot.imshow(heatmapVis[ix - 1, :, :], cmap='gray')
ix += 1
# show the figure
pyplot.savefig(
join(outputImagePath, "Visualize", basename(image) + "_heatmapFeatures" + str(i) + ".JPEG"))
# for j in range(0, len(heatmapVis)):
# img2 = cv2.resize(heatmapVis[j], (256, 256), interpolation=cv2.INTER_CUBIC)
# img4 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
# img3 = Image.new('RGB', (img1.width + img2.width, img2.height))
# img3.paste(img1, (0, 0))
# img3.paste(img2, (img1.width, 0))
# if not negetiveHeatMap:
# cv2.imwrite(join(outputImagePath, "Visualize", basename(image) + "_heatmap" + str(i) + "_" + str(j) + ".JPEG"), img4)
# img2.save(join(outputImagePath, "Visualize", basename(image) + "_heatmap" + str(i) + ".JPEG"))
# img1.save(join(outputImagePath, "Visualize", basename(image)))
# else:
# img3.save(outputImagePath + "_negetive_heatmap.JPEG")
def calcDisatenceRetrainSetWithTestSet(reTrainSetHeatmaps, testSetHeatMaps, metric):
with torch.no_grad():
disatenceRetrainSetWithTestSet = {}
cnt = 0
for retrainFile in reTrainSetHeatmaps:
cnt += cnt + 1
if cnt % 100 == 0:
print("distance is calcuated for " + str(cnt) + "/" + str(len(reTrainSetHeatmaps[retrainFile])))
# exit
if not (retrainFile in disatenceRetrainSetWithTestSet):
disatenceRetrainSetWithTestSet[retrainFile] = {}
for testFile in testSetHeatMaps:
disatenceRetrainSetWithTestSet[retrainFile][testFile] = doDistance(reTrainSetHeatmaps[retrainFile],
testSetHeatMaps[testFile], metric)
return disatenceRetrainSetWithTestSet
def collectHeatmaps(outPutPath, layerX):
allHM = {}
imgList = []
index2 = 0
HMDir = join(outPutPath, "Heatmaps", layerX)
for file in os.listdir(HMDir):
if file.endswith(".pt"):
imgList.append(file)
for file in imgList:
if torch.cuda.is_available():
heatMap = torch.load(join(HMDir, file))
heatMap.cuda()
else:
heatMap = torch.load(join(HMDir, file), map_location='cpu')
allHM[file.split(".")[0]] = heatMap
index2 = index2 + 1
if index2 % 1000 == 0:
print("Heatmap is collected for " + str(index2) + " images")
print("Collected " + str(layerX) + " heatmaps")
return allHM, imgList
def collectHeatmaps_Dir(HMDir):
allHM = {}
imgList = []
index2 = 0
for file in os.listdir(HMDir):
if file.endswith(".pt"):
imgList.append(file)
for file in imgList:
if torch.cuda.is_available():
heatMap = torch.load(join(HMDir, file))
heatMap.cuda()
else:
heatMap = torch.load(join(HMDir, file), map_location='cpu')
allHM[file.split(".")[0]] = heatMap
index2 = index2 + 1
if index2 % 1000 == 0:
print("Heatmap is collected for " + str(index2) + " images")
return allHM, imgList
def calcAndSaveHeatmapDistances(layerX, outPutPath: str, outputFile: str, metric):
start = time.time()
layerDistances = pd.DataFrame()
testHM, imgList = collectHeatmaps(outPutPath, layerX)
c1 = 0
for file in imgList:
diff = [-1.0] * len(imgList)
c2 = 0
HM1 = testHM[file.split(".")[0]]
for fileX in imgList:
if c2 == c1:
diff[c2] = 0.0
if c2 > c1:
HM2 = testHM[fileX.split(".")[0]]
diff[c2] = doDistance(HM1, HM2, metric)
c2 = c2 + 1
c1 = c1 + 1
for x in range(0, len(diff)):
if diff[x] == -1.0:
diff[x] = layerDistances[imgList[x].split(".")[0]][c1 - 1]
layerDistances[file.split(".")[0]] = diff
print("Collected distances of " + str(c1) + "/" + str(len(imgList)) + " images", end="\r")
end = time.time()
print("Time for one Layer " + str(layerX) + " time cost: " + str((end - start) / 60.0) + " minutes")
writer = pd.ExcelWriter(join(outPutPath, str(layerX) + outputFile), engine='xlsxwriter')
writer.book.use_zip64()
print("collected heatmapDistances of " + str(c1) + " images")
layerDistances.to_excel(writer)
writer.close()
# del allHM
def doDistance(tensor1, tensor2, metric):
if type(tensor1) is np.ndarray:
if metric == "Euc":
return math.sqrt(np.sum(np.power(np.subtract(tensor1, tensor2), 2)))
elif metric == "Man":
return None #FIXME
else:
if torch.cuda.is_available():
tensor1 = tensor1.cuda()
tensor2 = tensor2.cuda()
else:
tensor1 = tensor1.cpu()
tensor2 = tensor2.cpu()
if metric == "Euc":
return torch.sqrt(torch.sum(torch.pow(torch.sub(tensor1, tensor2), 2))).item()
elif metric == "Man":
return torch.sum(torch.abs(torch.sub(tensor1, tensor2))).item()
def unifyHM(outputPath, layerX):
newHM = {}
# if not exists(outputPath + "/" + str(layerX) + "HMDistance.xlsx"):
testHMPath = join(outputPath, "TestHeatmaps", str(layerX))
trainHMPath = join(outputPath, "TrainHeatmaps", str(layerX))
if not False:
if torch.cuda.is_available():
HM1 = torch.load(testHMPath)
HM2 = torch.load(trainHMPath)
else:
HM1 = torch.load(testHMPath, map_location={'cpu'})
HM2 = torch.load(trainHMPath, map_location={'cpu'})
for img in HM1:
newHM["Test_" + img] = HM1[img]
for img in HM2:
newHM["Train_" + img] = HM2[img]
return newHM
def saveLoadHM(inputPath, outputPath, maxFiles):
HMfile = {}
counter = 0
for file in os.listdir(inputPath):
# HMfile[file.split(".")[0]] = generateHeatMap(,None,False)
counter = counter + 1
if counter == maxFiles:
counter = 0
torch.save(HMfile, join(outputPath, file.split(".")[0] + ".pt"))
HMfile = {}
def calculate_pixel_distance(coord1, coord2):
diff = np.square(coord1 - coord2)
sum_diff = np.sqrt(diff[:, :, 0] + diff[:, :, 1])
avg = sum_diff.mean()
return avg, sum_diff
def makeFolder(inputPath):
if not exists(inputPath):
os.mkdir(inputPath)