(cfg, image_features_map,adapter,batch_size,beta)
| 24 | return T |
| 25 | |
| 26 | def 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 | |
| 61 | def cls_acc(output, target, topk=1): |
no test coverage detected