-
Notifications
You must be signed in to change notification settings - Fork 1
/
GradCam_Visualization_Regression.py
180 lines (141 loc) · 7.16 KB
/
GradCam_Visualization_Regression.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
# Base Library
import os
import random
import cv2
import numpy as np
import argparse
from PIL import Image
# Pytorch Related Library
import torch
from torchvision import transforms
from transformers import SwinForImageClassification
from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, \
EigenGradCAM, FullGrad
from pytorch_grad_cam.utils.image import show_cam_on_image
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_checkpoint', type=str,
default='./savedmodel/Swin_regression/Swin_regression6loss0.13acc0.19.pth')
parser.add_argument('--image_path', type=str,
default='./data/21015_fundus_left_1', help='Input image path')
parser.add_argument('--image', type=str, default='1000014_21015_0_0.png')
parser.add_argument('--method', type=str, default='gradcam')
parser.add_argument('--aug_smooth', dest='aug_smooth', action='store_true',
help='Apply test time augmentation to smooth the CAM')
parser.add_argument('--eigen_smooth', dest='eigen_smooth', action='store_true',
help='Apply test time augmentation to smooth the CAM')
parser.add_argument('--no_aug_smooth', dest='aug_smooth', action='store_false',
help='Apply test time augmentation to smooth the CAM')
parser.add_argument('--no_eigen_smooth', dest='eigen_smooth', action='store_false',
help='Apply test time augmentation to smooth the CAM')
parser.add_argument('--predictor_idx', type=int, default=0, help='which risk factor prediction you want to extract')
parser.add_argument('--class_idx', type=int, default=0, help='which class of specific risk factor you want to extract')
parser.set_defaults(aug_smooth=True, eigen_smooth=True)
args = parser.parse_args()
return args
class SwinforRegression(torch.nn.Module):
def __init__(self, model_name_or_path='microsoft/swin-large-patch4-window12-384-in22k', n_label=7):
super().__init__()
model = SwinForImageClassification.from_pretrained(model_name_or_path, num_labels=n_label,
ignore_mismatched_sizes=True)
self.swin = model.swin
self.regressor_age = torch.nn.Linear(1536, 1)
self.regressor_education = torch.nn.Linear(1536, 1)
self.regressor_sleep = torch.nn.Linear(1536, 1)
self.regressor_BMI = torch.nn.Linear(1536, 1)
self.regressor_dbp = torch.nn.Linear(1536, 1)
self.regressor_sbp = torch.nn.Linear(1536, 1)
self.regressor_HbA1C = torch.nn.Linear(1536, 1)
def forward(self, x):
y_swin = self.swin(x).pooler_output
y_age = self.regressor_age(y_swin)
y_education = self.regressor_education(y_swin)
y_sleep = self.regressor_sleep(y_swin)
y_BMI = self.regressor_BMI(y_swin)
y_dbp = self.regressor_dbp(y_swin)
y_sbp = self.regressor_sbp(y_swin)
y_HbA1C = self.regressor_HbA1C(y_swin)
return y_age, y_education, y_sleep, y_BMI, y_dbp, y_sbp, y_HbA1C
class SingleOutputModel(torch.nn.Module):
def __init__(self, model, output_index=0):
super().__init__()
self.model = model
self.output_index = output_index
def forward(self, x):
return self.model(x)[self.output_index]
class ClassifierOutputTarget:
def __init__(self, category):
self.category = category
def __call__(self, model_output):
return model_output[self.category]
def reshape_transform(tensor, height=18, width=18):
# this part of the code is from https://github.com/jacobgil/pytorch-grad-cam
result = tensor[:, :, :].reshape(tensor.size(0),
height, width, tensor.size(2))
# Bring the channels to the first dimension,
# like in CNNs.
result = result.transpose(2, 3).transpose(1, 2)
return result
if __name__ == '__main__':
random_state = 0
np.random.seed(random_state)
random.seed(random_state)
torch.manual_seed(random_state)
os.environ["PYTHONHASHSEED"] = str(random_state)
args = get_args()
result_dir = os.path.join('./result', 'GradCAM_Regression', args.method)
if not os.path.exists(result_dir):
os.makedirs(result_dir)
else:
pass
predictor_index = args.predictor_idx
class_index = args.class_idx
img = Image.open(os.path.join(args.image_path, args.image))
img = img.resize((576, 576))
img = np.array(img)
img = img.astype(np.float32)
img = img / 255
convert_tensor = transforms.Compose([transforms.ToTensor(),
transforms.Resize((576, 576))
])
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
input_tensor = convert_tensor(img).to(device)
model = SwinforRegression(model_name_or_path='microsoft/swin-large-patch4-window12-384-in22k', n_label=7)
checkpoint = torch.load(args.model_checkpoint, map_location=torch.device(device))
model.load_state_dict(checkpoint)
model_for_gradcam = SingleOutputModel(model, predictor_index)
model.to(device)
model.eval()
methods = \
{"gradcam": GradCAM,
"scorecam": ScoreCAM,
"gradcam++": GradCAMPlusPlus,
"ablationcam": AblationCAM,
"xgradcam": XGradCAM,
"eigencam": EigenCAM,
"eigengradcam": EigenGradCAM,
"fullgrad": FullGrad}
targets = [ClassifierOutputTarget(class_index)]
target_layers = [model_for_gradcam.model.swin.encoder.layers[-1].blocks[-1].layernorm_before]
cam = methods[args.method](model=model_for_gradcam, target_layers=target_layers, reshape_transform=reshape_transform)
pred = model_for_gradcam(input_tensor.unsqueeze(0))
output = cam(input_tensor=input_tensor.unsqueeze(0), targets=targets,
aug_smooth=args.aug_smooth, eigen_smooth=args.eigen_smooth)
cam = output[0, :]
cam_image = show_cam_on_image(img, cam)
if args.aug_smooth == True and args.eigen_smooth == True:
cv2.imwrite(os.path.join(result_dir, args.image[
:-4] + '_' + 'all_smooth' + '_' + str(args.predictor_idx) + '_' + str(args.class_idx) + '.jpg'),
cam_image)
elif args.aug_smooth == True and args.eigen_smooth == False:
cv2.imwrite(os.path.join(result_dir, args.image[
:-4] + '_' + 'aug_smooth' + '_' + str(args.predictor_idx) + '_' + str(args.class_idx) + '.jpg'),
cam_image)
elif args.aug_smooth == False and args.eigen_smooth == True:
cv2.imwrite(os.path.join(result_dir, args.image[
:-4] + '_' + 'eigen_smooth' + '_' + str(args.predictor_idx) + '_' + str(args.class_idx) + '.jpg'),
cam_image)
else:
cv2.imwrite(os.path.join(result_dir, args.image[
:-4] + '_' + 'no_smooth' + '_' + str(args.predictor_idx) + '_' + str(args.class_idx) + '.jpg'),
cam_image)