From d9e9f69fe3dd3aaa90224b397777a13360e7213f Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Mon, 23 Nov 2020 13:23:16 -0800 Subject: [PATCH 1/3] fix yolo formals --- python/tvm/relay/frontend/darknet.py | 2 +- tests/python/relay/test_ir_text_printer.py | 51 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/darknet.py b/python/tvm/relay/frontend/darknet.py index 87e55593e943..088ac72e32bb 100644 --- a/python/tvm/relay/frontend/darknet.py +++ b/python/tvm/relay/frontend/darknet.py @@ -40,7 +40,7 @@ def _darknet_not_support(attr, op="relay"): def _get_params_prefix(opname, layer_num): """Makes the params prefix name from opname and layer number.""" - return str(opname) + str(layer_num) + return str(opname).replace('.', '_') + str(layer_num) def _get_params_name(prefix, item): diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index 4a3569aca2ec..a238de01b776 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -21,6 +21,8 @@ import numpy as np from tvm.relay import Expr from tvm.relay.analysis import free_vars +import pytest +import sys DEBUG_PRINT = False @@ -46,6 +48,46 @@ def show(text): print(text) +def get_darknet(model_name): + from tvm.contrib.download import download_testdata + from tvm.relay.testing.darknet import __darknetffi__ + import tvm.relay.testing.yolo_detection + import tvm.relay.testing.darknet + + CFG_NAME = model_name + ".cfg" + WEIGHTS_NAME = model_name + ".weights" + REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" + CFG_URL = REPO_URL + "cfg/" + CFG_NAME + "?raw=true" + WEIGHTS_URL = "https://pjreddie.com/media/files/" + WEIGHTS_NAME + + cfg_path = download_testdata(CFG_URL, CFG_NAME, module="darknet") + weights_path = download_testdata(WEIGHTS_URL, WEIGHTS_NAME, module="darknet") + + # Download and Load darknet library + if sys.platform in ["linux", "linux2"]: + DARKNET_LIB = "libdarknet2.0.so" + DARKNET_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true" + elif sys.platform == "darwin": + DARKNET_LIB = "libdarknet_mac2.0.so" + DARKNET_URL = REPO_URL + "lib_osx/" + DARKNET_LIB + "?raw=true" + else: + err = "Darknet lib is not supported on {} platform".format(sys.platform) + raise NotImplementedError(err) + + lib_path = download_testdata(DARKNET_URL, DARKNET_LIB, module="darknet") + + DARKNET_LIB = __darknetffi__.dlopen(lib_path) + net = DARKNET_LIB.load_network(cfg_path.encode("utf-8"), weights_path.encode("utf-8"), 0) + dtype = "float32" + batch_size = 1 + + data = np.empty([batch_size, net.c, net.h, net.w], dtype) + shape_dict = {"data": data.shape} + mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=data.shape) + mod = relay.transform.InferType()(mod) + return mod, params + + def test_func(): x = relay.var("x", shape=(3, 2)) y = relay.var("y") @@ -190,6 +232,11 @@ def test_densenet(): astext(net) +def test_yolov3(): + net, _ = get_darknet("yolov3") + astext(net) + + def test_call_node_order(): x = relay.var("x") y = relay.var("y") @@ -269,6 +316,4 @@ def test_span(): if __name__ == "__main__": - import sys - - pytext.argv(sys.argv) + pytest.main(sys.argv) From b19f2ac94c927044e4960a0032da2bdde5723f4c Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Mon, 23 Nov 2020 15:39:15 -0800 Subject: [PATCH 2/3] fix lint --- python/tvm/relay/frontend/darknet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/darknet.py b/python/tvm/relay/frontend/darknet.py index 088ac72e32bb..363812fd562b 100644 --- a/python/tvm/relay/frontend/darknet.py +++ b/python/tvm/relay/frontend/darknet.py @@ -40,7 +40,7 @@ def _darknet_not_support(attr, op="relay"): def _get_params_prefix(opname, layer_num): """Makes the params prefix name from opname and layer number.""" - return str(opname).replace('.', '_') + str(layer_num) + return str(opname).replace(".", "_") + str(layer_num) def _get_params_name(prefix, item): From 6a6ad8be38040cd62f94b9bfaf87a59a5eccb853 Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Tue, 24 Nov 2020 17:43:01 -0800 Subject: [PATCH 3/3] move test to test_forward --- tests/python/frontend/darknet/test_forward.py | 15 ++++++ tests/python/relay/test_ir_text_printer.py | 48 +------------------ 2 files changed, 16 insertions(+), 47 deletions(-) diff --git a/tests/python/frontend/darknet/test_forward.py b/tests/python/frontend/darknet/test_forward.py index 77c72e770fef..1535c3a1b88f 100644 --- a/tests/python/frontend/darknet/test_forward.py +++ b/tests/python/frontend/darknet/test_forward.py @@ -45,6 +45,17 @@ ) +def astext(program, unify_free_vars=False): + """check that program is parsable in text format""" + text = program.astext() + if isinstance(program, relay.Expr): + roundtrip_program = tvm.parser.parse_expr(text) + else: + roundtrip_program = tvm.parser.fromtext(text) + + tvm.ir.assert_structural_equal(roundtrip_program, program, map_free_vars=True) + + def _read_memory_buffer(shape, data, dtype="float32"): length = 1 for x in shape: @@ -59,6 +70,10 @@ def _get_tvm_output(net, data, build_dtype="float32", states=None): """Compute TVM output""" dtype = "float32" mod, params = relay.frontend.from_darknet(net, data.shape, dtype) + # verify that from_darknet creates a valid, parsable relay program + mod = relay.transform.InferType()(mod) + astext(mod) + target = "llvm" shape_dict = {"data": data.shape} lib = relay.build(mod, target, params=params) diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index a238de01b776..72a243dbbb67 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -22,7 +22,6 @@ from tvm.relay import Expr from tvm.relay.analysis import free_vars import pytest -import sys DEBUG_PRINT = False @@ -48,46 +47,6 @@ def show(text): print(text) -def get_darknet(model_name): - from tvm.contrib.download import download_testdata - from tvm.relay.testing.darknet import __darknetffi__ - import tvm.relay.testing.yolo_detection - import tvm.relay.testing.darknet - - CFG_NAME = model_name + ".cfg" - WEIGHTS_NAME = model_name + ".weights" - REPO_URL = "https://github.com/dmlc/web-data/blob/main/darknet/" - CFG_URL = REPO_URL + "cfg/" + CFG_NAME + "?raw=true" - WEIGHTS_URL = "https://pjreddie.com/media/files/" + WEIGHTS_NAME - - cfg_path = download_testdata(CFG_URL, CFG_NAME, module="darknet") - weights_path = download_testdata(WEIGHTS_URL, WEIGHTS_NAME, module="darknet") - - # Download and Load darknet library - if sys.platform in ["linux", "linux2"]: - DARKNET_LIB = "libdarknet2.0.so" - DARKNET_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true" - elif sys.platform == "darwin": - DARKNET_LIB = "libdarknet_mac2.0.so" - DARKNET_URL = REPO_URL + "lib_osx/" + DARKNET_LIB + "?raw=true" - else: - err = "Darknet lib is not supported on {} platform".format(sys.platform) - raise NotImplementedError(err) - - lib_path = download_testdata(DARKNET_URL, DARKNET_LIB, module="darknet") - - DARKNET_LIB = __darknetffi__.dlopen(lib_path) - net = DARKNET_LIB.load_network(cfg_path.encode("utf-8"), weights_path.encode("utf-8"), 0) - dtype = "float32" - batch_size = 1 - - data = np.empty([batch_size, net.c, net.h, net.w], dtype) - shape_dict = {"data": data.shape} - mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=data.shape) - mod = relay.transform.InferType()(mod) - return mod, params - - def test_func(): x = relay.var("x", shape=(3, 2)) y = relay.var("y") @@ -232,11 +191,6 @@ def test_densenet(): astext(net) -def test_yolov3(): - net, _ = get_darknet("yolov3") - astext(net) - - def test_call_node_order(): x = relay.var("x") y = relay.var("y") @@ -316,4 +270,4 @@ def test_span(): if __name__ == "__main__": - pytest.main(sys.argv) + pytest.main([__file__])