tvm官网中,对从ONNX预训练模型中加载模型的教程说明

教程来自于:https://docs.tvm.ai/tutorials/frontend/from_onnx.html#sphx-glr-tutorials-frontend-from-onnx-py

首先我对教程进行了一些修改,很多东西没有必要,比如不是每次都需要从网上下载图片和模型,super_resolution.onnx和cat.png都预先下载到了文件同目录下,

同时,最新版本的tvm中不支持Python2.7,我没有编译llvm,所以我把我的设置都改到了cuda上,在24行和32行有体现,注意最新版本

 import onnx
import numpy as np
import tvm
import tvm.relay as relay
# from tvm.contrib.download import download_testdata # 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('super_resolution.onnx') from PIL import Image
# img_url = 'https://github.com/dmlc/mxnet.js/blob/master/data/cat.png?raw=true'
# img_path = download_testdata(img_url, 'cat.png', module='data')
img_path = 'cat.png'
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, :, :] target = 'cuda' input_name = ''
shape_dict = {input_name: x.shape}
sym, params = relay.frontend.from_onnx(onnx_model, shape_dict)
print(sym) with relay.build_config(opt_level=1):
intrp = relay.build_module.create_executor('graph', sym, tvm.gpu(0), target) dtype = 'float32'
tvm_output = intrp.evaluate(sym)(tvm.nd.array(x.astype(dtype)), **params).asnumpy()

第28行有一个从模型加载的函数from_onnx

官方的解释:tvm.relay.frontend.from_onnx(model, shape=None, dtype='float32')

Convert a ONNX model into an equivalent Relay Function.

ONNX graphs are represented as Python Protobuf objects. The companion parameters will be handled automatically. However, the input names from onnx graph is vague, mixing inputs and network weights/bias such as “1”, “2”… For convenience, we rename the real input names to “input_0”, “input_1”… And renaming parameters to “param_0”, “param_1”…

Parameters:
  • model (protobuf object) – ONNX ModelProto after ONNX v1.1.0
  • shape (dict of str to tuple, optional) – The input shape to the graph
  • dtype (str or dict of str to str) – The input types to the graph
Returns:
  • sym (tvm.relay.expr.Function) – Compatible relay function
  • params (dict of str to tvm.NDArray) – The parameter dict to be used by relay

看返回值,sym是relay Function,在后边加一个print(sym)输出,可以看到图这一级的IR

fn (%v1: Tensor[(1, 1, 224, 224), float32], %v2: Tensor[(64, 1, 5, 5), float32], %v3: Tensor[(64,), float32], %v4: Tensor[(64, 64, 3, 3), float32], %v5: Tensor[(64,), float32], %v6: Tensor[(32, 64, 3, 3), float32], %v7: Tensor[(32,), float32], %v8: Tensor[(9, 32, 3, 3), float32], %v9: Tensor[(9,), float32]) {
%0 = nn.conv2d(%v1, %v2, padding=[2, 2], kernel_size=[5, 5])
%1 = expand_dims(%v3, axis=1, num_newaxis=2)
%2 = add(%0, %1)
%3 = nn.relu(%2)
%4 = nn.conv2d(%3, %v4, padding=[1, 1], kernel_size=[3, 3])
%5 = expand_dims(%v5, axis=1, num_newaxis=2)
%6 = add(%4, %5)
%7 = nn.relu(%6)
%8 = nn.conv2d(%7, %v6, padding=[1, 1], kernel_size=[3, 3])
%9 = expand_dims(%v7, axis=1, num_newaxis=2)
%10 = add(%8, %9)
%11 = nn.relu(%10)
%12 = nn.conv2d(%11, %v8, padding=[1, 1], kernel_size=[3, 3])
%13 = expand_dims(%v9, axis=1, num_newaxis=2)
%14 = add(%12, %13)
%15 = reshape(%14, newshape=[1, 1, 3, 3, 224, 224])
%16 = transpose(%15, axes=[0, 1, 4, 2, 5, 3])
reshape(%16, newshape=[1, 1, 672, 672])
}

