|
|
| import torch |
| import torch.nn.functional as F |
| import numpy as np |
| def contrastive_correlation_loss( |
| vfm_feats, |
| clip_feats, |
| samples = 20, |
| shift = 0.12): |
| """ |
| input: |
| vfm_self_attn_feats-> [bs,c,h,w] |
| clip_self_attn_feats->[bs,c,h,w] |
| return |
| pos_intra_loss |
| """ |
| coord_shape = [vfm_feats.shape[0], samples, samples, 2] |
| coords = torch.rand(coord_shape, device=vfm_feats.device) * 2 - 1 |
| sampled_vfm_feats = sample(vfm_feats, coords) |
| sampled_clip_feats = sample(clip_feats, coords) |
| with torch.no_grad(): |
| |
| normd_sampled_vfm_feats=norm(sampled_vfm_feats) |
| vfm_self_attn = tensor_correlation(normd_sampled_vfm_feats,normd_sampled_vfm_feats) |
| old_mean = vfm_self_attn.mean() |
| vfm_self_attn -= vfm_self_attn.mean([3, 4], keepdim=True) |
| vfm_self_attn = vfm_self_attn - vfm_self_attn.mean() + old_mean |
| normd_sampled_clip_feats=norm(sampled_clip_feats) |
| clip_self_attn = tensor_correlation(normd_sampled_clip_feats, normd_sampled_clip_feats) |
| min_val = 0.0 |
| loss = - clip_self_attn.clamp(min_val) * (vfm_self_attn - shift) |
| return loss.mean() |
|
|
| def sample(t: torch.Tensor, coords: torch.Tensor): |
| return F.grid_sample(t, coords.permute(0, 2, 1, 3), padding_mode='border', align_corners=True) |
|
|
| def tensor_correlation(a, b): |
| return torch.einsum("nchw,ncij->nhwij", a, b) |
|
|
| def norm(t): |
| return F.normalize(t, dim=1, eps=1e-10) |
|
|
| def mask2box(mask): |
| ys, xs = np.where(mask) |
| y0, y1 = ys.min(), ys.max() |
| x0, x1 = xs.min(), xs.max() |
| return x0, y0, x1, y1 |
|
|
| def get_freeze_keys(args): |
| if args.model=="EVA02-CLIP-B-16": |
| if args.mode=="qk_vfm_distill": |
| freeze_keys=ViTB_16_QK_Distill_keys |
| elif args.mode=="ss_vfm_distill": |
| freeze_keys=ViTB_16_SS_Distill_keys |
| else: |
| freeze_keys=ViTB_16_Only_V_Distill_keys |
| else: |
| if args.mode=="qk_vfm_distill": |
| freeze_keys=ViTL_14_QK_Distill_keys |
| elif args.mode=="ss_vfm_distill": |
| freeze_keys=ViTL_14_SS_Distill_keys |
| else: |
| freeze_keys=ViTL_14_Only_V_Distill_keys |
| return freeze_keys |
|
|
| BASE_ViTB_16_freeze_keys=[ |
| 'logit_scale', |
| 'visual.blocks.11.norm2.weight', |
| 'visual.blocks.11.norm2.bias', |
| 'visual.blocks.11.mlp.w1.weight', |
| 'visual.blocks.11.mlp.w1.bias', |
| 'visual.blocks.11.mlp.w2.weight', |
| 'visual.blocks.11.mlp.w2.bias', |
| 'visual.blocks.11.mlp.w3.weight', |
| 'visual.blocks.11.mlp.w3.bias', |
| 'visual.blocks.11.mlp.ffn_ln.weight', |
| 'visual.blocks.11.mlp.ffn_ln.bias'] |
| BASE_ViTL_14_freeze_keys=[ |
| 'logit_scale', |
| 'visual.blocks.23.norm2.weight', |
| 'visual.blocks.23.norm2.bias', |
| 'visual.blocks.23.mlp.w1.weight', |
| 'visual.blocks.23.mlp.w1.bias', |
| 'visual.blocks.23.mlp.w2.weight', |
| 'visual.blocks.23.mlp.w2.bias', |
| 'visual.blocks.23.mlp.w3.weight', |
| 'visual.blocks.23.mlp.w3.bias', |
| 'visual.blocks.23.mlp.ffn_ln.weight', |
| 'visual.blocks.23.mlp.ffn_ln.bias'] |
|
|
| ViTB_16_SS_Distill_keys=['visual.blocks.11.attn.k_proj.weight'] + BASE_ViTB_16_freeze_keys |
| ViTL_14_SS_Distill_keys=['visual.blocks.23.attn.k_proj.weight'] + BASE_ViTL_14_freeze_keys |
|
|
| ViTB_16_Only_V_Distill_keys=['visual.blocks.11.attn.q_bias', |
| 'visual.blocks.11.attn.q_proj.weight', |
| 'visual.blocks.11.attn.k_proj.weight',] + BASE_ViTB_16_freeze_keys |
|
|
| ViTL_14_Only_V_Distill_keys=['visual.blocks.23.attn.q_bias', |
| 'visual.blocks.23.attn.q_proj.weight', |
| 'visual.blocks.23.attn.k_proj.weight',] + BASE_ViTL_14_freeze_keys |
|
|
| ViTB_16_QK_Distill_keys = BASE_ViTB_16_freeze_keys |
| ViTL_14_QK_Distill_keys = BASE_ViTL_14_freeze_keys |