0%

CoreML & TensorFlow For iOS

TensorFlowForIOS Demo

CoreML Tools

官网

下载网站

文档

1
2
3
pip install -U coremltools
# or
pip install -U coremltools --upgrade --ignore-installed six

TensorFlow

安装文档

环境

  • Python 2.7
  • Python 3.3+

这里采用 Installing with native pip

  • 检查pip版本
1
2
pip -V  # for Python 2.7
pip3 -V # for Python 3.n
  • 升级pip版本和six版本(建议pip版本>=8.1)
1
2
sudo easy_install --upgrade pip
sudo easy_install --upgrade six
  • pip安装TensorFlow
1
2
pip3 install tensorflow
pip install tensorflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ pip3 install tensorflow
Collecting tensorflow
Downloading tensorflow-1.2.1-cp35-cp35m-macosx_10_11_x86_64.whl (33.6MB)
100% |████████████████████████████████| 33.6MB 26kB/s
Collecting werkzeug>=0.11.10 (from tensorflow)
Using cached Werkzeug-0.12.2-py2.py3-none-any.whl
Collecting wheel>=0.26 (from tensorflow)
Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
100% |████████████████████████████████| 71kB 65kB/s
Collecting html5lib==0.9999999 (from tensorflow)
Using cached html5lib-0.9999999.tar.gz
Requirement already satisfied: numpy>=1.11.0 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from tensorflow)
Collecting protobuf>=3.2.0 (from tensorflow)
Collecting backports.weakref==1.0rc1 (from tensorflow)
Using cached backports.weakref-1.0rc1-py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from tensorflow)
Collecting bleach==1.5.0 (from tensorflow)
Using cached bleach-1.5.0-py2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorflow)
Using cached Markdown-2.6.8.tar.gz
Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from protobuf>=3.2.0->tensorflow)
Installing collected packages: werkzeug, wheel, html5lib, protobuf, backports.weakref, bleach, markdown, tensorflow
Running setup.py install for html5lib ... done
Running setup.py install for markdown ... done
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.6.8 protobuf-3.3.0 tensorflow-1.2.1 werkzeug-0.12.2 wheel-0.29.0
  • (可选)如果pip安装TensorFlow失败尝试以下命令
1
2
sudo pip  install --upgrade tfBinaryURL   # Python 2.7
sudo pip3 install --upgrade tfBinaryURL # Python 3.n

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
2
3
4
5
6
$ Python or Python3

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

Keras

github

  • 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ sudo pip3 install keras
Password:
The directory '/Users/shaoweinan154/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/shaoweinan154/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting keras
Downloading Keras-2.0.6.tar.gz (228kB)
100% |████████████████████████████████| 235kB 170kB/s
Collecting theano (from keras)
Downloading Theano-0.9.0.tar.gz (3.1MB)
100% |████████████████████████████████| 3.1MB 86kB/s
Collecting pyyaml (from keras)
Downloading PyYAML-3.12.tar.gz (253kB)
100% |████████████████████████████████| 256kB 70kB/s
Collecting six (from keras)
Downloading six-1.10.0-py2.py3-none-any.whl
Collecting numpy>=1.9.1 (from theano->keras)
Downloading numpy-1.13.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
100% |████████████████████████████████| 4.5MB 59kB/s
Collecting scipy>=0.14 (from theano->keras)
Downloading scipy-0.19.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.1MB)
100% |████████████████████████████████| 16.1MB 30kB/s
Installing collected packages: numpy, scipy, six, theano, pyyaml, keras
Running setup.py install for theano ... done
Running setup.py install for pyyaml ... done
Running setup.py install for keras ... done
Successfully installed keras-2.0.6 numpy-1.13.1 pyyaml-3.12 scipy-0.19.1 six-1.10.0 theano-0.9.0

编译TensorFlow For iOS

官方文档

  • Install XCode 7.3 or more recent.
  • 下载TensorFlow到项目根目录并编译

    在编译源文件之前, 要先进行修改TensorFlow Kernel的一些文件(版本 <= 1.2.1)

    kernel path: tensorflow/tensorflow/core/kernels

    • cwise_op_add_1.cc

      源码:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      ...
      ...
      #include "tensorflow/core/kernels/cwise_ops_common.h"

      namespace tensorflow {
      REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
      int64);

      #if TENSORFLOW_USE_SYCL
      ...
      ...

      修改为:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      ...
      ...
      #include "tensorflow/core/kernels/cwise_ops_common.h"

      namespace tensorflow {
      REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
      int64);

      // line 21 insert this code
      #if defined(__ANDROID_TYPES_SLIM__)
      REGISTER(BinaryOp, CPU, "Add", functor::add, int32);
      #endif // __ANDROID_TYPES_SLIM__
      // insert end

      #if TENSORFLOW_USE_SYCL
      ...
      ...
    • cwise_op_less.cc

      源码:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      ...
      ...
      #include "tensorflow/core/kernels/cwise_ops_common.h"

      namespace tensorflow {
      REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
      int32, int64, uint8, int8, int16);
      #if GOOGLE_CUDA
      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
      ...
      ...
      #include "tensorflow/core/kernels/cwise_ops_common.h"

      namespace tensorflow {
      REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
      int32, int64, uint8, int8, int16);

      // line 21 insert this code
      #if defined(__ANDROID_TYPES_SLIM__)
      REGISTER(BinaryOp, CPU, "Less", functor::less, int32);
      #endif // __ANDROID_TYPES_SLIM__
      // insert end

      #if GOOGLE_CUDA
      REGISTER7(BinaryOp, GPU, "Less", functor::less, float, Eigen::half, double,
      int64, uint8, int8, int16);
      ...
      ...
  • install the command-line tools using xcode-select

    1
    xcode-select --install
  • install automake/libtool:

    1
    2
    brew install automake
    brew install libtool
  • Building all at once

    1
    tensorflow/contrib/makefile/build_all_ios.sh

项目导入TensorFlow

  • 下载TensorFlow到项目根目录并编译

  • libtensorflow-core.a

    • Other Link Flags 中加入 $(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a
    • Library Search Paths 中加入 $(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib
  • libprotobuf.a & libprotobuf-lite.a

    • Build 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_load

  • Build Phases | Link Binary With Libraries 中加入 Accelerate.framework

  • C++ Language Dialect 设置为 GNU++11 or GNU++14

  • C++ Standard Library 设置为 libc++

  • Enable Bitcode 设置为 No

  • 删除所有使用的 -all_load ,替换为 -ObjC

    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 -ObjC flag.

  • 忽略TensorFlow编译的警告:

    • Other C Flags & Other C++ Flags中加入-isystem $(SRCROOT)/tensorflow

参考文档