Skip to content

Commit 12e8d51

Browse files
authored
DeepLab V3 demo on Android
Moved the project from [ExecuTorch](https://github.com/pytorch/executorch/tree/refs/tags/v0.5.0-rc3/examples/demo-apps/android/ExecuTorchDemo). Add the script to export the model and run the model on python. Add the instrumentation test for android.
1 parent bb147c6 commit 12e8d51

33 files changed

+1291
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
.idea
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild
9+
.cxx
10+
local.properties
11+
*.so
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ExecuTorch Android Demo App
2+
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+
5+
## Prerequisites
6+
* Download and install [Android Studio and SDK 34](https://developer.android.com/studio).
7+
* (For exporting the DL3 model) Python 3.10+ with `executorch` package installed.
8+
9+
## Exporting the model
10+
Run the script in `dl3/python/export.py` to export the model.
11+
12+
## Push the model to the phone
13+
The app loads a hardcoded model path (`/data/local/tmp/dl3_xnnpack_fp32.pte`) on the phone.
14+
Run the following adb command to push the model.
15+
```
16+
adb push dl3_xnnpack_fp32.pte /data/local/tmp/dl3_xnnpack_fp32.pte
17+
```
18+
19+
## Build and install to your phone
20+
(`cd dl3/android/DeepLanV3Demo` first)
21+
```
22+
./gradlew installDebug
23+
```
24+
25+
## Run unit test
26+
```
27+
./gradlew connectedAndroidTest
28+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
plugins {
10+
id("com.android.application")
11+
id("org.jetbrains.kotlin.android")
12+
}
13+
14+
android {
15+
namespace = "org.pytorch.executorchexamples.dl3"
16+
compileSdk = 34
17+
18+
defaultConfig {
19+
applicationId = "org.pytorch.executorchexamples.dl3"
20+
minSdk = 24
21+
targetSdk = 34
22+
versionCode = 1
23+
versionName = "1.0"
24+
25+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
26+
vectorDrawables { useSupportLibrary = true }
27+
}
28+
29+
buildTypes {
30+
release {
31+
isMinifyEnabled = false
32+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
33+
}
34+
}
35+
compileOptions {
36+
sourceCompatibility = JavaVersion.VERSION_1_8
37+
targetCompatibility = JavaVersion.VERSION_1_8
38+
}
39+
kotlinOptions { jvmTarget = "1.8" }
40+
buildFeatures { compose = true }
41+
composeOptions { kotlinCompilerExtensionVersion = "1.4.3" }
42+
}
43+
44+
dependencies {
45+
implementation("androidx.appcompat:appcompat:1.7.0")
46+
implementation("androidx.constraintlayout:constraintlayout:2.2.0")
47+
implementation("org.pytorch:executorch-android:0.5.1")
48+
testImplementation("junit:junit:4.13.2")
49+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
50+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
51+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
52+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
package org.pytorch.executorchexamples.dl3;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
import androidx.test.filters.SmallTest;
14+
import org.junit.Test;
15+
import org.pytorch.executorch.Module;
16+
import org.pytorch.executorch.Tensor;
17+
18+
@SmallTest
19+
public class SanityCheck {
20+
21+
@Test
22+
public void testModuleForward() {
23+
Module module = Module.load("/data/local/tmp/dl3_xnnpack_fp32.pte");
24+
// Test with sample inputs (ones) and make sure there is no crash.
25+
Tensor outputTensor = module.forward()[0].toTensor();
26+
long[] shape = outputTensor.shape();
27+
// batch_size * classes * width * height
28+
assertEquals(4, shape.length);
29+
assertEquals(1, shape[0]);
30+
assertEquals(21, shape[1]);
31+
assertEquals(224, shape[2]);
32+
assertEquals(224, shape[3]);
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="org.pytorch.executorchexamples.dl3">
5+
6+
<uses-sdk android:minSdkVersion="19"
7+
android:targetSdkVersion="34"
8+
android:maxSdkVersion="40" />
9+
10+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
11+
12+
<application
13+
android:allowBackup="false"
14+
android:dataExtractionRules="@xml/data_extraction_rules"
15+
android:fullBackupContent="@xml/backup_rules"
16+
android:icon="@mipmap/ic_launcher"
17+
android:label="@string/app_name"
18+
android:roundIcon="@mipmap/ic_launcher_round"
19+
android:supportsRtl="true"
20+
android:theme="@style/Theme.DeepLabV3Demo"
21+
android:extractNativeLibs="true"
22+
tools:targetApi="34">
23+
24+
<activity
25+
android:name=".MainActivity"
26+
android:exported="true"
27+
android:label="@string/app_name"
28+
android:theme="@style/Theme.DeepLabV3Demo">
29+
<intent-filter>
30+
<action android:name="android.intent.action.MAIN" />
31+
<category android:name="android.intent.category.LAUNCHER" />
32+
</intent-filter>
33+
</activity>
34+
</application>
35+
36+
</manifest>
8.49 KB
Loading
29.4 KB
Loading
36.1 KB
Loading

0 commit comments

Comments
 (0)