File size: 4,094 Bytes
eba0a14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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():
        # Comes straight from backbone which is currently frozen. this saves mem.
        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