当前位置: 首页 > news >正文

做的好的农产品网站近期国际热点大事件

做的好的农产品网站,近期国际热点大事件,文网文许可证办理条件,做网站赌博代理违法吗本篇文章译自英文文档 Compile OneFlow Models tvm 0.14.dev0 documentation 作者是 BBuf (Xiaoyu Zhang) GitHub 更多 TVM 中文文档可访问 →Apache TVM 是一个端到端的深度学习编译框架,适用于 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 flowvision==0.1.0
python3 -m pip install -f https://release.oneflow.info oneflow==0.7.0+cpu

或参考官网:
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, interpolation=Image.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)(pretrained=True)
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:00<00:00, 41.9MB/s]39%|###8      | 16.0M/41.5M [00:00<00:00, 40.1MB/s]54%|#####3    | 22.3M/41.5M [00:00<00:00, 45.4MB/s]65%|######4   | 26.9M/41.5M [00:00<00:00, 42.8MB/s]82%|########2 | 34.1M/41.5M [00:00<00:00, 51.3MB/s]95%|#########4| 39.3M/41.5M [00:00<00:00, 47.7MB/s]
100%|##########| 41.5M/41.5M [00:00<00:00, 46.0MB/s]

加载测试图像​

还是用猫的图像:

from PIL import Imageimg_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))# 预处理图像,并转换为张量
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", host="llvm")
dev = tvm.cpu(0)
with tvm.transform.PassContext(opt_level=3):lib = relay.build(mod, target=target, params=params)

输出结果:

/workspace/python/tvm/driver/build_module.py:268: UserWarning: target_host parameter is going to be deprecated. Please pass in tvm.target.Target(target, host=target_host) instead."target_host parameter is going to be deprecated. "

在 TVM 上执行可移植计算图​

接下来在 target 上部署编译好的模型:

target = "cuda"
with tvm.transform.PassContext(opt_level=10):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, module="data")
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, module="data")
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.khdw.cn/news/44204.html

相关文章:

  • 济南网站建设 伍际网络关键词列表
  • 免费的网站怎么做谷歌广告上海有限公司
  • 网站建设 作用营销策划有限公司经营范围
  • 网站怎么做网页百度收录查询
  • 大恒建设集团有限公司网站深圳抖音推广公司
  • web浏览器手机版下载网络排名优化软件
  • wordpress 模板丢失武汉seo网站优化运营
  • 电商网站代码百度seo推广方案
  • 公司为什么做网站长沙谷歌seo
  • 无锡网站seo报价海口百度seo公司
  • 同德县网站建设公司百度搜索广告推广
  • 杭州网站建设公司联系方式站长工具四叶草
  • wordpress企业站手机客户端爱上链外链购买平台
  • 中文的网站做不成二维码优化seo方法
  • 商城管理系统seo流量优化
  • 建一个购物网站多少钱百度竞价代理公司
  • 网站制作技术有哪些seo百度网站排名研究中心关键词首页优化
  • 网站推广的案例seo优化教程自学
  • 长沙网站制作多少钱保定网站推广公司
  • 安徽政府网站建设2022年每日新闻摘抄10一30字
  • 成免费crm特色学生版的特点app优化建议
  • 最好的微网站建设价格百度收藏夹使用方法
  • 用插件做网站百度官网登录入口手机版
  • 一个完整的个人网站推广的软件有哪些
  • 保定网站制作价格网站关键词优化方法
  • wordpress做多语言版seo是什么职位简称
  • 电子商城网站建设服装网络营销策划书
  • 如何搭建一个企业子账号网站网站排名查询工具有哪些
  • 婚纱摄影网站毕业设计php2022智慧树互联网与营销创新
  • wordpress网站正在建设中如何在网络上推广产品