怎样做seo网站推广,呼和浩特微信小程序公司,恶意点击软件哪个好,wordpress页面.html本篇文章译自英文文档 Compile OneFlow Models tvm 0.14.dev0 documentation
作者是 BBuf (Xiaoyu Zhang) GitHub
更多 TVM 中文文档可访问 →Apache TVM 是一个端到端的深度学习编译框架#xff0c;适用于 CPU、GPU 和各种机器学习加速芯片。 | Apache TVM 中文站
本文介…本篇文章译自英文文档 Compile OneFlow Models tvm 0.14.dev0 documentation
作者是 BBuf (Xiaoyu Zhang) · GitHub
更多 TVM 中文文档可访问 →Apache TVM 是一个端到端的深度学习编译框架适用于 CPU、GPU 和各种机器学习加速芯片。 | Apache TVM 中文站
本文介绍如何用 Relay 部署 OneFlow 模型。
首先安装 OneFlow 包可通过 pip 快速安装
pip install flowvision0.1.0
python3 -m pip install -f https://release.oneflow.info oneflow0.7.0cpu或参考官网 https://github.com/Oneflow-Inc/oneflow
目前 TVM 支持 OneFlow 0.7.0其他版本可能不稳定。
import os, math
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image# OneFlow 导入
import flowvision
import oneflow as flow
import oneflow.nn as nnimport tvm
from tvm import relay
from tvm.contrib.download import download_testdata输出结果
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional_pil.py:193: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.def resize(img, size, interpolationImage.BILINEAR):
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:65: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead.Image.NEAREST: nearest,
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:66: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.Image.BILINEAR: bilinear,
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:67: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead.Image.BICUBIC: bicubic,
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:68: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead.Image.BOX: box,
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:69: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead.Image.HAMMING: hamming,
/usr/local/lib/python3.7/dist-packages/flowvision/transforms/functional.py:70: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.Image.LANCZOS: lanczos,
/usr/local/lib/python3.7/dist-packages/flowvision/data/auto_augment.py:28: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead._RANDOM_INTERPOLATION (Image.BILINEAR, Image.BICUBIC)
/usr/local/lib/python3.7/dist-packages/flowvision/data/auto_augment.py:28: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead._RANDOM_INTERPOLATION (Image.BILINEAR, Image.BICUBIC)加载和保存 OneFlow 的预训练模型
model_name resnet18
model getattr(flowvision.models, model_name)(pretrainedTrue)
model model.eval()model_dir resnet18_model
if not os.path.exists(model_dir):flow.save(model.state_dict(), model_dir)输出结果
Downloading: https://oneflow-public.oss-cn-beijing.aliyuncs.com/model_zoo/flowvision/classification/ResNet/resnet18.zip to /workspace/.oneflow/flowvision_cache/resnet18.zip0%| | 0.00/41.5M [00:00?, ?B/s]19%|#9 | 7.99M/41.5M [00:0000:00, 41.9MB/s]39%|###8 | 16.0M/41.5M [00:0000:00, 40.1MB/s]54%|#####3 | 22.3M/41.5M [00:0000:00, 45.4MB/s]65%|######4 | 26.9M/41.5M [00:0000:00, 42.8MB/s]82%|########2 | 34.1M/41.5M [00:0000:00, 51.3MB/s]95%|#########4| 39.3M/41.5M [00:0000:00, 47.7MB/s]
100%|##########| 41.5M/41.5M [00:0000:00, 46.0MB/s]加载测试图像
还是用猫的图像
from PIL import Imageimg_url https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?rawtrue
img_path download_testdata(img_url, cat.png, moduledata)
img Image.open(img_path).resize((224, 224))# 预处理图像并转换为张量
from flowvision import transformsmy_preprocess transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225]),]
)
img my_preprocess(img)
img np.expand_dims(img.numpy(), 0)将计算图导入到 Relay 中
将 OneFlow 计算图转换为 Relay 计算图输入任意名称。
class Graph(flow.nn.Graph):def __init__(self, module):super().__init__()self.m moduledef build(self, x):out self.m(x)return outgraph Graph(model)
_ graph._compile(flow.randn(1, 3, 224, 224))mod, params relay.frontend.from_oneflow(graph, model_dir)使用 Relay 构建
用给定的输入规范将计算图编译为 llvm target。
target tvm.target.Target(llvm, hostllvm)
dev tvm.cpu(0)
with tvm.transform.PassContext(opt_level3):lib relay.build(mod, targettarget, paramsparams)输出结果
/workspace/python/tvm/driver/build_module.py:268: UserWarning: target_host parameter is going to be deprecated. Please pass in tvm.target.Target(target, hosttarget_host) instead.target_host parameter is going to be deprecated. 在 TVM 上执行可移植计算图
接下来在 target 上部署编译好的模型
target cuda
with tvm.transform.PassContext(opt_level10):intrp relay.build_module.create_executor(graph, mod, tvm.cuda(0), target)print(type(img))
print(img.shape)
tvm_output intrp.evaluate()(tvm.nd.array(img.astype(float32)), **params)输出结果
class numpy.ndarray
(1, 3, 224, 224)查找分类集名称
在 1000 个类的分类集中查找分数最高的第一个
synset_url .join([https://raw.githubusercontent.com/Cadene/,pretrained-models.pytorch/master/data/,imagenet_synsets.txt,]
)
synset_name imagenet_synsets.txt
synset_path download_testdata(synset_url, synset_name, moduledata)
with open(synset_path) as f:synsets f.readlines()synsets [x.strip() for x in synsets]
splits [line.split( ) for line in synsets]
key_to_classname {spl[0]: .join(spl[1:]) for spl in splits}class_url .join([https://raw.githubusercontent.com/Cadene/,pretrained-models.pytorch/master/data/,imagenet_classes.txt,]
)
class_name imagenet_classes.txt
class_path download_testdata(class_url, class_name, moduledata)
with open(class_path) as f:class_id_to_key f.readlines()class_id_to_key [x.strip() for x in class_id_to_key]# 获得 TVM 分数最高的第一个结果
top1_tvm np.argmax(tvm_output.numpy()[0])
tvm_class_key class_id_to_key[top1_tvm]# 将输入转换为 OneFlow 变量并获取 OneFlow 结果进行比较
with flow.no_grad():torch_img flow.from_numpy(img)output model(torch_img)# 获取 OneFlow 分数最高的第一个结果top_oneflow np.argmax(output.numpy())oneflow_class_key class_id_to_key[top_oneflow]print(Relay top-1 id: {}, class name: {}.format(top1_tvm, key_to_classname[tvm_class_key]))
print(OneFlow top-1 id: {}, class name: {}.format(top_oneflow, key_to_classname[oneflow_class_key])
)输出结果
Relay top-1 id: 281, class name: tabby, tabby cat
OneFlow top-1 id: 281, class name: tabby, tabby cat下载 Python 源代码「链接」
下载 Jupyter Notebook「链接」 文章转载自: http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.jjnry.cn.gov.cn.jjnry.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn