5
5
import sys
6
6
7
7
# LinAlg / Stats / Plotting Dependencies
8
- import h5py
9
8
import matplotlib .pyplot as plt
10
9
import numpy as np
11
10
import pandas as pd
16
15
# Torch Dependencies
17
16
import torch
18
17
import torch .multiprocessing
19
- import torchvision
20
18
from torchvision import transforms
21
19
from einops import rearrange , repeat
22
20
torch .multiprocessing .set_sharing_strategy ('file_system' )
23
21
24
22
# Local Dependencies
25
- import vision_transformer as vits
26
- import vision_transformer4k as vits4k
27
23
from hipt_heatmap_utils import *
28
- from hipt_model_utils import get_vit256 , get_vit4k , tensorbatch2im , eval_transforms , roll_batch2img
24
+ from hipt_model_utils import get_vit256 , get_vit4k , tensorbatch2im , eval_transforms
29
25
30
26
31
27
class HIPT_4K (torch .nn .Module ):
@@ -133,7 +129,6 @@ def _get_region_attention_scores(self, region, scale=1):
133
129
- attention_256 (torch.Tensor): [256, 256/scale, 256/scale, 3] torch.Tensor sequence of attention maps for 256-sized patches.
134
130
- attention_4k (torch.Tensor): [1, 4096/scale, 4096/scale, 3] torch.Tensor sequence of attention maps for 4k-sized regions.
135
131
"""
136
- eval_t = transforms .Compose ([transforms .ToTensor (), transforms .Normalize ([0.5 , 0.5 , 0.5 ], [0.5 , 0.5 , 0.5 ])])
137
132
x = eval_transforms ()(region ).unsqueeze (dim = 0 )
138
133
139
134
batch_256 , w_256 , h_256 = self .prepare_img_tensor (x )
@@ -150,7 +145,6 @@ def _get_region_attention_scores(self, region, scale=1):
150
145
151
146
features_grid256 = features_cls256 .reshape (w_256 , h_256 , 384 ).transpose (0 ,1 ).transpose (0 ,2 ).unsqueeze (dim = 0 )
152
147
features_grid256 = features_grid256 .to (self .device4k , non_blocking = True )
153
- features_cls4k = self .model4k .forward (features_grid256 ).detach ().cpu ()
154
148
155
149
attention_4k = self .model4k .get_last_selfattention (features_grid256 )
156
150
nh = attention_4k .shape [1 ] # number of head
@@ -185,13 +179,6 @@ def get_region_attention_heatmaps(self, x, offset=128, scale=4, alpha=0.5, cmap
185
179
region = Image .fromarray (tensorbatch2im (x )[0 ])
186
180
w , h = region .size
187
181
188
- region2 = add_margin (region .crop ((128 ,128 ,w ,h )),
189
- top = 0 , left = 0 , bottom = 128 , right = 128 , color = (255 ,255 ,255 ))
190
- region3 = add_margin (region .crop ((128 * 2 ,128 * 2 ,w ,h )),
191
- top = 0 , left = 0 , bottom = 128 * 2 , right = 128 * 2 , color = (255 ,255 ,255 ))
192
- region4 = add_margin (region .crop ((128 * 3 ,128 * 3 ,w ,h )),
193
- top = 0 , left = 0 , bottom = 128 * 4 , right = 128 * 4 , color = (255 ,255 ,255 ))
194
-
195
182
b256_1 , a256_1 , a4k_1 = self ._get_region_attention_scores (region , scale )
196
183
b256_2 , a256_2 , a4k_2 = self ._get_region_attention_scores (region , scale )
197
184
b256_3 , a256_3 , a4k_3 = self ._get_region_attention_scores (region , scale )
@@ -224,14 +211,6 @@ def get_region_attention_heatmaps(self, x, offset=128, scale=4, alpha=0.5, cmap
224
211
img_inverse [mask256 == 0.95 ] = 0
225
212
Image .fromarray (region256_hm + img_inverse ).save (os .path .join (output_dir , '%s_256th[%d].png' % (fname , i )))
226
213
227
- if False :
228
- for j in range (6 ):
229
- score4k_1 = concat_scores4k (a4k_1 [j ], size = (h_s ,w_s ))
230
- score4k = score4k_1 / 100
231
- color_block4k = (cmap (score4k )* 255 )[:,:,:3 ].astype (np .uint8 )
232
- region4k_hm = cv2 .addWeighted (color_block4k , alpha , save_region .copy (), 1 - alpha , 0 , save_region .copy ())
233
- Image .fromarray (region4k_hm ).save (os .path .join (output_dir , '%s_4k[%s].png' % (fname , j )))
234
-
235
214
hm4k , hm256 , hm4k_256 = [], [], []
236
215
for j in range (6 ):
237
216
score4k_1 = concat_scores4k (a4k_1 [j ], size = (h_s ,w_s ))
@@ -296,7 +275,6 @@ def get_region_attention_heatmaps(self, x, offset=128, scale=4, alpha=0.5, cmap
296
275
overlay256 [offset_2 :h_s , offset_2 :w_s ] += 100
297
276
score256 = (score256_1 + new_score256_2 )/ overlay256
298
277
299
- factorize = lambda data : (data - np .min (data )) / (np .max (data ) - np .min (data ))
300
278
score = (score4k * overlay4k + score256 * overlay256 )/ (overlay4k + overlay256 ) #factorize(score256*score4k)
301
279
color_block = (cmap (score )* 255 )[:,:,:3 ].astype (np .uint8 )
302
280
region4k_256_hm = cv2 .addWeighted (color_block , alpha , save_region .copy (), 1 - alpha , 0 , save_region .copy ())
0 commit comments