-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlora.py
More file actions
272 lines (226 loc) · 13.4 KB
/
Copy pathlora.py
File metadata and controls
272 lines (226 loc) · 13.4 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import torch
from utils import *
import os
from loralib.utils import mark_only_lora_as_trainable, apply_lora, get_lora_parameters, save_lora
from datetime import datetime
from torch.nn.parallel import DistributedDataParallel as DDP
from loss import *
import torch.distributed as dist
from transformers import get_cosine_schedule_with_warmup
from timm_vit_return_attn_patch import patch_timm_vit_return_attn_scores
from bert_modeling_bert_self_attn_patch import patch_bert_self_attn
def run_model(args, clip_model, merged_df, tokenizer, logit_scale, train_loader, preprocess):
if args.save_path == None:
raise ValueError('args.save_path cannot be None')
now = datetime.now()
foldername_time = now.strftime("%Y_%m_%d_%H_%M_%S")
save_dir = f'{args.save_path}/seed{args.seed}/{foldername_time}'
os.makedirs(save_dir, exist_ok=True)
patch_timm_vit_return_attn_scores()
patch_bert_self_attn()
# save hyperparameter values
with open(os.path.join(save_dir, 'model_hyperparameters.txt'),'w') as file:
for key, value in vars(args).items():
file.write(f"{key} : {value} \n")
list_lora_layers = apply_lora(args, clip_model)
mark_only_lora_as_trainable(clip_model)
clip_model = clip_model.to(args.device)
num_epochs = args.num_epochs
feature_dim = 512
if args.loss_type == 'clip_loss':
loss_fn = CLIPLoss(args, logit_scale).to(device=args.device)
elif args.loss_type == 'clip_loss_ace_hgnn':
loss_fn = CLIPLossACE_HGAT(args, logit_scale, feature_dim).to(device=args.device)
else:
raise ValueError(f'Invalid loss type is given: {args.loss_type}')
learnable_params = get_lora_parameters(clip_model) + list(loss_fn.parameters())
print("Number of learnable params: ", sum(p.numel() for p in learnable_params))
optimizer = torch.optim.AdamW(learnable_params, weight_decay=1e-2, betas=(0.9, 0.999), lr=args.lr)
scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=len(train_loader), num_training_steps=len(train_loader)*num_epochs, num_cycles=0.5)
best_epoch_loss = -1
best_acc_chexpert = -1
best_auc_chexpert = -1
best_acc_rsna = -1
best_auc_rsna = -1
best_acc_siim = -1
best_auc_siim = -1
best_auc_chest_xray_14 = -1
# training LoRA
scaler = torch.cuda.amp.GradScaler()
for epoch in range(num_epochs):
clip_model.train()
tot_samples = 0
loss_epoch = 0.
for i, (images, target, index) in enumerate(tqdm(train_loader)):
indices = index.tolist()
images = images.to(args.device)
if args.loss_type == 'clip_loss_ace_hgnn':
texts = tokenizer(target, context_length = args.context_length).to(args.device)
loss = loss_fn(clip_model, images, texts, merged_df, indices)
else:
texts = tokenizer(target, context_length = args.context_length).to(args.device)
text_features = clip_model.encode_text(texts, normalize=True) # Note that this normalization is L2 Norm, which is different than the frobenius norm (default option for torch.norm)
image_features = clip_model.encode_image(images, normalize=True)
loss = loss_fn(image_features, text_features, merged_df, indices)
loss_epoch += loss.item() * images.shape[0]
tot_samples += images.shape[0]
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
scheduler.step()
loss_epoch /= tot_samples
current_lr = scheduler.get_last_lr()[0]
print('LR: {:.6f}, Loss: {:.4f}'.format(current_lr, loss_epoch))
if best_epoch_loss == -1 or loss_epoch < best_epoch_loss:
print(f'Saving best epoch loss: {loss_epoch}')
print(f'current logit_scale: {loss_fn.logit_scale.item()}')
logit_scale = loss_fn.logit_scale
best_epoch_loss = loss_epoch
msg = f'best_epoch_{epoch + 1}'
save_lora(args, list_lora_layers, loss_fn, msg, save_dir)
if args.eval:
clip_model.eval()
acc, auc = zero_shot_chexpert_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=False)
if acc > best_acc_chexpert:
best_acc_chexpert = acc
if auc > best_auc_chexpert:
best_auc_chexpert = auc
with open(os.path.join(save_dir, 'chexpert5x200_eval.txt'), 'a') as file:
file.write(f'Chexpert 5x200: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_chexpert:.4f}, Best AUROC: {best_auc_chexpert:.4f} \n')
acc, auc = zero_shot_rsna_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=False)
if acc > best_acc_rsna:
best_acc_rsna = acc
if auc > best_auc_rsna:
best_auc_rsna = auc
with open(os.path.join(save_dir, 'RSNA_eval.txt'), 'a') as file:
file.write(f'RSNA: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_rsna:.4f}, Best AUROC: {best_auc_rsna:.4f} \n')
acc, auc = zero_shot_siim_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=False)
if acc > best_acc_siim:
best_acc_siim = acc
if auc > best_auc_siim:
best_auc_siim = auc
with open(os.path.join(save_dir, 'SIIM_eval.txt'), 'a') as file:
file.write(f'SIIM: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_siim:.4f}, Best AUROC: {best_auc_siim:.4f} \n')
# used for validation
auc = zero_shot_chest_xray_14_eval(args, clip_model, tokenizer, loss_fn, preprocess, trained_on_multi_gpu=False)
if auc > best_auc_chest_xray_14:
best_auc_chest_xray_14 = auc
with open(os.path.join(save_dir, 'Chest_xray_14_eval.txt'), 'a') as file:
file.write(f'Chest X-ray 14: Epoch: {epoch + 1}, AUROC: {auc:.4f}, Best AUROC: {best_auc_chest_xray_14:.4f} \n')
with open(os.path.join(save_dir, 'loss.txt'), 'a') as file:
file.write(f'Epoch: {epoch + 1}, LR : {current_lr:.6f}, loss : {loss_epoch:.4f}, Best loss : {best_epoch_loss:.4f}, logit_scale: {loss_fn.logit_scale.item():.4f} \n')
return
def run_model_multi_gpu(args, clip_model, merged_df, tokenizer, logit_scale, train_loader, preprocess):
if args.save_path == None:
raise ValueError('args.save_path cannot be None')
patch_timm_vit_return_attn_scores()
patch_bert_self_attn()
if args.rank == 0:
now = datetime.now()
foldername_time = now.strftime("%Y_%m_%d_%H_%M_%S")
save_dir = f'{args.save_path}/seed{args.seed}/{foldername_time}'
os.makedirs(save_dir, exist_ok=True)
with open(os.path.join(save_dir, 'model_hyperparameters.txt'),'w') as file:
for key, value in vars(args).items():
file.write(f"{key} : {value} \n")
list_lora_layers = apply_lora(args, clip_model)
mark_only_lora_as_trainable(clip_model)
clip_model = clip_model.to(args.rank)
clip_model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(clip_model)
clip_model = DDP(clip_model, device_ids=[args.rank])
num_epochs = args.num_epochs
feature_dim = 512
if args.loss_type == 'clip_loss':
loss_fn = CLIPLoss(args, logit_scale).to(device=args.rank)
elif args.loss_type == 'clip_loss_ace_hgnn':
loss_fn = CLIPLossACE_HGAT(args, logit_scale, feature_dim).to(device=args.rank)
else:
raise ValueError(f'Invalid loss type is given: {args.loss_type}')
learnable_params = get_lora_parameters(clip_model) + list(loss_fn.parameters())
print("Number of learnable params: ", sum(p.numel() for p in learnable_params))
optimizer = torch.optim.AdamW(learnable_params, weight_decay=1e-2, betas=(0.9, 0.999), lr=args.lr)
scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=len(train_loader), num_training_steps=len(train_loader)*num_epochs, num_cycles=0.5)
best_epoch_loss = -1
best_acc_chexpert = -1
best_auc_chexpert = -1
best_acc_rsna = -1
best_auc_rsna = -1
best_acc_siim = -1
best_auc_siim = -1
best_auc_chest_xray_14 = -1
# training LoRA
scaler = torch.cuda.amp.GradScaler()
for epoch in range(num_epochs):
train_loader.sampler.set_epoch(epoch)
clip_model.module.train()
tot_samples = 0
loss_epoch = 0.
for i, (images, target, index) in enumerate(tqdm(train_loader)):
indices = index.tolist()
images = images.to(args.rank)
if args.loss_type == 'clip_loss_ace_hgnn':
texts = tokenizer(target, context_length = args.context_length).to(args.rank)
loss = loss_fn(clip_model.module, images, texts, merged_df, indices)
else:
texts = tokenizer(target, context_length = args.context_length).to(args.rank)
text_features = clip_model.module.encode_text(texts, normalize=True) # Note that this normalization is L2 Norm, which is different than the frobenius norm (default option for torch.norm)
image_features = clip_model.module.encode_image(images, normalize=True)
loss = loss_fn(image_features, text_features, merged_df, indices)
loss_epoch += loss.item() * images.shape[0]
tot_samples += images.shape[0]
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
if args.rank == 0:
scheduler.step()
lr = scheduler.get_last_lr()[0]
torch.distributed.broadcast(torch.tensor(lr, device=args.rank), 0)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
dist.barrier()
if args.rank == 0:
loss_epoch /= tot_samples
current_lr = scheduler.get_last_lr()[0]
print('LR: {:.6f}, Loss: {:.4f}'.format(current_lr, loss_epoch))
if best_epoch_loss == -1 or loss_epoch < best_epoch_loss:
print(f'Saving best epoch loss: {loss_epoch}')
print(f'current logit_scale: {loss_fn.logit_scale.item()}')
logit_scale = loss_fn.logit_scale
best_epoch_loss = loss_epoch
msg = f'best_epoch_{epoch + 1}'
save_lora(args, list_lora_layers, loss_fn, msg, save_dir)
if args.eval:
clip_model.module.eval()
acc, auc = zero_shot_chexpert_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=True)
if acc > best_acc_chexpert:
best_acc_chexpert = acc
if auc > best_auc_chexpert:
best_auc_chexpert = auc
with open(os.path.join(save_dir, 'chexpert5x200_eval.txt'), 'a') as file:
file.write(f'Chexpert 5x200: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_chexpert:.4f}, Best AUROC: {best_auc_chexpert:.4f} \n')
acc, auc = zero_shot_rsna_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=True)
if acc > best_acc_rsna:
best_acc_rsna = acc
if auc > best_auc_rsna:
best_auc_rsna = auc
with open(os.path.join(save_dir, 'RSNA_eval.txt'), 'a') as file:
file.write(f'RSNA: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_rsna:.4f}, Best AUROC: {best_auc_rsna:.4f} \n')
acc, auc = zero_shot_siim_eval(args, clip_model, tokenizer, loss_fn, preprocess, logit_scale, trained_on_multi_gpu=True)
if acc > best_acc_siim:
best_acc_siim = acc
if auc > best_auc_siim:
best_auc_siim = auc
with open(os.path.join(save_dir, 'SIIM_eval.txt'), 'a') as file:
file.write(f'SIIM: Epoch: {epoch + 1}, Accuracy: {acc:.4f}, AUROC: {auc:.4f}, Best Accuracy: {best_acc_siim:.4f}, Best AUROC: {best_auc_siim:.4f} \n')
# used for validation
auc = zero_shot_chest_xray_14_eval(args, clip_model, tokenizer, loss_fn, preprocess, trained_on_multi_gpu=True)
if auc > best_auc_chest_xray_14:
best_auc_chest_xray_14 = auc
with open(os.path.join(save_dir, 'Chest_xray_14_eval.txt'), 'a') as file:
file.write(f'Chest X-ray 14: Epoch: {epoch + 1}, AUROC: {auc:.4f}, Best AUROC: {best_auc_chest_xray_14:.4f} \n')
with open(os.path.join(save_dir, 'loss.txt'), 'a') as file:
file.write(f'Epoch: {epoch + 1}, LR : {current_lr:.6f}, loss : {loss_epoch:.4f}, Best loss : {best_epoch_loss:.4f}, logit_scale: {loss_fn.logit_scale.item():.4f} \n')
dist.barrier()
return