|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | + |
| 5 | +from ..builder import LOSSES |
| 6 | + |
| 7 | + |
| 8 | +def ae_loss_per_image(tl_preds, br_preds, match): |
| 9 | + """Associative Embedding Loss in one image. |
| 10 | +
|
| 11 | + Associative Embedding Loss including two parts: pull loss and push loss. |
| 12 | + Pull loss makes embedding vectors from same object closer to each other. |
| 13 | + Push loss distinguish embedding vector from different objects, and makes |
| 14 | + the gap between them is large enough. |
| 15 | +
|
| 16 | + During computing, usually there are 3 cases: |
| 17 | + - no object in image: both pull loss and push loss will be 0. |
| 18 | + - one object in image: push loss will be 0 and pull loss is computed |
| 19 | + by the two corner of the only object. |
| 20 | + - more than one objects in image: pull loss is computed by corner pairs |
| 21 | + from each object, push loss is computed by each object with all |
| 22 | + other objects. We use confusion matrix with 0 in diagonal to |
| 23 | + compute the push loss. |
| 24 | +
|
| 25 | + Args: |
| 26 | + tl_preds (tensor): Embedding feature map of left-top corner. |
| 27 | + br_preds (tensor): Embedding feature map of bottim-right corner. |
| 28 | + match (list): Downsampled coordinates pair of each ground truth box. |
| 29 | + """ |
| 30 | + |
| 31 | + tl_list, br_list, me_list = [], [], [] |
| 32 | + if len(match) == 0: # no object in image |
| 33 | + pull_loss = tl_preds.sum()[None] * 0. |
| 34 | + push_loss = tl_preds.sum()[None] * 0. |
| 35 | + else: |
| 36 | + for m in match: |
| 37 | + [tl_y, tl_x], [br_y, br_x] = m |
| 38 | + tl_e = tl_preds[:, tl_y, tl_x].view(-1, 1) |
| 39 | + br_e = br_preds[:, br_y, br_x].view(-1, 1) |
| 40 | + tl_list.append(tl_e) |
| 41 | + br_list.append(br_e) |
| 42 | + me_list.append((tl_e + br_e) / 2.0) |
| 43 | + |
| 44 | + tl_list = torch.cat(tl_list) |
| 45 | + br_list = torch.cat(br_list) |
| 46 | + me_list = torch.cat(me_list) |
| 47 | + |
| 48 | + assert tl_list.size() == br_list.size() |
| 49 | + |
| 50 | + # N is object number in image, M is dimension of embedding vector |
| 51 | + N, M = tl_list.size() |
| 52 | + |
| 53 | + pull_loss = (tl_list - me_list).pow(2) + (br_list - me_list).pow(2) |
| 54 | + pull_loss = pull_loss.sum() / N |
| 55 | + |
| 56 | + margin = 1 # exp setting of CornerNet, details in section 3.3 of paper |
| 57 | + |
| 58 | + # confusion matrix of push loss |
| 59 | + conf_mat = me_list.expand((N, N, M)).permute(1, 0, 2) - me_list |
| 60 | + conf_weight = 1 - torch.eye(N).type_as(me_list) |
| 61 | + conf_mat = conf_weight * (margin - conf_mat.sum(-1).abs()) |
| 62 | + |
| 63 | + if N > 1: # more than one object in current image |
| 64 | + push_loss = F.relu(conf_mat).sum() / (N * (N - 1)) |
| 65 | + |
| 66 | + return pull_loss, push_loss |
| 67 | + |
| 68 | + |
| 69 | +@LOSSES.register_module() |
| 70 | +class AssociativeEmbeddingLoss(nn.Module): |
| 71 | + """Associative Embedding Loss. |
| 72 | +
|
| 73 | + More details can be found in |
| 74 | + `Associative Embedding <https://arxiv.org/abs/1611.05424>`_ and |
| 75 | + `CornerNet <https://arxiv.org/abs/1808.01244>`_ . |
| 76 | + Code is modified from `kp_utils.py <https://github.com/princeton-vl/CornerNet/blob/master/models/py_utils/kp_utils.py#L180>`_ # noqa: E501 |
| 77 | +
|
| 78 | + Args: |
| 79 | + pull_weight (float): Loss weight for corners from same object. |
| 80 | + push_weight (float): Loss weight for corners from different object. |
| 81 | + """ |
| 82 | + |
| 83 | + def __init__(self, pull_weight=0.25, push_weight=0.25): |
| 84 | + super(AssociativeEmbeddingLoss, self).__init__() |
| 85 | + self.pull_weight = pull_weight |
| 86 | + self.push_weight = push_weight |
| 87 | + |
| 88 | + def forward(self, pred, target, match): |
| 89 | + batch = pred.size(0) |
| 90 | + pull_all, push_all = 0.0, 0.0 |
| 91 | + for i in range(batch): |
| 92 | + pull, push = ae_loss_per_image(pred[i], target[i], match[i]) |
| 93 | + |
| 94 | + pull_all += self.pull_weight * pull |
| 95 | + push_all += self.push_weight * push |
| 96 | + |
| 97 | + return pull_all, push_all |
0 commit comments