First, please note that access to tensorFlow requires scaling a wall.


Step1 (the fastest and most basic experience, official website demo) :

https://www.tensorflow.org/lite/demo_ios

What are the main things that are going on here?

1. Github Repo contains demo projects.

2. Github repo running scripts can generate the required model, put into the demo project.

3. Run pod to get tensorFlow_lite.framework. It’s easy to integrate libraries with POD, fool-proof.

If we don’t want to download the full Github repo, we can get the demo project from someone else, find a suitable.tflite model file, and manually integrate tensorFlow_lite.framework (which contains header files and executable files). You can also run projects.

Project Settings: C++11 support (or later) should be enabled by setting C++ Language Dialect to GNU++11 (or GNU++14), and C++ Standard Library to libc++.

It is also possible to use c++11 in the actual test, and it is not necessary to choose GNU++(another dependency of my own project needs to use c++11, otherwise it will not compile, so I need to study this).

In addition, there is a path-related issue that only needs to be modified in the demo:

//#include "tensorflow/lite/kernels/register.h"//#include "tensorflow/lite/model.h"#include "tensorflow/contrib/lite/kernels/register.h"#include "tensorflow/contrib/lite/model.h"Copy the code

This is caused by the difference between the user’s compiled path and the framework’s path, which is easy to fix. It’s Google’s problem.


Step2 (manually integrate tensorflow_lite.framework) :

You need to manually set Framework Search Paths and Header Search Paths: fill in the corresponding Framework paths so that the project can find them.

Ex. :

Framework Search paths-> ‘${SRCROOT}/tensorflow_lite.framework’

header search paths-> ‘${SRCROOT}/tensorflow_lite.framework/Headers’

In addition, “Undefined symbols “_cblas_sgemm” will be reported when the project is run (Google will find this is caused by the absence of the accelerate library (high performance mathematics library), which can be resolved by adding it).


Step3 (generate.a library by yourself and integrate header files into the project by yourself):

https://www.tensorflow.org/lite/ios

Following the building section of the article above, it should be easy to generate the required. A libraries; But there’s also the problem with header files.

Google suggests we do the following:

The Header Search paths needs to contain:

  • the root folder of tensorflow,
  • tensorflow/lite/downloads
  • tensorflow/lite/downloads/flatbuffers/include

First of all, the path here is inconsistent with the actual path, so it took me a while to find the actual path. The important thing is that this is a simple integration for a stable project and I think it’s unscientific, all we need is the header file.

So I tried the following first:

Wouldn’t it be perfect to use tensorFlow_lite.framework headers here?

Ideal is full, reality is very skinny. Practice shows that for some reason these two don’t match, and when you fix the problem of compile failure, “+[CATransaction Synchronize] called within transaction Loaded model 1resolved reporter”

I think we need to include all the.h files in the lite folder in the.a library, but there are a lot of other files in the Lite folder, and if all of them were added to the project, the result would be a bunch of errors that would not compile.

Therefore, I tried using Python scripts to solve:

#! /usr/bin/python
# -*- coding: utf-8 -*-
import os
def gci(filepath):
Pass through all files under Filepath, including subdirectories
  files = os.listdir(filepath)
  for fi in files:
    fi_d = os.path.join(filepath,fi)            
    if os.path.isdir(fi_d):
      gci(fi_d)                  
    else:
      tempP = os.path.join(filepath,fi_d)
      ifos.path.splitext(tempP)[1]! ='.h':
      	os.remove(tempP)
      	#print tempP

Recursively traverses all files in /root
gci('/Users/zhenweiguan/Desktop/lite')
Copy the code

The purpose of this script is to remove all files except.h.

The project project folder and asset resource folder are included in the project folder. Delete them manually.

Integrate these header files again, introduce path, and bingo is finally running.


Conclusion: For those of you who want to integrate TensorFlowLite as quickly as possible, the best way to do this is to download the latest. Framework file to manually integrate tensorFlowLite (of course, it will be easier if the project supports POD).

For the project have any questions or need anything, you can contact me: www.github.com/dustturtle.

[email protected]