tensorflow 在 iOS 中的使用初体验

随着tensorflow最近的更新,编译iOS库方便了很多,话不多说,我们先看看运行的效果图。

是不是很神奇,就目前而已,虽然辨识度还不是很高,只能识别寻常的物品,但是相信在不久的将来会有很快的发展

以下是我下载tensorflow到在xcode上面运行成功的全部过程。

1, 下载 tensorflow

tensorflow的github地址 https://github.com/tensorflow/tensorflow.git

1
git clone https://github.com/tensorflow/tensorflow.git

下载完成以后,打开在项目 tensorflow/tensorflow/contrib/ios_examples 你会发现该目录下有三个项目文件和一个readme.md.
分别是 benchmark camera simple

这个时候如果我们运行项目肯定是不成功的,在readme.md中我们可以发现,在iOS平台上运行tensorflow例子需要满足下面三点:

你的Xcode版本必须在7.3以上,并且安装了 command-line工具本人用的Xcode8 默认就有安装。

项目中必须有一个静态库:libtensorflow-core.a 这个在下载的项目中是没有的,需要我们自己编译生成,生成过程我们待会会讲到。

下载 Inception v1,解压后将 imagenet_comp_graph_label_strings.txttensorflow_inception_graph.pb 放在 simple 和 camera 的项目中。

2, 生成libtensorflow-core.a静态库

我们的最终目的是能够编译运行 camera 项目 ,该项目在 tensorflow/tensorflow/contrib/ios_examples中,前面有提到过,之前我们已经通过 Inception v1得到了imagenet_comp_graph_label_strings.txttensorflow_inception_graph.pb两个文件,所以现在就是编译生成 libtensorflow-core.a静态库。

进入目录:tensorflow/tensorflow/contrib/makefile ,你可以看到一大堆 .sh 结尾的文件,找到 build_all_ios.sh ,Mac 上可以直接在 termina(终端)上运行命令编译

1
sh build_all_ios.sh

这是一个漫长的代码翻滚过程,几个小时是很正常的。在此过程中可能也会遇到问题,以下是我遇到的问题。以及解决方法:
问题一:

1
2
3
+ autoreconf -f -i -Wall,no-obsolete
Can't exec "aclocal": No such file or directory at /usr/local/Cellar/autoconf/2.69/share/autoconf/Autom4te/FileUtils.pm line 326.
autoreconf: failed to run aclocal: No such file or director

解决方案: 在mac上安装软件包管理工具 Homebrew
然后终端运行

1
brew install automake -v

问题二:

1
2
3
4
configure.ac:30: error: possibly undefined macro: AC_PROG_LIBTOOL
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
autoreconf: /usr/local/Cellar/autoconf/2.69/bin/autoconf failed with exit status: 1

该问题是应为没有安装libtool导致的,所以终端输入:

1
brew install libtool

3, 运行项目

编译成功后,你可以在目录 tensorflow/tensorflow/contrib/makefile/gen/lib 下找到一个静态库:libtensorflow-core.a,把这个静态库拷贝到camera项目中,然后编译运行。

参考博文

Share Comments