美文网首页TensorFlow参考资料2018~2019玩转树莓派树莓派
树莓派一键部署系列之一:tensorflow上的神经网络图像识别

树莓派一键部署系列之一:tensorflow上的神经网络图像识别

作者: clarkchan | 来源:发表于2017-06-11 11:55 被阅读6100次
树莓派3代

树莓派买了很久,各种小程序倒腾了半天,无非是控制个电灯开关,打开个网页,做个xmbc媒体播放器,但总是没有找到吊炸天的应用对不?

现在的树莓派3代,其实性能已经跟上来了,即使在上面跑神经网络图像识别软件,速度也勉强能接受了。本文教你如何一步完成这个神软件的部署。

谷歌2015年发布了tensorflow平台,2017年继续发布armv7版本,因此树莓派上现在能顺利跑tensorflow了。大家都知道,训练神经网络,非常耗费时间,要动用大量计算资源,还动不动得跑十几天才能完成,一般人玩不起这个。谷歌慷慨的将其在图像分类识别领域的最新训练成果Inception-v3模型的训练结果公布出来。这个模型在imageNet上面训练了几十万张共1000个不同类型的物品的图片,最终训练的错误率为3.46%(据说人眼识别的错误率仅为5.1%)。也就是说对于这1000种物品的识别率,Inception-v3模型已经高于人眼。


模型识别

那就让我们在树莓派上开始部署这个程序吧!

为了让大家很方便的使用到这个程序,笔者将将所有准备工作都编译到docker镜像中去了,这样大家可以跳过繁杂的安装过程,同时笔者还略微修改了谷歌的代码,使得输出结果能翻译成中文,便于理解。

首先您要先确定您的树莓派操作系统(raspbian)的版本要至少为jessie,如果还没有升级到这个版本,请先刷到这个版本。树莓派板卡最好为pi 3,前面几代应该也能运行(笔者没有测试过),但CPU速度比较慢,运行时间会比较长。

部署的过程其实只有一行代码:

在树莓派的命令行终端上输入以下代码

curl -s https://raw.githubusercontent.com/flyingtimes/image-recognition/master/one_step_install.sh|bash

这是一个脚本,会自动帮你把源代码下载下来,如果没有安装docker,会自动安装docker,并下载相关的docker镜像。tensorflow所需的所有组件都已经在docker中预先准备好了,您只要运行程序就行了。整个过程需要下载700M左右的数据,请耐心等待。

部署完以后,将你要识别的图像放到image_recognition文件夹里面,然后运行:

cd image-recognition/

bash imgdetect.sh [你的图片文件名]

就可以啦。例如

bash imgdetect.sh demo01.jpg

demo01.jpg是一个橙子的图片,程序会返回

orange(橙色)---(Matching = 0.85452)

受限于树莓派的CPU速度,您大约需要30秒才能得到识别结果,但已经很好玩了不是?

借助这个程序,您可以往下拓展,例如利用树莓派的摄像头,定时拍摄一幅照片,然后用这个程序来进行图像识别,用喇叭说出识别的结果来。这个用来给小朋友做玩具用,还是不错的。

觉得本文有帮助的,被忘了给我点个赞。感兴趣的人多的话,笔者将继续发布《一行代码在树莓派上进行人脸识别》哦。

PS:

近期由于“网络安全原因”,部分读者反映有几种异常情况,统一回复如下:

1)在git clone的时候等待很久,实际是因为网络原因无法在github上抓取代码,我把项目代码共享到百度网盘 https://pan.baidu.com/s/1b3R65g ,大家可以随意下载。下载后进入image-recogintion-master 运行 bash install.sh 即可。

2)有部分用户在执行过程中,显示“docker未安装”,然后长时间等待。这种情况也是因为网络不良导致无法安装docker。建议在树莓派上反复执行:

curl -sSL https://get.docker.com | sh     直到成功为止。

3)镜像下载是一个比较长的过程,建议耐心等待。镜像文件托管到dockerhub上,如果不成功,建议在网络良好的时候执行 docker pull clarkchan/rpi-tensorflow-imagenet

4) 部分用户在执行的时候反映“memory”问题,主要是前几代树莓派内存不足,无法运行程序导致的,请确认你的硬件升级到树莓派3

PS(2018-06-03):

今天重新运行了一次,发现由于网络原因,原来的安装脚本有些步骤我也不能用了。顺便修改了一下。改了几个地方:

1) docker的安装。由于官网经常失联,安装改为apt-get install docker-ce,不从官网直接下载了。

2)镜像获取的时候,改使用root权限,避免permission denied 问题。

3) 中文翻译的功能,采用简友 阿群1986 的方案,从百度翻译改为爱词霸翻译。

相关文章

网友评论

  • 8aff140ea2a0:大神,我在部署的时候遇到下面的问题:
    Warning: failed to get default registry endpoint from daemon (Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.37/info: dial unix /var/run/docker.sock: connect: permission denied). Using system default: https://index.docker.io/v1/
    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.37/images/create?fromImage=clarkchan%2Frpi-tensorflow-imagenet&tag=latest: dial unix /var/run/docker.sock: connect: permission denied
    这个应该怎么办啊?镜像文件能从别的地方下载吗?
    clarkchan:要sudo 一下
  • 阿群1986:print('%s(%s)---(Matching = %.5f)' % (human_string,baidu(human_string,dst='zh').encode('utf-8'), score)) 百度在线翻译暂时不可用, 目前只有爱词霸iciba的'zh'可用, 补丁已上传至https://github.com/flyingtimes/image-recognition/pull/1/files
  • 阿群1986:感谢博主, 已关注博主的GitHub:
    https://github.com/flyingtimes/face-recognizer
    clarkchan:@阿群1986 谢谢提醒!
    阿群1986:github上的完整仓库太大, 建议改为 --depth=1
    git clone --depth=1 https://github.com/flyingtimes/image-recognition.git
    阿群1986:https://github.com/flyingtimes/image-recognition
    https://hub.docker.com/r/clarkchan/rpi-tensorflow-imagenet/
  • 8d3e5b79895d:Traceback (most recent call last):
    File "/images/classify_image.py", line 229, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    File "/images/classify_image.py", line 195, in main
    run_inference_on_image(image)
    File "/images/classify_image.py", line 169, in run_inference_on_image
    print('%s(%s)---(Matching = %.5f)' % (human_string,baidu(human_string,dst='zh').encode('utf-8'), score))
    File "/usr/local/lib/python2.7/dist-packages/translation/__init__.py", line 28, in baidu
    dst = dst, proxies = proxies)
    File "/usr/local/lib/python2.7/dist-packages/translation/main.py", line 33, in get
    if r == '': raise TranslateError('No translation get, you may retry')
    translation.exception.TranslateError: No translation get, you may retry
    博主大大请问这是要怎么办啊
    阿群1986:@异次生活 补丁已上传至https://github.com/flyingtimes/image-recognition/pull/1/files
    d3f7d3dacbfc:遇到了同样的问题,识别后翻译时出错的?
  • acfd0724ac7a:大佬有加载好的镜像吗,我这个总是出错1049684642@qq.com
    clarkchan:应该可以用了
  • 为人民的美好生活而读书:我运行下面的命令之后就卡在这儿不动了,博主,请问这是什么原因啊?/
    sudo bash imgdetect.sh demo01.jpg
    Unable to find image 'clarkchan/rpi-tensorflow-imagenet:latest' locally
    latest: Pulling from clarkchan/rpi-tensorflow-imagenet
    77c45a60f4e6: Pull complete
    f390e8b7d434: Pull complete
    8789c51a4fa7: Pull complete
    8ac55180a8d1: Pull complete
    0afaed00e7f3: Pull complete
    5ec4f692ca72: Pull complete
    5d931e9540db: Pull complete
    2223e330671d: Pull complete
    99fd83e5f0f6: Pull complete
    29c8044f266a: Pull complete
    b897db329b94: Pull complete
    d5d735d03938: Pull complete
    6176cf61979a: Pull complete
    744f589472a5: Pull complete
    Digest: sha256:99dcbd05dbcfb7362db6c342e46fa9d82100567301e99773ff4d02e8be9f31c9
    Status: Downloaded newer image for clarkchan/rpi-tensorflow-imagenet:latest
    9331b4c5de35:@小果_9da3 请问问题解决了吗?我也出现了相同问题
    f412e9156a8e:@clarkchan
    再运行一次出现这样的错误?
    Traceback (most recent call last):
    File "/images/classify_image.py", line 229, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    File "/images/classify_image.py", line 195, in main
    run_inference_on_image(image)
    File "/images/classify_image.py", line 169, in run_inference_on_image
    print('%s(%s)---(Matching = %.5f)' % (human_string,baidu(human_string,dst='zh').encode('utf-8'), score))
    File "/usr/local/lib/python2.7/dist-packages/translation/__init__.py", line 28, in baidu
    dst = dst, proxies = proxies)
    File "/usr/local/lib/python2.7/dist-packages/translation/main.py", line 33, in get
    if r == '': raise TranslateError('No translation get, you may retry')
    translation.exception.TranslateError: No translation get, you may retry
    clarkchan:@emulate_7bcf 这是对的啊,镜像已经成功下载了。再运行一次有什么错误信息吗
  • b72d2c48fce2:TensorFlow是咋搭载进去的?
    clarkchan: @即将拥有人鱼线的能 用camerapi
    b72d2c48fce2:@clarkchan 那摄像头用啥呢
    clarkchan:@即将拥有人鱼线的能 通过docker打包进去的。
  • 178bbca2eea4:您好,我在树莓派上按照您给的指导,每次启动bash后,等上大约40S-1min。我的树莓派会自动重新启动。请问你是否出现过这样的问题?
    硬件:树莓派 Raspberry Pi 3 Model B
    clarkchan:@博川 应该不涉及watchdog,我这边都挺顺利的。有其他网友反映有可能直接下载不行(各省情况不一样),但科学上网以后就可以了
    178bbca2eea4:1)内存卡用的是8G
    2 )并没有Log打印,我还在测试中。会不会是WathchDog?我在重启后的信息里面看到WatchDog相关信息。但是只有一行。
    clarkchan:我这边不会,有什么提示信息吗?我这边用的是一摸一样的型号啊。您的tf卡上面的空间是否充足?输入df -h 看一下
  • c2682c408edf:期待更多树莓派的好文
    c2682c408edf:请问一下你第一篇文章tensorflow给的下载地址运行有错误
    c2682c408edf:已拜读
    clarkchan: @Kevin大神 第二篇已'发出
  • smilemouse:期待人脸识别
    clarkchan: @smilemouse 第二篇已释出

本文标题:树莓派一键部署系列之一:tensorflow上的神经网络图像识别

本文链接:https://www.haomeiwen.com/subject/kldhqxtx.html