Update for README.md files for ios_examples and makefile.

Change: 125601282
This commit is contained in:
A. Unique TensorFlower 2016-06-22 12:24:15 -08:00 committed by TensorFlower Gardener
parent 7614900108
commit e231a8b382
2 changed files with 252 additions and 84 deletions

View File

@ -6,38 +6,71 @@ This folder contains examples of how to build applications for iOS devices using
- You'll need Xcode 7.3 or later, with the command-line tools installed.
- Follow the instructions at [tensorflow/contrib/makefile](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile) to compile a static library containing the core TensorFlow code.
- Follow the instructions at
[tensorflow/contrib/makefile](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile)
under "iOS" to compile a static library containing the core TensorFlow code.
- Download [Inception v1](https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip), and extract the label and graph files into the data folders inside both the simple and camera examples.
- Download
[Inception v1](https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip),
and extract the label and graph files into the data folders inside both the
simple and camera examples.
- Load the Xcode project inside the `simple` subfolder, and press Command-R to build and run it on the simulator or your connected device.
- Load the Xcode project inside the `simple` subfolder, and press Command-R to
build and run it on the simulator or your connected device.
- You should see a single-screen app with a "Run Model" button. Tap that, and you should see some debug output appear below indicating that the example Grace Hopper image has been analyzed, with a military uniform recognized.
- You should see a single-screen app with a "Run Model" button. Tap that, and
you should see some debug output appear below indicating that the example
Grace Hopper image has been analyzed, with a military uniform recognized.
- Once that's been successfully run, make sure you have a real device connected and open up the Xcode project in the camera subfolder. Once you build and run that, you should get a live camera view that you can point at objects to get real-time recognition results.
- Once you have success there, make sure you have a real device connected and
open up the Xcode project in the camera subfolder. Once you build and run
that, you should get a live camera view that you can point at objects to get
real-time recognition results.
## Troubleshooting
If you're hitting problems, here's a checklist of common things to investigate:
- Make sure that you've run the `download_dependencies.sh` and `compile_ios_protobuf.sh` scripts before you run `compile_ios_tensorflow`.
- Make sure that you've run the `download_dependencies.sh` and
`compile_ios_protobuf.sh` scripts before you run `compile_ios_tensorflow`.
(These should be called by `build_all_ios.sh` if you are using it, but check
if they have run successful.)
- Check that you have version 7.3 of Xcode.
- If there are Eigen errors, look inside the build settings of your Xcode project. In the Search Paths section, you'll see an Eigen include directory that changes with each version of the framework. You may need to update this to may the version in your tensorflow/contrib/makefile/downloads folder.
- If there's a complaint about no Session's registered, that means that the C++ global constructors that TensorFlow relies on for registration haven't been linked in properly. You'll have to make sure your project uses force_load, as described below.
- If there's a complaint about no Sessions registered, that means that the C++
global constructors that TensorFlow relies on for registration haven't been
linked in properly. You'll have to make sure your project uses force_load, as
described below.
## 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:
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 in tensorflow/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.
- The `compile_ios_tensorflow.sh` script builds a universal static library in
`tensorflow/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 from tensorflow/contrib/makefile/gen/protobuf_ios/lib to your Build Stages and Library Search Paths.
- You'll also need to add `libprotobuf.a` and `libprotobuf-lite.a` from
`tensorflow/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-eigen-<current Eigen hash>, and tensorflow/contrib/makefile/gen/proto.
- 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-latest`, 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.
- 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.
- The library doesn't currently support bitcode, so you'll need to disable that in your project settings.
- The library doesn't currently support bitcode, so you'll need to disable that
in your project settings.

View File

@ -1,34 +1,81 @@
### TensorFlow Makefile
The recommended way to build TensorFlow from source is using the Bazel
open-source build system. Sometimes this isn't possible though:
open-source build system. Sometimes this isn't possible.
- The system may not have the RAM or processing power to support Bazel.
- Bazel dependencies might not be available.
- The build system may not have the RAM or processing power to support Bazel.
- Bazel or its dependencies may not be available.
- You may want to cross-compile for an unsupported target system.
This experimental project supplies a Makefile automatically derived from the
dependencies listed in the Bazel project, that can be used with GNU's make tool.
It offers the ability to compile the core C++ runtime into a static library, but
doesn't include more advanced features like Python or other language bindings,
or GPU support.
dependencies listed in the Bazel project that can be used with GNU's make tool.
With it, you can compile the core C++ runtime into a static library.
## Building
This static library will not contain:
To compile the library and an example program using it, first pull the
dependencies:
- Python or other language bindings
- GPU support
You can target:
- iOS
- OS X (macOS)
- Android
- Raspberry-PI
You will compile tensorflow and protobuf libraries that you can link into other
applications. You will also compile the [benchmark](../../tools/benchmark/)
application that will let you check your application.
## Before you start (all platforms)
```bash
First, clone this TensorFlow repository.
You will need to download all dependencies as well. We have provided a script
that does so, to be run (as with all commands) at the root of the repository:
```bash
tensorflow/contrib/makefile/download_dependencies.sh
```
You should only need to do this step once, it puts required libraries like Eigen
in the `tensorflow/contrib/makefile/downloads/` folder. You will also need to
make sure you have a version of [protobuf 3](https://github.com/google/protobuf)
installed on your system, either through package management or building from
source.
You should only need to do this step once. It downloads the required libraries
like Eigen in the `tensorflow/contrib/makefile/downloads/` folder.
Then you can build the project:
You should download the example graph from [https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip](https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip).
## Building on Linux
_Note: This has only been tested on Ubuntu._
Don't forget to download dependencies if you haven't already:
```bash
tensorflow/contrib/makefile/download_dependencies.sh
```
You will need install a version of
[protobuf 3](https://github.com/google/protobuf) on your system. We strongly
recommend that you compile and install the version downloaded in the script
above.
On Ubuntu, you can do this:
```bash
sudo apt-get install autoconf automake libtool curl make g++ unzip
pushd .
cd tensforflow/contrib/makefile/downloads/protobuf
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache
popd
```
If you have issues (or can't use apt-get), see
[these instructions](https://github.com/google/protobuf/blob/master/src/README.md)
for specific installation of C++ support tools.
After you have installed protobufs, you can run this from the repository root:
```bash
make -f tensorflow/contrib/makefile/Makefile
@ -36,100 +83,166 @@ make -f tensorflow/contrib/makefile/Makefile
This should compile a static library in
`tensorflow/contrib/makefile/gen/lib/tf_lib.a`, and create an example executable
at `tensorflow/contrib/makefile/gen/bin/benchmark`. To run the executable, use:
at `tensorflow/contrib/makefile/gen/bin/benchmark`.
Get the graph file, if you have not already:
```bash
tensorflow/contrib/makefile/gen/bin/benchmark --graph=tensorflow_inception_graph.pb
mkdir -p ~/graphs
curl -o ~/graphs/inception.zip \
https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
&& unzip ~/graphs/inception.zip -d ~/graphs/inception
```
You should download the example graph from [https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip](https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip).
To run the executable, use:
## Supported Systems
The script has been tested on Ubuntu, OS X, Android, and iOS. If you look in the
Makefile itself, you'll see it's broken up into host and target sections. If you
are cross-compiling, you should look at customizing the target settings to match
what you need for the system you're aiming at.
```bash
tensorflow/contrib/makefile/gen/bin/benchmark \
--graph=~/graphs/inception/tensorflow_inception_graph.pb
```
## Android
For Android, you'll need to explicitly specify that as the target, and supply
the location of the NDK toolchain on the command line, for example:
First, you will need to download and unzip the
[Native Development Kit (NDK)](http://developers.google.com/ndk). You will not
need to install the standalone toolchain, however.
Assign your NDK location to $NDK_ROOT:
```bash
make -f tensorflow/contrib/makefile/Makefile \
TARGET=ANDROID \
ANDROID_NDK_DIR=$(HOME)/toolchains/clang-21-stl-gnu
export NDK_ROOT=/absolute/path/to/NDK/android-ndk-rxxx/
```
You'll also need a compiled version of the protobuf libraries for Android. You
can use the helper script at `compile_android_protobuf.sh` to create these.
Download the graph if you haven't already:
```bash
mkdir -p ~/graphs
curl -o ~/graphs/inception.zip \
https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
&& unzip ~/graphs/inception.zip -d ~/graphs/inception
```
Then, execute the following:
```bash
tensorflow/contrib/makefile/download_dependencies.sh
tensorflow/contrib/makefile/compile_android_protobuf.sh -c
make -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID
```
At this point, you will have compiled libraries in `gen/lib/*` and the
[benchmark app](../../tools/benchmark) compiled for Android.
Run the benchmark by pushing both the benchmark and the graph file to your
attached Android device:
```bash
adb push ~/graphs/inception/tensorflow_inception_graph.pb /data/local/tmp/
adb push tensorflow/contrib/makefile/gen/bin/benchmark /data/local/tmp/
adb shell '/data/local/tmp/benchmark \
--graph=/data/local/tmp/classify_image_graph_def.pb \
--input_layer="input:0" \
--input_layer_shape="1,224,224,3" \
--input_layer_type="float" \
--output_layer="output:0"
'
```
For more details, see the [benchmark documentation](../../tools/benchmark).
## iOS
For iOS you'll need to first run `compile_ios_protobuf.sh` to build iOS versions
of the protobuf libraries. Once that's complete, you can run the makefile
specifying iOS as the target, along with the architecture you want to build for:
_Note: To use this library in an iOS application, see related instructions in
the [iOS examples](../ios_examples/] directory._
Install XCode 7.3 or more recent. If you have not already, you will need to
install the command-line tools using `xcode-select`:
```bash
make -f tensorflow/contrib/makefile/Makefile \
TARGET=IOS \
IOS_ARCH=ARM64
xcode-select --install
```
This will build the library for a single architecture, and the benchmark
program. Since the benchmark is command-line only, you'll need to load the
static library into an Xcode app project to use it.
If this is a new install, you will need to run XCode once to agree to the
license before continuing.
To build a complete universal library for iOS, containing all architectures,
you will need to run `compile_ios_protobuf.sh` followed by
`compile_ios_tensorflow.sh`. This creates a library in
`tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a` that you can link any
xcode project against. Here are complete build instructions:
Grab the source code for TensorFlow:
Also, download the graph if you haven't already:
```bash
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
mkdir -p ~/graphs
curl -o ~/graphs/inception.zip \
https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip \
&& unzip ~/graphs/inception.zip -d ~/graphs/inception
```
Download dependencies like Eigen and Protobuf:
### Building all at once
If you just want to get the libraries compiled in a hurry, you can run:
```bash
build_all_ios.sh
```
and wait a long time.
When this completes, you will have a library for a single architecture and the
benchmark program. Although successfully compiling the benchmark program is a
sign of success, the program is not a complete iOS app.
To see TensorFlow running on iOS, the example Xcode project in
[tensorflow/contrib/ios_example](../ios_example) shows how to use the static
library in a simple app.
### Building by hand
This section covers each step of building. For all the code in one place, see
[build_all_ios.sh](build_all_ios.sh).
If you have not already, you will need to download dependencies:
```bash
tensorflow/contrib/makefile/download_dependencies.sh
```
Build and install the host (OS X) copy of protobuf.
Next, you will need to compile protobufs for iOS:
```bash
cd tensorflow/contrib/makefile/downloads/protobuf/
./autogen.sh
./configure
make
sudo make install
cd ../../../../..
compile_ios_protobuf.sh
```
Build the iOS native versions of protobuf:
Then, you can run the makefile specifying iOS as the target, along with the
architecture you want to build for:
```bash
tensorflow/contrib/makefile/compile_ios_protobuf.sh
make -f tensorflow/contrib/makefile/Makefile \
TARGET=IOS \
IOS_ARCH=ARM64
```
Build all iOS architectures for TensorFlow:
This creates a library in
`tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a` that you can link any
xcode project against.
At this point, you will have a library for a single architecture and the
benchmark program. Although successfully compiling the benchmark program is a
sign of success, the program is not a complete iOS app.
To see TensorFlow running on iOS, the example Xcode project in
[tensorflow/contrib/ios_example](../ios_example) shows how to use the static
library in a simple app.
#### Universal binaries
In some situations, you will need a universal library. In that case, you will
still need to run `compile_ios_protobuf.sh`, but this time follow it with:
```bash
tensorflow/contrib/makefile/compile_ios_tensorflow.sh
compile_ios_tensorflow.sh
```
You will need to use -force_load in the linker flags
In XCode, you will need to use -force_load in the linker flags
section of the build settings to pull in the global constructors that are used
to register ops and kernels.
The example Xcode project in tensorflow/contrib/ios_example shows how to use the
static library in a simple app.
## Raspberry Pi
Building on the Raspberry Pi is similar to a normal Linux system, though we
@ -155,10 +268,19 @@ some extra optimization flags to give you code that will run faster:
```bash
make -f tensorflow/contrib/makefile/Makefile HOST_OS=PI TARGET=PI \
OPTFLAGS="-Os -mfpu=neon-vfpv4 -funsafe-math-optimizations -ftree-vectorize"
OPTFLAGS="-Os -mfpu=neon-vfpv4 -funsafe-math-optimizations -ftree-vectorize"
```
## Dependencies
# Other notes
## Supported Systems
The Make script has been tested on Ubuntu and OS X. If you look in the Makefile
itself, you'll see it's broken up into host and target sections. If you are
cross-compiling, you should look at customizing the target settings to match
what you need for your desired system.
## Dependency Managment
The Makefile loads in a list of dependencies stored in text files. These files
are generated from the main Bazel build by running
@ -171,3 +293,16 @@ make header changes you will need to run this command to recompile cleanly:
```bash
make -f tensorflow/contrib/makefile/Makefile clean
```
### Cleaning up
In some situations, you may want to completely clean up. The dependencies,
intermediate stages, and generated files are stored in:
```bash
tensorflow/contrib/makefile/downloads
tensorflow/contrib/makefile/gen
```
Those directories can safely be removed, but you will have to start over with
`download_dependencies.sh` once you delete them.