赞
踩
| 芯片名 | 英文名 | 中文名 | 厂商 | 主要任务 | 是否训练 | 是否推理 | 算力 | 速度 |
|---|---|---|---|---|---|---|---|---|
| CPU | Central Processing Unit(CPU) | 中央处理器 | 各大厂商 | 通用计算 | 是 | 是 | 高 | 中等 |
| GPU | Graphics Processing Unit(GPU) | 图形处理器 | NVIDIA、AMD等 | 图形渲染、深度学习加速 | 是 | 是 | 高 | 高 |
| TPU | Tensor Processing Unit(TPU) | 张量处理器 | 谷歌 | 机器学习中的张量运算 | 是 | 是 | 高 | 高 |
| NPU | Neural Processing Unit(NPU) | 神经网络处理器 | 华为、联发科等 | 深度学习模型的性能提升 | 是 | 是 | 高 | 中等 |
| VPU | Vision Processing Unit(VPU) | 视觉处理器 | 英特尔、博通等 | 图像和视频处理 | 否 | 是 | 中等 | 中等 |
| DSP | Digital Signal Processor(DSP) | 数字信号处理器 | 德州仪器、高通等 | 数字信号处理、音频信号处理 | 否 | 是 | 中等 | 中等 |
| FPGA | Field-Programmable Gate Array(FPGA) | 现场可编程门阵列 | 英特尔、赛灵思等 | 可编程硬件加速器 | 是 | 是 | 高 | 中等 |
从这两张图可以很明显的看到,当有了中间表示 ONNX 后,从原来的 M × N M \times N M×N 变为了 M + N M + N M+N,让模型部署的流程变得简单。
开源机器学习通用中间格式,由微软、Facebook(Meta)、亚马逊、IBM 共同发起的。它可以兼容各种深度学习框架,也可以兼容各种推理引擎和终端硬件、操作系统。
pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime -i https://pypi.tuna.tsinghua.edu.cn/simple
import torch from torchvision import models device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print(f"正在使用的设备: {device}") # 创建一个训练好的模型 model = models.resnet18(pretrained=True) # ImageNet 预训练权重 model = model.eval().to(device) # 构建一个输入 dummy_input = torch.randn(size=[1, 3, 256, 256]).to(device) # [N, B, H, W] # 让模型推理 output = model(dummy_input) print(f"output.shape: {output.shape}") # 使用 PyTorch 自带的函数将模型转换为 ONNX 格式 onnx_save_path = 'ONNX/saves/resnet18_imagenet.onnx' # 导出的ONNX模型路径 with torch.no_grad(): torch.onnx.export( model=model, # 要转换的模型 args=dummy_input, # 模型的输入 f=onnx_save_path, # 导出的ONNX模型路径 input_names=['input'], # ONNX模型输入的名字(自定义) output_names=['output'], # ONNX模型输出的名字(自定义) opset_version=11, # Opset算子集合的版本(默认为17) ) print(f"ONNX 模型导出成功,路径为:{onnx_save_path}")
正在使用的设备: cpu
/home/leovin/anaconda3/envs/wsl/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
warnings.warn(
/home/leovin/anaconda3/envs/wsl/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /home/leovin/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 44.7M/44.7M [00:03<00:00, 13.9MB/s]
output.shape: torch.Size([1, 1000])
ONNX 模型导出成功,路径为:ONNX/saves/resnet18_imagenet.onnx
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55534
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。