CoreML Tools
1  | pip install -U coremltools  | 
TensorFlow
环境
Python 2.7Python 3.3+这里采用
Installing with native pip
- 检查pip版本
 
1  | pip -V # for Python 2.7  | 
- 升级pip版本和six版本(建议pip版本>=8.1)
 
1  | sudo easy_install --upgrade pip  | 
- pip安装TensorFlow
 
1  | pip3 install tensorflow  | 
1  | $ pip3 install tensorflow  | 
- (可选)如果pip安装TensorFlow失败尝试以下命令
 
1  | sudo pip install --upgrade tfBinaryURL # Python 2.7  | 
tfBinaryURL是指TensorFlow Python package的URL,查找合适的下载地址
例如:
 1
2 sudo pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl
- 验证安装正确
 
1  | $ Python or Python3  | 
Keras
- To install Keras, cd to the Keras folder and run the install command:
 
1  | sudo python setup.py install  | 
- You can also install Keras from PyPI:
 
1  | sudo pip install keras  | 
1  | $ sudo pip3 install keras  | 
编译TensorFlow For iOS
- Install 
XCode 7.3or more recent. 
下载TensorFlow到项目根目录并编译
在编译源文件之前, 要先进行修改TensorFlow Kernel的一些文件(版本 <= 1.2.1)
kernel path:
tensorflow/tensorflow/core/kernelscwise_op_add_1.cc源码:
1
2
3
4
5
6
7
8
9
10
11...
...
namespace tensorflow {
REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
int64);
...
...修改为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17...
...
namespace tensorflow {
REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
int64);
// line 21 insert this code
REGISTER(BinaryOp, CPU, "Add", functor::add, int32);
// insert end
...
...cwise_op_less.cc源码:
1
2
3
4
5
6
7
8
9
10
11
12...
...
namespace tensorflow {
REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
int32, int64, uint8, int8, int16);
REGISTER7(BinaryOp, GPU, "Less", functor::less, float, Eigen::half, double,
int64, uint8, int8, int16);
...
...修改为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19...
...
namespace tensorflow {
REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
int32, int64, uint8, int8, int16);
// line 21 insert this code
REGISTER(BinaryOp, CPU, "Less", functor::less, int32);
// insert end
REGISTER7(BinaryOp, GPU, "Less", functor::less, float, Eigen::half, double,
int64, uint8, int8, int16);
...
...
install the
command-line toolsusing xcode-select1
xcode-select --install
- 
 
1
2brew install automake
brew install libtool Building all at once
1
tensorflow/contrib/makefile/build_all_ios.sh
项目导入TensorFlow
下载TensorFlow到项目根目录并编译
libtensorflow-core.aOther Link Flags中加入$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.aLibrary Search Paths中加入$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib
libprotobuf.a & libprotobuf-lite.aBuild Phases | Link Binary With Libraries中加入libprotobuf.a & libprotobuf-lite.a(path:tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib/)Library Search Paths中加入$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib
Header Search Paths中加入$(SRCROOT)/tensorflow/$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src/$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads/eigen$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/proto
Other Link Flags中加入-force_loadBuild Phases | Link Binary With Libraries中加入Accelerate.frameworkC++ Language Dialect设置为GNU++11orGNU++14C++ Standard Library设置为libc++Enable Bitcode设置为No删除所有使用的
-all_load,替换为-ObjCRemove any use of the
-all_loadflag in your project. The protocol buffers libraries (full and lite versions) contain duplicate symbols, and the-all_loadflag will cause these duplicates to become link errors. If you were using-all_loadto avoid issues with Objective-C categories in static libraries, you may be able to replace it with the-ObjCflag.忽略TensorFlow编译的警告:
- 在
Other C Flags&Other C++ Flags中加入-isystem $(SRCROOT)/tensorflow 
- 在
 
参考文档
编译TensorFlow:
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile
修改Kernels错误:
https://github.com/h4x3rotab/emoji-tf-ios/blob/master/README.md
项目中导入TensorFlow静态库:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/ios/README.md
忽略TensorFlow警告: