-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_zero.py
169 lines (130 loc) · 6.8 KB
/
test_zero.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
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 sklearn.metrics import roc_auc_score
from scipy.ndimage import gaussian_filter
from dataset.medical_zero import MedTestDataset, MedTrainDataset
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 precision_recall_curve
from loss import FocalLoss, BinaryDiceLoss
from utils import augment, encode_text_with_prompt_ensemble
from prompt import REAL_NAME
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}
CLASS_INDEX_INV = {3:'Brain', 2:'Liver', 1:'Retina_RESC', -1:'Retina_OCT2017', -2:'Chest', -3:'Histopathology'}
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='Retina_RESC')
parser.add_argument('--data_path', type=str, default='./data/')
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--img_size', type=int, default=240)
parser.add_argument('--save_path', type=str, default='./ckpt/zero-shot/')
parser.add_argument("--epoch", type=int, default=50, help="epochs")
parser.add_argument("--learning_rate", type=float, default=0.0001, 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)
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"])
# 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 dataset and loader
kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}
train_dataset = MedTrainDataset(args.data_path, args.obj, args.img_size, args.batch_size)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True, **kwargs)
test_dataset = MedTestDataset(args.data_path, args.obj, args.img_size)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, **kwargs)
# losses
loss_focal = FocalLoss()
loss_dice = BinaryDiceLoss()
loss_bce = torch.nn.BCEWithLogitsLoss()
text_feature_list = [0]
# text prompt
with torch.cuda.amp.autocast(), torch.no_grad():
for i in [1,2,3,-3,-2,-1]:
text_feature = encode_text_with_prompt_ensemble(clip_model, REAL_NAME[CLASS_INDEX_INV[i]], device)
text_feature_list.append(text_feature)
score = test(args, model, test_loader, text_feature_list[CLASS_INDEX[args.obj]])
def test(args, seg_model, test_loader, text_features):
gt_list = []
gt_mask_list = []
image_scores = []
segment_scores = []
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():
_, ori_seg_patch_tokens, ori_det_patch_tokens = seg_model(image)
ori_seg_patch_tokens = [p[0, 1:, :] for p in ori_seg_patch_tokens]
ori_det_patch_tokens = [p[0, 1:, :] for p in ori_det_patch_tokens]
# image
anomaly_score = 0
patch_tokens = ori_det_patch_tokens.copy()
for layer in range(len(patch_tokens)):
patch_tokens[layer] /= patch_tokens[layer].norm(dim=-1, keepdim=True)
anomaly_map = (100.0 * patch_tokens[layer] @ text_features).unsqueeze(0)
anomaly_map = torch.softmax(anomaly_map, dim=-1)[:, :, 1]
anomaly_score += anomaly_map.mean()
image_scores.append(anomaly_score.cpu())
# pixel
patch_tokens = ori_seg_patch_tokens
anomaly_maps = []
for layer in range(len(patch_tokens)):
patch_tokens[layer] /= patch_tokens[layer].norm(dim=-1, keepdim=True)
anomaly_map = (100.0 * 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())
final_score_map = np.sum(anomaly_maps, axis=0)
gt_mask_list.append(mask.squeeze().cpu().detach().numpy())
gt_list.extend(y.cpu().detach().numpy())
segment_scores.append(final_score_map)
gt_list = np.array(gt_list)
gt_mask_list = np.asarray(gt_mask_list)
gt_mask_list = (gt_mask_list>0).astype(np.int_)
segment_scores = np.array(segment_scores)
image_scores = np.array(image_scores)
segment_scores = (segment_scores - segment_scores.min()) / (segment_scores.max() - segment_scores.min())
image_scores = (image_scores - image_scores.min()) / (image_scores.max() - image_scores.min())
img_roc_auc_det = roc_auc_score(gt_list, image_scores)
print(f'{args.obj} AUC : {round(img_roc_auc_det,4)}')
if CLASS_INDEX[args.obj] > 0:
seg_roc_auc = roc_auc_score(gt_mask_list.flatten(), segment_scores.flatten())
print(f'{args.obj} pAUC : {round(seg_roc_auc,4)}')
return seg_roc_auc + img_roc_auc_det
else:
return img_roc_auc_det
if __name__ == '__main__':
main()