Skip to content

Commit

Permalink
Add FasterRCNN model
Browse files Browse the repository at this point in the history
  • Loading branch information
echuraev committed Jul 10, 2023
1 parent b02429b commit 024fd0b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
36 changes: 36 additions & 0 deletions evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 23 additions & 29 deletions vm_samples/run_with_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


USE_VM = True
RUN_ON_HOST = False
RUN_ON_HOST = True

rpc_key = "android"
target_c = "opencl -device=adreno"
Expand Down Expand Up @@ -117,18 +117,16 @@ 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)
return model_file


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 = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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):
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 024fd0b

Please sign in to comment.