|
| 1 | +# Building an ExecuTorch iOS Demo App |
| 2 | + |
| 3 | +Welcome to the tutorial on setting up the ExecuTorch iOS Demo App! |
| 4 | + |
| 5 | +This app uses the |
| 6 | +[MobileNet v3](https://pytorch.org/vision/main/models/mobilenetv3.html) model to |
| 7 | +process live camera images leveraging three different backends: |
| 8 | +[XNNPACK](https://github.com/google/XNNPACK), |
| 9 | +[Core ML](https://developer.apple.com/documentation/coreml) and |
| 10 | +[Metal Performance Shaders (MPS)](https://developer.apple.com/documentation/metalperformanceshaders) |
| 11 | +(Xcode 15+ and iOS 17+ only). |
| 12 | + |
| 13 | +<p align="center"> |
| 14 | + <img src="https://github.com/pytorch/executorch/blob/main/docs/source/_static/img/demo_ios_app.png" width="50%"> |
| 15 | +</p> |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before we start, make sure you have the following tools installed: |
| 20 | + |
| 21 | +### 1. Xcode 15+ and Command Line Tools |
| 22 | + |
| 23 | +Install Xcode 15+ from the |
| 24 | +[Mac App Store](https://apps.apple.com/app/xcode/id497799835) and then install |
| 25 | +the Command Line Tools using the terminal: |
| 26 | + |
| 27 | +```bash |
| 28 | +xcode-select --install |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Python 3.10+ |
| 32 | + |
| 33 | +Python 3.10 or above, along with `pip3`, should be pre-installed on MacOS 13.5+. |
| 34 | +If needed, [download Python](https://www.python.org/downloads/macos/) and |
| 35 | +install it. Verify the Python and pip versions using these commands: |
| 36 | + |
| 37 | +```bash |
| 38 | +which python3 pip3 |
| 39 | +python3 --version |
| 40 | +pip3 --version |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Check Swift Package Manager version |
| 44 | +The prebuilt ExecuTorch runtime, backend, and kernels are available as a Swift PM |
| 45 | +package. Ensure the latest SwiftPM version is linked to your Xcode installation. |
| 46 | +The steps to add the necessary package dependency are available in the |
| 47 | +[Using ExecuTorch for iOS](https://pytorch.org/executorch/main/using-executorch-ios.html#swift-package-manager) documentation. |
| 48 | + |
| 49 | +### 4. Set Up ExecuTorch |
| 50 | + |
| 51 | +Clone ExecuTorch and set up the environment as explained in the [Building from Source tutorial](https://pytorch.org/executorch/main/using-executorch-building-from-source.html): |
| 52 | + |
| 53 | +```bash |
| 54 | +git clone -b viable/strict https://github.com/pytorch/executorch.git && cd executorch |
| 55 | + |
| 56 | +python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip |
| 57 | + |
| 58 | +./install_executorch.sh |
| 59 | +``` |
| 60 | + |
| 61 | +### 5. Backend Dependencies |
| 62 | + |
| 63 | +Install additional dependencies for [Core ML](https://pytorch.org/executorch/main/backends-coreml.html) and [MPS](https://pytorch.org/executorch/main/backends-mps.html) backends: |
| 64 | + |
| 65 | +```bash |
| 66 | +./backends/apple/coreml/scripts/install_requirements.sh |
| 67 | + |
| 68 | +./backends/apple/mps/install_requirements.sh |
| 69 | +``` |
| 70 | + |
| 71 | +### 6. Clone the Demo App |
| 72 | + |
| 73 | +```bash |
| 74 | +git clone --depth 1 https://github.com/pytorch-labs/executorch-examples.git |
| 75 | +``` |
| 76 | + |
| 77 | +## Models and Labels |
| 78 | + |
| 79 | +Now, let's move on to exporting and bundling the MobileNet v3 model. |
| 80 | + |
| 81 | +### 1. Export Model |
| 82 | + |
| 83 | +Export the MobileNet v3 model using the command line and move the exported model to |
| 84 | +a specific location where the Demo App will pick them up: |
| 85 | + |
| 86 | +```bash |
| 87 | +python3 -m examples.portable.scripts.export --model_name="mv3" |
| 88 | + |
| 89 | +APP_PATH="executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo" |
| 90 | +mkdir -p "$APP_PATH/Resources/Models/MobileNet/" |
| 91 | +mv mv3.pte "$APP_PATH/Resources/Models/MobileNet/" |
| 92 | +``` |
| 93 | + |
| 94 | +Next, export the MobileNet v3 model with Core ML, MPS and XNNPACK backends by runnning this [`export.py`](https://github.com/pytorch-labs/executorch-examples/mv3/apple/ExecuTorchDemo/export.py) script. |
| 95 | + |
| 96 | +```bash |
| 97 | +python3 export.py |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +### 2. Download Labels |
| 102 | + |
| 103 | +Download the MobileNet model labels required for image classification: |
| 104 | + |
| 105 | +```bash |
| 106 | +curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \ |
| 107 | + -o "$APP_PATH/Resources/Models/MobileNet/imagenet_classes.txt" |
| 108 | +``` |
| 109 | + |
| 110 | +## Final Steps |
| 111 | + |
| 112 | +We're almost done! Now, we just need to open the project in Xcode, run the |
| 113 | +tests, and finally run the app. |
| 114 | + |
| 115 | +### 1. Open Project in Xcode |
| 116 | + |
| 117 | +Double-click on the project file under |
| 118 | +`executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo` to openit with Xcode, or run the command: |
| 119 | + |
| 120 | +```bash |
| 121 | +open $APP_PATH.xcodeproj |
| 122 | +``` |
| 123 | + |
| 124 | +### 2. Run Tests |
| 125 | + |
| 126 | +You can run tests on Simulaltor directly in Xcode with `Cmd + U` or use the |
| 127 | +command line: |
| 128 | + |
| 129 | +```bash |
| 130 | +xcrun simctl create executorch "iPhone 15" |
| 131 | +xcodebuild clean test \ |
| 132 | + -project executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo.xcodeproj \ |
| 133 | + -scheme App \ |
| 134 | + -destination name=executorch |
| 135 | +xcrun simctl delete executorch |
| 136 | +``` |
| 137 | + |
| 138 | +### 3. Run App |
| 139 | + |
| 140 | +Finally, connect the device, set up Code Signing in Xcode, and then run the app |
| 141 | +using `Cmd + R`. Try installing a Release build for better performance. |
| 142 | + |
| 143 | +Congratulations! You've successfully set up the ExecuTorch iOS Demo App. Now, |
| 144 | +you can explore and enjoy the power of ExecuTorch on your iOS device! |
| 145 | + |
| 146 | +Learn more about integrating and running [ExecuTorch on Apple](https://pytorch.org/executorch/main/using-executorch-ios.html) platforms. |
| 147 | + |
| 148 | +For specific examples, take a look at this [GitHub repository](https://github.com/pytorch-labs/executorch-examples/mv3/apple/ExecuTorchDemo/). |
0 commit comments