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 /** 64-bit double precision floating point. */ 54 TFLTensorDataTypeFloat64, 55 }; 56 57 /** 58 * An input or output tensor in a TensorFlow Lite model. 59 * 60 * @warning Each `TFLTensor` instance is associated with a `TFLInterpreter` instance. Multiple 61 * `TFLTensor` instances of the same TensorFlow Lite model are associated with the same 62 * `TFLInterpreter` instance. As long as a `TFLTensor` instance is still in use, its associated 63 * `TFLInterpreter` instance will not be deallocated. 64 */ 65 @interface TFLTensor : NSObject 66 67 /** Name of the tensor. */ 68 @property(nonatomic, readonly, copy) NSString *name; 69 70 /** Data type of the tensor. */ 71 @property(nonatomic, readonly) TFLTensorDataType dataType; 72 73 /** Parameters for asymmetric quantization. `nil` if the tensor does not use quantization. */ 74 @property(nonatomic, readonly, nullable) TFLQuantizationParameters *quantizationParameters; 75 76 /** Unavailable. */ 77 - (instancetype)init NS_UNAVAILABLE; 78 79 /** 80 * Copies the given data into an input tensor. This is allowed only for an input tensor and only 81 * before the interpreter is invoked; otherwise an error will be returned. 82 * 83 * @param data The data to set. The byte size of the data must match what's required by the input 84 * tensor. 85 * @param error An optional error parameter populated when there is an error in copying the data. 86 * 87 * @return Whether the data was copied into the input tensor successfully. Returns NO if an error 88 * occurred. 89 */ 90 - (BOOL)copyData:(NSData *)data error:(NSError **)error; 91 92 /** 93 * Retrieves a copy of data in the tensor. For an output tensor, the data is only available after 94 * the interpreter invocation has successfully completed; otherwise an error will be returned. 95 * 96 * @param error An optional error parameter populated when there is an error in retrieving the data. 97 * 98 * @return A copy of data in the tensor. `nil` if there is an error in retrieving the data or the 99 * data is not available. 100 */ 101 - (nullable NSData *)dataWithError:(NSError **)error; 102 103 /** 104 * Retrieves the shape of the tensor, an array of positive unsigned integers containing the size 105 * of each dimension. For example: the shape of [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]] is 106 * [2, 2, 3] (i.e. an array of 2 arrays of 2 arrays of 3 numbers). 107 * 108 * @param error An optional error parameter populated when there is an error in retrieving the 109 * shape. 110 * 111 * @return The shape of the tensor. `nil` if there is an error in retrieving the shape. 112 */ 113 - (nullable NSArray<NSNumber *> *)shapeWithError:(NSError **)error; 114 115 @end 116 117 NS_ASSUME_NONNULL_END 118