xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/g3doc/guide/ios.md (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# iOS quickstart
2
3To get started with TensorFlow Lite on iOS, we recommend exploring the following
4example:
5
6<a class="button button-primary" href="https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/ios">iOS
7image classification example</a>
8
9For an explanation of the source code, you should also read
10[TensorFlow Lite iOS image classification](https://github.com/tensorflow/examples/blob/master/lite/examples/image_classification/ios/EXPLORE_THE_CODE.md).
11
12This example app uses
13[image classification](https://www.tensorflow.org/lite/examples/image_classification/overview)
14to continuously classify whatever it sees from the device's rear-facing camera,
15displaying the top most probable classifications. It allows the user to choose
16between a floating point or
17[quantized](https://www.tensorflow.org/lite/performance/post_training_quantization)
18model and select the number of threads to perform inference on.
19
20Note: Additional iOS applications demonstrating TensorFlow Lite in a variety of
21use cases are available in [Examples](https://www.tensorflow.org/lite/examples).
22
23## Add TensorFlow Lite to your Swift or Objective-C project
24
25TensorFlow Lite offers native iOS libraries written in
26[Swift](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/swift)
27and
28[Objective-C](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/objc).
29Start writing your own iOS code using the
30[Swift image classification example](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/ios)
31as a starting point.
32
33The sections below demonstrate how to add TensorFlow Lite Swift or Objective-C
34to your project:
35
36### CocoaPods developers
37
38In your `Podfile`, add the TensorFlow Lite pod. Then, run `pod install`.
39
40#### Swift
41
42```ruby
43use_frameworks!
44pod 'TensorFlowLiteSwift'
45```
46
47#### Objective-C
48
49```ruby
50pod 'TensorFlowLiteObjC'
51```
52
53#### Specifying versions
54
55There are stable releases, and nightly releases available for both
56`TensorFlowLiteSwift` and `TensorFlowLiteObjC` pods. If you do not specify a
57version constraint as in the above examples, CocoaPods will pull the latest
58stable release by default.
59
60You can also specify a version constraint. For example, if you wish to depend on
61version 2.0.0, you can write the dependency as:
62
63```ruby
64pod 'TensorFlowLiteSwift', '~> 2.0.0'
65```
66
67This will ensure the latest available 2.x.y version of the `TensorFlowLiteSwift`
68pod is used in your app. Alternatively, if you want to depend on the nightly
69builds, you can write:
70
71```ruby
72pod 'TensorFlowLiteSwift', '~> 0.0.1-nightly'
73```
74
75From 2.4.0 version and latest nightly releases, by default
76[GPU](https://www.tensorflow.org/lite/performance/gpu) and
77[Core ML delegates](https://www.tensorflow.org/lite/performance/coreml_delegate)
78are excluded from the pod to reduce the binary size. You can include them by
79specifying subspec:
80
81```ruby
82pod 'TensorFlowLiteSwift', '~> 0.0.1-nightly', :subspecs => ['CoreML', 'Metal']
83```
84
85This will allow you to use the latest features added to TensorFlow Lite. Note
86that once the `Podfile.lock` file is created when you run `pod install` command
87for the first time, the nightly library version will be locked at the current
88date's version. If you wish to update the nightly library to the newer one, you
89should run `pod update` command.
90
91For more information on different ways of specifying version constraints, see
92[Specifying pod versions](https://guides.cocoapods.org/using/the-podfile.html#specifying-pod-versions).
93
94### Bazel developers
95
96In your `BUILD` file, add the `TensorFlowLite` dependency to your target.
97
98#### Swift
99
100```python
101swift_library(
102  deps = [
103      "//tensorflow/lite/swift:TensorFlowLite",
104  ],
105)
106```
107
108#### Objective-C
109
110```python
111objc_library(
112  deps = [
113      "//tensorflow/lite/objc:TensorFlowLite",
114  ],
115)
116```
117
118#### C/C++ API
119
120Alternatively, you can use
121[C API](https://www.tensorflow.org/code/tensorflow/lite/c/c_api.h)
122or [C++ API](https://tensorflow.org/lite/api_docs/cc)
123
124```python
125# Using C API directly
126objc_library(
127  deps = [
128      "//tensorflow/lite/c:c_api",
129  ],
130)
131
132# Using C++ API directly
133objc_library(
134  deps = [
135      "//third_party/tensorflow/lite:framework",
136  ],
137)
138```
139
140### Import the library
141
142For Swift files, import the TensorFlow Lite module:
143
144```swift
145import TensorFlowLite
146```
147
148For Objective-C files, import the umbrella header:
149
150```objectivec
151#import "TFLTensorFlowLite.h"
152```
153
154Or, the module if you set `CLANG_ENABLE_MODULES = YES` in your Xcode project:
155
156```objectivec
157@import TFLTensorFlowLite;
158```
159
160Note: For CocoaPods developers who want to import the Objective-C TensorFlow
161Lite module, you must also include `use_frameworks!` in your `Podfile`.
162