Custom TFLite Library for iOS Developer (English)

1 분 소요

Custom TFLite Library for iOS Developer (한국어)

Intro

: TFLite framework for iOS Developer is distributed in CocoaPod. So once you make the Podfile and specify the following, any developers can use the TFLite framework easily.

# Swift #
use_frameworks!
pod TensorFlowLiteSwift

# Objective-C #
pod TensorFlowLiteObjC

If you wanna make an Application using TFLite, You just need to use CocoaPod. But, How about developing and distributing libraries use TFLite? If you distribute your library via CocoaPod, you can simply specify its dependency with TensorFlowLite(Swift/Objc) in CocoaPod Spec. But. If you distribute your library through other channels, You need to build TensorFlowLite yourself.

Also, TFLite was built to *.framework(instead of *.xcframework). Currently, *.framework isn’t preferable because Apple recommend to use *.xcframework. So, It is good to build to *.xcframework of TFLite yourself.

This post is helpful for iOS Developer that build TFLite itself. This post is written as of TensorFlow 2.4, If you use another version, maybe need to modify some build script.

Universal Library Build

: In TFLite Project, You can find build script of Universal Library for iOS(See this Link). Let’s assume you use x86_64 mac, run build script with specifying arguments.

./build_ios_universal_lib.sh -a "x86_64 arm64"

after build completes, libtensorflow-lite.a will be generated.

Add libtensorflow-lite.a in Xcode Project

Add Header File

: Let’s import libtensorflow-lite.a in Xcode Project.

Hmm… Can we build this project now? Sadly, there are some task yet. libtensorflow-lite.a is C/C++ library, so we have to import header files in Xcode Project.

In this Link, you can find many *.c/cpp and header files of TFLite. We imported libtensorflow-lite.a previously, we just need to import header files. Unfortunately, some header is incompatible for iOS. Which header is incompatible? Seriously, we have to check one by one? Somewhere in the world, the great developer has already classified usable headers. Let’s visit this repository(Link), then download and import header files.

Specify Header Path

: importing header files, you have to specify Header Search Paths in Build Settings. Specify proper path according to your project.

Specify Linker Flag

: For using C/C++ library, specify Other Linker Flags. I specified -lc++.

Conclusion

: Project setting is completed for building TFLite then importing libtensorflow-lite.a. Did you build successfully? The project setting gave me too much trouble. I hope that this post help to save your time.

댓글남기기