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. php操作成功返回当前页并刷新

    echo "<script>alert('操作成功');location.href='".$_SERVER["HTTP_REFERER"].&quo ...

  2. Ideal 报错之 Class ** is never used 解决办法

    错误信息: 解决办法: file  ------setting ---------inspections----------Groovy--------------Unuse Declaration  ...

  3. Spring Security(1):认证和授权的核心组件介绍及源码分析

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方式的安全框架.它包括认证(Authentication)和授权(Authorization)两个部 ...

  4. @vue/cli3中解决Elint中console.log报错的问题

    方法一:package.json中”eslintConfig”>"rules”字段添加如下代码 "no-console": "off", &qu ...

  5. ps命令入门使用指南

    声明:本文算不上原创,主要是参考和整理了该博客ps命令详解 Shell 命令: ps [options] [--help] ps 常用参数: l 长格式输出: u 按用户名和启动时间的顺序来显示进程: ...

  6. 【miscellaneous】如何利用硬盘号和CPU序列号为软件加密

    原文:http://www.jiamisoft.com/blog/index.php/3469-yingpanhaocpuruanjianjiami.html 计算机软件是一种特殊的产品,为了防止软件 ...

  7. prometheus 监控 redis + rabbitmq

    1.安装部署 1.1 wget https://github.com/oliver006/redis_exporter/releases/download/v0.15.0/redis_exporter ...

  8. IDEA插件之FindBugs

    1.是个啥? Findbugs,它是一个静态分析工具,用来查找Java代码中的程序错误.它使用静态分析来识别Java程序中上百种不同类型的潜在错误. 2.安装 File -> Settings ...

  9. DB2部分查询SQL

    /* 部分SQL */ --添加主键 alter TABLE TABLE_SCHEMA.TABLE_NAME add constraint PK_TABLE_NAME primary key(COL1 ...

  10. Chrome浏览器控制台报 POST http://*** net::ERR_BLOCKED_BY_CLIENT

    开发项目广告模块时,遇到前端提交的请求后台拿不到,好像被什么拦截了,查看了过滤器,拦截器都无错误,且请求也到不了拦截器,chrome浏览器报:ERR_BLOCKED_BY_CLIENT错误 搞腾一半天 ...