整合 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.