-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_clip.py
417 lines (357 loc) · 16.7 KB
/
model_clip.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import datetime
import os
import random
from argparse import ArgumentParser
from typing import Any
from pytorch_lightning.utilities.types import STEP_OUTPUT
# from torch.utils.tensorboard import SummaryWriter
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torcheval.metrics import AUC, MulticlassAccuracy, MulticlassConfusionMatrix
from cosine_annealing_warmup import CosineAnnealingWarmupRestarts
import torch.distributed as dist
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.strategies import DDPStrategy
from lightning_fabric.strategies import FSDPStrategy
from backbones.encoder_clip import BertEncoder, ImageEncoder
from backbones.encoder_cleft import SwinEncoder, DinoEncoder, CausalLMEncoder, BaselineEncoder
from backbones.loss import DINOLoss
import utils_mae.lr_decay as lrd
from transformers import Adafactor
from sklearn.metrics import roc_auc_score, accuracy_score
from peft import (PromptTuningConfig, PromptTuningInit, get_peft_model, TaskType)
from lightly.loss import NTXentLoss
from lightly.models.modules import SimCLRProjectionHead
from memory_profiler import profile
from backbones.mgca_encoder import ImageEncoder as MGCAImageEncoder
from backbones.mgca_encoder import BertEncoder as MGCABertEncoder
torch.autograd.set_detect_anomaly(True)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CHEXPERT_BASE_CAPTION = "this is a chest x ray of a patient with "
# os.environ['CUDA_VISIBLE_DEVICES']='0,1'
os.environ['WANDB_START_METHOD'] = 'thread'
class CLIP(LightningModule):
def __init__(self,
img_encoder: str = "vit_base",
freeze_llm: bool = False,
emb_dim: int = 128,
softmax_temperature: float = 0.07,
learning_rate: float = 2e-5,
momentum: float = 0.9,
weight_decay: float = 0.05,
batch_size: int = 144,
num_workers: int = 8,
num_heads: int = 1,
lamb: float = 0.75,
epsilon: float = 0.05,
img_mask_ratio: float = 0,
*args,
**kwargs
):
super().__init__()
self.save_hyperparameters()
self.confmat = MulticlassConfusionMatrix(self.hparams.num_classes)
self.all_scores = None
self.all_labels = None
# init encoders
if self.hparams.mgca_encoder:
self.img_encoder_q = MGCAImageEncoder(text_feat_dim=128, output_dim=128)
self.text_encoder_q = MGCABertEncoder(output_dim=128, freeze_bert=freeze_llm)
mgca_model = torch.load('./pretrained/vit_base.ckpt', map_location='cpu')
mgca_vision_state_dict = {}
for k, v in mgca_model['state_dict'].items():
if 'text_encoder' in k:
continue
if 'local_atten_layer' in k or 'prototype_layer' in k:
continue
k = k.replace('img_encoder_q.', '')
mgca_vision_state_dict[k] = v
self.img_encoder_q.load_state_dict(mgca_vision_state_dict)
mgca_text_state_dict = {}
for k, v in mgca_model['state_dict'].items():
if 'img_encoder_q' in k:
continue
if 'local_atten_layer' in k or 'prototype_layer' in k:
continue
k = k.replace('text_encoder_q.', '')
mgca_text_state_dict[k] = v
self.text_encoder_q.load_state_dict(mgca_text_state_dict)
else:
self.img_encoder_q = ImageEncoder(
model_name=img_encoder, output_dim=self.hparams.emb_dim,
pretrained_pth=self.hparams.pretrained_encoder,
mae_ratio=self.hparams.img_mask_ratio,
freeze_vit=self.hparams.freeze_vit)
if not self.hparams.img_cls_ft:
self.text_encoder_q = BertEncoder(
output_dim=self.hparams.emb_dim, freeze_llm=freeze_llm)
self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / self.hparams.softmax_temperature))
self.zero_shot_text_feats = None
# create a global classifier
if self.hparams.img_cls_ft:
self.img_encoder_q.global_embed = nn.Linear(self.img_encoder_q.feature_dim, self.hparams.num_classes)
self.img_encoder_q.global_embed.weight.requires_grad = True
self.img_encoder_q.global_embed.bias.requires_grad = True
def forward(self, batch, batch_idx, split="train"):
'''Forward step of our method'''
# Forward of query image encoder
img_feat_q, patch_feat_q, loss_mae, pred_mae, mask_mae, pred_feat = self.img_encoder_q(batch["imgs"])
# patch_emb_q = self.img_encoder_q.local_embed(patch_feat_q)
# patch_emb_q = F.normalize(patch_emb_q, dim=-1)
img_emb_q = self.img_encoder_q.global_embed(pred_feat.mean(dim=1))
img_emb_q = F.normalize(img_emb_q, dim=-1)
# Forward of query text encoder
report_feat_q_full, word_feat_q_full, word_attn_q_full, sents_full = self.text_encoder_q(
batch["caption_ids"], batch["attention_mask"], batch["token_type_ids"])
# word_emb_q = self.text_encoder_q.local_embed(word_feat_q_full)
# word_emb_q = F.normalize(word_emb_q, dim=-1)
report_emb_q = self.text_encoder_q.global_embed(report_feat_q_full)
report_emb_q = F.normalize(report_emb_q, dim=-1)
########### image-text contrastive loss ################
bz = img_emb_q.size(0)
labels = torch.arange(bz).type_as(report_emb_q).long()
scores = img_emb_q.mm(report_emb_q.t())
scores *= self.logit_scale.exp()
scores1 = scores.transpose(0, 1)
loss0 = F.cross_entropy(scores, labels)
loss1 = F.cross_entropy(scores1, labels)
loss_c = loss0 + loss1
# compute retrieval accuracy
i2t_acc1, i2t_acc5 = self.precision_at_k(scores, labels, top_k=(1, 5))
t2i_acc1, t2i_acc5 = self.precision_at_k(scores1, labels, top_k=(1, 5))
acc1 = (i2t_acc1 + t2i_acc1) / 2.
acc5 = (i2t_acc5 + t2i_acc5) / 2.
return loss_c, acc1, acc5
def zero_shot_inference(self, batch, batch_idx):
'''Inference with zero shot setting'''
# Forward of query image encoder
img_feat_q, patch_feat_q, loss_mae, pred_mae, mask_mae, pred_feat = self.img_encoder_q(batch["imgs"])
# Use classification token instead of averaged patch tokens
if self.hparams.mgca_encoder:
img_emb_q = self.img_encoder_q.global_embed(img_feat_q)
else:
img_emb_q = self.img_encoder_q.global_embed(pred_feat.mean(dim=1))
img_emb_q = F.normalize(img_emb_q, dim=-1)
# Forward of query text encoder
# N x CLS x S
bsz = img_emb_q.size(0) # N x C
batch_scores = []
fixed_caption_ids = batch["caption_ids"][0] # 14 x S, get rid of batch dim
fixed_attention_mask = batch["attention_mask"][0]
fixed_token_type_ids = batch["token_type_ids"][0]
for idx in range(bsz):
if self.zero_shot_text_feats is None:
report_feat_q_full, word_feat_q_full, word_attn_q_full, sents_full = self.text_encoder_q(
fixed_caption_ids, fixed_attention_mask, fixed_token_type_ids)
report_emb_q = self.text_encoder_q.global_embed(report_feat_q_full)
report_emb_q = F.normalize(report_emb_q, dim=-1)
self.zero_shot_text_feats = report_emb_q
scores = img_emb_q[idx:idx+1].mm(self.zero_shot_text_feats.t()) # 1 x CLS
scores *= self.logit_scale.exp()
batch_scores.append(scores.squeeze(0))
scores = torch.stack(batch_scores, dim=0) # N x CLS
########### image-text zero-shot cls loss ################
labels = batch["multi_hot_label"].type_as(self.zero_shot_text_feats) # N x CLS
# Image to text classification loss
loss0 = F.cross_entropy(scores, labels.argmax(dim=-1))
if self.hparams.devices > 1:
score_list = [torch.zeros_like(scores) for _ in range(dist.get_world_size())]
dist.all_gather(score_list, scores)
all_scores = torch.cat(score_list, dim=0)
label_list = [torch.zeros_like(labels) for _ in range(dist.get_world_size())]
dist.all_gather(label_list, labels)
all_labels = torch.cat(label_list, dim=0)
else:
all_scores = scores
all_labels = labels
self.confmat.update(
torch.argmax(all_scores, dim=-1), all_labels.argmax(dim=-1))
all_scores = all_scores.detach().to(torch.float32)
all_scores = torch.softmax(all_scores, dim=-1).cpu().numpy()
all_labels = all_labels.detach().to(torch.float32).cpu().numpy()
if self.all_scores is None:
self.all_scores = all_scores
else:
self.all_scores = np.concatenate([self.all_scores, all_scores], axis=0)
if self.all_labels is None:
self.all_labels = all_labels
else:
self.all_labels = np.concatenate([self.all_labels, all_labels], axis=0)
# compute retrieval accuracy
i2t_acc1 = self.precision_at_k(scores, labels.argmax(dim=-1), top_k=(1,))[0]
return loss0, i2t_acc1, 0.
def visual_forward(self, batch, batch_idx, split="train"):
# Forward of query image encoder
img_feat_q, patch_feat_q, loss_mae, pred_mae, mask_mae, pred_feat = self.img_encoder_q(batch["imgs"])
# Use classification token instead of averaged patch tokens
img_emb_q = self.img_encoder_q.global_embed(pred_feat.mean(dim=1))
img_emb_q = F.normalize(img_emb_q, dim=-1)
########### Classification loss ################
labels = batch["multi_hot_label"].type_as(img_emb_q) # N x CLS
# Image classification loss
loss0 = F.cross_entropy(img_emb_q, labels.argmax(dim=-1))
# compute retrieval accuracy
i2t_acc1, i2t_acc5 = self.precision_at_k(img_emb_q, labels.argmax(dim=-1), top_k=(1, 2))
if split == 'test':
if self.hparams.devices > 1:
img_emb_q_list = [torch.zeros_like(img_emb_q) for _ in range(dist.get_world_size())]
dist.all_gather(img_emb_q_list, img_emb_q)
all_img_emb_qs = torch.cat(img_emb_q_list, dim=0)
label_list = [torch.zeros_like(labels) for _ in range(dist.get_world_size())]
dist.all_gather(label_list, labels)
all_labels = torch.cat(label_list, dim=0)
else:
all_img_emb_qs = img_emb_q
all_labels = labels
self.confmat.update(
torch.argmax(all_img_emb_qs, dim=-1), all_labels.argmax(dim=-1))
all_img_emb_qs = all_img_emb_qs.detach().to(torch.float32)
all_img_emb_qs = torch.softmax(all_img_emb_qs, dim=-1).cpu().numpy()
all_labels = all_labels.detach().to(torch.float32).cpu().numpy()
if self.all_scores is None:
self.all_scores = all_img_emb_qs
else:
self.all_scores = np.concatenate([self.all_scores, all_img_emb_qs], axis=0)
if self.all_labels is None:
self.all_labels = all_labels
else:
self.all_labels = np.concatenate([self.all_labels, all_labels], axis=0)
return loss0, i2t_acc1, i2t_acc5
def training_step(self, batch, batch_idx):
if self.hparams.img_cls_ft:
loss_c, acc1, acc5 = self.visual_forward(
batch, batch_idx, "train")
else:
loss_c, acc1, acc5 = self(
batch, batch_idx, "train")
loss = loss_c
log = {
"train_loss": loss,
"train_loss_c": loss_c,
"train_acc1": acc1,
"train_acc5": acc5
}
self.log_dict(log, batch_size=self.hparams.batch_size,
sync_dist=True, prog_bar=True)
return loss
def validation_step(self, batch, batch_idx):
if self.hparams.img_cls_ft:
loss_c, acc1, acc5 = self.visual_forward(
batch, batch_idx, "valid")
else:
loss_c, acc1, acc5 = self(
batch, batch_idx, "valid")
loss = loss_c
log = {
"val_loss": loss,
"val_loss_c": loss_c,
"val_acc1": acc1,
"val_acc5": acc5
}
self.log_dict(log, batch_size=self.hparams.batch_size,
sync_dist=True, prog_bar=True)
return loss
def test_step(self, batch, batch_idx):
if self.hparams.img_cls_ft:
loss_c, acc1, acc5 = self.visual_forward(
batch, batch_idx, "test")
else:
loss_c, acc1, acc5 = self.zero_shot_inference(
batch, batch_idx)
loss = loss_c
log = {
"test_loss": loss,
"test_loss_c": loss_c,
"test_acc1": acc1,
"test_acc5": acc5
}
self.log_dict(log, batch_size=self.hparams.batch_size,
sync_dist=True, prog_bar=True)
return loss
def on_test_epoch_end(self):
# Calculate the confusion matrix using the accumulated predictions and targets
conf_matrix = self.confmat.compute()
print("### Confusion Matrix:\n", conf_matrix)
# Calculate the accuracy using the accumulated predictions and targets
acc = 100 * accuracy_score(np.argmax(self.all_labels, -1), np.argmax(self.all_scores, -1))
try:
if self.hparams.num_classes == 2:
auc = 100 * roc_auc_score(self.all_labels, self.all_scores)
else:
auc = 100 * roc_auc_score(np.argmax(self.all_labels, -1), self.all_scores, multi_class="ovr")
except Exception as e:
print("### Warning: AUC calculation failed with error:", e)
auc = 0
print("### Accuracy: {:.4f}".format(acc))
print("### AUC: {:.4f}".format(auc))
# Reset metrics for the next test run
self.confmat.reset()
self.all_scores = None
self.all_labels = None
@staticmethod
def precision_at_k(output: torch.Tensor, target: torch.Tensor, top_k=(1,)):
''' Compute the accuracy over the k top predictions for the specified values of k'''
with torch.no_grad():
maxk = max(top_k)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in top_k:
correct_k = correct[:k].contiguous(
).view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def configure_optimizers(self):
parameters = lrd.param_groups_lrd_moco(self, self.hparams.weight_decay, no_weight_decay_list=[],
lr_layer_wise="2e-5,2e-5,2e-5")
debugc = 1
optimizer = torch.optim.AdamW(
parameters,
self.hparams.learning_rate,
betas=(self.hparams.momentum, 0.999),
weight_decay=self.hparams.weight_decay
)
lr_scheduler = CosineAnnealingWarmupRestarts(
optimizer,
first_cycle_steps=self.training_steps,
cycle_mult=1.0,
max_lr=self.hparams.learning_rate,
min_lr=1e-8,
warmup_steps=int(self.training_steps * 0.4)
)
scheduler = {
"scheduler": lr_scheduler,
"interval": "step",
"frequency": 1
}
return {"optimizer": optimizer, "lr_scheduler": scheduler}
def on_after_backward(self) -> None:
pass
# print("on_after_backward enter")
# for name, p in self.named_parameters():
# if p.grad is None and p.requires_grad:
# print(name)
# print("on_after_backward exit")
@staticmethod
def add_model_specific_args(parent_parser):
pass
@staticmethod
def _use_ddp_or_dpp2(trainer: Trainer) -> bool:
if trainer:
return isinstance(trainer.training_type_plugin, DDPStrategy)
else:
return torch.distributed.is_initialized()
@staticmethod
def num_training_steps(trainer, dm) -> int:
"""Total training steps inferred from datamodule and devices."""
dataset = dm.train_dataloader()
dataset_size = len(dataset)
num_devices = trainer.num_devices
effective_batch_size = trainer.accumulate_grad_batches * num_devices
return trainer.max_steps