Skip to content

Commit 5bae6e8

Browse files
qaihm-botkory
authored andcommitted
v0.4.0
Signed-off-by: QAIHM Team <[email protected]>
1 parent 11b9063 commit 5bae6e8

File tree

857 files changed

+21748
-4591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

857 files changed

+21748
-4591
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.jpg filter=lfs diff=lfs merge=lfs -text
22
*.png filter=lfs diff=lfs merge=lfs -text
3+
*.jar filter=lfs diff=lfs merge=lfs -text

.gitignore

-115
This file was deleted.

README.md

+67-51
Large diffs are not rendered by default.
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### Requirements
2+
3+
1. Java, android-sdk and sdkmanager is already set at user's end
4+
2. User should have Linux QNN SDK in local machine.
5+
6+
7+
## Info
8+
Right now we use mobilenet_v3_small.tflite model which takes 224x224 as input and gives array of 1000 as output. You can replace it with any tflite classification model, but you have to change the pre-processing, post-processing and dimensions in the app code based on model parameters.
9+
10+
11+
## Preprocessing
12+
13+
14+
```
15+
for (int x = 0; x < input_dims1; x++) {
16+
for (int y = 0; y < input_dims2; y++) {
17+
int pixel = inputBitmap.getPixel(x, y);
18+
List<Float> rgb = Arrays.asList((float)Color.red(pixel), (float)Color.green(pixel), (float)Color.blue(pixel));
19+
for(int z = 0;z<3; z++){
20+
floatinputarray[0][z][x][y] = (float)((rgb.get(z))-ImageMean.get(z))/ImageStd.get(z);
21+
}
22+
}
23+
}
24+
```
25+
26+
27+
## PostProcessing
28+
29+
30+
```
31+
public static List<Integer> findTop3Indices(float[] arr) {
32+
List<Integer> topIndices = new ArrayList<>();
33+
34+
for (int i = 0; i < 3; i++) {
35+
int maxIndex = 0;
36+
float maxValue = arr[0];
37+
38+
for (int j = 1; j < arr.length; j++) {
39+
if (arr[j] > maxValue && !topIndices.contains(j)) {
40+
maxValue = arr[j];
41+
maxIndex = j;
42+
}
43+
}
44+
45+
topIndices.add(maxIndex);
46+
}
47+
48+
return topIndices;
49+
}
50+
```
51+
52+
### Build App:
53+
54+
You have to run build_apk.py for Image Classification. It will generate classification-debug.apk and install it in connected device.
55+
56+
57+
build_apk.py [-h] -q QNNSDK (-m MODEL_PATH | -e MODEL_NAME)
58+
59+
60+
61+
### Example
62+
63+
Here, with -m, give your tflite model path i.e. till `*.tflite file`, and it will copy model file to assets folder to build andoid app.
64+
```
65+
python build_apk.py -q "<QNN_SDK_PATH>" -m "Path\to\TFLITE\Model"
66+
```
67+
68+
Also, you can use AI-HUB Model name as mentioned in models directory, to directly export the model from AI-Hub and copy it to app assets.
69+
70+
```
71+
python build_apk.py -q "<QNN_SDK_PATH>" -e <Model Name>
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
3+
plugins {
4+
id 'com.android.application' version '7.2.1' apply false
5+
id 'com.android.library' version '7.2.1' apply false
6+
}
7+
8+
task clean(type: Delete) {
9+
delete rootProject.buildDir
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# ---------------------------------------------------------------------
2+
# Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
# ---------------------------------------------------------------------
5+
import argparse
6+
import glob
7+
import os
8+
import shutil
9+
import subprocess
10+
import sys
11+
from enum import Enum
12+
13+
14+
class MODELNAME(Enum):
15+
mobilenet_v3_large = 1
16+
resnet50 = 2
17+
resnext50 = 3
18+
inception_v3 = 4
19+
20+
21+
def printmenu():
22+
print("*****************************")
23+
print("* TYPE OF MODEL *")
24+
print("*****************************")
25+
for m in MODELNAME:
26+
print(str(m.value) + ". " + m.name)
27+
print("*****************************")
28+
29+
30+
## Initialize parser
31+
parser = argparse.ArgumentParser()
32+
parser.add_argument("-q", "--qnnsdk", required=True, help="Give path of QNN SDK")
33+
34+
parser.add_argument("-m", "--model_name", type=str, help="Model Name")
35+
36+
37+
# group = parser.add_mutually_exclusive_group()
38+
# group.add_argument('-stopdownload', '--stopdownload', action = "store_true", help = "Do NOT Download Model from AI HUB")
39+
parser.add_argument("-path", "--model_path", type=str, help="TFLITE model file")
40+
41+
args = parser.parse_args()
42+
43+
44+
##based on this pre-post can be decided
45+
if not args.model_name:
46+
printmenu()
47+
inp_model_name = int(input("Please select one: "))
48+
args.model_name = MODELNAME(inp_model_name).name
49+
50+
51+
destAsset = os.path.join(".", "classification", "src", "main", "assets")
52+
if not os.path.exists(destAsset):
53+
os.makedirs(destAsset)
54+
55+
56+
## MODEL PATH NOT MENTIONED, add information into model_path
57+
if not args.model_path:
58+
exportstatus = input("Do you want us to download the model from AI hub (y/n)")
59+
60+
##DOWNLAOD USING EXPORT.PY
61+
if exportstatus.lower().startswith("y"):
62+
print("EXPORT form path")
63+
pathtomodel = os.path.join(
64+
"..",
65+
"..",
66+
"..",
67+
"",
68+
"qai_hub_models",
69+
"models",
70+
args.model_name,
71+
"export.py",
72+
)
73+
if not os.path.exists(pathtomodel):
74+
print("PATH DO NOT EXIST: " + pathtomodel)
75+
exit()
76+
subprocess.run(["python", pathtomodel, "--skip-inferencing"])
77+
tflite_file = glob.glob(
78+
"build" + os.sep + args.model_name + os.sep + "*.tflite", recursive=True
79+
)
80+
args.model_path = tflite_file[0]
81+
# shutil.copy(tflite_file[0], destAsset+os.sep+"superresmodel.tflite")
82+
83+
##GET USER TO GIVE PATH
84+
else:
85+
args.model_path = input("Give model File as input")
86+
# if not os.path.exists(tflite_file):
87+
# print("PATH DO NOT EXIST: "+tflite_file)
88+
# exit()
89+
# shutil.copy(tflite_file, destAsset+os.sep+"superresmodel.tflite")
90+
91+
92+
if args.model_path:
93+
print(args.model_path)
94+
if not os.path.exists(args.model_path):
95+
print("PATH DO NOT EXIST: " + args.model_path)
96+
exit()
97+
shutil.copy(args.model_path, destAsset + os.sep + "classification.tflite")
98+
99+
100+
## COPYING REQUIRED FILES FROM QNN SDK
101+
destJNI = os.path.join(".", "classification", "src", "main", "jniLibs", "arm64-v8a")
102+
if not os.path.exists(destJNI):
103+
os.makedirs(destJNI)
104+
105+
# copy *.so from $qnn_sdk/libs/aarch64-android to $jni_lib_dir
106+
qnnbasiclibs = os.path.join(args.qnnsdk, "lib", "aarch64-android")
107+
shutil.copytree(qnnbasiclibs, destJNI, dirs_exist_ok=True)
108+
109+
# copy $qnn_sdk/lib/hexagon-v**/unsigned/libQnnHtpV**Skel.so to $jni_lib_dir
110+
skelstubfiles = os.path.join(args.qnnsdk, "lib", "hexagon-v**", "unsigned", "*.so")
111+
for file in glob.glob(skelstubfiles):
112+
shutil.copy(file, destJNI)
113+
114+
# copy qtld-release.aar to $test_app_root/Application/
115+
destaar = os.path.join(".", "classification", "libs")
116+
if not os.path.exists(destaar):
117+
os.makedirs(destaar)
118+
aarfile = os.path.join(args.qnnsdk, "lib", "android", "qtld-release.aar")
119+
shutil.copy(aarfile, destaar)
120+
121+
122+
## BUILDING APK
123+
if sys.platform.startswith("win"):
124+
print("Detected platform is windows")
125+
gradleoutput = subprocess.run(["gradlew.bat", "assembleDebug"], cwd=".")
126+
elif sys.platform.startswith("darwin"):
127+
print("Detected platform is MAC")
128+
gradleoutput = subprocess.run(["./gradlew", "assembleDebug"], cwd=".")
129+
else:
130+
print("Detected platform is Linux")
131+
gradleoutput = subprocess.run(["./gradlew", "assembleDebug"], cwd=".")
132+
133+
134+
## COPYING APK TO CWD
135+
ApkPath = os.path.join(
136+
os.getcwd(),
137+
"classification",
138+
"build",
139+
"outputs",
140+
"apk",
141+
"debug",
142+
"classification-debug.apk",
143+
)
144+
print("APK Is copied at current Working Directory")
145+
shutil.copy(ApkPath, ".")
146+
147+
148+
install_perm = input("Do you want to install this apk in connected device")
149+
## INSTALLING AND RUNNING APK
150+
if install_perm.lower().startswith("y"):
151+
command_to_install = ["adb", "install", "classification-debug.apk"]
152+
subprocess.run(command_to_install, cwd=".")
153+
command_to_run = [
154+
"adb",
155+
"shell",
156+
"am",
157+
"start",
158+
"-a",
159+
"com.example.ACTION_NAME",
160+
"-n",
161+
"com.qcom.imagesuperres/com.qcom.imagesuperres.QNNActivity",
162+
]
163+
subprocess.run(command_to_run, cwd=".")

0 commit comments

Comments
 (0)