Skip to content

Commit 2283a3e

Browse files
authored
[iOS] Add the Core ML tutorial (pytorch#1707)
* [iOS] Add the Core ML tutorial * fix the rst syntax error * rephrase the introduction
1 parent e35ec76 commit 2283a3e

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
(Prototype) Convert Mobilenetv2 to Core ML
2+
==========================================
3+
4+
**Author**: `Tao Xu <https://github.com/xta0>`_
5+
6+
Introduction
7+
------------
8+
9+
Core ML provides access to powerful and efficient NPUs(Neural Process Unit) on modern iPhone devices. This tutorial shows how to prepare a computer vision model (mobilenetv2) to use the PyTorch Core ML mobile backend.
10+
11+
Note that this feature is currently in the “prototype” phase and only supports a limited numbers of operators, but we expect to solidify the integration and expand our operator support over time. The APIs are subject to change in the future.
12+
13+
Environment Setup (MacOS)
14+
-------------------------
15+
16+
Let's start off by creating a new conda environment.
17+
18+
.. code:: shell
19+
20+
conda create --name 1.10 python=3.8 --yes
21+
conda activate 1.10
22+
23+
Next, since the Core ML delegate is a prototype feature, let's install the PyTorch nightly build and coremltools
24+
25+
.. code:: shell
26+
27+
pip3 install --pre torch torchvision torchaudio -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
28+
29+
pip3 install coremltools==5.0b5
30+
31+
32+
Model Preparation
33+
-------------------
34+
35+
To convert a pre-trained mobilenetv2 model to be Core ML compatible, we're going to use the ``to_backend()`` API, which is a prototype feature for delegating model executions to some specific backends. The following python code shows how to use it to convert the mobilenetv2 torchscript model.
36+
37+
.. code:: python
38+
39+
import torch
40+
import torchvision
41+
42+
from torch.backends._coreml.preprocess import (
43+
CompileSpec,
44+
TensorSpec,
45+
CoreMLComputeUnit,
46+
)
47+
48+
def mobilenetv2_spec():
49+
return {
50+
"forward": CompileSpec(
51+
inputs=(
52+
TensorSpec(
53+
shape=[1, 3, 224, 224],
54+
),
55+
),
56+
outputs=(
57+
TensorSpec(
58+
shape=[1, 1000],
59+
),
60+
),
61+
backend=CoreMLComputeUnit.ALL,
62+
allow_low_precision=True,
63+
),
64+
}
65+
66+
67+
def main():
68+
model = torchvision.models.mobilenet_v2(pretrained=True)
69+
model.eval()
70+
example = torch.rand(1, 3, 224, 224)
71+
model = torch.jit.trace(model, example)
72+
compile_spec = mobilenetv2_spec()
73+
mlmodel = torch._C._jit_to_backend("coreml", model, compile_spec)
74+
mlmodel._save_for_lite_interpreter("./mobilenetv2_coreml.ptl")
75+
76+
77+
if __name__ == "__main__":
78+
main()
79+
80+
81+
First, we need to call ``.eval()`` to set the model to inference mode. Secondly, we defined a ``mobilenetv2_spec()`` function to tell Core ML what the model looks like. Note that the ``CoreMLComputeUnit`` corresponds to `Apple's processing unit <https://developer.apple.com/documentation/coreml/mlcomputeunits>`_ whose value can be ``CPU``, ``CPUAndGPU`` and ``ALL``. In our example, we set the ``backend`` type to ``ALL`` which means Core ML will try to run the model on Neural Engine. Finally, we called the ``to_backend`` API to convert the torchscript model to a Core ML compatible model and save it to the disk.
82+
83+
Run the python script. If everything works well, you should see following outputs from coremltools
84+
85+
.. code:: shell
86+
87+
Converting Frontend ==> MIL Ops: 100%|███████████████████████████████████████████████████████████████████████████████▊| 384/385 [00:00<00:00, 1496.98 ops/s]
88+
Running MIL Common passes: 0%|
89+
0/33 [00:00<?, ? passes/s]/Users/distill/anaconda3/envs/1.10/lib/python3.8/site-packages/coremltools/converters/mil/mil/passes/name_sanitization_utils.py:129: UserWarning: Output, '647', of the source model, has been renamed to 'var_647' in the Core ML model.
90+
warnings.warn(msg.format(var.name, new_name))
91+
Running MIL Common passes: 100%|███████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:00<00:00, 84.16 passes/s]
92+
Running MIL Clean up passes: 100%|██████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 138.17 passes/s]
93+
Translating MIL ==> NeuralNetwork Ops: 100%|██████████████████████████████████████████████████████████████████████████| 495/495 [00:00<00:00, 1977.15 ops/s]
94+
[W backend_detail.cpp:376] Warning: Backend [coreml] is not available. Execution of this Module is still possible by saving and loading on a device where the backend is available. (function codegen_backend_module)
95+
96+
We can safely ignore the warning above, as we don't plan to run our model on desktop.
97+
98+
iOS app integration
99+
---------------------
100+
101+
Now that the model is ready, we can integrate it to our app. We'll be using the pytorch nightly cocoapods which contains the code for executing the Core ML model. Simply add the following code to your Podfile
102+
103+
.. code:: shell
104+
105+
pod LibTorch-Lite-Nightly
106+
107+
In this tutorial, we'll be reusing our `HelloWorld <https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld-CoreML>`_ project. Feel free to walk through the code there.
108+
109+
To benchmark the latency, you can simply put the following code before and after the PyTorch ``forward`` function
110+
111+
.. code:: objective-c
112+
113+
caffe2::Timer t;
114+
auto outputTensor = _impl.forward({tensor}).toTensor().cpu();
115+
std::cout << "forward took: " << t.MilliSeconds() << std::endl;
116+
117+
Conclusion
118+
----------
119+
120+
In this tutorial, we demonstrated how to convert a mobilenetv2 model to a Core ML compatible model. Please be aware of that Core ML feature is still under development, new operators/models will continue to be added. APIs are subject to change in the future versions.
121+
122+
Thanks for reading! As always, we welcome any feedback, so please create an issue `here <https://github.com/pytorch/pytorch/issues>`_ if you have any.
123+
124+
Learn More
125+
----------
126+
127+
- The `Mobilenetv2 <https://pytorch.org/hub/pytorch_vision_mobilenet_v2/>`_ from Torchvision
128+
- Information about `Core ML <https://developer.apple.com/documentation/coreml>`_

0 commit comments

Comments
 (0)