本帖子旨在帮助各位初学者填坑,快速运行起来,排版不优美的地方以及有错误的地方请指正。
1. 下载源码
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r1.2
2. 先测试下用brazel
编译
这一步骤可以不做,一基友没做的情况下再Android Studio里面也可以运行成功。
2.1 首先要修改下WORKSPACE
文件里的内容,注意下当前的位置
vim tensorflow/WORKSPACE
修改文件中对应行
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 23,
# Ensure that you have the build_tools_version below installed in the
# SDK manager as it updates periodically.
build_tools_version = "25.0.2",
# Replace with path to Android SDK on your system
path = "/Users/superchange/Library/Android/sdk",
)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
# 请不要无视上面这行话,由于我无视了它,导致我多花了1天时间,这是没有必要的,请去下载一个r12的NDK,然后对应着修改下面的路径
android_ndk_repository(
name="androidndk",
path="/Users/superchange/Documents/TensorFlow_learn/android-ndk-r12b",
# This needs to be 14 or higher to compile TensorFlow.
# Note that the NDK version is not the API level.
api_level=14)
2.2 运行下面这行命令进行编译
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
编译需要很久,且中间会去谷歌的服务器上下载些文件,所以要保持全局翻墙,否则很慢,还可能会失败,编译成功的提示
CONFLICT: asset:WORKSPACE is provided with ambiguous priority from:
external/mobile_multibox/WORKSPACE
external/inception5h/WORKSPACE
CONFLICT: asset:WORKSPACE is provided with ambiguous priority from:
external/stylize/WORKSPACE
external/mobile_multibox/WORKSPACE
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: 1779.182s, Critical Path: 90.91s
注: CONFLICT
应该没有啥关系,我测试了apk可以安装并运行;
我的电脑配置是16年的mbp15寸高配,编译花了我30分钟,编译的时候CPU占用基本上在30%左右,可能主要是下载文件花了时间
3. 使用Android Studio编译并运行
3.1 导入工程到Android
Studio
3.2 运行demo
4. 训练自己的分类器
本步骤可以实现让app识别自己想让它识别的东西,比如hotdog啥的
4.1 新建文件夹
需要注意的是,至少要准备2个类别,否则训练是不能完成的,因此先新建文件夹,这里我用的是马里奥和皮卡丘
mkdir tf_files
cd tf_files
mkdir games
cd games
mkdir pikaqiu
mkdir mario
cd ../..
现在该去下载图片了,因为只是试验demo行不行,我没有用爬虫去爬,直接用的chrome的插件来批量下载图片
安装插件以后用Google或者百度搜索图片,输入关键字 皮卡丘
或者mario
文件下载完成后去download文件夹里面找到,批量修改图片的名字,再复制的之前新建的文件夹中,每种类别至少30张图片。
4.2 训练模型
python3 tensorflow/examples/image_retraining/retrain.py \
--bottleneck_dir=/Users/superchange/Documents/TensorFlow_learn/tensorflow/tf_files/bottleneck \
--how_many_training_steps 4000 \
--model_dir=/Users/superchange/Documents/TensorFlow_learn/tensorflow/tf_files/inception \
--output_graph=/Users/superchange/Documents/TensorFlow_learn/tensorflow/tf_files/retrained_graph.pb \
--output_labels=/Users/superchange/Documents/TensorFlow_learn/tensorflow/tf_files/retrained_labels.txt \
--image_dir /Users/superchange/Documents/TensorFlow_learn/tensorflow/tf_files/games
注意: 以上代码段中的/Users/superchange/Documents/TensorFlow_learn/
路径替换成你自己的对应的路径
4.3 模型优化
python3 tensorflow/python/tools/optimize_for_inference.py \
--input=/Users/superchange/Documents/TensorFlow_learn/tensorflow//tf_files/retrained_graph.pb \
--output=/Users/superchange/Documents/TensorFlow_learn/tensorflow//tf_files/optimized_graph.pb \
--input_names="Mul" \
--output_names="final_result"
注意: 以上代码段中的/Users/superchange/Documents/TensorFlow_learn/
路径替换成你自己的对应的路径
4.4 训练完成
在tf_files
文件夹下会出现retrained_labels.txt
和optimized_graph.pb
这两个文件,需要将其复制到/Users/superchange/Documents/TensorFlow_learn/tensorflow/tensorflow/examples/android
5. 修改ClassifierActivity.java中的参数
注意android studio里面要做适当的修改
private static final int INPUT_SIZE = 299;
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128f;
private static final String INPUT_NAME = "Mul";
private static final String OUTPUT_NAME = "final_result";
private static final String MODEL_FILE = "file:///android_asset/optimized_graph.pb";
private static final String LABEL_FILE =
"file:///android_asset/retrained_labels.txt";
上面的参数都要修改,我尝试过只改文件路径以及INPUT_NAME
和OUTPUT_NAME
结果程序运行的时候会报错
感谢:
· 感谢大厂google
· 本文主要参考了Nilhcem的帖子