Convert ONNX models into Apple Core ML format onnx 转换为 mlmodel

Convert ONNX models into Apple Core ML format

  1. 安装onnx-coreml
/// 如果报错 使用python3 因为你的电脑可能有两个版本的python mac自带的python的是2
pip install torchvision onnx-coreml 
/// 或者
pip3 install torchvision onnx-coreml 
  1. 创建 onnx_to_coreml2.py
import sys
from onnx import onnx_pb
from onnx_coreml import convert

model_in = sys.argv[1]
model_out = sys.argv[2]

"""
IMAGE_NET_MEAN = [0.485, 0.456, 0.406]
IMAGE_NET_STD = [0.229, 0.224, 0.225]
"""

scale = 1.0 / (1.0 * 255.0)
args = dict(is_bgr=False, red_bias = -(0 * 255.0) * scale  , green_bias = -(0 * 255.0) * scale , blue_bia = -(0 * 255.0) * scale, image_scale = scale)

model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
coreml_model = convert(model_proto,image_input_names = ['input'],preprocessing_args = args, minimum_ios_deployment_target='13')
coreml_model.save(model_out)

需要注意的是在做模型转换的时候 需要设置 image_input_names = [‘input’] 这样 mlmodel 才会识别是图片而不是数组 不然会报错 还需要注意归一化处理和 通道颜色值偏差

  1. 执行脚本
/// python onnx_to_coreml2.py  inputfileName.onnx outputfileName.mlmodel 
/// 报错的话 直接使用python3
python3 onnx_to_coreml2.py  inputfileName.onnx outputfileName.mlmodel
  1. 结果
