TensorFlow is a multipurpose machine learning framework. TensorFlow can be used anywhere from training huge models across clusters in the cloud, to running models locally on an embedded system like your phone.

What you'll Learn

What you will build

A simple app that runs a TensorFlow image recognition program on your photos, to identify flowers.

CC-BY by Felipe VenĂ¢ncio

Most of this codelab will be using the terminal. Open it now.

Install TensorFlow

Before we can begin the tutorial you need to install several pieces of software:

If you have the git repository from the first codelab

This codelab uses files generated during the TensorFlow for Poets 1 codelab. If you have not completed that codelab we recommend you take a look. If you prefer not to, instructions for downloading the missing files are given in the next sub-section.

In TensorFlow for Poets 1, you also cloned the relevant files for this codelab. Ensure that it is your current working directory, checkout the branch and check the contents, as follows:

cd tensorflow-for-poets-2

This directory should contain four other subdirectories. We will be using three of them.

ls tf_files/
flower_photos/ retrained_graph.pb  retrained_labels.txt

Otherwise (if you don't have the files from the first Codelab)

Clone the Git repository

The following command will clone the Git repository containing the files required for this codelab:

git clone https://github.com/googlecodelabs/tensorflow-for-poets-2

Now cd into the directory of the clone you just created. That's where you will be working for the rest of this codelab:

cd tensorflow-for-poets-2

The repo contains three directories: android/, scripts/, and tf_files/

Checkout the files from the end_of_first_codelab branch

git checkout end_of_first_codelab

ls tf_files
flower_photos/ retrained_graph.pb  retrained_labels.txt

The demo app requires several additional tools:

  1. Xcode

You can download Xcode here

  1. Xcode command line tools

Install the xcode command line tools with the following command:

xcode-select --install
  1. Cocoapods

Cocoapods use ruby, which is installed by default on macOS.

To install cocoapods, run this command:

sudo gem install cocoapods

Install TFLite Cocoapod

Use the following command to install TensorFlow Lite and create the .xcworkspace file using cocoapods:

pod install --project-directory=ios/tflite/

Open the project with Xcode

You can open the project in Xcode by running the following command:

open ios/tflite/tflite_camera_example.xcworkspace

Or launch Xcode and click the "Open another Project" button:

Then navigate to the .xcworkspace file (not the .xcproject file):

The app is a simple example that runs an image recognition model on in the iOS simulator. The simulator doesn't support camera input so the app reads from the photos library.

Before inserting our customized model, let's test the baseline version of the app which uses the base "mobilenet" trained on the 1000 ImageNet categories.

Hit the play button, , in the upper right corner of the Xcode window launch the app in the Simulator.

The "Next Photo" button loops through the photos on the device.

The result should look something like this:

The default app setup classifies images into one of the 1000 ImageNet classes, using the standard MobileNet. This is without the retraining we did in part 1.

Now let's modify the app so that it will use our retrained model with our custom image categories.

Add your model files to the project

The demo project is configured to search for a graph.lite, and a labels.txt files in the android/tflite/app/src/main/assets/ directory. Replace those two files with your versions. The following command accomplishes this task:

cp tf_files/optimized_graph.lite ios/tflite/data/graph.lite
cp tf_files/retrained_labels.txt ios/tflite/data/labels.txt

Run your app

Hit the play button, , in the upper right corner of the Xcode window re-launch the app in the Simulator. Drop in some files from the flower_photos/ directory and see how your model does.

It should look something like this:

CC-BY by Felipe VenĂ¢ncio

The default images aren't of flowers. To really try out the model: Either drag-and-drop in some of the training data images you downloaded earlier, or download some images from a Google search.

So now that you have the app running, let's look at the TensorFlow Lite specific code.

TensorFlowLite Pod

This app uses a pre-compiled TFLite Cocoapod. The Podfile includes the cocoapod in the project:


platform :ios, '8.0'

target 'tflite_photos_example'
       pod 'TensorFlowLite'

Using theTFLite iOS API

The code interfacing to the TFLite is all contained in CameraExampleViewController.mm.


The first block of interest, after the necessary imports, is the viewDidLoad method:


#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/contrib/lite/string_util.h"
#include "tensorflow/contrib/lite/tools/mutable_op_resolver.h"


- (void)viewDidLoad {
  [super viewDidLoad];
  labelLayers = [[NSMutableArray alloc] init];

  NSString* graph_path = FilePathForResourceName(model_file_name, model_file_type);
  model = tflite::FlatBufferModel::BuildFromFile([graph_path UTF8String]);
  if (!model) {
    LOG(FATAL) << "Failed to mmap model " << graph_path;
  LOG(INFO) << "Loaded model " << graph_path;
  LOG(INFO) << "resolved reporter";


The key like in this first half of the method is the model = tflite::FlatBufferModel::BuildFromFile([graph_path UTF8String]); line. This creates a FlatBufferModel from the graph file.

A FlatBuffer is a memory mappable data structure. These are a key feature TFLite as they allow the system to better manage the memory used by the model. The system can transparently swap parts of the model in or out of memory as needed.

The second part of the method builds an interpreter for the model, attaching op implementations to the graph data structure we loaded earlier:


- (void)viewDidLoad {

  tflite::ops::builtin::BuiltinOpResolver resolver;
  LoadLabels(labels_file_name, labels_file_type, &labels);

  tflite::InterpreterBuilder(*model, resolver)(&interpreter);
  if (!interpreter) {
    LOG(FATAL) << "Failed to construct interpreter";
  if (interpreter->AllocateTensors() != kTfLiteOk) {
    LOG(FATAL) << "Failed to allocate tensors!";

  [self attachPreviewLayer];

If you're familiar with TensorFlow in python, this is roughly equivalent to building a tf.Session().

Run the model

The UpdatePhoto method handles all the details of fetching the next photo, updating the preview window, and running the model on the photo.


- (void)UpdatePhoto{  
  PHAsset* asset;
  if (photos==nil || photos_index >= photos.count){
    [self updatePhotosLibrary];
  if (photos.count){
    asset = photos[photos_index];
    photos_index += 1;
    input_image = [self convertImageFromAsset:asset
                                   targetSize:CGSizeMake(wanted_input_width, wanted_input_height)
    display_image = [self convertImageFromAsset:asset
    [self DrawImage];
  if (input_image != nil){
    image_data image = [self CGImageToPixels:input_image.CGImage];
    [self inputImageToModel:image];
    [self runModel];

It's the last three lines that we are interested in.

The CGImageToPixels method converts the CGImage returned by the iOS Photos library to a simple structure containing the width, height, channels, and pixel data.


typedef struct {
  int width;
  int height;
  int channels;
  std::vector<uint8_t> data;
} image_data;

The inputImageToModel method handles inserting the image into the interpreter memory. This includes resizing the image and adjusting the pixel values to match what's expected by the model.


- (void)inputImageToModel:(image_data)image{
  float* out = interpreter->typed_input_tensor<float>(0);
  const float input_mean = 127.5f;
  const float input_std = 127.5f;
  assert(image.channels >= wanted_input_channels);
  uint8_t* in = image.data.data();

  for (int y = 0; y < wanted_input_height; ++y) {
    const int in_y = (y * image.height) / wanted_input_height;
    uint8_t* in_row = in + (in_y * image.width * image.channels);
    float* out_row = out + (y * wanted_input_width * wanted_input_channels);
    for (int x = 0; x < wanted_input_width; ++x) {
      const int in_x = (x * image.width) / wanted_input_width;
      uint8_t* in_pixel = in_row + (in_x * image.channels);
      float* out_pixel = out_row + (x * wanted_input_channels);
      for (int c = 0; c < wanted_input_channels; ++c) {
        out_pixel[c] = (in_pixel[c] - input_mean) / input_std;

We know the model only has one input, so the float* out = interpreter->typed_input_tensor<float>(0); line asks the interpreter a pointer to the memory for input 0. The rest of the method handles the pointer arithmetic and pixel scaling to copy the data into that input array.

Finally the runModel method executes the model:


- (void)runModel {
  double startTimestamp = [[NSDate new] timeIntervalSince1970];
  if (interpreter->Invoke() != kTfLiteOk) {
    LOG(FATAL) << "Failed to invoke!";
  double endTimestamp = [[NSDate new] timeIntervalSince1970];
  total_latency += (endTimestamp - startTimestamp);
  total_count += 1;
  NSLog(@"Time: %.4lf, avg: %.4lf, count: %d", endTimestamp - startTimestamp,
        total_latency / total_count,  total_count);



Next runModel reads back the results. To do this it asks the interpreter for a pointer to the output array's data. The output is a simple array of floats. The GetTopN method handles the extraction of the top 5 results (using a priority queue).


- (void)runModel {

  const int output_size = (int)labels.size();
  const int kNumResults = 5;
  const float kThreshold = 0.1f;

  std::vector<std::pair<float, int>> top_results;

  float* output = interpreter->typed_output_tensor<float>(0);
  GetTopN(output, output_size, kNumResults, kThreshold, &top_results);

The next few lines simply convert those top 5 (probability, class_id) pairs into (probability, label) pairs, and then passes off that result, asynchronously, to the setPredictionValues method which updates the on screen report:


- (void)runModel {
  std::vector<std::pair<float, std::string>> newValues;
  for (const auto& result : top_results) {
    std::pair<float, std::string> item;
    item.first = result.first;
    item.second = labels[result.second];
  dispatch_async(dispatch_get_main_queue(), ^(void) {
    [self setPredictionValues:newValues];

Here are some links for more information: