dn_compoments.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import torch
  2. def inverse_sigmoid(x, eps=1e-5):
  3. x = x.clamp(min=0., max=1.)
  4. return torch.log(x.clamp(min=eps) / (1 - x).clamp(min=eps))
  5. def box_cxcywh_to_xyxy(x):
  6. x_c, y_c, w, h = x.unbind(-1)
  7. b = [(x_c - 0.5 * w), (y_c - 0.5 * h),
  8. (x_c + 0.5 * w), (y_c + 0.5 * h)]
  9. return torch.stack(b, dim=-1)
  10. def box_xyxy_to_cxcywh(x):
  11. x0, y0, x1, y1 = x.unbind(-1)
  12. b = [(x0 + x1) / 2, (y0 + y1) / 2,
  13. (x1 - x0), (y1 - y0)]
  14. return torch.stack(b, dim=-1)
  15. def get_contrastive_denoising_training_group(targets,
  16. num_classes,
  17. num_queries,
  18. class_embed,
  19. num_denoising=100,
  20. label_noise_ratio=0.5,
  21. box_noise_scale=1.0,):
  22. if num_denoising <= 0:
  23. return None, None, None, None
  24. num_gts = [len(t['labels']) for t in targets]
  25. device = targets[0]['labels'].device
  26. max_gt_num = max(num_gts)
  27. if max_gt_num == 0:
  28. return None, None, None, None
  29. num_group = num_denoising // max_gt_num
  30. num_group = 1 if num_group == 0 else num_group
  31. # pad gt to max_num of a batch
  32. bs = len(num_gts)
  33. input_query_class = torch.full([bs, max_gt_num], num_classes, dtype=torch.int32, device=device)
  34. input_query_bbox = torch.zeros([bs, max_gt_num, 4], device=device)
  35. pad_gt_mask = torch.zeros([bs, max_gt_num], dtype=torch.bool, device=device)
  36. for i in range(bs):
  37. num_gt = num_gts[i]
  38. if num_gt > 0:
  39. input_query_class[i, :num_gt] = targets[i]['labels']
  40. input_query_bbox[i, :num_gt] = targets[i]['boxes']
  41. pad_gt_mask[i, :num_gt] = 1
  42. # each group has positive and negative queries.
  43. input_query_class = input_query_class.tile([1, 2 * num_group])
  44. input_query_bbox = input_query_bbox.tile([1, 2 * num_group, 1])
  45. pad_gt_mask = pad_gt_mask.tile([1, 2 * num_group])
  46. # positive and negative mask
  47. negative_gt_mask = torch.zeros([bs, max_gt_num * 2, 1], device=device)
  48. negative_gt_mask[:, max_gt_num:] = 1
  49. negative_gt_mask = negative_gt_mask.tile([1, num_group, 1])
  50. positive_gt_mask = 1 - negative_gt_mask
  51. # contrastive denoising training positive index
  52. positive_gt_mask = positive_gt_mask.squeeze(-1) * pad_gt_mask
  53. dn_positive_idx = torch.nonzero(positive_gt_mask)[:, 1]
  54. dn_positive_idx = torch.split(dn_positive_idx, [n * num_group for n in num_gts])
  55. # total denoising queries
  56. num_denoising = int(max_gt_num * 2 * num_group)
  57. if label_noise_ratio > 0:
  58. mask = torch.rand_like(input_query_class, dtype=torch.float) < (label_noise_ratio * 0.5)
  59. # randomly put a new one here
  60. new_label = torch.randint_like(mask, 0, num_classes, dtype=input_query_class.dtype)
  61. input_query_class = torch.where(mask & pad_gt_mask, new_label, input_query_class)
  62. if box_noise_scale > 0:
  63. known_bbox = box_cxcywh_to_xyxy(input_query_bbox)
  64. diff = torch.tile(input_query_bbox[..., 2:] * 0.5, [1, 1, 2]) * box_noise_scale
  65. rand_sign = torch.randint_like(input_query_bbox, 0, 2) * 2.0 - 1.0
  66. rand_part = torch.rand_like(input_query_bbox)
  67. rand_part = (rand_part + 1.0) * negative_gt_mask + rand_part * (1 - negative_gt_mask)
  68. rand_part *= rand_sign
  69. known_bbox += rand_part * diff
  70. known_bbox.clip_(min=0.0, max=1.0)
  71. input_query_bbox = box_xyxy_to_cxcywh(known_bbox)
  72. input_query_bbox = inverse_sigmoid(input_query_bbox)
  73. input_query_class = class_embed(input_query_class)
  74. tgt_size = num_denoising + num_queries
  75. # attn_mask = torch.ones([tgt_size, tgt_size], device=device) < 0
  76. attn_mask = torch.full([tgt_size, tgt_size], False, dtype=torch.bool, device=device)
  77. # match query cannot see the reconstruction
  78. attn_mask[num_denoising:, :num_denoising] = True
  79. # reconstruct cannot see each other
  80. for i in range(num_group):
  81. if i == 0:
  82. attn_mask[max_gt_num * 2 * i: max_gt_num * 2 * (i + 1), max_gt_num * 2 * (i + 1): num_denoising] = True
  83. if i == num_group - 1:
  84. attn_mask[max_gt_num * 2 * i: max_gt_num * 2 * (i + 1), :max_gt_num * i * 2] = True
  85. else:
  86. attn_mask[max_gt_num * 2 * i: max_gt_num * 2 * (i + 1), max_gt_num * 2 * (i + 1): num_denoising] = True
  87. attn_mask[max_gt_num * 2 * i: max_gt_num * 2 * (i + 1), :max_gt_num * 2 * i] = True
  88. dn_meta = {
  89. "dn_positive_idx": dn_positive_idx,
  90. "dn_num_group": num_group,
  91. "dn_num_split": [num_denoising, num_queries]
  92. }
  93. return input_query_class, input_query_bbox, attn_mask, dn_meta