A, integration,

Integration with POD is directly below

pod 'TensorFlowLiteObjC'

Ii. Introduction to use

#import <TFLTensorFlowLite.h>

// Get the model path

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"model" ofType:@"tflite"];

NSError *error;

/ / model initialization interpreter TFLInterpreter * interpreter = [[TFLInterpreter alloc] initWithModelPath: modelPath error: & error];

if (error ! = nil) { /* Error handling... * /}

// Allocate memory for the model’s input “TFLTensor”.

[interpreter allocateTensorsWithError:&error];

if (error ! = nil) { /* Error handling... * /}

NSMutableData *inputData; // Should be initialized

// Prepare the input data…

// Copy input data to the input TFLTensor.

[interpreter copyData:inputData toInputTensorAtIndex:0 error:&error];

if (error ! = nil) { /* Error handling... * /}

// Run inference by invoking the TFLInterpreter.

// Run the inference by calling “TFLInterpreter”

[interpreter invokeWithError:&error];

if (error ! = nil) { /* Error handling... * /}

// Get the output TFLTensor

// Get the TFLTensor of the output

TFLTensor *outputTensor = [interpreter outputTensorAtIndex:0 error:&error];

if (error ! = nil) { /* Error handling... * /}

// Copy the output to “NSData” to process the inferred results

NSData *outputData = [outputTensor dataWithError:&amp;error];

if (error ! = nil) { /* Error handling... * /}

3. Application in practical projects

Create the TFLInterpreter instance object

TFLInterpreterOptions *options = [[TFLInterpreterOptions alloc] init];

   

        self.interpreter = [[TFLInterpreter alloc] initWithModelPath:filePath options:options error:nil];

        NSError *allocError;

        BOOL isSuccess = [self.interpreter allocateTensorsWithError:&allocError];
Copy the code

2. Use TFLTensor to predict the input data and output the predicted results

 TFLTensor *inputTensor = [self.interpreter inputTensorAtIndex:0 error:&error];

       [inputTensor copyData:currentData error:&error];

       [self.interpreter invokeWithError:&error];

      TFLTensor *outputTensor = [self.interpreter outputTensorAtIndex:0 error:&error];

       NSData *outputData = [outputTensor dataWithError:&error];
Copy the code