MCPcopy Index your code
hub / github.com/CHENGY12/PLOT / tip_plot

Function tip_plot

plot-adapter/utils.py:26–58  ·  view source on GitHub ↗
(cfg, image_features_map,adapter,batch_size,beta)

Source from the content-addressed store, hash-verified

24 return T
25
26def tip_plot(cfg, image_features_map,adapter,batch_size,beta):
27
28 n_cls = len(cfg['classnames'])
29 N= cfg['N']
30 M = cfg['M']
31 k_shot = cfg['shots']
32 affinity = adapter(image_features_map)
33 affinity = affinity.view(M,batch_size,N,n_cls*k_shot) # M x B x N x (n_cls*k_shot)
34 affinity = affinity.permute(0,2,1,3)
35
36 feat_exp = ((-1) * (beta - beta * affinity)).exp()
37 feat_exp = F.normalize(feat_exp, dim=-1)
38 sim = feat_exp @ (cfg['cache_values'])
39 sim = sim.view(M,N,batch_size*n_cls)
40 sim = sim.permute(2,0,1)
41 wdist = 1.0 - sim
42
43 xx=torch.zeros(batch_size*n_cls, M, dtype=sim.dtype, device=sim.device).fill_(1. / M)
44 yy=torch.zeros(batch_size*n_cls, N, dtype=sim.dtype, device=sim.device).fill_(1. / N)
45
46 with torch.no_grad():
47 KK = torch.exp(-wdist / cfg['thre'])
48 T = Sinkhorn(KK,xx,yy)
49 try:
50 torch.isnan(T).any()
51 except None:
52 print('There is none value in your tensor, please try to adjust #thre and #eps to align data.')
53
54 sim_op = torch.sum(T * sim, dim=(1, 2))
55 sim_op = sim_op.contiguous().view(batch_size,n_cls)
56 plot_logits = 100. * sim_op
57
58 return plot_logits
59
60
61def cls_acc(output, target, topk=1):

Callers 4

search_hpFunction · 0.85
search_hp_imagenetFunction · 0.85
run_tip_adapter_FFunction · 0.85
run_tip_adapter_FFunction · 0.85

Calls 1

SinkhornFunction · 0.85

Tested by

no test coverage detected