编译ONNX模型Compile ONNX Models

编译ONNX模型Compile ONNX Models

本文是一篇介绍如何使用Relay部署ONNX模型的说明。             

首先,必须安装ONNX包。             

一个快速的解决方案是安装protobuf编译器,然后

pip install onnx –user

或者参考官方网站: https://github.com/onnx/onnx

import onnx

import numpy as np

import tvm

from tvm import te

import tvm.relay as relay

from tvm.contrib.download import download_testdata

Load pretrained ONNX model

这里使用的示例超分辨率模型与onnx说明中的模型完全相同

http://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html

跳过pytorch模型构造部分,下载保存的onnx模型

model_url = "".join(

    [

        "https://gist.github.com/zhreshold/",

        "bcda4716699ac97ea44f791c24310193/raw/",

        "93672b029103648953c4e5ad3ac3aadf346a4cdc/",

        "super_resolution_0.2.onnx",

    ]

)

model_path = download_testdata(model_url, "super_resolution.onnx", module="onnx")

# now you have super_resolution.onnx on disk

onnx_model = onnx.load(model_path)

Out:

File /workspace/.tvm_test_data/onnx/super_resolution.onnx exists, skip.

Load a test image

Load a test image

A single cat dominates the examples!

from PIL import Image

 

img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"

img_path = download_testdata(img_url, "cat.png", module="data")

img = Image.open(img_path).resize((224, 224))

img_ycbcr = img.convert("YCbCr")  # convert to YCbCr

img_y, img_cb, img_cr = img_ycbcr.split()

x = np.array(img_y)[np.newaxis, np.newaxis, :, :]

Out:

File /workspace/.tvm_test_data/data/cat.png exists, skip.

Compile the model with relay

target = "llvm"

 

input_name = "1"

shape_dict = {input_name: x.shape}

mod, params = relay.frontend.from_onnx(onnx_model, shape_dict)

 

with tvm.transform.PassContext(opt_level=1):

    intrp = relay.build_module.create_executor("graph", mod, tvm.cpu(0), target)

Out:

/workspace/docs/../python/tvm/relay/frontend/onnx.py:2737: UserWarning: Mismatched attribute type in ' : kernel_shape'

 

==> Context: Bad node spec: input: "1" input: "2" output: "11" op_type: "Conv" attribute { name: "kernel_shape" ints: 5 ints: 5 } attribute { name: "strides" ints: 1 ints: 1 } attribute { name: "pads" ints: 2 ints: 2 ints: 2 ints: 2 } attribute { name: "dilations" ints: 1 ints: 1 } attribute { name: "group" i: 1 }

  warnings.warn(str(e))

Execute on TVM

dtype = "float32"

tvm_output = intrp.evaluate()(tvm.nd.array(x.astype(dtype)), **params).asnumpy()

Display results

We put input and output image neck to neck

from matplotlib import pyplot as plt

 

out_y = Image.fromarray(np.uint8((tvm_output[0, 0]).clip(0, 255)), mode="L")

out_cb = img_cb.resize(out_y.size, Image.BICUBIC)

out_cr = img_cr.resize(out_y.size, Image.BICUBIC)

result = Image.merge("YCbCr", [out_y, out_cb, out_cr]).convert("RGB")

canvas = np.full((672, 672 * 2, 3), 255)

canvas[0:224, 0:224, :] = np.asarray(img)

canvas[:, 672:, :] = np.asarray(result)

plt.imshow(canvas.astype(np.uint8))

plt.show()

 

 

 Notes

默认情况下,ONNX以动态形状定义模型。ONNX导入器在导入时保留这种动态性,编译器在编译时尝试将模型转换为静态形状。如果失败,模型中可能仍有动态操作。目前并非所有TVM内核都支持动态形状,请在discuss.tvm.apache.org上提交问题讨论,如果使用动态内核遇到错误。.

https://tvm.apache.org/docs/tutorials/frontend/from_onnx.html#sphx-glr-tutorials-frontend-from-onnx-py

Download Python source code: from_onnx.py

Download Jupyter notebook: from_onnx.ipynb

 

posted @ 2020-12-09 13:00  吴建明wujianming  阅读(310)  评论(0编辑  收藏  举报