iOS 集成tensorflow

来源:互联网 发布:燕郊seo 编辑:程序博客网 时间:2024/05/29 15:21

集成 tensorflow

tensorflow 是Google开放的关于机器学习的框架,最近正式版本1.0发布了,作者就自己玩了一下,关于踩坑有以下:

当在Github上下载了整个包,我们需要在终端进入tensorflow/tensorflow/contrib/makefile,可以看到有一个shell脚本:build_all_ios.sh 

坑1:

执行build_all_ios.sh 脚本时候的网络环境,sed: tensorflow/contrib/makefile/downloads/eigen/Eigen/src/Core/arch/NEON/Complex.h: No such file or directory

解决办法是:

需要当前网络环境支持https


上一步完成之后,执行脚本之后,会遇到

坑2:

+ autoreconf -f -i -Wall,no-obsolete
./autogen.sh: line 48: autoreconf: command not found

解决办法:

brew install autoconf


上一步完成之后,继续执行脚本,遇到

坑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 directory

解决办法:

brew install automake

上一步完成之后,继续执行脚本,遇到

坑4:

+ autoreconf -f -i -Wall,no-obsolete
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

解决办法是:

brew install libtool

继续执行脚本,没有问题了,整个脚本执行需要时间,最后完成的标志是:

tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib路径下有libprotobuf.a,libprotobuf-lite.a
tensorflow/tensorflow/contrib/makefile/gen/lib路径下有libtensorflow-core.a

搞定以上之后,进入路径:

tensorflow/contrib/ios_examples/simple

打开其中的tf_ios_makefile_example.xcodeproj工程,编译通过,点击App中的Run Model按钮,执行完成,你会看到tensorflow_inception_graph.pb文件被加载完成

然后进入路径:tensorflow/contrib/ios_examples/camera

打开其中的camera_example.xcodeproj工程,在带有摄像头的真机上编译执行,然后就大功告成,可以自己玩了


Creating your Own App

You'll need to update various settings in your app to link against TensorFlow. You can view them in the example projects, but here's a full rundown:

  • The compile_ios_tensorflow.sh script builds a universal static library intensorflow/contrib/makefile/gen/lib/libtensorflow-core.a. You'll need to add this to your linking build stage, and in Search Paths add tensorflow/contrib/makefile/gen/lib to the Library Search Paths setting.

  • You'll also need to add libprotobuf.a and libprotobuf-lite.a fromtensorflow/contrib/makefile/gen/protobuf_ios/lib to your Build Stages and Library Search Paths.

  • The Header Search paths needs to contain:

    • the root folder of tensorflow,
    • tensorflow/contrib/makefile/downloads/protobuf/src
    • tensorflow/contrib/makefile/downloads,
    • tensorflow/contrib/makefile/downloads/eigen, and
    • tensorflow/contrib/makefile/gen/proto.
  • In the Linking section, you need to add -force_load followed by the path to the TensorFlow static library in the Other Linker Flags section. This ensures that the global C++ objects that are used to register important classes inside the library are not stripped out. To the linker, they can appear unused because no other code references the variables, but in fact their constructors have the important side effect of registering the class.

  • You'll need to include the Accelerate framework in the "Link Binary with Libraries" build phase of your project.

  • C++11 support (or later) should be enabled by setting C++ Language Dialect to GNU++11 (or GNU++14), and C++ Standard Library to libc++.

  • The library doesn't currently support bitcode, so you'll need to disable that in your project settings.

  • Remove any use of the -all_load flag in your project. The protocol buffers libraries (full and lite versions) contain duplicate symbols, and the -all_load flag will cause these duplicates to become link errors. If you were using -all_load to avoid issues with Objective-C categories in static libraries, you may be able to replace it with the -ObjCflag.








0 0