|
| 1 | +""" |
| 2 | +MXNet implementation of SMOT: Single-Shot Multi Object Tracking |
| 3 | +https://arxiv.org/abs/2010.16031 |
| 4 | +""" |
| 5 | +from mxnet import gluon |
| 6 | +from gluoncv.nn.bbox import BBoxCenterToCorner |
| 7 | + |
| 8 | + |
| 9 | +class NormalizedLandmarkCenterDecoder(gluon.HybridBlock): |
| 10 | + """ |
| 11 | + Decode bounding boxes training target with normalized center offsets. |
| 12 | + This decoder must cooperate with NormalizedBoxCenterEncoder of same `stds` |
| 13 | + in order to get properly reconstructed bounding boxes. |
| 14 | +
|
| 15 | + Returned bounding boxes are using corner type: `x_{min}, y_{min}, x_{max}, y_{max}`. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + stds : array-like of size 4 |
| 20 | + Std value to be divided from encoded values, default is (0.1, 0.1, 0.2, 0.2). |
| 21 | + means : array-like of size 4 |
| 22 | + Mean value to be subtracted from encoded values, default is (0., 0., 0., 0.). |
| 23 | + clip: float, default is None |
| 24 | + If given, bounding box target will be clipped to this value. |
| 25 | +
|
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, stds=(0.1, 0.1, 0.2, 0.2), means=(0., 0., 0., 0.), |
| 29 | + convert_anchor=True): |
| 30 | + super(NormalizedLandmarkCenterDecoder, self).__init__() |
| 31 | + assert len(stds) == 4, "Box Encoder requires 4 std values." |
| 32 | + self._stds = stds |
| 33 | + self._means = means |
| 34 | + if convert_anchor: |
| 35 | + self.center_to_conner = BBoxCenterToCorner(split=True) |
| 36 | + else: |
| 37 | + self.center_to_conner = None |
| 38 | + |
| 39 | + def hybrid_forward(self, F, x, anchors): |
| 40 | + """center decoder forward""" |
| 41 | + if self.center_to_conner is not None: |
| 42 | + a = self.center_to_conner(anchors) |
| 43 | + else: |
| 44 | + a = anchors.split(axis=-1, num_outputs=4) |
| 45 | + ld = F.split(x, axis=-1, num_outputs=10) |
| 46 | + |
| 47 | + x0 = F.broadcast_add(F.broadcast_mul(ld[0] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 48 | + y0 = F.broadcast_add(F.broadcast_mul(ld[1] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 49 | + x1 = F.broadcast_add(F.broadcast_mul(ld[2] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 50 | + y1 = F.broadcast_add(F.broadcast_mul(ld[3] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 51 | + x2 = F.broadcast_add(F.broadcast_mul(ld[4] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 52 | + y2 = F.broadcast_add(F.broadcast_mul(ld[5] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 53 | + x3 = F.broadcast_add(F.broadcast_mul(ld[6] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 54 | + y3 = F.broadcast_add(F.broadcast_mul(ld[7] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 55 | + x4 = F.broadcast_add(F.broadcast_mul(ld[8] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 56 | + y4 = F.broadcast_add(F.broadcast_mul(ld[9] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 57 | + |
| 58 | + return F.concat(x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, dim=-1) |
| 59 | + |
| 60 | + |
| 61 | +class GeneralNormalizedKeyPointsDecoder(gluon.HybridBlock): |
| 62 | + """ |
| 63 | + Decode bounding boxes training target with normalized center offsets. |
| 64 | + This decoder must cooperate with NormalizedBoxCenterEncoder of same `stds` |
| 65 | + in order to get properly reconstructed bounding boxes. |
| 66 | +
|
| 67 | + Returned bounding boxes are using corner type: `x_{min}, y_{min}, x_{max}, y_{max}`. |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + stds : array-like of size 4 |
| 72 | + Std value to be divided from encoded values, default is (0.1, 0.1, 0.2, 0.2). |
| 73 | + means : array-like of size 4 |
| 74 | + Mean value to be subtracted from encoded values, default is (0., 0., 0., 0.). |
| 75 | + clip: float, default is None |
| 76 | + If given, bounding box target will be clipped to this value. |
| 77 | +
|
| 78 | + """ |
| 79 | + |
| 80 | + def __init__(self, num_points, stds=(0.2, 0.2), means=(0.5, 0.2), |
| 81 | + convert_anchor=True): |
| 82 | + super(GeneralNormalizedKeyPointsDecoder, self).__init__() |
| 83 | + assert len(stds) == 2, "Box Encoder requires 4 std values." |
| 84 | + self._stds = stds |
| 85 | + self._means = means |
| 86 | + self._size = num_points * 2 |
| 87 | + if convert_anchor: |
| 88 | + self.center_to_conner = BBoxCenterToCorner(split=True) |
| 89 | + else: |
| 90 | + self.center_to_conner = None |
| 91 | + |
| 92 | + def hybrid_forward(self, F, x, anchors): |
| 93 | + """key point decoder forward""" |
| 94 | + if self.center_to_conner is not None: |
| 95 | + a = self.center_to_conner(anchors) |
| 96 | + else: |
| 97 | + a = anchors.split(axis=-1, num_outputs=4) |
| 98 | + ld = F.split(x, axis=-1, num_outputs=self._size) |
| 99 | + |
| 100 | + outputs = [] |
| 101 | + for i in range(0, self._size, 2): |
| 102 | + x = F.broadcast_add(F.broadcast_mul(ld[i] * self._stds[0] + self._means[0], a[2] - a[0]), a[0]) |
| 103 | + y = F.broadcast_add(F.broadcast_mul(ld[i+1] * self._stds[1] + self._means[1], a[3] - a[1]), a[1]) |
| 104 | + outputs.extend([x, y]) |
| 105 | + |
| 106 | + return F.concat(*outputs, dim=-1) |
0 commit comments