Skip to content

Commit

Permalink
Remove dead code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjhbw committed Jun 27, 2022
1 parent ecb4b83 commit 07968bc
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions HIPT_4K/hipt_4k.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys

# LinAlg / Stats / Plotting Dependencies
import h5py
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -16,16 +15,13 @@
# Torch Dependencies
import torch
import torch.multiprocessing
import torchvision
from torchvision import transforms
from einops import rearrange, repeat
torch.multiprocessing.set_sharing_strategy('file_system')

# Local Dependencies
import vision_transformer as vits
import vision_transformer4k as vits4k
from hipt_heatmap_utils import *
from hipt_model_utils import get_vit256, get_vit4k, tensorbatch2im, eval_transforms, roll_batch2img
from hipt_model_utils import get_vit256, get_vit4k, tensorbatch2im, eval_transforms


class HIPT_4K(torch.nn.Module):
Expand Down Expand Up @@ -133,7 +129,6 @@ def _get_region_attention_scores(self, region, scale=1):
- attention_256 (torch.Tensor): [256, 256/scale, 256/scale, 3] torch.Tensor sequence of attention maps for 256-sized patches.
- attention_4k (torch.Tensor): [1, 4096/scale, 4096/scale, 3] torch.Tensor sequence of attention maps for 4k-sized regions.
"""
eval_t = transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
x = eval_transforms()(region).unsqueeze(dim=0)

batch_256, w_256, h_256 = self.prepare_img_tensor(x)
Expand All @@ -150,7 +145,6 @@ def _get_region_attention_scores(self, region, scale=1):

features_grid256 = features_cls256.reshape(w_256, h_256, 384).transpose(0,1).transpose(0,2).unsqueeze(dim=0)
features_grid256 = features_grid256.to(self.device4k, non_blocking=True)
features_cls4k = self.model4k.forward(features_grid256).detach().cpu()

attention_4k = self.model4k.get_last_selfattention(features_grid256)
nh = attention_4k.shape[1] # number of head
Expand Down Expand Up @@ -185,13 +179,6 @@ def get_region_attention_heatmaps(self, x, offset=128, scale=4, alpha=0.5, cmap
region = Image.fromarray(tensorbatch2im(x)[0])
w, h = region.size

region2 = add_margin(region.crop((128,128,w,h)),
top=0, left=0, bottom=128, right=128, color=(255,255,255))
region3 = add_margin(region.crop((128*2,128*2,w,h)),
top=0, left=0, bottom=128*2, right=128*2, color=(255,255,255))
region4 = add_margin(region.crop((128*3,128*3,w,h)),
top=0, left=0, bottom=128*4, right=128*4, color=(255,255,255))

b256_1, a256_1, a4k_1 = self._get_region_attention_scores(region, scale)
b256_2, a256_2, a4k_2 = self._get_region_attention_scores(region, scale)
b256_3, a256_3, a4k_3 = self._get_region_attention_scores(region, scale)
Expand Down Expand Up @@ -224,14 +211,6 @@ def get_region_attention_heatmaps(self, x, offset=128, scale=4, alpha=0.5, cmap
img_inverse[mask256 == 0.95] = 0
Image.fromarray(region256_hm+img_inverse).save(os.path.join(output_dir, '%s_256th[%d].png' % (fname, i)))

if False:
for j in range(6):
score4k_1 = concat_scores4k(a4k_1[j], size=(h_s,w_s))
score4k = score4k_1 / 100
color_block4k = (cmap(score4k)*255)[:,:,:3].astype(np.uint8)
region4k_hm = cv2.addWeighted(color_block4k, alpha, save_region.copy(), 1-alpha, 0, save_region.copy())
Image.fromarray(region4k_hm).save(os.path.join(output_dir, '%s_4k[%s].png' % (fname, j)))

hm4k, hm256, hm4k_256 = [], [], []
for j in range(6):
score4k_1 = concat_scores4k(a4k_1[j], size=(h_s,w_s))
Expand Down

0 comments on commit 07968bc

Please sign in to comment.