给你的应用添加图像识别--TensorFlow
TensorFlow介绍
TensorFlow™ 是一个开放源代码软件库,是谷歌大脑的第二代机器学习系统,用于进行高性能数值计算。于2015年11月9日在Apache 2.0开源许可证下发布。目前最新的版本是1.12。总的来说,TensorFlow计算框架可以很好地支持深度学习的各种算法,很好地兼容了学术研究和工业生产的不同需求,是一个比较厉害的一个机器学习框架。
实际运用
1.配置环境
- MacBook Pro macOS Mojava 版本 10.14(本机环境)
方式一
由于Mac系统自带Python环境,所以,我们在安装Python的时候需要注意一下,TensorFlow的官方文档上介绍的说支持Python2.X,但是我在Python2.7环境的安装下出现了很多意外的问题,比如编码的问题和其它依赖库不匹配问题,所以后来不得不重新安装其他的版本。Python官网最新的Python版本是Python3.7.1,但是当前的TensorFlow不能很好的兼容,所以建议安装Python3.5左右的版本。
这里给出部分相关安装命令:
//查询系统默认的Python版本
ls /System/Library/Frameworks/Python.framework/Versions
//自己安装的Python版本检测
ls /Library/Frameworks/Python.framework/Versions
//安装pip(Python3.0以下并没有pip管理器,需要手动安装)
sudo easy_install pip
Python各个版本下载 (注意:勾选“Add Python to PATH”)
安装完之后,打开终端输入python 就可以获取到你当前安装的Python版本号,说明安装成功。
//安装TensorFlow(CPU)
pip install tensorflow
安装完成后,打开终端输入python,接着输入下列代码:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
打印b’Hello, TensorFlow!’,则安装成功!
接着,我们下载并安装相关API运行支持组件:
//图像处理标准库
pip install pillow
//解析库
pip install lxml
//开源的Web应用程序(可视化)
pip install jupyter
//Python的绘图库
pip install matplotlib
基本的环境配置基本结束,我们需要验证一下安装的相关组件是否正确,我们在PC上运行一下试试!
下载源码并进行编译:
//下载源码
git clone https://github.com/tensorflow/models.git
//安装编译器
brew install protobuf
注:如没brew,安装命令如下:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
protobuf安装完成后,输入protoc --version,得到对应安装的版本,则安装成功。
models的源码下载完成后,进入models目录下进行编译:
protoc object_detection/protos/*.proto --python_out=.
这个命令在Mac上编译时没有有问题,但是在windows7上的话,可能会提示找不到路径的问题,那么需要换成另一个命令:
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.
到此基本环境已经配置好了,我们再次打开终端,cd 到models目录下,输入:
jupyter-notebook
则会在浏览器中打开,我们找到research/object_detection/object_detection_tutorial.ipynb文件,打开,点击Cell按钮,选中 Run all,稍等一会(第一次比较慢,得下载运行的模型),就完成了图像的识别。结果图如下:
在research/object_detection/test_images文件夹替换我们自己的图片,再试试看!
识别视频的代码如下:
//忙,下次补上
好了,到此我们的环境基本配置完整了,安装的组件稍多,也比较乱,这里我建议使用一个统一的软件进行管理。
方式二
anaconda各个版本下载 ,这种方式比较直观简易,通过Anaconda来安装和管理相关Python的组件,可谓是一键操作!具体的安装细节,大家可自行查询,不再赘述!
2.模型训练
看到了识别的效果,那我们怎么识别我们自己的图片呢?我们可以进行迁移训练,即直接使用已经写好的Python。(也可用其他方式训练)
下载hub项目:
git clone https://github.com/tensorflow/hub.git
找到:
hub/examples/image_retraining/retrain.py
使用retrain.py文件,我们进行重新训练,获得我们需要模型。
接下来准备要训练的图片:
这里我从网上抓取了100张苹果和100张手表的图片进行训练:
命令如下:
python3 /Users/tianchuangxin1/hub/examples/image_retraining/retrain.py \
--image_dir /Users/tianchuangxin1/tensorflow_image \
--how_many_training_steps=2000 \
----model_dir=/Users/tianchuangxin1/tensorflow_image \
--output_graph /Users/tianchuangxin1/tensorflow_image/output_graph.pb \
--output_labels /Users/tianchuangxin1/tensorflow_image/output_labels.txt \
--architecture=mobilenet_1.0_224_quant \
--summaries_dir /Users/tianchuangxin1/tensorflow_image \
--intermediate_output_graphs_dir /Users/tianchuangxin1/tensorflow_image \
--saved_model_dir /Users/tianchuangxin1/tensorflow_image
相关参数说明在retrain.py代码中。
训练过程中我们可以使用TensorBoard进行直观的观察。
//tensorboard的打开命令
tensorboard --logdir /tmp/retrain_logs
效果展示:
训练完成后tensorflow_image文件夹下生成output_labels.txt和output_graph.pb两个文件,为了确保生成模型正确,我们验证一下:
命令如下:
python3 /Users/tianchuangxin1/tensorflow/tensorflow/examples/label_image/label_image.py \
--graph=/Users/tianchuangxin1/tensorflow_image/output_graph.pb --labels=/Users/tianchuangxin1/tensorflow_image/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=/Users/tianchuangxin1/tensorflow_image/apple/apple5.jpg
得到如下结果:
apple 0.99963665
watch 0.00036338356
可以看到正确率很高,训练的十分完美!接下来,我们将pb文件转化为tflite类型供Android端使用。
3.模型转化
安装tflite:
pip install tensorflow-hub
命令如下:
tflite_convert \
--graph_def_file=/Users/tianchuangxin1/tensorflow_image/output_graph.pb \
--output_file=/Users/tianchuangxin1/tensorflow_image/output_graph.tflite \
--output_format=TFLITE \
--input_shape=1,299,299,3 \
--input_array=Placeholder \
--output_array=final_result \
--inference_type=FLOAT \
--input_data_type=FLOAT
注意的是input_array和output_array这两个参数值得设置。获取这两个参数值得方法如下:
import tensorflow as tf
model_file = "/Users/tianchuangxin1/tensorflow_image/output_graph.pb"
def load_graph(pbmodelFile):
with tf.gfile.GFile(pbmodelFile, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)
input_name = graph.get_operations()[0].name+':0'
output_name = graph.get_operations()[-1].name+':0'
return graph, input_name, output_name
graph, inputName, outputName = load_graph(model_file)
input_x = graph.get_tensor_by_name(inputName)
output_y = graph.get_tensor_by_name(outputName)
print(input_x)
print(output_y)
转化完成之后,会在你的文件夹下生成一个.tflite的文件。
4.Android端移植
下载TensorFlow的源码后
tensorflow/tensorflow/examples/android
导入Android studio,等待编译结束,运行,会在手机上安装了三个APP,TFL classify,TFL Detect,TFL Speech,我们是图像识别,所以找到对应的Classify相关即可。output_labels.txt和output_graph.tflite移入assert,转化的时候用的FLOAT类型,input_shape是299,相关参数更改后,再运行。
得到如下效果: