-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_few.py
254 lines (187 loc) · 11 KB
/
test_few.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
import os
import argparse
import random
import math
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from tqdm import tqdm
from scipy.ndimage import gaussian_filter
from dataset.medical_few import MedDataset
from CLIP.clip import create_model
from CLIP.tokenizer import tokenize
from CLIP.adapter import CLIP_Inplanted
from PIL import Image
from sklearn.metrics import roc_auc_score, precision_recall_curve, pairwise
from loss import FocalLoss, BinaryDiceLoss
from utils import augment, cos_sim, encode_text_with_prompt_ensemble
from prompt import REAL_NAME
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import warnings
warnings.filterwarnings("ignore")
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
CLASS_INDEX = {'Brain':3, 'Liver':2, 'Retina_RESC':1, 'Retina_OCT2017':-1, 'Chest':-2, 'Histopathology':-3}
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def main():
parser = argparse.ArgumentParser(description='Testing')
parser.add_argument('--model_name', type=str, default='ViT-L-14-336', help="ViT-B-16-plus-240, ViT-L-14-336")
parser.add_argument('--pretrain', type=str, default='openai', help="laion400m, openai")
parser.add_argument('--obj', type=str, default='Liver')
parser.add_argument('--data_path', type=str, default='./data/')
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--save_model', type=int, default=1)
parser.add_argument('--save_path', type=str, default='./ckpt/few-shot/')
parser.add_argument('--img_size', type=int, default=240)
parser.add_argument("--epoch", type=int, default=50, help="epochs")
parser.add_argument("--learning_rate", type=float, default=0.001, help="learning rate")
parser.add_argument("--features_list", type=int, nargs="+", default=[6, 12, 18, 24], help="features used")
parser.add_argument('--seed', type=int, default=111)
parser.add_argument('--shot', type=int, default=4)
parser.add_argument('--iterate', type=int, default=0)
args = parser.parse_args()
setup_seed(args.seed)
# fixed feature extractor
clip_model = create_model(model_name=args.model_name, img_size=args.img_size, device=device, pretrained=args.pretrain, require_pretrained=True)
clip_model.eval()
model = CLIP_Inplanted(clip_model=clip_model, features=args.features_list).to(device)
model.eval()
checkpoint = torch.load(os.path.join(f'{args.save_path}', f'{args.obj}.pth'))
model.seg_adapters.load_state_dict(checkpoint["seg_adapters"])
model.det_adapters.load_state_dict(checkpoint["det_adapters"])
for name, param in model.named_parameters():
param.requires_grad = True
# optimizer for only adapters
seg_optimizer = torch.optim.Adam(list(model.seg_adapters.parameters()), lr=args.learning_rate, betas=(0.5, 0.999))
det_optimizer = torch.optim.Adam(list(model.det_adapters.parameters()), lr=args.learning_rate, betas=(0.5, 0.999))
# load test dataset
kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}
test_dataset = MedDataset(args.data_path, args.obj, args.img_size, args.shot, args.iterate)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, **kwargs)
# few-shot image augmentation
augment_abnorm_img, augment_abnorm_mask = augment(test_dataset.fewshot_abnorm_img, test_dataset.fewshot_abnorm_mask)
augment_normal_img, augment_normal_mask = augment(test_dataset.fewshot_norm_img)
augment_fewshot_img = torch.cat([augment_abnorm_img, augment_normal_img], dim=0)
augment_fewshot_mask = torch.cat([augment_abnorm_mask, augment_normal_mask], dim=0)
augment_fewshot_label = torch.cat([torch.Tensor([1] * len(augment_abnorm_img)), torch.Tensor([0] * len(augment_normal_img))], dim=0)
train_dataset = torch.utils.data.TensorDataset(augment_fewshot_img, augment_fewshot_mask, augment_fewshot_label)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True, **kwargs)
# memory bank construction
support_dataset = torch.utils.data.TensorDataset(augment_normal_img)
support_loader = torch.utils.data.DataLoader(support_dataset, batch_size=1, shuffle=True, **kwargs)
# losses
loss_focal = FocalLoss()
loss_dice = BinaryDiceLoss()
loss_bce = torch.nn.BCEWithLogitsLoss()
# text prompt
with torch.cuda.amp.autocast(), torch.no_grad():
text_features = encode_text_with_prompt_ensemble(clip_model, REAL_NAME[args.obj], device)
best_result = 0
seg_features = []
det_features = []
for image in support_loader:
image = image[0].to(device)
with torch.no_grad():
_, seg_patch_tokens, det_patch_tokens = model(image)
seg_patch_tokens = [p[0].contiguous() for p in seg_patch_tokens]
det_patch_tokens = [p[0].contiguous() for p in det_patch_tokens]
seg_features.append(seg_patch_tokens)
det_features.append(det_patch_tokens)
seg_mem_features = [torch.cat([seg_features[j][i] for j in range(len(seg_features))], dim=0) for i in range(len(seg_features[0]))]
det_mem_features = [torch.cat([det_features[j][i] for j in range(len(det_features))], dim=0) for i in range(len(det_features[0]))]
result = test(args, model, test_loader, text_features, seg_mem_features, det_mem_features)
def test(args, model, test_loader, text_features, seg_mem_features, det_mem_features):
gt_list = []
gt_mask_list = []
det_image_scores_zero = []
det_image_scores_few = []
seg_score_map_zero = []
seg_score_map_few= []
for (image, y, mask) in tqdm(test_loader):
image = image.to(device)
mask[mask > 0.5], mask[mask <= 0.5] = 1, 0
with torch.no_grad(), torch.cuda.amp.autocast():
_, seg_patch_tokens, det_patch_tokens = model(image)
seg_patch_tokens = [p[0, 1:, :] for p in seg_patch_tokens]
det_patch_tokens = [p[0, 1:, :] for p in det_patch_tokens]
if CLASS_INDEX[args.obj] > 0:
# few-shot, seg head
anomaly_maps_few_shot = []
for idx, p in enumerate(seg_patch_tokens):
cos = cos_sim(seg_mem_features[idx], p)
height = int(np.sqrt(cos.shape[1]))
anomaly_map_few_shot = torch.min((1 - cos), 0)[0].reshape(1, 1, height, height)
anomaly_map_few_shot = F.interpolate(torch.tensor(anomaly_map_few_shot),
size=args.img_size, mode='bilinear', align_corners=True)
anomaly_maps_few_shot.append(anomaly_map_few_shot[0].cpu().numpy())
score_map_few = np.sum(anomaly_maps_few_shot, axis=0)
seg_score_map_few.append(score_map_few)
# zero-shot, seg head
anomaly_maps = []
for layer in range(len(seg_patch_tokens)):
seg_patch_tokens[layer] /= seg_patch_tokens[layer].norm(dim=-1, keepdim=True)
anomaly_map = (100.0 * seg_patch_tokens[layer] @ text_features).unsqueeze(0)
B, L, C = anomaly_map.shape
H = int(np.sqrt(L))
anomaly_map = F.interpolate(anomaly_map.permute(0, 2, 1).view(B, 2, H, H),
size=args.img_size, mode='bilinear', align_corners=True)
anomaly_map = torch.softmax(anomaly_map, dim=1)[:, 1, :, :]
anomaly_maps.append(anomaly_map.cpu().numpy())
score_map_zero = np.sum(anomaly_maps, axis=0)
seg_score_map_zero.append(score_map_zero)
else:
# few-shot, det head
anomaly_maps_few_shot = []
for idx, p in enumerate(det_patch_tokens):
cos = cos_sim(det_mem_features[idx], p)
height = int(np.sqrt(cos.shape[1]))
anomaly_map_few_shot = torch.min((1 - cos), 0)[0].reshape(1, 1, height, height)
anomaly_map_few_shot = F.interpolate(torch.tensor(anomaly_map_few_shot),
size=args.img_size, mode='bilinear', align_corners=True)
anomaly_maps_few_shot.append(anomaly_map_few_shot[0].cpu().numpy())
anomaly_map_few_shot = np.sum(anomaly_maps_few_shot, axis=0)
score_few_det = anomaly_map_few_shot.mean()
det_image_scores_few.append(score_few_det)
# zero-shot, det head
anomaly_score = 0
for layer in range(len(det_patch_tokens)):
det_patch_tokens[layer] /= det_patch_tokens[layer].norm(dim=-1, keepdim=True)
anomaly_map = (100.0 * det_patch_tokens[layer] @ text_features).unsqueeze(0)
anomaly_map = torch.softmax(anomaly_map, dim=-1)[:, :, 1]
anomaly_score += anomaly_map.mean()
det_image_scores_zero.append(anomaly_score.cpu().numpy())
gt_mask_list.append(mask.squeeze().cpu().detach().numpy())
gt_list.extend(y.cpu().detach().numpy())
gt_list = np.array(gt_list)
gt_mask_list = np.asarray(gt_mask_list)
gt_mask_list = (gt_mask_list>0).astype(np.int_)
if CLASS_INDEX[args.obj] > 0:
seg_score_map_zero = np.array(seg_score_map_zero)
seg_score_map_few = np.array(seg_score_map_few)
seg_score_map_zero = (seg_score_map_zero - seg_score_map_zero.min()) / (seg_score_map_zero.max() - seg_score_map_zero.min())
seg_score_map_few = (seg_score_map_few - seg_score_map_few.min()) / (seg_score_map_few.max() - seg_score_map_few.min())
segment_scores = 0.5 * seg_score_map_zero + 0.5 * seg_score_map_few
seg_roc_auc = roc_auc_score(gt_mask_list.flatten(), segment_scores.flatten())
print(f'{args.obj} pAUC : {round(seg_roc_auc,4)}')
segment_scores_flatten = segment_scores.reshape(segment_scores.shape[0], -1)
roc_auc_im = roc_auc_score(gt_list, np.max(segment_scores_flatten, axis=1))
print(f'{args.obj} AUC : {round(roc_auc_im, 4)}')
return seg_roc_auc + roc_auc_im
else:
det_image_scores_zero = np.array(det_image_scores_zero)
det_image_scores_few = np.array(det_image_scores_few)
det_image_scores_zero = (det_image_scores_zero - det_image_scores_zero.min()) / (det_image_scores_zero.max() - det_image_scores_zero.min())
det_image_scores_few = (det_image_scores_few - det_image_scores_few.min()) / (det_image_scores_few.max() - det_image_scores_few.min())
image_scores = 0.5 * det_image_scores_zero + 0.5 * det_image_scores_few
img_roc_auc_det = roc_auc_score(gt_list, image_scores)
print(f'{args.obj} AUC : {round(img_roc_auc_det,4)}')
return img_roc_auc_det
if __name__ == '__main__':
main()