def get_multibox_detection_output(np_cls_prob, np_loc_preds, np_anchors, batch_size, num_anchors, num_classes): import nnvm.symbol as sym cls_prob = sym.Variable("cls_prob") loc_preds = sym.Variable("loc_preds") anchors = sym.Variable("anchors") transform_loc_data, valid_count = sym.multibox_transform_loc( cls_prob=cls_prob, loc_pred=loc_preds, anchor=anchors) out = sym.nms(data=transform_loc_data, valid_count=valid_count) target = "llvm" dtype = "float32" ctx = tvm.cpu() graph, lib, _ = nnvm.compiler.build( out, target, { "cls_prob": (batch_size, num_classes, num_anchors), "loc_preds": (batch_size, num_anchors * 4), "anchors": (1, num_anchors, 4) }) m = graph_runtime.create(graph, lib, ctx) m.set_input(**{ "cls_prob": np_cls_prob, "loc_preds": np_loc_preds, "anchors": np_anchors }) m.run() _, out_shape = nnvm.compiler.graph_util.infer_shape(graph, shape={"data": dshape}) out = m.get_output(0, tvm.nd.empty( tuple(out_shape[0]), dtype)) # output of "mbox_conf_softmax", shape: (1, 21, 8732) return out
def test_nms(): dshape = (1, 5, 6) data = sym.Variable("data") valid_count = sym.Variable("valid_count", dtype="int32") nms_threshold = 0.7 force_suppress = True nms_topk = 2 out = sym.nms(data=data, valid_count=valid_count, nms_threshold=nms_threshold, force_suppress=force_suppress, nms_topk=nms_topk) np_data = np.array([[[0, 0.8, 1, 20, 25, 45], [1, 0.7, 30, 60, 50, 80], [0, 0.4, 4, 21, 19, 40], [2, 0.9, 35, 61, 52, 79], [1, 0.5, 100, 60, 70, 110]]]).astype("float32") np_valid_count = np.array([4]).astype("int32") np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45], [0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79], [-1, -1, -1, -1, -1, -1]]]) target = "llvm" ctx = tvm.cpu() graph, lib, _ = nnvm.compiler.build(out, target, {"data": dshape, "valid_count": (dshape[0],)}, dtype={"data": "float32", "valid_count": "int32"}) m = graph_runtime.create(graph, lib, ctx) m.set_input(**{"data": np_data, "valid_count": np_valid_count}) m.run() out = m.get_output(0, tvm.nd.empty(np_result.shape, "float32")) np.testing.assert_allclose(out.asnumpy(), np_result, atol=1e-5, rtol=1e-5)
def test_nms(): dshape = (1, 5, 6) data = sym.Variable("data") valid_count = sym.Variable("valid_count", dtype="int32") nms_threshold = 0.7 force_suppress = True nms_topk = 2 out = sym.nms(data=data, valid_count=valid_count, nms_threshold=nms_threshold, force_suppress=force_suppress, nms_topk=nms_topk) np_data = np.array([[[0, 0.8, 1, 20, 25, 45], [1, 0.7, 30, 60, 50, 80], [0, 0.4, 4, 21, 19, 40], [2, 0.9, 35, 61, 52, 79], [1, 0.5, 100, 60, 70, 110]]]).astype("float32") np_valid_count = np.array([4]).astype("int32") np_result = np.array([[[2, 0.9, 35, 61, 52, 79], [0, 0.8, 1, 20, 25, 45], [0, 0.4, 4, 21, 19, 40], [-1, 0.9, 35, 61, 52, 79], [-1, -1, -1, -1, -1, -1]]]) target = "llvm" ctx = tvm.cpu() graph, lib, _ = nnvm.compiler.build(out, target, {"data": dshape, "valid_count": (dshape[0],)}, dtype={"data": "float32", "valid_count": "int32"}) m = graph_runtime.create(graph, lib, ctx) m.set_input(**{"data": np_data, "valid_count": np_valid_count}) m.run() out = m.get_output(0, tvm.nd.empty(np_result.shape, "float32")) tvm.testing.assert_allclose(out.asnumpy(), np_result, atol=1e-5, rtol=1e-5)
def test_multibox_transform_loc(): batch_size = 1 num_anchors = 3 num_classes = 3 cls_prob = sym.Variable("cls_prob") loc_preds = sym.Variable("loc_preds") anchors = sym.Variable("anchors") transform_loc_data, valid_count = sym.multibox_transform_loc(cls_prob=cls_prob, loc_pred=loc_preds, anchor=anchors) out = sym.nms(data=transform_loc_data, valid_count=valid_count) # Manually create test case np_cls_prob = np.array([[[0.2, 0.5, 0.3], [0.25, 0.3, 0.45], [0.7, 0.1, 0.2]]]) np_loc_preds = np.array([[0.1, -0.2, 0.3, 0.2, 0.2, 0.4, 0.5, -0.3, 0.7, -0.2, -0.4, -0.8]]) np_anchors = np.array([[[-0.1, -0.1, 0.1, 0.1], [-0.2, -0.2, 0.2, 0.2], [1.2, 1.2, 1.5, 1.5]]]) expected_np_out = np.array([[[1, 0.69999999, 0, 0, 0.10818365, 0.10008108], [0, 0.44999999, 1, 1, 1, 1], [0, 0.30000001, 0, 0, 0.22903419, 0.20435292]]]) target = "llvm" dtype = "float32" ctx = tvm.cpu() graph, lib, _ = nnvm.compiler.build(out, target, {"cls_prob": (batch_size, num_anchors, num_classes), "loc_preds": (batch_size, num_anchors * 4), "anchors": (1, num_anchors, 4)}) m = graph_runtime.create(graph, lib, ctx) m.set_input(**{"cls_prob": np_cls_prob.astype(dtype), "loc_preds": np_loc_preds.astype(dtype), "anchors": np_anchors.astype(dtype)}) m.run() out = m.get_output(0, tvm.nd.empty(expected_np_out.shape, dtype)) np.testing.assert_allclose(out.asnumpy(), expected_np_out, atol=1e-5, rtol=1e-5)
def test_multibox_transform_loc(): batch_size = 1 num_anchors = 3 num_classes = 3 cls_prob = sym.Variable("cls_prob") loc_preds = sym.Variable("loc_preds") anchors = sym.Variable("anchors") transform_loc_data, valid_count = sym.multibox_transform_loc(cls_prob=cls_prob, loc_pred=loc_preds, anchor=anchors) out = sym.nms(data=transform_loc_data, valid_count=valid_count) # Manually create test case np_cls_prob = np.array([[[0.2, 0.5, 0.3], [0.25, 0.3, 0.45], [0.7, 0.1, 0.2]]]) np_loc_preds = np.array([[0.1, -0.2, 0.3, 0.2, 0.2, 0.4, 0.5, -0.3, 0.7, -0.2, -0.4, -0.8]]) np_anchors = np.array([[[-0.1, -0.1, 0.1, 0.1], [-0.2, -0.2, 0.2, 0.2], [1.2, 1.2, 1.5, 1.5]]]) expected_np_out = np.array([[[1, 0.69999999, 0, 0, 0.10818365, 0.10008108], [0, 0.44999999, 1, 1, 1, 1], [0, 0.30000001, 0, 0, 0.22903419, 0.20435292]]]) target = "llvm" dtype = "float32" ctx = tvm.cpu() graph, lib, _ = nnvm.compiler.build(out, target, {"cls_prob": (batch_size, num_anchors, num_classes), "loc_preds": (batch_size, num_anchors * 4), "anchors": (1, num_anchors, 4)}) m = graph_runtime.create(graph, lib, ctx) m.set_input(**{"cls_prob": np_cls_prob.astype(dtype), "loc_preds": np_loc_preds.astype(dtype), "anchors": np_anchors.astype(dtype)}) m.run() out = m.get_output(0, tvm.nd.empty(expected_np_out.shape, dtype)) tvm.testing.assert_allclose(out.asnumpy(), expected_np_out, atol=1e-5, rtol=1e-5)