You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dl3/android/DeepLabV3Demo/README.md
+70-38Lines changed: 70 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,26 @@
2
2
3
3
This guide explains how to setup ExecuTorch for Android using a demo app. The app employs a [DeepLab v3](https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/) model for image segmentation tasks. Models are exported to ExecuTorch using [XNNPACK FP32 backend](https://pytorch.org/executorch/main/backends-xnnpack.html#xnnpack-backend).
4
4
5
+
## Features
6
+
-**Image Segmentation**: Detects and highlights all 21 PASCAL VOC classes (Person, Dog, Cat, Car, etc.)
7
+
-**Overlay Visualization**: Segmentation mask blends with the original image at 50% opacity
8
+
-**Inference Time Display**: Shows model inference latency in milliseconds
9
+
-**In-App Model Download**: Download the model directly from the app
10
+
-**Image Picker**: Select any image from your device's gallery
11
+
-**Sample Images**: 3 built-in sample images for quick testing
12
+
5
13
## Prerequisites
6
14
* Download and install [Android Studio and SDK 34](https://developer.android.com/studio).
7
15
* (For exporting the DL3 model) Python 3.10+ with `executorch` package installed.
8
16
9
-
## Step 1: Export the model
10
-
Run the script in `dl3/python/export.py` to export the model.
17
+
## Step 1: Export the Model (Optional)
18
+
The app can download the model automatically. If you want to export it yourself:
19
+
```bash
20
+
cd dl3/python
21
+
python export.py
22
+
```
11
23
12
-
## Step 2: Set up your device or emulator
13
-
You can run the app on either a physical device or an emulator. To set up your device or emulator, follow these steps:
24
+
## Step 2: Set Up Your Device or Emulator
14
25
15
26
### Using a Physical Device
16
27
* Connect your device to your computer via USB.
@@ -20,52 +31,73 @@ You can run the app on either a physical device or an emulator. To set up your d
20
31
* Open Android Studio and create a new virtual device.
21
32
* Start the emulator by clicking the "Play" button next to the device name.
22
33
23
-
## Step 3: Build, install, and run the app on your phone
24
-
### On your terminal
25
-
(`cd dl3/android/DeepLabV3Demo` first)
26
-
```
34
+
## Step 3: Build and Run the App
35
+
36
+
### Using Terminal
37
+
```bash
38
+
cd dl3/android/DeepLabV3Demo
27
39
./gradlew installDebug
28
-
adb shell am start -W -S -n org.pytorch.executorchexamples.dl3/.MainActivity
40
+
adb shell am start -n org.pytorch.executorchexamples.dl3/.MainActivity
29
41
```
30
42
31
-
### On Android Studio
32
-
Open Android Studio and open the project path `dl3/android/DeepLabV3Demo`. Wait for gradle sync to complete.
33
-
Then simply press "Run app" button (Control + r) to run the app either on physical device / emulator.
43
+
### Using Android Studio
44
+
1. Open the project at `dl3/android/DeepLabV3Demo`
45
+
2. Wait for Gradle sync to complete
46
+
3. Click "Run app" (Control + R)
34
47
35
-
## Step 4: Push the model to the phone or emulator
36
-
The app loads a hardcoded model path (`/data/local/tmp/dl3_xnnpack_fp32.pte`) on the phone.
Note: If you want to use a QNN lowered model, you mush modify the maven executorch dependency to [executorch-qnn](https://mvnrepository.com/artifact/org.pytorch/executorch-android-qnn) and rebuild the app.
50
+
### Option A: Download from the App (Recommended)
51
+
When the app launches, tap the **"Download Model"** button. The model will be downloaded and extracted automatically.
43
52
44
-
##Step 5: Load and Test Custom Images (No APK Rebuild Needed)
45
-
You can now test image segmentation on your own images (supported formats: .jpg, .jpeg, .png) without rebuilding the APK. The app supports loading .jpg, .jpeg, and .png images from the /sdcard/Pictures/ directory, with user-granted permissions.
53
+
### Option B: Export and Push Manually
54
+
If you exported the model yourself or want to use a custom model, you need to copy it to the app's private storage. Since the app is built in debug mode, we can use `run-as`:
- If prompted, grant the app permission to access the /sdcard/Pictures/ folder.
56
-
- The image should appear immediately in the app.
57
-
- You can now run dynamic image segmentation and view the results.
58
63
59
-
Tip: You can use images containing Dog, Sheep, Person, or any combination of these classes.
64
+
> **Note:** For QNN backend, change the maven dependency to [executorch-qnn](https://mvnrepository.com/artifact/org.pytorch/executorch-android-qnn) and rebuild the app.
60
65
61
-
This feature enhances the testing/validation process by allowing users to quickly test different images without the need to rebuild the application.
66
+
## Step 5: Using the App
62
67
63
-
## Step 6: Run unit test
64
-
### On your terminal
65
-
```
68
+
### Sample Images
69
+
Tap **"Next sample image"** to cycle through 3 built-in sample images.
70
+
71
+
### Pick Your Own Image
72
+
1. Tap **"Pick Image"** to open your device's gallery
73
+
2. Select any image (it will be automatically resized to 224x224)
74
+
3. Tap **"Run"** to perform segmentation
75
+
76
+
### Run Segmentation
77
+
1. Tap **"Run"** to start inference
78
+
2. The segmentation overlay appears blended with the original image
79
+
3. Inference time is displayed below the image
80
+
81
+
### Reset
82
+
Tap **"Reset"** to restore the original image without the segmentation overlay.
83
+
84
+
## Supported Classes
85
+
The app detects all 21 PASCAL VOC classes with distinct color overlays:
86
+
87
+
| Class | Color | Class | Color |
88
+
|-------|-------|-------|-------|
89
+
| Person | Red | Dog | Green |
90
+
| Cat | Magenta | Car | Cyan |
91
+
| Bird | Yellow | Bicycle | Green |
92
+
| Boat | Blue | Bottle | Orange |
93
+
| And 13 more... ||||
94
+
95
+
## Step 6: Run Unit Tests
96
+
97
+
### Using Terminal
98
+
```bash
66
99
./gradlew connectedAndroidTest
67
100
```
68
101
69
-
### On Android Studio
70
-
In Android Studio project, open file `app/src/androidTest/java/org/pytorch/executorchexamples/dl3/SanityCheck.java`,
71
-
and click the "Play" button for `public class SanityCheck` (Control + Shift + r).
102
+
### Using Android Studio
103
+
Open `app/src/androidTest/java/org/pytorch/executorchexamples/dl3/SanityCheck.java` and click the Play button.
0 commit comments