In Masterarbeit:"Anomalie-Detektion in Zellbildern zur Anwendung der Leukämieerkennung" verwendete CSI Methode.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

contrastive_loss.py 2.6KB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import torch
  2. import torch.distributed as dist
  3. import diffdist.functional as distops
  4. def get_similarity_matrix(outputs, chunk=2, multi_gpu=False):
  5. '''
  6. Compute similarity matrix
  7. - outputs: (B', d) tensor for B' = B * chunk
  8. - sim_matrix: (B', B') tensor
  9. '''
  10. if multi_gpu:
  11. outputs_gathered = []
  12. for out in outputs.chunk(chunk):
  13. gather_t = [torch.empty_like(out) for _ in range(dist.get_world_size())]
  14. gather_t = torch.cat(distops.all_gather(gather_t, out))
  15. outputs_gathered.append(gather_t)
  16. outputs = torch.cat(outputs_gathered)
  17. sim_matrix = torch.mm(outputs, outputs.t()) # (B', d), (d, B') -> (B', B')
  18. return sim_matrix
  19. def NT_xent(sim_matrix, temperature=0.5, chunk=2, eps=1e-8):
  20. '''
  21. Compute NT_xent loss
  22. - sim_matrix: (B', B') tensor for B' = B * chunk (first 2B are pos samples)
  23. '''
  24. device = sim_matrix.device
  25. B = sim_matrix.size(0) // chunk # B = B' / chunk
  26. eye = torch.eye(B * chunk).to(device) # (B', B')
  27. sim_matrix = torch.exp(sim_matrix / temperature) * (1 - eye) # remove diagonal
  28. denom = torch.sum(sim_matrix, dim=1, keepdim=True)
  29. sim_matrix = -torch.log(sim_matrix / (denom + eps) + eps) # loss matrix
  30. loss = torch.sum(sim_matrix[:B, B:].diag() + sim_matrix[B:, :B].diag()) / (2 * B)
  31. return loss
  32. def Supervised_NT_xent(sim_matrix, labels, temperature=0.5, chunk=2, eps=1e-8, multi_gpu=False):
  33. '''
  34. Compute NT_xent loss
  35. - sim_matrix: (B', B') tensor for B' = B * chunk (first 2B are pos samples)
  36. '''
  37. device = sim_matrix.device
  38. if multi_gpu:
  39. gather_t = [torch.empty_like(labels) for _ in range(dist.get_world_size())]
  40. labels = torch.cat(distops.all_gather(gather_t, labels))
  41. labels = labels.repeat(2)
  42. logits_max, _ = torch.max(sim_matrix, dim=1, keepdim=True)
  43. sim_matrix = sim_matrix - logits_max.detach()
  44. B = sim_matrix.size(0) // chunk # B = B' / chunk
  45. eye = torch.eye(B * chunk).to(device) # (B', B')
  46. sim_matrix = torch.exp(sim_matrix / temperature) * (1 - eye) # remove diagonal
  47. denom = torch.sum(sim_matrix, dim=1, keepdim=True)
  48. sim_matrix = -torch.log(sim_matrix / (denom + eps) + eps) # loss matrix
  49. labels = labels.contiguous().view(-1, 1)
  50. Mask = torch.eq(labels, labels.t()).float().to(device)
  51. #Mask = eye * torch.stack([labels == labels[i] for i in range(labels.size(0))]).float().to(device)
  52. Mask = Mask / (Mask.sum(dim=1, keepdim=True) + eps)
  53. loss = torch.sum(Mask * sim_matrix) / (2 * B)
  54. return loss