1. TensorFlow Lite sample applications
下载 tensorflow example代码:
git clone https://github.com/tensorflow/examples
examples/lite
目录下是TensorFlow Lite sample applications。每个示例程序都用Android 和 iOS开发,介绍了Tesnsorflow Lite如何应用到移动开发应用程序上。
详细参考examples/lite/README.txt
。
例如:
lite/examples/image_classification/ios
是一图片分类程序在iOS开发中的应用,Xcode打开既可以运行。
参考lite/examples/image_classification/ios/EXPLORE_THE_CODE.md
,有详细介绍。
额外使用tensorflow-lite使用开发应用程序,参考官网https://tensorflow.google.cn/lite/examples?hl=zh_cn
2. 将TensorFlow Lite加入到Swift or Objective-C工程
TensorFlow Lite官方提供原生iOS库,用Swift和Objective-C开发。以图像分类实例程序为起点开发你的应用程序:https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/ios
下面介绍怎么把TensorFlow Lite加入到你的swift或者是objective-c工程中。
2-1. CocoaPods导入
在Podfile文件中加入TensorFlow Lite引用,运行pod install
安装。
Swift:
use_frameworks!
pod 'TensorFlowLiteSwift'
Objective-C:
pod 'TensorFlowLiteObjC'
如果基于Objective-C开发,TensorFlow Lite引用, 在Podfile中必须包含use_frameworks。
上述默认引用最新的稳定发行版。如果进行引用指定版本,如下引用2.0.0版本。
pod 'TensorFlowLiteSwift', '~> 2.0.0'
如果引用每晚发型的版本:
pod 'TensorFlowLiteSwift', '0.0.1-nightly'
第一次运行pod install
将会创建Podfile.lock文件,这将会锁定nightly library version 到今天版本。运行pod update
命令进行版本更新。
版本号如何填写,参考 Specifying pod versions。
2-2. Bazel开发
在BUILD文件中, 加入TensorFlowLite依赖。
Swift:
swift_library(
deps = [
"//tensorflow/lite/experimental/swift:TensorFlowLite",
],
)
Objective-C:
objc_library(
deps = [
"//tensorflow/lite/experimental/objc:TensorFlowLite",
],
)
2-3. 导入TensorFlow Lite
Swift源代码中,导入TensorFlow Lite模块:
import TensorFlowLite
Objective-C源文件, 导入头文件:
#import "TFLTensorFlowLite.h"
在Xoce工程中, 设置模块CLANG_ENABLE_MODULES = YES:
@import TFLTensorFlowLite;
Note: For CocoaPods developers who want to import the Objective-C TensorFlow Lite module, you must also include use_frameworks! in your Podfile.
如何编译iOS TensorFlow Lite,请参考https://blog.csdn.net/mandagod/article/details/104354961