-
Notifications
You must be signed in to change notification settings - Fork 20
/
train_zero.py
245 lines (193 loc) · 10.3 KB
/
train_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
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
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("--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()
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 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)
save_score = 0.0
for epoch in range(args.epoch):
print('epoch', epoch, ':')
loss_list = []
idx = 0
for (image, image_label, mask, seg_idx) in tqdm(train_loader):
if idx % (len(train_loader) // 5) == 0:
score = test(args, model, test_loader, text_feature_list[CLASS_INDEX[args.obj]])
if score >= save_score:
save_score = score
ckp_path = f'./ckpt/zero-shot/{args.obj}.pth'
torch.save({'seg_adapters': model.seg_adapters.state_dict(),
'det_adapters': model.det_adapters.state_dict()},
ckp_path)
print(f'best epoch found: epoch {epoch} batch {idx}')
print('\n')
idx += 1
image = image.squeeze(0).to(device)
seg_idx = seg_idx.item()
with 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]
# image level
det_loss = 0
image_label = image_label.squeeze(0).to(device)
for layer in range(len(det_patch_tokens)):
det_patch_tokens[layer] = det_patch_tokens[layer] / det_patch_tokens[layer].norm(dim=-1, keepdim=True)
anomaly_map = (100.0 * det_patch_tokens[layer] @ text_feature_list[seg_idx]).unsqueeze(0)
anomaly_map = torch.softmax(anomaly_map, dim=-1)[:, :, 1]
anomaly_score = torch.mean(anomaly_map, dim=-1)
det_loss += loss_bce(anomaly_score, image_label)
if seg_idx > 0:
# pixel level
seg_loss = 0
mask = mask.squeeze(0).to(device)
mask[mask > 0.5], mask[mask <= 0.5] = 1, 0
for layer in range(len(seg_patch_tokens)):
seg_patch_tokens[layer] = seg_patch_tokens[layer] / seg_patch_tokens[layer].norm(dim=-1, keepdim=True)
# print(seg_patch_tokens[layer].shape, text_feature_list[seg_idx].shape) # torch.Size([289, 768]) torch.Size([768, 2])
anomaly_map = (100.0 * seg_patch_tokens[layer] @ text_feature_list[seg_idx]).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)
seg_loss += loss_focal(anomaly_map, mask)
seg_loss += loss_dice(anomaly_map[:, 1, :, :], mask)
loss = seg_loss + det_loss # = focal(seg_out, mask) + bce(det_out, y)
loss.requires_grad_(True)
seg_optimizer.zero_grad()
det_optimizer.zero_grad()
loss.backward()
seg_optimizer.step()
det_optimizer.step()
else:
loss = det_loss
loss.requires_grad_(True)
det_optimizer.zero_grad()
loss.backward()
det_optimizer.step()
loss_list.append(loss.item())
train_dataset.shuffle_dataset()
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True, **kwargs)
# logs
print("Loss: ", np.mean(loss_list))
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()