From 3e20b7af87c7690e10f9f8f19978fe4fa5ad196f Mon Sep 17 00:00:00 2001 From: Bin Li Date: Tue, 26 Jun 2018 15:40:46 +0800 Subject: [PATCH] android demo build docs and script --- .../models/demo_app_models.yaml | 65 +++++++------------ mace/examples/android/README.md | 27 ++++++++ mace/examples/android/README_zh.md | 15 ----- .../android/app/src/main/assets/BUILD | 15 ----- .../xiaomi/mace/demo/result/ResultData.java | 2 +- mace/examples/android/build.sh | 13 ++++ .../android/macelibrary/CMakeLists.txt | 3 +- .../src/main/cpp/image_classify.cc | 20 +++--- 8 files changed, 77 insertions(+), 83 deletions(-) create mode 100644 mace/examples/android/README.md delete mode 100644 mace/examples/android/README_zh.md delete mode 100644 mace/examples/android/app/src/main/assets/BUILD create mode 100755 mace/examples/android/build.sh diff --git a/docs/getting_started/models/demo_app_models.yaml b/docs/getting_started/models/demo_app_models.yaml index 7523cd51..df130c4b 100644 --- a/docs/getting_started/models/demo_app_models.yaml +++ b/docs/getting_started/models/demo_app_models.yaml @@ -1,56 +1,39 @@ # The name of library -library_name: library_name -target_abis: [armeabi-v7a, arm64-v8a] -# The soc serialno of specific device. -# Get by command `adb shell getprop | grep ro.board.platform | cut -d [ -f3 | cut -d ] -f1`. -target_socs: [msm8998] +library_name: mobilenet +target_abis: [arm64-v8a] embed_model_data: 1 # The build mode for model(s). # 'code' stand for transfer model(s) into cpp code, 'proto' for model(s) in protobuf file(s). build_type: code -linkshared: 1 +linkshared: 0 # One yaml config file can contain multi models' config message. models: - model_name: # model tag, which will be used in model loading and must be specific. + mobilenet_v1: # model tag, which will be used in model loading and must be specific. platform: tensorflow - model_file_path: path/to/model64.pb # also support http:// and https:// - model_sha256_checksum: 7f7462333406e7dea87222737590ebb7d94490194d2f21a7d72bafa87e64e9f9 + # support local path, http:// and https:// + model_file_path: https://cnbj1.fds.api.xiaomi.com/mace/miai-models/mobilenet-v1/mobilenet-v1-1.0.pb + model_sha256_checksum: 71b10f540ece33c49a7b51f5d4095fc9bd78ce46ebf0300487b2ee23d71294e6 subgraphs: - - input_tensors: input_node - input_shapes: 1,64,64,3 - output_tensors: output_node - output_shapes: 1,64,64,2 - runtime: gpu - data_type: fp16_fp32 + - input_tensors: input + input_shapes: 1,224,224,3 + output_tensors: MobilenetV1/Predictions/Reshape_1 + output_shapes: 1,1001 + runtime: cpu+gpu limit_opencl_kernel_time: 0 nnlib_graph_mode: 0 - obfuscate: 1 + obfuscate: 0 winograd: 0 - input_files: - - path/to/input_files # support http:// - second_net: - platform: caffe - model_file_path: path/to/model.prototxt - weight_file_path: path/to/weight.caffemodel - model_sha256_checksum: 05d92625809dc9edd6484882335c48c043397aed450a168d75eb8b538e86881a - weight_sha256_checksum: 05d92625809dc9edd6484882335c48c043397aed450a168d75eb8b538e86881a + mobilenet_v2: + platform: tensorflow + model_file_path: https://cnbj1.fds.api.xiaomi.com/mace/miai-models/mobilenet-v2/mobilenet-v2-1.0.pb + model_sha256_checksum: 369f9a5f38f3c15b4311c1c84c032ce868da9f371b5f78c13d3ea3c537389bb4 subgraphs: - - input_tensors: - - input_node0 - - input_node1 - input_shapes: - - 1,256,256,3 - - 1,128,128,3 - output_tensors: - - output_node0 - - output_node1 - output_shapes: - - 1,256,256,2 - - 1,1,1,2 - validation_inputs_data: - - path/to/input_files # support http:// - runtime: cpu - limit_opencl_kernel_time: 1 + - input_tensors: input + input_shapes: 1,224,224,3 + output_tensors: MobilenetV2/Predictions/Reshape_1 + output_shapes: 1,1001 + runtime: cpu+gpu + limit_opencl_kernel_time: 0 nnlib_graph_mode: 0 - obfuscate: 1 + obfuscate: 0 winograd: 0 diff --git a/mace/examples/android/README.md b/mace/examples/android/README.md new file mode 100644 index 00000000..48eb50e6 --- /dev/null +++ b/mace/examples/android/README.md @@ -0,0 +1,27 @@ +Android Demo +------------ +------------ + +Try it by downloading the [APK](https://cnbj1.fds.api.xiaomi.com/mace/demo/mace_android_demo.apk). + +How to build +--------------- +* Build native library, please refer to [docs](docs) for more information. + ``` + # Execute following commands from the project's root directory + python tools/converter.py build --config=docs/getting_started/models/demo_app_models.yaml + cp -r build/mobilenet/include/ mace/examples/android/macelibrary/src/main/cpp/ + cp -r build/mobilenet/lib/ mace/examples/android/macelibrary/src/main/cpp/ + ``` +* Build APK + * Import the `mace/examples/android` directory as a new Android Studio project and select `install run` + * Or build it with gradle: + ``` + cd mace/exampls/android + ./gradlew installAppRelease + ``` +* You can also build native library and APK with `mace/examples/android/build.sh` + ``` + cd mace/exampls/android + ./build.sh + ``` diff --git a/mace/examples/android/README_zh.md b/mace/examples/android/README_zh.md deleted file mode 100644 index eedbc7aa..00000000 --- a/mace/examples/android/README_zh.md +++ /dev/null @@ -1,15 +0,0 @@ -# mace demo使用方法 - -* 使用前需要生成静态库和头文件,具体参考[文档](docs) - * 把mace/public目录下的mace.h和mace_runtime.h拷贝到macelibrary/src/main/cpp/include下面 - * 把生成的mace/codegen/engine/mace_engine_factory.h拷贝到macelibrary/src/main/cpp/include下面 - * 静态库的路径是在mace/build/demo_app_models/lib/下 -* 使用android studio 导入项目,然后运行install run -* 还可以使用gradle命令(需要安装gradle)生成apk 具体命令例如:./gradlew assemble(或者Release|Debug) - -## 交流与反馈 -* 欢迎通过Github Issues提交问题报告与建议 -* QQ群: 756046893 - -## License -[Apache License 2.0](LICENSE). diff --git a/mace/examples/android/app/src/main/assets/BUILD b/mace/examples/android/app/src/main/assets/BUILD deleted file mode 100644 index c827de7b..00000000 --- a/mace/examples/android/app/src/main/assets/BUILD +++ /dev/null @@ -1,15 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) # Apache 2.0 - -# It is necessary to use this filegroup rather than globbing the files in this -# folder directly the examples/android:tensorflow_demo target due to the fact -# that assets_dir is necessarily set to "" there (to allow using other -# arbitrary targets as assets). -filegroup( - name = "asset_files", - srcs = glob( - ["**/*"], - exclude = ["BUILD"], - ), -) diff --git a/mace/examples/android/app/src/main/java/com/xiaomi/mace/demo/result/ResultData.java b/mace/examples/android/app/src/main/java/com/xiaomi/mace/demo/result/ResultData.java index 34744653..a89961fe 100644 --- a/mace/examples/android/app/src/main/java/com/xiaomi/mace/demo/result/ResultData.java +++ b/mace/examples/android/app/src/main/java/com/xiaomi/mace/demo/result/ResultData.java @@ -17,7 +17,7 @@ package com.xiaomi.mace.demo.result; public class ResultData { public String name; public float probability; - public long costTime; //毫秒 + public long costTime; // ms public ResultData(String name, float probability) { this.name = name; diff --git a/mace/examples/android/build.sh b/mace/examples/android/build.sh new file mode 100755 index 00000000..9a662d97 --- /dev/null +++ b/mace/examples/android/build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e -u -o pipefail + +pushd ../../../ +python tools/converter.py build --config=docs/getting_started/models/demo_app_models.yaml + +cp -r build/mobilenet/include/ mace/examples/android/macelibrary/src/main/cpp/ +cp -r build/mobilenet/lib/ mace/examples/android/macelibrary/src/main/cpp/ + +popd + +./gradlew installAppRelease diff --git a/mace/examples/android/macelibrary/CMakeLists.txt b/mace/examples/android/macelibrary/CMakeLists.txt index c3445197..9f482a20 100644 --- a/mace/examples/android/macelibrary/CMakeLists.txt +++ b/mace/examples/android/macelibrary/CMakeLists.txt @@ -13,7 +13,8 @@ cmake_minimum_required(VERSION 3.4.1) #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../app/libs/${ANDROID_ABI}) include_directories(${CMAKE_SOURCE_DIR}/) -file(GLOB static_file ${CMAKE_SOURCE_DIR}/../../../build/demo_app_models/lib/arm64-v8a/*.a) +include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include) +file(GLOB static_file ${CMAKE_SOURCE_DIR}/src/main/cpp/lib/arm64-v8a/*.a) MESSAGE(STATUS "FILE URL = ${CMAKE_SOURCE_DIR}") MESSAGE(STATUS "FILE URL = ${static_file}") diff --git a/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc b/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc index 1f4af4fb..0a070270 100755 --- a/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc +++ b/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc @@ -25,9 +25,9 @@ #include #include -#include "src/main/cpp/include/mace.h" -#include "src/main/cpp/include/mace_runtime.h" -#include "src/main/cpp/include/mace_engine_factory.h" +#include "src/main/cpp/include/mace/public/mace.h" +#include "src/main/cpp/include/mace/public/mace_runtime.h" +#include "src/main/cpp/include/mace/public/mace_engine_factory.h" namespace { @@ -45,9 +45,9 @@ struct MaceContext { mace::DeviceType device_type = mace::DeviceType::CPU; std::map model_infos = { {"mobilenet_v1", {"input", "MobilenetV1/Predictions/Reshape_1", - {1, 224, 224, 3}, {1, 1, 1, 1001}}}, + {1, 224, 224, 3}, {1, 1001}}}, {"mobilenet_v2", {"input", "MobilenetV2/Predictions/Reshape_1", - {1, 224, 224, 3}, {1, 1, 1, 1001}}} + {1, 224, 224, 3}, {1, 1001}}} }; }; @@ -146,11 +146,11 @@ Java_com_xiaomi_mace_JniMaceUtils_maceMobilenetCreateEngine( mace::MaceStatus create_engine_status = CreateMaceEngineFromCode(mace_context.model_name, - std::string(), - input_names, - output_names, - mace_context.device_type, - &mace_context.engine); + std::string(), + input_names, + output_names, + mace_context.device_type, + &mace_context.engine); __android_log_print(ANDROID_LOG_ERROR, "image_classify attrs", -- GitLab