Skip to content

Commit d4f9ecc

Browse files
committed
[android] Update HelloWorld android tutorial
1 parent 6525ec2 commit d4f9ecc

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

_mobile/android.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This application runs TorchScript serialized TorchVision pretrained resnet18 mod
1717

1818
#### 1. Model Preparation
1919

20-
Let’s start with model preparation. If you are familiar with PyTorch, you probably should already know how to train and save your model. In case you don’t, we are going to use a pre-trained image classification model ([Resnet18](https://pytorch.org/hub/pytorch_vision_resnet/)), which is packaged in [TorchVision](https://pytorch.org/docs/stable/torchvision/index.html).
20+
Let’s start with model preparation. If you are familiar with PyTorch, you probably should already know how to train and save your model. In case you don’t, we are going to use a pre-trained image classification model ([MobileNetV2](https://pytorch.org/hub/pytorch_vision_mobilenet_v2/)).
2121
To install it, run the command below:
2222
```
2323
pip install torchvision
@@ -27,12 +27,14 @@ To serialize the model you can use python [script](https://github.com/pytorch/an
2727
```
2828
import torch
2929
import torchvision
30+
from torch.utils.mobile_optimizer import optimize_for_mobile
3031
31-
model = torchvision.models.resnet18(pretrained=True)
32+
model = torchvision.models.mobilenet_v2(pretrained=True)
3233
model.eval()
3334
example = torch.rand(1, 3, 224, 224)
3435
traced_script_module = torch.jit.trace(model, example)
35-
traced_script_module.save("app/src/main/assets/model.pt")
36+
traced_script_module_optimized = optimize_for_mobile(traced_script_module)
37+
traced_script_module_optimized.save("app/src/main/assets/model.pt")
3638
```
3739
If everything works well, we should have our model - `model.pt` generated in the assets folder of android application.
3840
That will be packaged inside android application as `asset` and can be used on the device.
@@ -62,8 +64,8 @@ repositories {
6264
}
6365
6466
dependencies {
65-
implementation 'org.pytorch:pytorch_android:1.4.0'
66-
implementation 'org.pytorch:pytorch_android_torchvision:1.4.0'
67+
implementation 'org.pytorch:pytorch_android:1.8.0'
68+
implementation 'org.pytorch:pytorch_android_torchvision:1.8.0'
6769
}
6870
```
6971
Where `org.pytorch:pytorch_android` is the main dependency with PyTorch Android API, including libtorch native library for all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64).

0 commit comments

Comments
 (0)