Encode the variances from the priorbox layers into the ground truth boxes we have matched (based on jaccard overlap) with the prior boxes. Args: matched: (tensor) Coords of ground truth for each prior in point-form Shape: [num_priors, 4]. priors: (tensor) Prior bo
(matched, priors, variances)
| 113 | |
| 114 | |
| 115 | def encode(matched, priors, variances): |
| 116 | """Encode the variances from the priorbox layers into the ground truth boxes |
| 117 | we have matched (based on jaccard overlap) with the prior boxes. |
| 118 | Args: |
| 119 | matched: (tensor) Coords of ground truth for each prior in point-form |
| 120 | Shape: [num_priors, 4]. |
| 121 | priors: (tensor) Prior boxes in center-offset form |
| 122 | Shape: [num_priors,4]. |
| 123 | variances: (list[float]) Variances of priorboxes |
| 124 | Return: |
| 125 | encoded boxes (tensor), Shape: [num_priors, 4] |
| 126 | """ |
| 127 | |
| 128 | # dist b/t match center and prior's center |
| 129 | g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2] |
| 130 | # encode variance |
| 131 | g_cxcy /= (variances[0] * priors[:, 2:]) |
| 132 | # match wh / prior wh |
| 133 | g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:] |
| 134 | g_wh = torch.log(g_wh) / variances[1] |
| 135 | # return target for smooth_l1_loss |
| 136 | return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4] |
| 137 | |
| 138 | |
| 139 | # Adapted from https://github.com/Hakuyume/chainer-ssd |