MCPcopy Create free account
hub / github.com/apache/tvm / _multibox_ref_numpy

Function _multibox_ref_numpy

tests/python/relax/test_op_vision.py:1492–1534  ·  view source on GitHub ↗

Numpy reference aligned with ``topi.vision.multibox_transform_loc``.

(
    cls_pred, loc_pred, anchor, variances, clip=False, threshold=0.0, keep_background=True
)

Source from the content-addressed store, hash-verified

1490
1491
1492def _multibox_ref_numpy(
1493 cls_pred, loc_pred, anchor, variances, clip=False, threshold=0.0, keep_background=True
1494):
1495 """Numpy reference aligned with ``topi.vision.multibox_transform_loc``."""
1496
1497 def _softmax(x, axis):
1498 x_max = np.max(x, axis=axis, keepdims=True)
1499 exp = np.exp(x - x_max)
1500 return exp / np.sum(exp, axis=axis, keepdims=True)
1501
1502 B, C, N = cls_pred.shape
1503 loc = loc_pred.reshape(B, N, 4)
1504 scores = _softmax(cls_pred.astype("float64"), axis=1).astype(np.float32)
1505 if threshold > 0.0:
1506 scores = np.where(scores >= threshold, scores, 0.0).astype(np.float32)
1507 if not keep_background:
1508 scores = scores.copy()
1509 scores[:, 0, :] = 0.0
1510 vx, vy, vw, vh = variances
1511 boxes = np.zeros((B, N, 4), dtype=np.float32)
1512 for b in range(B):
1513 for a in range(N):
1514 left, top, right, bottom = anchor[0, a, :]
1515 ay = (top + bottom) * 0.5
1516 ax = (left + right) * 0.5
1517 ah = bottom - top
1518 aw = right - left
1519 ex, ey, ew, eh = loc[b, a, :]
1520 ycenter = ey * vy * ah + ay
1521 xcenter = ex * vx * aw + ax
1522 half_h = 0.5 * np.exp(eh * vh) * ah
1523 half_w = 0.5 * np.exp(ew * vw) * aw
1524 ymin = ycenter - half_h
1525 xmin = xcenter - half_w
1526 ymax = ycenter + half_h
1527 xmax = xcenter + half_w
1528 if clip:
1529 ymin = np.clip(ymin, 0.0, 1.0)
1530 xmin = np.clip(xmin, 0.0, 1.0)
1531 ymax = np.clip(ymax, 0.0, 1.0)
1532 xmax = np.clip(xmax, 0.0, 1.0)
1533 boxes[b, a, :] = (ymin, xmin, ymax, xmax)
1534 return boxes, scores
1535
1536
1537@pytest.mark.skipif(not env.has_llvm(), reason="need llvm")

Calls 7

_softmaxFunction · 0.85
whereMethod · 0.80
reshapeMethod · 0.45
astypeMethod · 0.45
copyMethod · 0.45
zerosMethod · 0.45
expMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…