编译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
编译ONNX模型Compile ONNX Models的更多相关文章
- 使用Relay部署编译ONNX模型
使用Relay部署编译ONNX模型 本文介绍如何使用Relay部署ONNX模型的入门. 首先,必须安装ONNX软件包. 一个快速的解决方案是安装protobuf编译器,然后 pip install o ...
- 【推理引擎】ONNX 模型解析
定义模型结构 首先使用 PyTorch 定义一个简单的网络模型: class ConvBnReluBlock(nn.Module): def __init__(self) -> None: su ...
- python正则表达式--编译正则表达式re.compile
编译正则表达式-- re.compile 使用re的一般步骤是先将正则表达式的字符串形 式编译为pattern实例,然后使用pattern实例处理文本并获取匹配结果(一个Match实例(值为True) ...
- python正则表达式(2)--编译正则表达式re.compile
编译正则表达式-- re.compile 使用re的一般步骤是先将正则表达式的字符串形 式编译为pattern实例,然后使用pattern实例处理文本并获取匹配结果(一个Match实例(值为True) ...
- Django模型类之models字段类型和参数以及元数据meta
models之字段类型和参数 示例: # class Test(models.Model): # courses_test # """测试学习用""& ...
- 隐马尔科夫模型(Hidden Markov Models)
链接汇总 http://www.csie.ntnu.edu.tw/~u91029/HiddenMarkovModel.html 演算法笔记 http://read.pudn.com/downloads ...
- 隐马尔科夫模型(Hidden Markov Models) 系列之三
转自:http://blog.csdn.net/eaglex/article/details/6418219 隐马尔科夫模型(Hidden Markov Models) 定义 隐马尔科夫模型可以用一个 ...
- Django模型层(models.py)之模型创建
Django数据库操作是十分重要的内容,这两天简单学习了数据库的操作,这里做个总结. 1.ORM简介 简单的来说,ORM就是对象-关系-映射.它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖 ...
- ffmpeg源码编译安装(Compile ffmpeg with source) Part 2 : 扩展安装
在Ubuntu,Debian,Mint上编译ffmpeg 本文主要为在Ubuntu,Debian和Mint上编译安装ffmpeg和库文件以及一些扩展的编解码器.当然这与从源中安装无关. 请首先看一下通 ...
随机推荐
- 织梦seo
建站-->采集文章-->sitemap-->robots->百度提交链接(主推和自动结合)-->后续优化 http://jingyan.baidu.com/article ...
- <input type="file" id="fileID">文本框里的值清空方法
一般情况下,不允许通过脚本来对文件上传框赋值. 下面是一个变通的方法.就是创建一个新的input type="file" 把原来的替换掉. <!DOCTYPE html PU ...
- 前端Excel表格导入导出,包括合并单元格,表格自定义样式等
表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...
- Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may ne
更多精彩关注微信公众号 错误原因 在pom中引入了mybatis-spring-boot-starter ,Spring boot默认会加载org.springframework.boot.autoc ...
- 获取汉字首字母并分组排列 PHP
1.代码class Character{ /** * 数组根据首首字母排序 */ /** * 二维数组根据首字母分组排序 * @param array $data 二维数组 * @param stri ...
- SSM久别遇新坑
SSM久别遇新坑 久别个锤子,也就几天没看,改bug改到怀疑人生 maven的父子模块问题 众所周知,用maven建立一个空的模块,在它之下,将原本的各层次结构分别新建为一个子模块,就能够将各业务进行 ...
- [bug] Navicat 连 虚拟机MySQL
参考 https://www.cnblogs.com/brankoliu/p/10845491.html https://blog.csdn.net/qq_40087740/article/detai ...
- [刷题] PTA 03-树2 List Leaves
程序: 1 #include <stdio.h> 2 #include <queue> 3 #define MaxTree 20 4 #define Null -1 5 usi ...
- make clean 清除之前编译的可执行文件及配置文件。 make distclean 清除所有生成的文件。
https://blog.csdn.net/bb807777/article/details/108302105 make clean 清除之前编译的可执行文件及配置文件.make distclean ...
- GB 18030-2000《信息技术信息交换用汉字编码字符集基本集的扩充
中文编码:GB2312编码.GBK编码.GB18030编码 2016-09-01 0 By ADMIN 一.GB 2312编码 中华人民共和国国家标准简体中文字符集,全称<信息交换用汉字编码字符 ...