demon@Demon Coreml % python3 /Users/demon/Desktop/Coreml/onnx_to_coreml2.py  nima_mobilenetv2-simple.onnx dezhi_nima.mlmodel
1/153: Converting Node Type Conv
2/153: Converting Node Type BatchNormalization
3/153: Converting Node Type Relu
4/153: Converting Node Type Conv
5/153: Converting Node Type BatchNormalization
6/153: Converting Node Type Relu
7/153: Converting Node Type Conv
8/153: Converting Node Type BatchNormalization
9/153: Converting Node Type Conv
10/153: Converting Node Type BatchNormalization
11/153: Converting Node Type Relu
12/153: Converting Node Type Conv
13/153: Converting Node Type BatchNormalization
14/153: Converting Node Type Relu
15/153: Converting Node Type Conv
16/153: Converting Node Type BatchNormalization
17/153: Converting Node Type Conv
18/153: Converting Node Type BatchNormalization
19/153: Converting Node Type Relu
20/153: Converting Node Type Conv
21/153: Converting Node Type BatchNormalization
22/153: Converting Node Type Relu
23/153: Converting Node Type Conv
24/153: Converting Node Type BatchNormalization
25/153: Converting Node Type Add
26/153: Converting Node Type Conv
27/153: Converting Node Type BatchNormalization
28/153: Converting Node Type Relu
29/153: Converting Node Type Conv
30/153: Converting Node Type BatchNormalization
31/153: Converting Node Type Relu
32/153: Converting Node Type Conv
33/153: Converting Node Type BatchNormalization
34/153: Converting Node Type Conv
35/153: Converting Node Type BatchNormalization
36/153: Converting Node Type Relu
37/153: Converting Node Type Conv
38/153: Converting Node Type BatchNormalization
39/153: Converting Node Type Relu
40/153: Converting Node Type Conv
41/153: Converting Node Type BatchNormalization
42/153: Converting Node Type Add
43/153: Converting Node Type Conv
44/153: Converting Node Type BatchNormalization
45/153: Converting Node Type Relu
46/153: Converting Node Type Conv
47/153: Converting Node Type BatchNormalization
48/153: Converting Node Type Relu
49/153: Converting Node Type Conv
50/153: Converting Node Type BatchNormalization
51/153: Converting Node Type Add
52/153: Converting Node Type Conv
53/153: Converting Node Type BatchNormalization
54/153: Converting Node Type Relu
55/153: Converting Node Type Conv
56/153: Converting Node Type BatchNormalization
57/153: Converting Node Type Relu
58/153: Converting Node Type Conv
59/153: Converting Node Type BatchNormalization
60/153: Converting Node Type Conv
61/153: Converting Node Type BatchNormalization
62/153: Converting Node Type Relu
63/153: Converting Node Type Conv
64/153: Converting Node Type BatchNormalization
65/153: Converting Node Type Relu
66/153: Converting Node Type Conv
67/153: Converting Node Type BatchNormalization
68/153: Converting Node Type Add
69/153: Converting Node Type Conv
70/153: Converting Node Type BatchNormalization
71/153: Converting Node Type Relu
72/153: Converting Node Type Conv
73/153: Converting Node Type BatchNormalization
74/153: Converting Node Type Relu
75/153: Converting Node Type Conv
76/153: Converting Node Type BatchNormalization
77/153: Converting Node Type Add
78/153: Converting Node Type Conv
79/153: Converting Node Type BatchNormalization
80/153: Converting Node Type Relu
81/153: Converting Node Type Conv
82/153: Converting Node Type BatchNormalization
83/153: Converting Node Type Relu
84/153: Converting Node Type Conv
85/153: Converting Node Type BatchNormalization
86/153: Converting Node Type Add
87/153: Converting Node Type Conv
88/153: Converting Node Type BatchNormalization
89/153: Converting Node Type Relu
90/153: Converting Node Type Conv
91/153: Converting Node Type BatchNormalization
92/153: Converting Node Type Relu
93/153: Converting Node Type Conv
94/153: Converting Node Type BatchNormalization
95/153: Converting Node Type Conv
96/153: Converting Node Type BatchNormalization
97/153: Converting Node Type Relu
98/153: Converting Node Type Conv
99/153: Converting Node Type BatchNormalization
100/153: Converting Node Type Relu
101/153: Converting Node Type Conv
102/153: Converting Node Type BatchNormalization
103/153: Converting Node Type Add
104/153: Converting Node Type Conv
105/153: Converting Node Type BatchNormalization
106/153: Converting Node Type Relu
107/153: Converting Node Type Conv
108/153: Converting Node Type BatchNormalization
109/153: Converting Node Type Relu
110/153: Converting Node Type Conv
111/153: Converting Node Type BatchNormalization
112/153: Converting Node Type Add
113/153: Converting Node Type Conv
114/153: Converting Node Type BatchNormalization
115/153: Converting Node Type Relu
116/153: Converting Node Type Conv
117/153: Converting Node Type BatchNormalization
118/153: Converting Node Type Relu
119/153: Converting Node Type Conv
120/153: Converting Node Type BatchNormalization
121/153: Converting Node Type Conv
122/153: Converting Node Type BatchNormalization
123/153: Converting Node Type Relu
124/153: Converting Node Type Conv
125/153: Converting Node Type BatchNormalization
126/153: Converting Node Type Relu
127/153: Converting Node Type Conv
128/153: Converting Node Type BatchNormalization
129/153: Converting Node Type Add
130/153: Converting Node Type Conv
131/153: Converting Node Type BatchNormalization
132/153: Converting Node Type Relu
133/153: Converting Node Type Conv
134/153: Converting Node Type BatchNormalization
135/153: Converting Node Type Relu
136/153: Converting Node Type Conv
137/153: Converting Node Type BatchNormalization
138/153: Converting Node Type Add
139/153: Converting Node Type Conv
140/153: Converting Node Type BatchNormalization
141/153: Converting Node Type Relu
142/153: Converting Node Type Conv
143/153: Converting Node Type BatchNormalization
144/153: Converting Node Type Relu
145/153: Converting Node Type Conv
146/153: Converting Node Type BatchNormalization
147/153: Converting Node Type Conv
148/153: Converting Node Type BatchNormalization
149/153: Converting Node Type Relu
150/153: Converting Node Type AveragePool
151/153: Converting Node Type Conv
152/153: Converting Node Type Reshape
153/153: Converting Node Type Softmax
Translation to CoreML spec completed. Now compiling the CoreML model.
Model Compilation done.

需要注意的是 在输入输出的时候 你可能需要查看 .onnx .mlmodel等输入输出的变量等可以使用

netron 这个软件 如图
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值