|
1 |
| -```{include} ../../examples/demo-apps/apple_ios/ExecuTorchDemo/README.md |
| 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 | + |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before we start, make sure you have the following tools installed: |
| 18 | + |
| 19 | +### 1. Xcode 15 and Command Line Tools |
| 20 | + |
| 21 | +Install Xcode 15 from the |
| 22 | +[Mac App Store](https://apps.apple.com/app/xcode/id497799835) and then install |
| 23 | +the Command Line Tools using the terminal: |
| 24 | + |
| 25 | +```bash |
| 26 | +xcode-select --install |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Python 3.10+ |
| 30 | + |
| 31 | +Python 3.10 or above, along with `pip`, should be pre-installed on MacOS 13.5+. |
| 32 | +If needed, [download Python](https://www.python.org/downloads/macos/) and |
| 33 | +install it. Verify the Python and pip versions using these commands: |
| 34 | + |
| 35 | +```bash |
| 36 | +which python3 pip |
| 37 | +python3 --version |
| 38 | +pip --version |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Getting Started Tutorial |
| 42 | + |
| 43 | +Follow the [Setting Up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) |
| 44 | +tutorial to configure the basic environment: |
| 45 | + |
| 46 | +```bash |
| 47 | +git clone -b viable/strict https://github.com/pytorch/executorch.git && cd executorch |
| 48 | + |
| 49 | +python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip |
| 50 | + |
| 51 | +./install_executorch.sh |
| 52 | +``` |
| 53 | + |
| 54 | +### 4. Clone the Demo App |
| 55 | + |
| 56 | +```bash |
| 57 | +git clone --depth 1 https://github.com/pytorch-labs/executorch-examples.git |
| 58 | +``` |
| 59 | + |
| 60 | +### 5. Backend Dependencies |
| 61 | + |
| 62 | +Also, follow the corresponding sections from [Core ML](https://pytorch.org/executorch/stable/build-run-coreml) and |
| 63 | +[MPS](https://pytorch.org/executorch/stable/build-run-mps) tutorials to install additional dependencies for those |
| 64 | +backends: |
| 65 | + |
| 66 | +```bash |
| 67 | +./backends/apple/coreml/scripts/install_requirements.sh |
| 68 | + |
| 69 | +./backends/apple/mps/install_requirements.sh |
| 70 | +``` |
| 71 | + |
| 72 | +## Models and Labels |
| 73 | + |
| 74 | +Now, let's move on to exporting and bundling the MobileNet v3 model. |
| 75 | + |
| 76 | +### 1. Export Model |
| 77 | + |
| 78 | +Export the MobileNet v3 model with Core ML, MPS and XNNPACK backends, and move |
| 79 | +the exported model to a specific location where the Demo App will pick them up: |
| 80 | + |
| 81 | +```bash |
| 82 | +MODEL_NAME="mv3" |
| 83 | + |
| 84 | +python3 -m examples.portable.scripts.export --model_name="$MODEL_NAME" |
| 85 | +python3 -m examples.apple.coreml.scripts.export --model_name="$MODEL_NAME" |
| 86 | +python3 -m examples.apple.mps.scripts.mps_example --model_name="$MODEL_NAME" |
| 87 | +python3 -m examples.xnnpack.aot_compiler --model_name="$MODEL_NAME" --delegate |
| 88 | + |
| 89 | +APP_PATH="executorch-examples/apple/ExecuTorchDemo/ExecuTorchDemo" |
| 90 | + |
| 91 | +mkdir -p "$APP_PATH/Resources/Models/MobileNet/" |
| 92 | +mv $MODEL_NAME*.pte "$APP_PATH/Resources/Models/MobileNet/" |
| 93 | +``` |
| 94 | + |
| 95 | +### 2. Download Labels |
| 96 | + |
| 97 | +Download the MobileNet model labels required for image classification: |
| 98 | + |
| 99 | +```bash |
| 100 | +curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \ |
| 101 | + -o "$APP_PATH/Resources/Models/MobileNet/imagenet_classes.txt" |
| 102 | +``` |
| 103 | + |
| 104 | +## Final Steps |
| 105 | + |
| 106 | +We're almost done! Now, we just need to open the project in Xcode, run the |
| 107 | +tests, and finally run the app. |
| 108 | + |
| 109 | +### 1. Open Project in Xcode |
| 110 | + |
| 111 | +Double-click on the project file under `executorch-examples/apple/ExecuTorchDemo` or run the command: |
| 112 | + |
| 113 | +```bash |
| 114 | +open $APP_PATH.xcodeproj |
| 115 | +``` |
| 116 | + |
| 117 | +### 2. Run Tests |
| 118 | + |
| 119 | +You can run tests on Simulaltor directly in Xcode with `Cmd + U` or use the |
| 120 | +command line: |
| 121 | + |
| 122 | +```bash |
| 123 | +xcrun simctl create executorch "iPhone 15" |
| 124 | +xcodebuild clean test \ |
| 125 | + -project $APP_PATH.xcodeproj \ |
| 126 | + -scheme App \ |
| 127 | + -destination name=executorch |
| 128 | +xcrun simctl delete executorch |
| 129 | +``` |
| 130 | + |
| 131 | +### 3. Run App |
| 132 | + |
| 133 | +Finally, connect the device, set up Code Signing in Xcode, and then run the app |
| 134 | +using `Cmd + R`. Try installing a Release build for better performance. |
| 135 | + |
| 136 | +Congratulations! You've successfully set up the ExecuTorch iOS Demo App. Now, |
| 137 | +you can explore and enjoy the power of ExecuTorch on your iOS device! |
| 138 | + |
| 139 | +Learn more about integrating and running [ExecuTorch on Apple](https://pytorch.org/executorch/stable/apple-runtime) platforms. |
| 140 | + |
| 141 | + |
0 commit comments