Skip to content

Commit fc389c5

Browse files
committed
Redirecting Model Preparation for Android Recipe to Executorch.
1 parent eaf9699 commit fc389c5

File tree

1 file changed

+4
-79
lines changed

1 file changed

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

4-
This recipe demonstrates how to prepare a PyTorch MobileNet v2 image classification model for Android apps, and how to set up Android projects to use the mobile-ready model file.
4+
This tutorial has been deprecated. There is a new tutorial on this topic.
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 the `Quantization Recipe <quantization.html>`_), converted to TorchScript so Android apps can load it, and optimized for mobile apps. Furthermore, Android 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-
Android Studio 3.5.1 or above with NDK installed
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-
53-
.. warning::
54-
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.
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 Android
67-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68-
69-
* In your current or a new Android Studio project, open the build.gradle file, and add the following two lines (the second one is required only if you plan to use a TorchVision model):
70-
71-
::
72-
73-
implementation 'org.pytorch:pytorch_android:1.6.0'
74-
implementation 'org.pytorch:pytorch_android_torchvision:1.6.0'
75-
76-
* Drag and drop the model file `mobilenetv2_quantized.pt` to your project's assets folder.
77-
78-
That's it! Now you can build your Android app with the PyTorch library and the model ready to use. To actually write code to use the model, refer to the PyTorch Mobile `Android Quickstart with a HelloWorld Example <https://pytorch.org/mobile/android/#quickstart-with-a-helloworld-example>`_ and `Android Hackathon Example <https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruAndroid>`_.
79-
80-
Learn More
81-
-----------------
82-
83-
1. `PyTorch Mobile site <https://pytorch.org/mobile>`_
84-
85-
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)