dn_compoments.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 bbox_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 bbox_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. max_gt_num = max(num_gts)
  26. if max_gt_num == 0:
  27. return None, None, None, None
  28. num_group = num_denoising // max_gt_num
  29. num_group = 1 if num_group == 0 else num_group
  30. # pad gt to max_num of a batch
  31. bs = len(targets)
  32. # [bs, max_gt_num]
  33. input_query_class = torch.full([bs, max_gt_num], num_classes).long()
  34. # [bs, max_gt_num, 4]
  35. input_query_bbox = torch.zeros([bs, max_gt_num, 4])
  36. pad_gt_mask = torch.zeros([bs, max_gt_num])
  37. for i in range(bs):
  38. num_gt = num_gts[i]
  39. if num_gt > 0:
  40. input_query_class[i, :num_gt] = targets[i]["labels"].squeeze(-1)
  41. input_query_bbox[i, :num_gt] = targets[i]["boxes"]
  42. pad_gt_mask[i, :num_gt] = 1
  43. # each group has positive and negative queries.
  44. input_query_class = input_query_class.repeat(1, 2 * num_group) # [bs, 2*num_denoising], num_denoising = 2 * num_group * max_gt_num
  45. input_query_bbox = input_query_bbox.repeat(1, 2 * num_group, 1) # [bs, 2*num_denoising, 4]
  46. pad_gt_mask = pad_gt_mask.repeat(1, 2 * num_group)
  47. # positive and negative mask
  48. negative_gt_mask = torch.zeros([bs, max_gt_num * 2, 1])
  49. negative_gt_mask[:, max_gt_num:] = 1
  50. negative_gt_mask = negative_gt_mask.repeat(1, num_group, 1)
  51. positive_gt_mask = 1 - negative_gt_mask
  52. # contrastive denoising training positive index
  53. positive_gt_mask = positive_gt_mask.squeeze(-1) * pad_gt_mask
  54. dn_positive_idx = torch.nonzero(positive_gt_mask)[:, 1]
  55. dn_positive_idx = torch.split(dn_positive_idx, [n * num_group for n in num_gts])
  56. # total denoising queries
  57. num_denoising = int(max_gt_num * 2 * num_group) # num_denoising *= 2
  58. if label_noise_ratio > 0:
  59. input_query_class = input_query_class.flatten() # [bs * num_denoising]
  60. pad_gt_mask = pad_gt_mask.flatten()
  61. # half of bbox prob
  62. mask = torch.rand(input_query_class.shape) < (label_noise_ratio * 0.5)
  63. chosen_idx = torch.nonzero(mask * pad_gt_mask).squeeze(-1)
  64. # randomly put a new one here
  65. new_label = torch.randint_like(
  66. chosen_idx, 0, num_classes, dtype=input_query_class.dtype)
  67. # [bs * num_denoising]
  68. input_query_class = torch.scatter(input_query_class, 0, chosen_idx, new_label)
  69. # input_query_class.scatter_(chosen_idx, new_label)
  70. # [bs * num_denoising] -> # [bs, num_denoising]
  71. input_query_class = input_query_class.reshape(bs, num_denoising)
  72. pad_gt_mask = pad_gt_mask.reshape(bs, num_denoising)
  73. if box_noise_scale > 0:
  74. known_bbox = bbox_cxcywh_to_xyxy(input_query_bbox)
  75. diff = torch.tile(input_query_bbox[..., 2:] * 0.5,
  76. [1, 1, 2]) * box_noise_scale
  77. rand_sign = torch.randint_like(input_query_bbox, 0, 2) * 2.0 - 1.0
  78. rand_part = torch.rand(input_query_bbox.shape)
  79. rand_part = (rand_part + 1.0) * negative_gt_mask + rand_part * (
  80. 1 - negative_gt_mask)
  81. rand_part *= rand_sign
  82. known_bbox += rand_part * diff
  83. known_bbox.clamp_(min=0.0, max=1.0)
  84. input_query_bbox = bbox_xyxy_to_cxcywh(known_bbox)
  85. input_query_bbox = inverse_sigmoid(input_query_bbox)
  86. # [num_classes + 1, hidden_dim]
  87. class_embed = torch.cat([class_embed, torch.zeros([1, class_embed.shape[-1]])])
  88. # input_query_class = paddle.gather(class_embed, input_query_class.flatten(), axis=0)
  89. # input_query_class: [bs, num_denoising] -> [bs*num_denoising, hidden_dim]
  90. input_query_class = torch.torch.index_select(class_embed, 0, input_query_class.flatten())
  91. # [bs*num_denoising, hidden_dim] -> [bs, num_denoising, hidden_dim]
  92. input_query_class = input_query_class.reshape(bs, num_denoising, -1)
  93. tgt_size = num_denoising + num_queries
  94. attn_mask = torch.ones([tgt_size, tgt_size]) < 0
  95. # match query cannot see the reconstruction
  96. attn_mask[num_denoising:, :num_denoising] = True
  97. # reconstruct cannot see each other
  98. for i in range(num_group):
  99. if i == 0:
  100. attn_mask[max_gt_num * 2 * i:max_gt_num * 2 * (i + 1), max_gt_num *
  101. 2 * (i + 1):num_denoising] = True
  102. if i == num_group - 1:
  103. attn_mask[max_gt_num * 2 * i:max_gt_num * 2 * (i + 1), :max_gt_num *
  104. i * 2] = True
  105. else:
  106. attn_mask[max_gt_num * 2 * i:max_gt_num * 2 * (i + 1), max_gt_num *
  107. 2 * (i + 1):num_denoising] = True
  108. attn_mask[max_gt_num * 2 * i:max_gt_num * 2 * (i + 1), :max_gt_num *
  109. 2 * i] = True
  110. attn_mask = ~attn_mask
  111. dn_meta = {
  112. "dn_positive_idx": dn_positive_idx,
  113. "dn_num_group": num_group,
  114. "dn_num_split": [num_denoising, num_queries]
  115. }
  116. return input_query_class, input_query_bbox, attn_mask, dn_meta