tensorflow 训练自己的模型, 在android中使用

1> 自己的图片资源搜集

借助工具: Fatkun Batch Download Image 

google chrome 插件, 可以下载当前页面中的图片, 本例下载三类:美食/旅行/宠物

保存为: images/food/**.jpg ; images/travel; images/pet

2> 训练自己的模型

python tensorflow/examples/image_retraining/retrain.py --bottleneck_dir=/tf_files/bottlenecks --how_many_training_steps 4000 --model_dir=/tf_files/inception --output_graph=/tf_files/retrained_graph.pb --output_labels=/tf_files/retrained_labels.txt --image_dir /tf_files/images

3> 为移动版本优化模型PB文件

./configure

bazel build tensorflow/python/tools:optimize_for_inference

bazel-bin/tensorflow/python/tools/optimize_for_inference  --input=/tf_files/retrained_graph.pb --output=/tf_files/retrained_graph_optimized.pb --input_names=Mul  --output_names=final_result


4> 编译tensorflow中 example中 android项目, 编译完毕后,在android studio中生成apk

此时 assets中会有例子中自带的一些 model.pb, labels.txt

5> 修改ClassifierActivity.java中的参数

private static final int INPUT_SIZE =299;
private static final int IMAGE_MEAN =128;
private static final float IMAGE_STD =128;
private static final String INPUT_NAME ="Mul";
private static final String OUTPUT_NAME ="final_result";
private static final String MODEL_FILE ="file:///android_asset/retrained_graph_optimized.pb";
private static final String LABEL_FILE ="file:///android_asset/retrained_labels.txt";


6> 将第4步中训练的retrained_graph_optimized.pb, retrained_label.txt  复制到assets中

7> 重新编译和运行应用, TF Classify 即可

你可能感兴趣的:(tensorflow 训练自己的模型, 在android中使用)