Skip to content

Commit ac7667c

Browse files
committed
Redirecting Model Preparation for iOS Recipe to Executorch.
1 parent 8ce0de5 commit ac7667c

File tree

1 file changed

+4
-89
lines changed

1 file changed

+4
-89
lines changed
Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,10 @@
11
Model Preparation for iOS Recipe
22
=====================================
33

4-
This recipe demonstrates how to prepare a PyTorch MobileNet v2 image classification model for iOS apps, and how to set up an iOS project to use the mobile-ready model file.
4+
PyTorch Mobile is no longer actively supported. Please check out Executorch.
55

6-
Introduction
7-
-----------------
6+
Redirecting in 3 seconds...
87

9-
After a PyTorch model is trained or a pre-trained model is made available, it is normally not ready to be used in mobile apps yet. It needs to be quantized (see `Quantization Recipe <quantization.html>`_ for more details), converted to TorchScript so iOS apps can load it and optimized for mobile apps (see `Script and Optimize for Mobile Recipe <script_optimized.html>`_). Furthermore, iOS apps need to be set up correctly to enable the use of PyTorch Mobile libraries, before they can load and use the model for inference.
8+
.. raw:: html
109

11-
Pre-requisites
12-
-----------------
13-
14-
PyTorch 1.6.0 or 1.7.0
15-
16-
torchvision 0.6.0 or 0.7.0
17-
18-
Xcode 11 or 12
19-
20-
Steps
21-
-----------------
22-
23-
1. Get Pretrained and Quantized MobileNet v2 Model
24-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
26-
To get the MobileNet v2 quantized model, simply do:
27-
28-
::
29-
30-
import torchvision
31-
32-
model_quantized = torchvision.models.quantization.mobilenet_v2(pretrained=True, quantize=True)
33-
34-
2. Script and Optimize the Model for Mobile Apps
35-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36-
37-
Use either the script or trace method to convert the quantized model to the TorchScript format:
38-
39-
::
40-
41-
import torch
42-
43-
dummy_input = torch.rand(1, 3, 224, 224)
44-
torchscript_model = torch.jit.trace(model_quantized, dummy_input)
45-
46-
or
47-
48-
::
49-
50-
torchscript_model = torch.jit.script(model_quantized)
51-
52-
.. warning::
53-
The `trace` method only scripts the code path executed during the trace, so it will not work properly for models that include decision branches. See the `Script and Optimize for Mobile Recipe <script_optimized.html>`_ for more details.
54-
55-
56-
Then optimize the TorchScript formatted model for mobile and save it:
57-
58-
::
59-
60-
from torch.utils.mobile_optimizer import optimize_for_mobile
61-
torchscript_model_optimized = optimize_for_mobile(torchscript_model)
62-
torch.jit.save(torchscript_model_optimized, "mobilenetv2_quantized.pt")
63-
64-
With the total 7 or 8 (depending on if the script or trace method is called to get the TorchScript format of the model) lines of code in the two steps above, we have a model ready to be added to mobile apps.
65-
66-
3. Add the Model and PyTorch Library on iOS
67-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68-
69-
To use the mobile-ready model `mobilenetv2_quantized.pt` in an iOS app, either create a new Xcode project or in your existing Xcode project, then follow the steps below:
70-
71-
* Open a Mac Terminal, cd to your iOS app's project folder;
72-
73-
* If your iOS app does not use Cocoapods yet, run `pod init` first to generate the `Podfile` file.
74-
75-
* Edit `Podfile` either from Xcode or any editor, and add the following line under the target:
76-
77-
::
78-
79-
pod 'LibTorch', '~>1.6.1'
80-
81-
* Run `pod install` from the Terminal and then open your project's xcworkspace file;
82-
83-
* Save the two files `TorchModule.h` and `TorchModule.mm` from `here <https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld/HelloWorld/HelloWorld/TorchBridge>`_ and drag and drop them to your project. If your project is Swift based, a message box with the title "Would you like to configure an Objective-C bridging header?" will show up; click the "Create Bridging Header" button to create a Swift to Objective-c bridging header file, and add `#import "TorchModule.h"` to the header file `<your_project_name>-Bridging-Header.h`;
84-
85-
* Drag and drop the model file `mobilenetv2_quantized.pt` to the project.
86-
87-
After these steps, you can successfully build and run your Xcode project. To actually write code to use the model, refer to the PyTorch Mobile `iOS Code Walkthrough <https://pytorch.org/mobile/ios/#code-walkthrough>`_ and two complete ready-to-run sample iOS apps `HelloWorld <https://github.com/pytorch/ios-demo-app/tree/master/HelloWorld>`_ and `iOS Hackathon Example <https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruIOS>`_.
88-
89-
90-
Learn More
91-
-----------------
92-
93-
1. `PyTorch Mobile site <https://pytorch.org/mobile>`_
94-
95-
2. `Introduction to TorchScript <https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html>`_
10+
<meta http-equiv="Refresh" content="3; url='https://pytorch.org/executorch/stable/index.html'" />

0 commit comments

Comments
 (0)