Build TensorFlowLite Swift Custom Framework

Viewed 1118

I need to build TensorFlowLite Swift Framework/cocoapod from the sources and then use it instead of the original framework in one of the Swift projects.

Downloaded the code and followed the instructions in few scenarios but without success:

1) https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/experimental/swift

a) python configure.py

b) CocoaPods developers isn't applicable as it doesn't take the source from the local TensofFlow folder (or I am mistaken?)

c) Bazel developers I do: bazel build tensorflow/lite/experimental/swift:TensorFlowLite

bazel test tensorflow/lite/experimental/swift:Tests --swiftcopt=-enable-testing

and eventually

generate_xcodeproj.sh --genconfig tensorflow/lite/experimental/swift/TensorFlowLite.tulsiproj:TensorFlowLite --outputfolder ~/path/to/generated/TensorFlowLite.xcodeproj

as a result I get a project with a libtensorflow-lite-experimental-swift-TensorFlowLite.a and few idx files. It compiles and I see the .a file (but both the app and tests don't compile for various reasons) and I don't quite understand how I can use that .a lib with Swift files as there are no module/headers etc. files.

After all these exercises there is TensorFlowLiteSwift.podspec in the root of TensorFlow and in the experimental/Swift folders. It still depends on the TensorFlowLiteC and if I refer to this podspec in another project via path param I can do import TensorFlowLiteC but not import TensorFlowLiteSwift / import TensorFlowLite and I believe the TensorFlowLiteC is still taken from the Internet, not locally.

2) Custom framework path from here: https://firebase.google.com/docs/ml-kit/ios/use-custom-tflite

Here are few problems:

a) if I try to just use a project with tensorflow_lite.framework added internally then I can't reference TensorFlowLite / TensorFlowLiteC etc. in the Swift file. b) if I try to follow a route of creating the local pod and reference the pod project then pod install doesn't work because mandatory parameters are not set (used provided example).

I expect to get a Xcode project or workspace which is built from the local TensorFlowFiles and which I can reference from other projects like 'import TensorFlowLiteSwift' or 'import TensorFlowLite'

The problem is that I don't understand even what's the correct route to make it happen.

Thanks.

1 Answers

All right, I figured out the answer here:

  1. build TensorFlowLiteC framework from the root tensorflow folder:

     bazel build --config=ios_fat -c opt //tensorflow/lite/experimental/ios:TensorFlowLiteC_framework
    
  2. The result can be found here: bazel-bin/tensorflow/lite/experimental/ios/TensorFlowLiteC_framework.zip
  3. Unzip the file and add the contents to the new XCode project (in my case it was Swift Single Window App). If you do it via drag'n'drop then XCode will automatically change the Frameworks, libraries and embedded content section, also will modify the Framework paths
  4. Change this setting Build Settings\Linking\Other Linker Flags to -lc++
  5. Done. You can do

     import TensorFlowLiteC
    
  6. and use the source files from the swift folder.
Related