LoginSignup
7
9

More than 5 years have passed since last update.

AndroidでTensorFlowデモ(YOLO版)

Last updated at Posted at 2017-07-19

TensorFlowのAndroidデモはYOLOに対応しているがデフォルトでは無効になっている。
YOLOを有効にしてビルドを行うための手順を記載。
バージョンはv1.2.1を使用。

TensorFlowビルド

TensorFlowに必要なBazelのインストールは以下参照
http://knowledge.sakura.ad.jp/knowledge/6174/

TensorFlowのビルドについては以下参照
http://qiita.com/tchkwkzk/items/aa481db14126b6abdaa1
http://qiita.com/tomoima525/items/99a2df5cb0559c41647a

ポイントはNDK r12bを使うこと
TensorFlowのWORKSPACEには

Android NDK r12b is recommended (higher may cause issues with Bazel)

とある。

古いNDKは以下からダウンロードできる。
https://developer.android.com/ndk/downloads/revision_history.html

ビルドエラー missing input file '@androidsdk//:build-tools/26.0.0/lib/apksigner.jar'

WORKSPACEのandroid_sdk_repositorybuild_tools_version="25.0.2"にしたところビルドできた。
(25.0.2はAndroidStudioであらかじめインストールしておいた)

darkflowインストールとモデルの変換

この時点ではTensorFlowのデモはYOLO未使用版である。

YOLOのモデルをTensorFlowで使うためにDarkFlowで変換を行う必要がある。

$ git clone https://github.com/thtrieu/darkflow.git
$ cd darkflow
$ pip3 install . --user

インストールされていなければ以下もインストールしておく

$ pip3 install --user tensorflow opencv-python

モデルの変換

YOLOのページからcfgweightsを持ってきてflowコマンドでpbファイルに変換する

$ flow --model tiny-yolo-voc.cfg --load tiny-yolo-voc.weights --savepb

tiny-yolo-voc.pbが出来上がるのでそれをtensorflow/example/android/assetsに格納する

デモの再ビルド

以下の変更を行い再ビルドするとYOLOが有効になったDetectionデモが実行できる

diff --git a/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java b/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java
index 5800f8065..c3b24846d 100644
--- a/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java
+++ b/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java
@@ -67,14 +67,14 @@ public class DetectorActivity extends CameraActivity implements OnImageAvailable
   // Graphs and models downloaded from http://pjreddie.com/darknet/yolo/ may be converted e.g. via
   // DarkFlow (https://github.com/thtrieu/darkflow). Sample command:
   // ./flow --model cfg/tiny-yolo-voc.cfg --load bin/tiny-yolo-voc.weights --savepb --verbalise=True
-  private static final String YOLO_MODEL_FILE = "file:///android_asset/graph-tiny-yolo-voc.pb";
+  private static final String YOLO_MODEL_FILE = "file:///android_asset/tiny-yolo-voc.pb";
   private static final int YOLO_INPUT_SIZE = 416;
   private static final String YOLO_INPUT_NAME = "input";
   private static final String YOLO_OUTPUT_NAMES = "output";
   private static final int YOLO_BLOCK_SIZE = 32;

   // Default to the included multibox model.
-  private static final boolean USE_YOLO = false;
+  private static final boolean USE_YOLO = true;

   private static final int CROP_SIZE = USE_YOLO ? YOLO_INPUT_SIZE : MB_INPUT_SIZE;
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9