Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile ios example in tensorflow

Tags:

tensorflow

I just realized the tensorflow has supported ios now. But how to compile the example in contrib/ios_examples directory?

thanks!

josh

like image 445
josh Avatar asked Dec 14 '22 05:12

josh


1 Answers

We're still finishing off all the documentation, but here's a draft of the README I'll be adding to the ios_examples directory. I'd be interested to hear if this helps, and if you have ideas for improvements.

TensorFlow iOS Examples

This folder contains examples of how to build applications for iOS devices using TensorFlow.

Building the Examples

  • You'll need Xcode 7.3 or later, with the command-line tools installed.

  • Follow the instructions at tensorflow/contrib/makefile to compile a static library containing the core TensorFlow code.

  • Download Inception v1, 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.

  • 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.

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.

  • 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.

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

  • 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-, 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.

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

like image 188
Pete Warden Avatar answered Feb 23 '23 05:02

Pete Warden