• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #import <Foundation/Foundation.h>
16 
17 @class TFLQuantizationParameters;
18 
19 NS_ASSUME_NONNULL_BEGIN
20 
21 /**
22  * @enum TFLTensorDataType
23  * This enum specifies supported TensorFlow Lite tensor data types.
24  */
25 typedef NS_ENUM(NSUInteger, TFLTensorDataType) {
26   /** Tensor data type not available. This indicates an error with the model. */
27   TFLTensorDataTypeNoType,
28 
29   /** 32-bit single precision floating point. */
30   TFLTensorDataTypeFloat32,
31 
32   /** 16-bit half precision floating point. */
33   TFLTensorDataTypeFloat16,
34 
35   /** 32-bit signed integer. */
36   TFLTensorDataTypeInt32,
37 
38   /** 8-bit unsigned integer. */
39   TFLTensorDataTypeUInt8,
40 
41   /** 64-bit signed integer. */
42   TFLTensorDataTypeInt64,
43 
44   /** Boolean. */
45   TFLTensorDataTypeBool,
46 
47   /** 16-bit signed integer. */
48   TFLTensorDataTypeInt16,
49 
50   /** 8-bit signed integer. */
51   TFLTensorDataTypeInt8,
52 };
53 
54 /**
55  * An input or output tensor in a TensorFlow Lite model.
56  *
57  * @warning Each `TFLTensor` instance is associated with a `TFLInterpreter` instance. Multiple
58  *     `TFLTensor` instances of the same TensorFlow Lite model are associated with the same
59  *     `TFLInterpreter` instance. As long as a `TFLTensor` instance is still in use, its associated
60  *     `TFLInterpreter` instance will not be deallocated.
61  */
62 @interface TFLTensor : NSObject
63 
64 /** Name of the tensor. */
65 @property(nonatomic, readonly, copy) NSString *name;
66 
67 /** Data type of the tensor. */
68 @property(nonatomic, readonly) TFLTensorDataType dataType;
69 
70 /** Parameters for asymmetric quantization. `nil` if the tensor does not use quantization. */
71 @property(nonatomic, readonly, nullable) TFLQuantizationParameters *quantizationParameters;
72 
73 /** Unavailable. */
74 - (instancetype)init NS_UNAVAILABLE;
75 
76 /**
77  * Copies the given data into an input tensor. This is allowed only for an input tensor and only
78  * before the interpreter is invoked; otherwise an error will be returned.
79  *
80  * @param data The data to set. The byte size of the data must match what's required by the input
81  *     tensor.
82  * @param error An optional error parameter populated when there is an error in copying the data.
83  *
84  * @return Whether the data was copied into the input tensor successfully. Returns NO if an error
85  *     occurred.
86  */
87 - (BOOL)copyData:(NSData *)data error:(NSError **)error;
88 
89 /**
90  * Retrieves a copy of data in the tensor. For an output tensor, the data is only available after
91  * the interpreter invocation has successfully completed; otherwise an error will be returned.
92  *
93  * @param error An optional error parameter populated when there is an error in retrieving the data.
94  *
95  * @return A copy of data in the tensor. `nil` if there is an error in retrieving the data or the
96  *     data is not available.
97  */
98 - (nullable NSData *)dataWithError:(NSError **)error;
99 
100 /**
101  * Retrieves the shape of the tensor, an array of positive unsigned integers containing the size
102  * of each dimension. For example: the shape of [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] is
103  * [2, 2, 3] (i.e. an array of 2 arrays of 2 arrays of 3 numbers).
104  *
105  * @param error An optional error parameter populated when there is an error in retrieving the
106  *     shape.
107  *
108  * @return The shape of the tensor. `nil` if there is an error in retrieving the shape.
109  */
110 - (nullable NSArray<NSNumber *> *)shapeWithError:(NSError **)error;
111 
112 @end
113 
114 NS_ASSUME_NONNULL_END
115