Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 96548e3

Browse files
authored
small example (#490)
1 parent 65797e0 commit 96548e3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/small_example.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import torch
2+
import torch.nn as nn
3+
import torch.nn.functional as F
4+
from onnx_coreml import convert
5+
6+
# Step 0 - (a) Define ML Model
7+
class small_model(nn.Module):
8+
def __init__(self):
9+
super(small_model, self).__init__()
10+
self.fc1 = nn.Linear(768, 256)
11+
self.fc2 = nn.Linear(256, 10)
12+
13+
def forward(self, x):
14+
y = F.relu(self.fc1(x))
15+
y = F.softmax(self.fc2(y))
16+
return y
17+
18+
# Step 0 - (b) Create model or Load from distk
19+
model = small_model()
20+
dummy_input = torch.randn(768)
21+
22+
# Step 1 - PyTorch to ONNX model
23+
torch.onnx.export(model, dummy_input, './small_model.onnx')
24+
25+
# Step 2 - ONNX to CoreML model
26+
mlmodel = convert(model='./small_model.onnx', target_ios='13')
27+
# Save converted CoreML model
28+
mlmodel.save('small_model.mlmodel')

0 commit comments

Comments
 (0)