参考链接:
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#7
- 迁移学习训练模型:
- git源代码: https://github.com/googlecodelabs/tensorflow-for-poets-2
- shell命令:设置图片大小和迁移学习的模型:
IMAGE_SIZE=224
,ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"
- 启动tensorboard:
tensorboard --logdir tf_files/training_summaries &
- 训练模型:
python -m scripts.retrain --bottleneck_dir=tf_files/bottlenecks --how_many_training_steps=500(可以不写,默认4000) --model_dir=tf_files/models/ --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" --output_graph=tf_files/retrained_graph.pb --output_labels=tf_files/retrained_labels.txt --architecture="${ARCHITECTURE}" --image_dir=tf_files/flower_photos
- 测试训练好的模型:
python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=tf_files/flower_photos/daisy/xx.jpg
- android应用with TF Lite:
- 用toco把pb文件转成lite文件:
IMAGE_SIZE=224, toco --graph_def_file=tf_files/retrained_graph.pb --output_file=tf_files/optimized_graph.lite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 --input_array=input --output_array=final_result --inference_type=FLOAT --input_data_type=FLOAT
- 导入android:
open tensorflow-for-poets-2/android/tflite
- 设置android ADVM(用本机摄像头):
create virtual device-->show advanced settings-->camera front:webcam0, back:webcam0
- 自定义模型:拷贝optimized_graph.lite和retrained_labels.txt到tflite/app/src/main/assets/目录下:
cp tf_files/optimized_graph.lite android/tflite/app/src/main/assets/graph.lite, cp tf_files/retrained_labels.txt android/tflite/app/src/main/assets/labels.txt
- run app
- 用toco把pb文件转成lite文件:
- 打包成apk文件:未完成