Skip to content

Commit 46bf245

Browse files
authored
Merge pull request #3 from lp6m/dev
v1.0
2 parents 3eef889 + 858da16 commit 46bf245

File tree

87 files changed

+7632
-25
lines changed

Some content is hidden

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

87 files changed

+7632
-25
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "yolov5"]
2+
path = yolov5
3+
url = https://github.com/ultralytics/yolov5

README.md

+75-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,90 @@
1-
# yolov5s_android
2-
The implementation of yolov5s on android for the [yolov5s export contest](https://github.com/ultralytics/yolov5/discussions/3213)
1+
# yolov5s_android:rocket:
2+
<div align="center">
3+
<img src="https://github.com/lp6m/yolov5s_android/raw/media/android_app.gif" width=30%>
4+
<img src="https://github.com/lp6m/yolov5s_android/raw/media/screenshot.jpg" width=30%>
5+
</div>
36

7+
The implementation of yolov5s on android for the [yolov5s export contest](https://github.com/ultralytics/yolov5/discussions/3213).
8+
Download the latest android apk from [release](https://github.com/lp6m/yolov5s_android/releases) and install your device.
49

5-
## Overview
10+
## Environment
11+
- Host Ubuntu18.04
12+
- Docker
13+
* Tensorflow 2.4.0
14+
* PyTorch 1.7.0
15+
* OpenVino 2021.3
16+
- Android App
17+
* Android Studio 4.2.1
18+
* minSdkVersion 28
19+
* targetSdkVersion 29
20+
* TfLite 2.4.0
21+
- Android Device
22+
* Xiaomi Mi11 (Storage 128GB/ RAM8GB)
23+
* OS MUI 12.5.8
24+
25+
We use docker container for host evaluation and model conversion.
26+
```sh
27+
git clone --recursive https://github.com/lp6m/yolov5s_android
28+
cd yolov5s_android
29+
docker build ./ -f ./docker/Dockerfile -t yolov5s_android
30+
docker run -it --gpus all -v `pwd`:/workspace yolov5s_anrdoid bash
31+
```
32+
33+
## Files
34+
- `./app`
35+
* Android application.
36+
* To build application by yourself, copy `./tflite_model/*.tflite` to `app/tflite_yolov5_test/app/src/main/assets/`, and build on Android Studio.
37+
* The app can perform inference with various configurations of input size, inference accuracy, and model accuracy.
38+
* For 'Open Directory Mode', save the detected bounding boxes results as a json file in coco format.
39+
* Realtime deteciton from camera image (precision and input size is fixed to int8/320). Achieved FPS is about **15FPS** on Mi11.
40+
- `./benchmark`
41+
* Benchmark script and results by [TFLite Model Benchmark Tool with C++ Binary](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/benchmark#profiling-model-operators).
42+
- `./convert_model`
43+
* Model conversion guide and model quantization script.
44+
- `./docker`
45+
* Dockerfile for the evaluation and model conversion environment.
46+
- `./host`
47+
* `detect.py` : Run detection for image with TfLite model on host environment.
48+
* `evaluate.py`: Run evaluation with coco validation dataset and inference results.
49+
- `./tflite_model`
50+
* Converted TfLite Model.
651

752
## Performance
853
### Latency (inference)
9-
These results are measured by [TFLite Model Benchmark Tool with C++ Binary](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/benchmark#profiling-model-operators).
10-
The latency does not contain the image pre-processing time and data transfer time.
54+
These results are measured on `Xiaomi Mi11`.
55+
Please refer [`benchmark/README.md`](https://github.com/lp6m/yolov5s_android/tree/master/benchmark) about the detail of benchmark command.
56+
The latency does not contain the pre/post processing time and data transfer time.
1157
#### float32 model
12-
When using NNAPI with the float32 model, the results are the same as when `qti-gpu` is specified, even if accelerator-name is not specified.
1358

14-
| delegatem option      | latency [ms] |
15-
| :----------------------- | :----------- |
16-
| None (CPU) | xxx |
17-
| NNAPI (qti-default, fp32) | xxx |
18-
| NNAPI (qti-default, fp16) | xxx |
59+
| delegate | 640x640 [ms] | 320x320 [ms] |
60+
| :-------------------- | -----------: | -----------: |
61+
| None (CPU) | 249 | 61 |
62+
| NNAPI (qti-gpu, fp32) | 156 | 112 |
63+
| NNAPI (qti-gpu, fp16) | 92 | 79 |
1964

2065
#### int8 model
66+
We tried to accelerate the inference process by using `NNAPI (qti-dsp)` and offload calculation to Hexagon DSP, but it doesn't work for now. Please see [here](https://github.com/lp6m/yolov5s_android/tree/dev/benchmark#nnapi-qti-dsp-not-working) in detail.
2167

22-
| delegate | latency [ms] |
23-
| :--------- | :----------- |
24-
| None (CPU) | xxx |
25-
| NNAPI (qti-default) | xxx |
26-
| NNAPI (qti-dsp) | Not worked |
27-
28-
### Latency (inference + postprocess)
68+
| delegate | 640x640 [ms] | 320x320 [ms] |
69+
| :------------------- | -----------: | -----------: |
70+
| None (CPU) | 95 | 23 |
71+
| NNAPI (qti-default) | Not working | Not working |
72+
| NNAPI (qti-dsp) | Not working | Not working |
2973

30-
### Accuracy
31-
#### Evaluation on host
32-
33-
#### Evaluation on android
74+
## Accuracy
75+
Please refer [host/README.md](https://github.com/lp6m/yolov5s_android/tree/master/host#example2) about the evaluation method.
76+
We set `conf_thresh=0.25` and `iou_thresh=0.45` for nms parameter.
77+
| device, model, delegate | 640x640 mAP | 320x320 mAP |
78+
| :-------------------------------- | ----------: | ----------: |
79+
| host GPU (Tflite + PyTorch, fp32) | 27.8 | 26.6 |
80+
| host CPU (Tflite + PyTorch, int8) | 26.6 | 25.5 |
81+
| NNAPI (qti-gpu, fp16) | 28.5 | 26.8 |
82+
| CPU (int8) | 27.2 | 25.8 |
3483

3584

3685
## Model conversion
37-
This project focuses on obtaining a tflite model by model conversion from PyTorch original implementation, rather than doing its own implementation in tflite.
38-
86+
This project focuses on obtaining a tflite model by **model conversion from PyTorch original implementation, rather than doing own implementation in tflite**.
87+
We convert models in this way: `PyTorch -> ONNX -> OpenVino -> TfLite`.
88+
To convert the model from OpenVino to TfLite, we use [openvino2tensorflow](https://github.com/PINTO0309/openvino2tensorflow).
89+
Please refer [convert_model/README.md](https://github.com/lp6m/yolov5s_android/tree/master/convert_model) about the model conversion.
3990

40-
## TODO

app/tflite_yolov5_test/.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
16+
17+
app/src/main/assets/*.tflite
18+
app/release

app/tflite_yolov5_test/.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/gradle.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/jarRepositories.xml

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/runConfigurations.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/.idea/vcs.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/tflite_yolov5_test/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.4.1)
2+
3+
add_library( # Sets the name of the library.
4+
native-lib
5+
6+
# Sets the library as a shared library.
7+
SHARED
8+
9+
# Provides a relative path to your source file(s).
10+
src/main/cpp/postprocess.cpp )
11+
12+
# Searches for a specified prebuilt library and stores the path as a
13+
# variable. Because CMake includes system libraries in the search path by
14+
# default, you only need to specify the name of the public NDK library
15+
# you want to add. CMake verifies that the library exists before
16+
# completing its build.
17+
18+
find_library( # Sets the name of the path variable.
19+
log-lib
20+
21+
# Specifies the name of the NDK library that
22+
# you want CMake to locate.
23+
log )
24+
25+
# Specifies libraries CMake should link to your target library. You
26+
# can link multiple libraries, such as libraries you define in this
27+
# build script, prebuilt third-party libraries, or system libraries.
28+
29+
target_link_libraries( # Specifies the target library.
30+
native-lib
31+
32+
# Links the target library to the log library
33+
# included in the NDK.
34+
${log-lib} )
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
compileSdkVersion 29
7+
buildToolsVersion "30.0.3"
8+
9+
defaultConfig {
10+
ndk {
11+
abiFilters 'armeabi-v7a', 'arm64-v8a'
12+
}
13+
applicationId "com.example.tflite_yolov5_test"
14+
minSdkVersion 28
15+
targetSdkVersion 29
16+
versionCode 1
17+
versionName "1.0"
18+
19+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20+
externalNativeBuild {
21+
cmake {
22+
cppFlags "-O3"
23+
}
24+
}
25+
}
26+
27+
buildTypes {
28+
release {
29+
minifyEnabled false
30+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
31+
}
32+
}
33+
compileOptions {
34+
sourceCompatibility JavaVersion.VERSION_1_8
35+
targetCompatibility JavaVersion.VERSION_1_8
36+
}
37+
externalNativeBuild {
38+
cmake {
39+
path "CMakeLists.txt"
40+
}
41+
}
42+
buildFeatures {
43+
viewBinding true
44+
}
45+
}
46+
47+
dependencies {
48+
implementation 'org.nanohttpd:nanohttpd:2.3.1'
49+
implementation 'androidx.appcompat:appcompat:1.2.0'
50+
implementation 'com.google.android.material:material:1.2.1'
51+
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
52+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
53+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
54+
implementation 'androidx.navigation:navigation-fragment:2.3.0'
55+
implementation 'androidx.navigation:navigation-ui:2.3.0'
56+
57+
implementation 'org.tensorflow:tensorflow-lite:2.4.0'
58+
//implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
59+
//implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
60+
//implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly'
61+
//implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly-SNAPSHOT'
62+
//implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly-SNAPSHOT'
63+
64+
testImplementation 'junit:junit:4.+'
65+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
66+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
67+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.tflite_yolov5_test;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.tflite_yolov5_test", appContext.getPackageName());
25+
}
26+
}

0 commit comments

Comments
 (0)