Tensorflow Android Demo 使用 Yolo

它默认支持三种网络,

TF_OD_API, MULTIBOX, YOLO;

除了 YOLO ,都给了下载链接,想试试 YOLO ,没找到

graph-tiny-yolo-voc.pb

的下载,自己动手丰衣足食。

git clone git@github.com:thtrieu/darkflow.git
cd darkflow
python3 setup.py build_ext --inplace
pip install -e .

下载对应的 yolo weight 文件

wget https://pjreddie.com/media/files/yolo.weights

更多权重文件下载 :https://drive.google.com/drive/folders/0B1tW_VtY7onidEwyQ2FtQVplWEU

https://pjreddie.com/darknet/yolo/

flow --model cfg/yolo.cfg --load yolo.weights --savepb
Parsing ./cfg/yolo.cfg
Parsing cfg/yolo.cfg
Loading yolo.weights ...
Successfully identified 203934260 bytes
Finished in 0.008934736251831055s
Model has a coco model name, loading coco labels.

Building net ...
Source | Train? | Layer description | Output size
-------+--------+----------------------------------+---------------
 | | input | (?, 608, 608, 3)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 608, 608, 32)
 Load | Yep! | maxp 2x2p0_2 | (?, 304, 304, 32)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 304, 304, 64)
 Load | Yep! | maxp 2x2p0_2 | (?, 152, 152, 64)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 152, 152, 128)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 152, 152, 64)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 152, 152, 128)
 Load | Yep! | maxp 2x2p0_2 | (?, 76, 76, 128)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 76, 76, 256)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 76, 76, 128)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 76, 76, 256)
 Load | Yep! | maxp 2x2p0_2 | (?, 38, 38, 256)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 38, 38, 512)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 38, 38, 256)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 38, 38, 512)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 38, 38, 256)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 38, 38, 512)
 Load | Yep! | maxp 2x2p0_2 | (?, 19, 19, 512)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 19, 19, 512)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 19, 19, 512)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | concat [16] | (?, 38, 38, 512)
 Load | Yep! | conv 1x1p0_1 +bnorm leaky | (?, 38, 38, 64)
 Load | Yep! | local flatten 2x2 | (?, 19, 19, 256)
 Load | Yep! | concat [27, 24] | (?, 19, 19, 1280)
 Load | Yep! | conv 3x3p1_1 +bnorm leaky | (?, 19, 19, 1024)
 Load | Yep! | conv 1x1p0_1 linear | (?, 19, 19, 425)
-------+--------+----------------------------------+---------------
Running entirely on CPU
2017-12-08 17:49:27.012124: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Finished in 5.091068267822266s

Rebuild a constant version ...
Done

训练好的文件在 ./built_graph/ 文件夹

将生成的文件拷贝到

android_asset

编译成功。。。

效果不是很理想,很多东西被认成  person 了。。。

TensorFlow Lite 初探

目录位置在

tensorflow/contrib/lite/java/demo

下载训练好的model

https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip

解压后放到这里

tensorflow/contrib/lite/java/demo/app/src/main/assets/

中文版lables:  TensorFlow Demo labels 文件中文版(汉化进行中)

build 的时候,

gradle 版本提示需要 3.5 ,当前是 3.3

升级,不带  ./ 可能报 gradlew: command not found

./gradlew build

Project Structure -> Project

修改  Gradle version 为  3.5

再去build : OK

之前TensorFlow Android 编译完体积 50m ,Lite 版本 编译完 7m多。

TensorFlow Android demo 调试记录 ( macOS )

特别注意:全程请保持网络能正常访问 google

GITHUB:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android
  1. 准备  android 5.1  版本以上的 手机 低版本的不支持。
  2. 下载 tensorflow 源码
    git clone --recurse-submodules https://github.com/tensorflow/tensorflow
    git clone git@github.com:tensorflow/tensorflow.git
  3. 用 android studio 打开
  4. 试着 run了一下 出现错误
    Error:Execution failed for task ':buildNativeBazel'.
    
    > A problem occurred starting process 'command '/usr/local/bin/bazel''
    1. 需要安装  bazel  ( 我在 mac 上开发的。 ubuntu 或者window 参考  https://docs.bazel.build/versions/master/install-os-x.html
      brew cask install caskroom/versions/java8
      brew install bazel
    2. 需要安装 NDK
      android studio : file -> project structure -> SDK location    最后的  Android NDK location 如果为空 ,点下面的 download  下载,并解压。将路径填到上面的框里。

      https://dl.google.com/android/repository/android-ndk-r12b-darwin-x86_64.zip
  5. 到tensorflow 工作目录 编译(需要下载包)
  6. bazel build -c opt //tensorflow/examples/android:tensorflow_demo
  7. 出错误  ,原因是 tensorflow 工作目录里没有 andorid sdk
  8. This build requires an Android SDK. Please add the android_sdk_repository rule to your WORKSPACE.
  9. clang: error: unknown argument: '-m<platform_for_version_min>-version-min=11.1'

    打开 tensorflow 根目录下的 WORKSPACE 编辑  sdk 和 ndk

  10. 又报错误
    C++ compilation of rule '@protobuf_archive//:protobuf' failed (Exit 1).
    
    fatal error: 'stdlib.h' file not found
    
    #include <stdlib.h>

    原因是 git 时没有加   –recurse-submodules 参数  。用于获取 TesorFlow 依赖的 protobuf 库

  11. 依然还有错误,一堆 .h 文件找不到。查了些资料,说 NDK不能超过 r12b ,重新下载个 r12b , 替换掉默认的。( tensorflow 文档推荐的是 14b,我没有试应该也可以。我之前下的是 16)
    https://dl.google.com/android/repository/android-ndk-r12b-darwin-x86_64.zip
  12. OK成功
    Target //tensorflow/examples/android:tensorflow_demo up-to-date:
    
     bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar
    
     bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk
    
     bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
    
    INFO: Elapsed time: 861.504s, Critical Path: 99.12s
  13. 编译完的 apk 在一个非常诡异的位置
    //tensorflow/bazel-out/darwin_x86_64-opt/bin/tensorflow/examples/android/tensorflow_demo.apk
    adb install -r  ./bazel-out/darwin_x86_64-opt/bin/tensorflow/examples/android/tensorflow_demo.apk

    mac安装 adb

    brew cask install android-platform-tools

     

  14. 可能出现如下错误
  15. Gradle sync failed: Cause: error in opening zip file
     Consult IDE log for more details (Help | Show Log) (451ms)

     

    将 gradle-wrapper.properties  中

    distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

    替换成

    distributionUrl=http\://services.gradle.org/distributions/gradle-4.1-all.zip

    出错误Xcode version must be specified to use an Apple CROSSTOOL

bazel clean --expunge

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

sudo xcodebuild -license

  1. 附,解决最后问题的一篇文章,顺带致谢。
    http://blog.csdn.net/wojiaopanpan/article/details/77488056

Lite 版 移步