-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
719 lines (634 loc) · 25.6 KB
/
main.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
import argparse
import datetime
import json
import numpy as np
import cv2
import os
import time
from pathlib import Path
import importlib
import torch
# import torchinfo
import torch.backends.cudnn as cudnn
from torch.utils.tensorboard import SummaryWriter
import timm
# assert timm.__version__ == "0.5.4" # version check
from timm.models.layers import trunc_normal_
import timm.optim.optim_factory as optim_factory
from timm.data.mixup import Mixup
from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy
import util.lr_decay_spikformer as lrd
import util.misc as misc
from util.datasets import build_dataset
from util.misc import NativeScalerWithGradNormCount as NativeScaler
from util.kd_loss import DistillationLoss
from PIL import Image
from torchvision import transforms
import models
# import models_sew
from urllib.request import urlretrieve
from models.engine_finetune import train_one_epoch, evaluate
from timm.data import create_loader
import torch
import torchinfo
import torch.nn as nn
from spikingjelly.clock_driven.neuron import (
MultiStepParametricLIFNode,
MultiStepLIFNode,
)
from spikingjelly.clock_driven import layer
from timm.models import (
create_model,
resume_checkpoint
)
import models
from models.modeling import VisionTransformer, CONFIGS
from models import v2_models
from timm.models.layers import to_2tuple, trunc_normal_, DropPath
from timm.models.registry import register_model
from timm.models.vision_transformer import _cfg
from einops.layers.torch import Rearrange
import torch.nn.functional as F
from functools import partial
from torch.nn import CrossEntropyLoss, Dropout, Softmax, Linear, Conv2d, LayerNorm
os.makedirs("attention_data", exist_ok=True)
if not os.path.isfile("attention_data/ilsvrc2012_wordnet_lemmas.txt"):
urlretrieve("https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt", "attention_data/ilsvrc2012_wordnet_lemmas.txt")
imagenet_labels = dict(enumerate(open('attention_data/ilsvrc2012_wordnet_lemmas.txt')))
def get_args_parser():
# important params
parser = argparse.ArgumentParser(
"MAE fine-tuning for image classification", add_help=False
)
parser.add_argument(
"--test_img_dir",
default="",
type=str,
help="Directory of Image-Under-Test",
)
parser.add_argument(
"--modelUT",
default="sdt-v2_8_512",
type=str,
help="sdt-v2_8_512, sdt-v1, vanilla_ViT_b_16",
)
parser.add_argument(
"--parameterUT",
default="qk",
type=str,
help="qk[SDT-v1,SDT-v2,Vanilla-ViT], qk_hp[SDT-v1,SDT-v2], attn_mp[SDT-v2]",
)
parser.add_argument(
"--batch_size",
default=1,
type=int,
help="Batch size per GPU (effective batch size is batch_size * accum_iter * # gpus",
)
parser.add_argument("--epochs", default=200, type=int) # 20/30(T=4)
parser.add_argument(
"--accum_iter",
default=1,
type=int,
help="Accumulate gradient iterations (for increasing the effective batch size under memory constraints)",
)
parser.add_argument("--finetune", default="", help="finetune from checkpoint")
parser.add_argument(
"--data_path", default="/raid/ligq/imagenet1-k/", type=str, help="dataset path"
)
# Model parameters
parser.add_argument(
"--model",
default="spikformer_8_384_CAFormer",
type=str,
metavar="MODEL",
help="Name of model to train",
)
parser.add_argument(
"--model_mode",
default="ms",
type=str,
help="Mode of model to train",
)
parser.add_argument("--input_size", default=224, type=int, help="images input size")
parser.add_argument(
"--drop_path",
type=float,
default=0.1,
metavar="PCT",
help="Drop path rate (default: 0.1)",
)
# Optimizer parameters
parser.add_argument(
"--clip_grad",
type=float,
default=None,
metavar="NORM",
help="Clip gradient norm (default: None, no clipping)",
)
parser.add_argument(
"--weight_decay", type=float, default=0.05, help="weight decay (default: 0.05)"
)
parser.add_argument(
"--lr",
type=float,
default=None,
metavar="LR",
help="learning rate (absolute lr)",
)
parser.add_argument(
"--blr",
type=float,
default=6e-4,
metavar="LR", # 1e-5,2e-5(T=4)
help="base learning rate: absolute_lr = base_lr * total_batch_size / 256",
)
parser.add_argument(
"--layer_decay",
type=float,
default=1.0,
help="layer-wise lr decay from ELECTRA/BEiT",
)
parser.add_argument(
"--min_lr",
type=float,
default=1e-6,
metavar="LR",
help="lower lr bound for cyclic schedulers that hit 0",
)
parser.add_argument(
"--warmup_epochs", type=int, default=10, metavar="N", help="epochs to warmup LR"
)
# Augmentation parameters
parser.add_argument(
"--color_jitter",
type=float,
default=None,
metavar="PCT",
help="Color jitter factor (enabled only when not using Auto/RandAug)",
)
parser.add_argument(
"--aa",
type=str,
default="rand-m9-mstd0.5-inc1",
metavar="NAME",
help='Use AutoAugment policy. "v0" or "original". " + "(default: rand-m9-mstd0.5-inc1)',
),
parser.add_argument(
"--smoothing", type=float, default=0.1, help="Label smoothing (default: 0.1)"
)
# * Random Erase params
parser.add_argument(
"--reprob",
type=float,
default=0.25,
metavar="PCT",
help="Random erase prob (default: 0.25)",
)
parser.add_argument(
"--remode",
type=str,
default="pixel",
help='Random erase mode (default: "pixel")',
)
parser.add_argument(
"--recount", type=int, default=1, help="Random erase count (default: 1)"
)
parser.add_argument(
"--resplit",
action="store_true",
default=False,
help="Do not random erase first (clean) augmentation split",
)
# * Mixup params
parser.add_argument(
"--mixup", type=float, default=0, help="mixup alpha, mixup enabled if > 0."
)
parser.add_argument(
"--cutmix", type=float, default=0, help="cutmix alpha, cutmix enabled if > 0."
)
parser.add_argument(
"--cutmix_minmax",
type=float,
nargs="+",
default=None,
help="cutmix min/max ratio, overrides alpha and enables cutmix if set (default: None)",
)
parser.add_argument(
"--mixup_prob",
type=float,
default=1.0,
help="Probability of performing mixup or cutmix when either/both is enabled",
)
parser.add_argument(
"--mixup_switch_prob",
type=float,
default=0.5,
help="Probability of switching to cutmix when both mixup and cutmix enabled",
)
parser.add_argument(
"--mixup_mode",
type=str,
default="batch",
help='How to apply mixup/cutmix params. Per "batch", "pair", or "elem"',
)
# * Finetuning params
parser.add_argument("--global_pool", action="store_true")
parser.set_defaults(global_pool=True)
parser.add_argument(
"--cls_token",
action="store_false",
dest="global_pool",
help="Use class token instead of global pool for classification",
)
parser.add_argument("--time_steps", default=1, type=int)
# Dataset parameters
parser.add_argument(
"--nb_classes",
default=1000,
type=int,
help="number of the classification types",
)
parser.add_argument(
"--output_dir",
default="/raid/ligq/htx/spikemae/output_dir",
help="path where to save, empty for no saving",
)
parser.add_argument(
"--log_dir",
default="/raid/ligq/htx/spikemae/output_dir",
help="path where to tensorboard log",
)
parser.add_argument(
"--device", default="cuda", help="device to use for training / testing"
)
parser.add_argument("--seed", default=0, type=int)
parser.add_argument("--resume", default=None, help="resume from checkpoint")
parser.add_argument(
"--start_epoch", default=0, type=int, metavar="N", help="start epoch"
)
parser.add_argument("--eval", action="store_true", help="Perform evaluation only")
parser.add_argument(
"--dist_eval",
action="store_true",
default=False,
help="Enabling distributed evaluation (recommended during training for faster monitor",
)
parser.add_argument("--num_workers", default=10, type=int)
parser.add_argument(
"--pin_mem",
action="store_true",
help="Pin CPU memory in DataLoader for more efficient (sometimes) transfer to GPU.",
)
parser.add_argument("--no_pin_mem", action="store_false", dest="pin_mem")
parser.set_defaults(pin_mem=True)
# Distillation parameters
parser.add_argument(
"--kd",
action="store_true",
default=False,
help="kd or not",
)
parser.add_argument(
"--teacher_model",
default="caformer_b36_in21ft1k",
type=str,
metavar="MODEL",
help='Name of teacher model to train (default: "caformer_b36_in21ft1k"',
)
parser.add_argument(
"--distillation_type",
default="none",
choices=["none", "soft", "hard"],
type=str,
help="",
)
parser.add_argument("--distillation_alpha", default=0.5, type=float, help="")
parser.add_argument("--distillation_tau", default=1.0, type=float, help="")
# distributed training parameters
parser.add_argument(
"--world_size", default=1, type=int, help="number of distributed processes"
)
parser.add_argument("--local-rank", default=-1, type=int)
parser.add_argument("--dist_on_itp", action="store_true")
parser.add_argument(
"--dist_url", default="env://", help="url used to set up distributed training"
)
return parser
def main(args):
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
model_under_test = args.modelUT
if model_under_test == "sdt-v2_8_512":
model = v2_models.__dict__[args.model](kd=args.kd)
model.T = args.time_steps
model.eval()
checkpoint = torch.load("./checkpoint/55M_kd_T4.pth", map_location=torch.device('cpu'))
state_dict = checkpoint["model"]
model.load_state_dict(state_dict, strict=False)
elif model_under_test == "sdt-v1":
model = create_model(
"sdt",
T=4,
pretrained=False,
drop_rate=0.0,
drop_path_rate=0.2,
num_heads=8,
num_classes=1000,
pooling_stat='1111',
img_size_h=224,
img_size_w=224,
embed_dims=384,
mlp_ratios=4,
in_channels=3,
qkv_bias=False,
depths=8,
sr_ratios=1,
spike_mode="lif",
dvs_mode=False,
TET=False,
)
model.eval()
resume_checkpoint(
model,
"./checkpoint/8_384.pth.tar",
optimizer=None,
loss_scaler=None,
log_info=True,
)
elif model_under_test == "vanilla_ViT_b_16":
config = CONFIGS["ViT-B_16"]
model = VisionTransformer(config, num_classes=1000, zero_head=False, img_size=224, vis=True)
model.load_from(np.load("attention_data/ViT-B_16-224.npz"))
model.eval()
# im = Image.open("/data/dataset/ImageNet/val/n07892512/ILSVRC2012_val_00033598.JPEG")
im = Image.open(args.test_img_dir)
x = transform(im)
if model_under_test == "sdt-v2_8_512":
y, qk, attn_lif_mp, qk_hadmard_product_sum = model(x)
elif model_under_test == "sdt-v1":
fr_dict, nz_dict = {"t0": dict(), "t1": dict(), "t2": dict(), "t3": dict()}, {
"t0": dict(),
"t1": dict(),
"t2": dict(),
"t3": dict(),
}
cls_output, firing_dict, qk, kv_mp, qk_hadmard_product_sum = model(x, hook=dict())
elif model_under_test == "vanilla_ViT_b_16":
logits, att_mat = model(x.unsqueeze(0))
parameter_under_test = args.parameterUT
if parameter_under_test == "qk":
if model_under_test != "vanilla_ViT_b_16":
qk_time_avg = []
for qk_item in qk:
qk_item = qk_item.mean(dim=0)
qk_item = qk_item.squeeze(0)
qk_item = qk_item.mean(dim=0)
qk_item = qk_item.squeeze(0)
qk_time_avg.append(qk_item)
print(len(qk_time_avg))
print(qk_time_avg[0].shape)
########attn_lif_mp_visualization##########
attention_maps = []
for i in range(0, len(qk_time_avg)):
tmp_attn_map = qk_time_avg[i].reshape(14, 14)
# print(tmp_attn_map.shape)
grid_size = tmp_attn_map.shape[0]
mask = tmp_attn_map.detach().numpy()
# print(mask.shape)
mask = cv2.resize(mask / mask.max(), im.size)
mask = (mask * 255).astype(np.uint8)
heatmap = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
output_filename = f'attention_mask_{i}.png'
attention_maps.append(heatmap)
cv2.imwrite(output_filename, heatmap)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
else:
att_mat = torch.stack(att_mat).squeeze(1) # torch.stack()进行扩维拼接
# print(att_mat.shape)
# Average the attention weights across all heads.
att_mat = torch.mean(att_mat, dim=1)
# print(att_mat.shape)
# To account for residual connections, we add an identity matrix to the
# attention matrix and re-normalize the weights.
residual_att = torch.eye(att_mat.size(1))
aug_att_mat = att_mat + residual_att
# print(aug_att_mat.shape)
aug_att_mat = aug_att_mat / aug_att_mat.sum(dim=-1).unsqueeze(-1)
# print(aug_att_mat.shape)
#-----------my code ---------#
#print each block's attention map
print(aug_att_mat.shape)
attention_maps = []
for i in range(0, aug_att_mat.shape[0]):
tmp_attn_map = aug_att_mat[i]
grid_size = int(np.sqrt(aug_att_mat.size(-1)))
mask = tmp_attn_map[0, 1:].reshape(grid_size, grid_size).detach().numpy()
# print(mask.shape)
mask = cv2.resize(mask / mask.max(), im.size)
mask = (mask * 255).astype(np.uint8)
heatmap = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
output_filename = f'attention_mask_{i}.png'
attention_maps.append(heatmap)
# cv2.imwrite(output_filename, heatmap)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps)
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
elif parameter_under_test == "qk_hp":
if model_under_test != "vanilla_ViT_b_16":
qk_hadmard_product_sum_time_avg = []
for qk_item in qk_hadmard_product_sum:
qk_item = qk_item.mean(dim=0)
qk_item = qk_item.squeeze(0)
qk_item = qk_item.mean(dim=0)
qk_item = qk_item.squeeze(0)
qk_hadmard_product_sum_time_avg.append(qk_item)
attention_maps = []
for i in range(0, len(qk_hadmard_product_sum_time_avg)):
tmp_attn_map = qk_hadmard_product_sum_time_avg[i].reshape(14, 14)
# print(tmp_attn_map.shape)
grid_size = tmp_attn_map.shape[0]
mask = tmp_attn_map.detach().numpy()
# print(mask.shape)
mask = cv2.resize(mask / mask.max(), im.size)
mask = (mask * 255).astype(np.uint8)
heatmap = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
output_filename = f'attention_mask_{i}.png'
attention_maps.append(heatmap)
cv2.imwrite(output_filename, heatmap)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
elif parameter_under_test == "attn_mp":
if model_under_test == "sdt-v2_8_512":
for i in range(len(attn_lif_mp)):
attn_lif_mp[i] = attn_lif_mp[i].squeeze(0)
attn_lif_mp[i] = attn_lif_mp[i].squeeze(0)
attn_lif_mp[i] = attn_lif_mp[i].T
print(attn_lif_mp[0].shape)
attention_maps = []
for i in range(0, len(attn_lif_mp)):
tmp_attn_map = attn_lif_mp[i].mean(dim=1).reshape(14, 14)
# print(tmp_attn_map.shape)
grid_size = tmp_attn_map.shape[0]
mask = tmp_attn_map.detach().numpy()
# print(mask.shape)
mask = cv2.resize(mask / mask.max(), im.size)
mask = np.clip(mask * 255 * 0.6, 0, 255).astype(np.uint8)
heatmap = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
output_filename = f'attention_mask_{i}.png'
attention_maps.append(heatmap)
cv2.imwrite(output_filename, heatmap)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
map_height, map_width, _ = attention_maps[0].shape
num_maps = len(attention_maps) + 1
canvas_height = map_height
canvas_width = map_width * num_maps
canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8)
for i, heatmap in enumerate(attention_maps):
canvas[:, i * map_width:(i + 1) * map_width, :] = heatmap
# canvas.append(im)
# 将原始图像转换为 numpy 数组
im = np.array(im)
if im.ndim == 2: # 如果是灰度图像,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
elif im.shape[2] == 4: # 如果有 alpha 通道,则转换为 BGR
im = cv2.cvtColor(im, cv2.COLOR_RGBA2BGR)
# 将原始图像放在最后
canvas[:, -map_width:, :] = cv2.resize(im, (map_width, map_height))
cv2.imwrite('attention_maps_canvas.png', canvas)
# cv2.imwrite('attention_maps_canvas.png', canvas)
# # # Recursively multiply the weight matrices
# # joint_attentions = torch.zeros(aug_att_mat.size())
# # joint_attentions[0] = aug_att_mat[0]
# # for n in range(1, aug_att_mat.size(0)):
# # joint_attentions[n] = torch.matmul(aug_att_mat[n], joint_attentions[n-1])
# # # Attention from the output token to the input space.
# # v = joint_attentions[-1]
# # # print(v.shape)
# # grid_size = int(np.sqrt(aug_att_mat.size(-1)))
# # # print(aug_att_mat.size(-1))
# # # print(np.sqrt(aug_att_mat.size(-1)))
# # # print(grid_size)
# # pre_mask = v[0, 1:].detach().numpy()
# # print(pre_mask)
# # mask = v[0, 1:].reshape(grid_size, grid_size).detach().numpy()
# # print(mask)
# # mask = cv2.resize(mask / mask.max(), im.size)[..., np.newaxis]
# # print(mask)
# # result = (mask * im).astype("uint8")
# # im = np.array(im)
# # black_image = np.zeros_like(im)
# # colored_mask = (mask * black_image + (1 - mask) * im).astype(np.uint8)
# # fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(16, 16))
# # ax1.set_title('Original')
# # ax2.set_title('Attention Map')
# # _ = ax1.imshow(im)
# # _ = ax2.imshow(result)
# # fig.savefig('output_image.png')
# # probs = torch.nn.Softmax(dim=-1)(logits)
# # top5 = torch.argsort(probs, dim=-1, descending=True)
# # print("Prediction Label and Attention Map!\n")
# # for idx in top5[0, :5]:
# # print(f'{probs[0, idx.item()]:.5f} : {imagenet_labels[idx.item()]}', end='')
if __name__ == "__main__":
args = get_args_parser()
args = args.parse_args()
if args.output_dir:
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)