ONNX预训练模型加载的更多相关文章

  1. [.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

    最近在看微软开源的机器学习框架ML.NET使用别人的预训练模型(开放神经网络交换格式.onnx)来识别图像,然后逛github发现一个好玩的repo.决定整活一期博客. 首先还是稍微科普一下机器学习相 ...

  2. 全面解析Pytorch框架下模型存储,加载以及冻结

    最近在做试验中遇到了一些深度网络模型加载以及存储的问题,因此整理了一份比较全面的在 PyTorch 框架下有关模型的问题.首先咱们先定义一个网络来进行后续的分析: 1.本文通用的网络模型 import ...

  3. PyTorch保存模型与加载模型+Finetune预训练模型使用

    Pytorch 保存模型与加载模型 PyTorch之保存加载模型 参数初始化参 数的初始化其实就是对参数赋值.而我们需要学习的参数其实都是Variable,它其实是对Tensor的封装,同时提供了da ...

  4. 关于document.write()加载JS等静态资源 和 异步async加载JS

    现流行浏览器对于静态资源的预加载 传统的浏览器,对于静态资源加载,会阻塞 HTML 解析器的线程进行,无论内联还是外链. 例如: <script src="test1.js" ...

  5. [Pytorch]Pytorch加载预训练模型(转)

    转自:https://blog.csdn.net/Vivianyzw/article/details/81061765 东风的地方 1. 直接加载预训练模型 在训练的时候可能需要中断一下,然后继续训练 ...

  6. 如何使用 opencv 加载 darknet yolo 预训练模型?

    如何使用 opencv 加载 darknet yolo 预训练模型? opencv 版本 > 3.4 以上 constexpr const char *image_path = "da ...

  7. 【tf.keras】tf.keras加载AlexNet预训练模型

    目录 从 PyTorch 中导出模型参数 第 0 步:配置环境 第 1 步:安装 MMdnn 第 2 步:得到 PyTorch 保存完整结构和参数的模型(pth 文件) 第 3 步:导出 PyTorc ...

  8. PyTorch-网络的创建,预训练模型的加载

    本文是PyTorch使用过程中的的一些总结,有以下内容: 构建网络模型的方法 网络层的遍历 各层参数的遍历 模型的保存与加载 从预训练模型为网络参数赋值 主要涉及到以下函数的使用 add_module ...

  9. pytorch中修改后的模型如何加载预训练模型

    问题描述 简单来说,比如你要加载一个vgg16模型,但是你自己需要的网络结构并不是原本的vgg16网络,可能你删掉某些层,可能你改掉某些层,这时你去加载预训练模型,就会报错,错误原因就是你的模型和原本 ...

随机推荐

  1. Linux-文件系统的简单操作

    文件系统的简单操作 磁盘与目录的容量:df.du df [option] [目录或文件名] 参数: -a:列出所有的文件系统,包括系统特有的/proc等系统 -k:以KB的容量显示各文件系统 -m:以 ...

  2. linux简单命令6---挂载

  3. 移动端BI的设计

    在移动化.大数据浪潮的今天,基于数据做决策应该是每一家公司的标配:每家公司都有专门负责数据的人,也都应该有一个BI部门.而移动BI,基于手机端随时随地进行数据查询和分析——更是BI中不可或缺的一部分. ...

  4. Node.js启动服务报错SyntaxError: Unexpected token import

    启动服务报错如下: Last login: Wed Nov :: on ttys000 localhost:~ sipeng$ cd /Users/sipeng/Desktop/彭思/2017年学习/ ...

  5. TextView的封装和自定义

    实现的效果如下: #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property (nonatomic , ...

  6. HAProxy+Keepalived 高可用负载均衡

    转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...

  7. windows配置apache支持https

    https://my.oschina.net/ososchina/blog/510065

  8. 如何制作红蓝3d电影(详细教程)

    自20世纪初以来,电影制作人一直试图通过制作3D电影来利用我们的双眼.现在,由于大量相对实惠的3D电视,你可以享受电影院以外的额外空间 - 你自己拍摄的视频.对于大预算的电影,电影摄影师使用两个相连的 ...

  9. CSRF利用

    使用burpsuite的csrf poc选项,可以生成HTML代码 json CSRF flash + 307跳转 https://github.com/sp1d3r/swf_json_csrf

  10. 首款物联网防火墙himqtt开源

    随着5G的推进和物联网的快速推进,MQTT已然是物联网(Internet of Things,IoT)不可或缺的标准协议,预计到2025年将有超过750亿台设备连接到互联网,但物联网方面的防火墙很少. ...