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 這個軟件 如圖
在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章