Running the example TensorFlow Lite iOS apps

There are two TensorFlow Lite example apps for iOS, named simple and camera, similar to the TensorFlow Mobile iOS apps simple and camera, but implemented in the TensorFlow Lite API, in the official releases of TensorFlow 1.5 - 1.8 at https://github.com/tensorflow/tensorflow/releases, and likely also in the latest TensorFlow repo. You can run the following commands to prepare and run the two apps, similarly documented under "iOS Demo App" at https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite:

cd tensorflow/contrib/lite/examples/ios
./download_models.sh
sudo gem install cocoapods
cd camera
pod install
open tflite_camera_example.xcworkspace
cd ../simple
pod install
open simple.xcworkspace

Now you'll have two Xcode iOS projects, simple and camera (named  tflite_simple_example and tflite_camera_example, respectively, in Xcode), launched and you can install and run them on your iOS device (the simple app can also run on your iOS simulator).

download_models.sh will download a zip file that contains the mobilenet_quant_v1_224.tflite model file and labels.txt label file, then copy them to the simple/data and camera/data directories. Notice that somehow this script is not included in the official TensorFlow 1.5.0 and 1.6.0 releases. You'll need to do git clone https://github.com/tensorflow/tensorflow and clone the latest source (as of March 2018) to get it.

You can take a look at the source code in the Xcode tflite_camera_example project's CameraExampleViewController.mm file and the tflite_simple_example RunModelViewController.mm file to get an idea of how to use the TensorFlow Lite API to load and run a TensorFlow Lite model. Before we walk you through a step-by-step tutorial of how to create a new iOS app and add the TensorFlow Lite support to it to run a prebuilt TensorFlow Lite model, we'll quickly show you in concrete numbers one of the benefits of using TensorFlow Lite, as we mentioned earlier—the app binary size:

The tensorflow_inception.graph.pb model file used in the tf_camera_example  TensorFlow Mobile example app, located in the tensorflow/examples/ios/camera folder, is 95.7 MB, while the mobilenet_quant_v1_224.tflite TensorFlow Lite model file used in the tflite_camera_example TensorFlow Lite example app, located in the tensorflow/contrib/lite/examples/ios/camera folder, is only 4.3 MB. The quantized version of the TensorFlow Mobile retrained Inception 3 model file, as we've seen in the HelloTensorFlow app in Chapter 2Classifying Images with Transfer Learning, is about 22.4 MB, and the retrained MobileNet TensorFlow Mobile model file is 17.6 MB. In summary, the following lists the sizes of four different types of models:

If you install and run the two apps on your iPhone, you'll see from your iPhone's settings that the app size for tflite_camera_example is about 18.7 MB, and the size for tf_camera_example is about 44.2 MB.

It's true that the accuracy with the Inception 3 model is a bit higher than with a MobileNet model, but in many use cases, the small accuracy difference can be ignored. Also, admittedly, mobile apps these days can easily take dozens of MB and the difference of 20 or 30 MB in app size may not sound like a big deal in some use cases, but size would be more sensitive in smaller embedded devices, and if we can achieve about the same accuracy with a faster speed and smaller size, without going through too much trouble, it'd always be a nice thing for users.