From 024fd0b80ec10844aba167135a39d2f90eb27a5c Mon Sep 17 00:00:00 2001 From: Egor Churaev Date: Mon, 5 Jun 2023 10:43:41 +0300 Subject: [PATCH] Add FasterRCNN model --- evaluate.py | 36 ++++++++++++++++++++++++ vm_samples/run_with_python.py | 52 ++++++++++++++++------------------- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/evaluate.py b/evaluate.py index befe7360b415..09a5ea67110e 100644 --- a/evaluate.py +++ b/evaluate.py @@ -363,6 +363,42 @@ def import_onnx_yolo_v3(self, target="llvm", dtype="float32"): return (mod, params, shape_dict, dtype, target, ONNXTestSamplesValidator(test_files_dir, input_names=list(shape_dict.keys()))) + def import_onnx_faster_rcnn(self, target="llvm", dtype="float32"): + archive_url = "https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/faster-rcnn/model/FasterRCNN-12.onnx" + filename = "FasterRCNN-12" + from tvm.contrib import download + import onnx + download.download(archive_url, filename) + onnx_model = onnx.load(filename) + shape_dict = { + "image": (3, 800, 800), + } + mod_file = "onnx_faster_rcnn_mod.json" + params_file = "onnx_faster_rcnn_params.json" + if not os.path.exists(mod_file): + mod, params = relay.frontend.from_onnx(onnx_model, shape_dict, freeze_params=True) + + # downcast to float16 + mod = convert_to_dtype(mod["main"], dtype) + with open(mod_file, "w") as file: + file.write(tvm.ir.save_json(mod)) + + with open(params_file, "wb") as file: + file.write(relay.save_param_dict(params)) + else: + with open(mod_file, "r") as file: + mod = tvm.ir.load_json(file.read()) + + with open(params_file, "rb") as file: + params = relay.load_param_dict(file.read()) + dtype = "float32" if dtype == "float32" else "float16" + print("=" * 10) + print(mod) + print("=" * 10) + + return (mod, params, shape_dict, dtype, target) + + def get_args(): import argparse diff --git a/vm_samples/run_with_python.py b/vm_samples/run_with_python.py index 501550f1e091..39d4c99de6c7 100644 --- a/vm_samples/run_with_python.py +++ b/vm_samples/run_with_python.py @@ -13,7 +13,7 @@ USE_VM = True -RUN_ON_HOST = False +RUN_ON_HOST = True rpc_key = "android" target_c = "opencl -device=adreno" @@ -117,10 +117,8 @@ def get_ssd_model(): return module, params1, shape_dict -def download_resnet_model(): +def download_model(model_url, model_file): print("Download model...") - model_file = "resnet50-v2-7.onnx" - model_url = "https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx" if not os.path.exists(model_file): import urllib.request urllib.request.urlretrieve(model_url, model_file) @@ -128,7 +126,7 @@ def download_resnet_model(): def get_resnet_model(): - model_file = download_resnet_model() + model_file = download_model("https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx", "resnet50-v2-7.onnx") print("Import model...") onnx_model = onnx.load(model_file) shape_dict = { @@ -141,18 +139,8 @@ def get_resnet_model(): return model, params, shape_dict -def download_resnet_ssd_model(): - print("Download model...") - model_file = "resnet-ssd.onnx" - model_url = "https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/ssd/model/ssd-12.onnx" - if not os.path.exists(model_file): - import urllib.request - urllib.request.urlretrieve(model_url, model_file) - return model_file - - def get_resnet_ssd_model(): - model_file = download_resnet_ssd_model() + model_file = download_model("https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/ssd/model/ssd-12.onnx", "resnet-ssd.onnx") print("Import model...") onnx_model = onnx.load(model_file) shape_dict = { @@ -165,18 +153,8 @@ def get_resnet_ssd_model(): return model, params, shape_dict -def download_onnx_yolo_model(): - print("Download model...") - model_file = "onnx_yolov3.onnx" - model_url = "https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/yolov3/model/yolov3-12.onnx" - if not os.path.exists(model_file): - import urllib.request - urllib.request.urlretrieve(model_url, model_file) - return model_file - - def get_onnx_yolo_model(): - model_file = download_onnx_yolo_model() + model_file = download_model("https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/yolov3/model/yolov3-12.onnx", "onnx_yolov3.onnx") print("Import model...") onnx_model = onnx.load(model_file) shape_dict = { @@ -190,6 +168,20 @@ def get_onnx_yolo_model(): return model, params, shape_dict +def get_onnx_faster_rcnn_model(): + model_file = download_model("https://github.com/onnx/models/raw/main/vision/object_detection_segmentation/faster-rcnn/model/FasterRCNN-12.onnx", "FasterRCNN.onnx") + print("Import model...") + onnx_model = onnx.load(model_file) + shape_dict = { + "image": (3, 800, 800), + } + model, params = relay.frontend.from_onnx(onnx_model, shape_dict, freeze_params=True) + print("=" * 10) + print(model) + print("=" * 10) + return model, params, shape_dict + + def compile_model_for_vm(name, model, params, target): lib_name = f"{name}.vm.so" with tvm.transform.PassContext(opt_level=3): @@ -265,14 +257,16 @@ def run_model_with_ge(input_dict, lib_name): target = Target(target_c, host=target_h) #name = "resnet_vm_model" #model, params, shape_dict = get_resnet_model() - name = "my_conv2d" - model, params, shape_dict = get_model() + #name = "my_conv2d" + #model, params, shape_dict = get_model() #name = "resnet_ssd_vm_model" #model, params, shape_dict = get_resnet_ssd_model() #name = "my_ssd_vm_model" #model, params, shape_dict = get_ssd_model() #name = "onnx_yolo_vm_model" #model, params, shape_dict = get_onnx_yolo_model() + name = "onnx_faster_rcnn_model" + model, params, shape_dict = get_onnx_faster_rcnn_model() input_dict = {} for k, v in shape_dict.items(): img = np.random.rand(*v).astype("float32")