लेगसी कस्टम मॉडल एपीआई से माइग्रेट करना

Firebase/MLModelInterpreter लाइब्रेरी के वर्शन 0.20.0 में, एक नई सुविधा जोड़ी गई है getLatestModelFilePath() तरीका, जिससे डिवाइस की जगह की जानकारी का पता चलता है कस्टम मॉडल की ज़रूरत होती है. TensorFlow Lite को सीधे तौर पर इंस्टैंशिएट करने के लिए, इस तरीके का इस्तेमाल किया जा सकता है Firebase के ModelInterpreter के बजाय, Interpreter ऑब्जेक्ट का इस्तेमाल किया जा सकता है रैपर.

आगे से, यही तरीका इस्तेमाल किया जाएगा. क्योंकि TensorFlow Lite अनुवादक वर्शन अब Firebase लाइब्रेरी वर्शन के साथ काम नहीं करता है, तो TensorFlow Lite के नए वर्शन पर अपग्रेड करने के लिए, या आसानी से कस्टम TensorFlow Lite बिल्ड इस्तेमाल करना चाहते हैं.

यह पेज बताता है कि आप ModelInterpreter का इस्तेमाल करके TensorFlow Lite Interpreter.

1. प्रोजेक्ट डिपेंडेंसी अपडेट करें

अपने प्रोजेक्ट की Podfile को अपडेट करें, ताकि Firebase/MLModelInterpreter लाइब्रेरी या उसके बाद की लाइब्रेरी और TensorFlow Lite लाइब्रेरी:

इससे पहले

Swift

pod 'Firebase/MLModelInterpreter', '0.19.0'

Objective-C

pod 'Firebase/MLModelInterpreter', '0.19.0'

इसके बाद

Swift

pod 'Firebase/MLModelInterpreter', '~> 0.20.0'
pod 'TensorFlowLiteSwift'

Objective-C

pod 'Firebase/MLModelInterpreter', '~> 0.20.0'
pod 'TensorFlowLiteObjC'

2. Firebase Model interpreter के बजाय, TensorFlow Lite का अनुवाद करने वाला टूल बनाएं

Firebase ModelInterpreter बनाने के बजाय, मॉडल की जगह की जानकारी पाएं getLatestModelFilePath() वाला डिवाइस और इसका इस्तेमाल TensorFlow Lite बनाने के लिए करें Interpreter.

इससे पहले

Swift

let remoteModel = CustomRemoteModel(
    name: "your_remote_model"  // The name you assigned in the Firebase console.
)
interpreter = ModelInterpreter.modelInterpreter(remoteModel: remoteModel)

Objective-C

// Initialize using the name you assigned in the Firebase console.
FIRCustomRemoteModel *remoteModel =
        [[FIRCustomRemoteModel alloc] initWithName:@"your_remote_model"];
interpreter = [FIRModelInterpreter modelInterpreterForRemoteModel:remoteModel];

इसके बाद

Swift

let remoteModel = CustomRemoteModel(
    name: "your_remote_model"  // The name you assigned in the Firebase console.
)
ModelManager.modelManager().getLatestModelFilePath(remoteModel) { (remoteModelPath, error) in
    guard error == nil, let remoteModelPath = remoteModelPath else { return }
    do {
        interpreter = try Interpreter(modelPath: remoteModelPath)
    } catch {
        // Error?
    }
}

Objective-C

FIRCustomRemoteModel *remoteModel =
        [[FIRCustomRemoteModel alloc] initWithName:@"your_remote_model"];
[[FIRModelManager modelManager] getLatestModelFilePath:remoteModel
                                            completion:^(NSString * _Nullable filePath,
                                                         NSError * _Nullable error) {
    if (error != nil || filePath == nil) { return; }

    NSError *tfError = nil;
    interpreter = [[TFLInterpreter alloc] initWithModelPath:filePath error:&tfError];
}];

3. इनपुट और आउटपुट तैयारी कोड अपडेट करें

ModelInterpreter की मदद से, मॉडल के इनपुट और आउटपुट के आकार तय किए जाते हैं दौड़ते समय अनुवादक को ModelInputOutputOptions ऑब्जेक्ट पास करें इसे.

TensorFlow Lite के अनुवादक के लिए, इसके बजाय आप allocateTensors() को इस नंबर पर कॉल करें: यह मॉडल के इनपुट और आउटपुट के लिए स्पेस देता है. इसके बाद, अपने इनपुट डेटा को इनपुट टेंसर.

उदाहरण के लिए, अगर आपके मॉडल का इनपुट साइज़ [1 224 224 3] float वैल्यू है और [1 1000] float वैल्यू के आउटपुट शेप में, ये बदलाव करें:

इससे पहले

Swift

let ioOptions = ModelInputOutputOptions()
do {
    try ioOptions.setInputFormat(
        index: 0,
        type: .float32,
        dimensions: [1, 224, 224, 3]
    )
    try ioOptions.setOutputFormat(
        index: 0,
        type: .float32,
        dimensions: [1, 1000]
    )
} catch let error as NSError {
    print("Failed to set input or output format with error: \(error.localizedDescription)")
}

let inputs = ModelInputs()
do {
    let inputData = Data()
    // Then populate with input data.

    try inputs.addInput(inputData)
} catch let error {
    print("Failed to add input: \(error)")
}

interpreter.run(inputs: inputs, options: ioOptions) { outputs, error in
    guard error == nil, let outputs = outputs else { return }
    // Process outputs
    // ...
}

Objective-C

FIRModelInputOutputOptions *ioOptions = [[FIRModelInputOutputOptions alloc] init];
NSError *error;
[ioOptions setInputFormatForIndex:0
                             type:FIRModelElementTypeFloat32
                       dimensions:@[@1, @224, @224, @3]
                            error:&error];
if (error != nil) { return; }
[ioOptions setOutputFormatForIndex:0
                              type:FIRModelElementTypeFloat32
                        dimensions:@[@1, @1000]
                             error:&error];
if (error != nil) { return; }

FIRModelInputs *inputs = [[FIRModelInputs alloc] init];
NSMutableData *inputData = [[NSMutableData alloc] initWithCapacity:0];
// Then populate with input data.

[inputs addInput:inputData error:&error];
if (error != nil) { return; }

[interpreter runWithInputs:inputs
                   options:ioOptions
                completion:^(FIRModelOutputs * _Nullable outputs,
                             NSError * _Nullable error) {
  if (error != nil || outputs == nil) {
    return;
  }
  // Process outputs
  // ...
}];

इसके बाद

Swift

do {
    try interpreter.allocateTensors()

    let inputData = Data()
    // Then populate with input data.

    try interpreter.copy(inputData, toInputAt: 0)

    try interpreter.invoke()
} catch let err {
    print(err.localizedDescription)
}

Objective-C

NSError *error = nil;

[interpreter allocateTensorsWithError:&error];
if (error != nil) { return; }

TFLTensor *input = [interpreter inputTensorAtIndex:0 error:&error];
if (error != nil) { return; }

NSMutableData *inputData = [[NSMutableData alloc] initWithCapacity:0];
// Then populate with input data.

[input copyData:inputData error:&error];
if (error != nil) { return; }

[interpreter invokeWithError:&error];
if (error != nil) { return; }

4. आउटपुट हैंडलिंग कोड अपडेट करें

आखिर में, ModelOutputs ऑब्जेक्ट के साथ मॉडल का आउटपुट पाने के बजाय output() तरीका है, तो अनुवादक से आउटपुट टेंसर पाएं और इसे डेटा को किसी भी स्ट्रक्चर में बदल दें.

उदाहरण के लिए, यदि आप वर्गीकरण कर रहे हैं, तो आप फ़ॉलो किया जा रहा है:

इससे पहले

Swift

let output = try? outputs.output(index: 0) as? [[NSNumber]]
let probabilities = output?[0]

guard let labelPath = Bundle.main.path(
    forResource: "custom_labels",
    ofType: "txt"
) else { return }
let fileContents = try? String(contentsOfFile: labelPath)
guard let labels = fileContents?.components(separatedBy: "\n") else { return }

for i in 0 ..< labels.count {
    if let probability = probabilities?[i] {
        print("\(labels[i]): \(probability)")
    }
}

Objective-C

// Get first and only output of inference with a batch size of 1
NSError *error;
NSArray *probabilites = [outputs outputAtIndex:0 error:&error][0];
if (error != nil) { return; }

NSString *labelPath = [NSBundle.mainBundle pathForResource:@"retrained_labels"
                                                    ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:labelPath
                                                   encoding:NSUTF8StringEncoding
                                                      error:&error];
if (error != nil || fileContents == NULL) { return; }
NSArray<NSString *> *labels = [fileContents componentsSeparatedByString:@"\n"];
for (int i = 0; i < labels.count; i++) {
    NSString *label = labels[i];
    NSNumber *probability = probabilites[i];
    NSLog(@"%@: %f", label, probability.floatValue);
}

इसके बाद

Swift

do {
    // After calling interpreter.invoke():
    let output = try interpreter.output(at: 0)
    let probabilities =
            UnsafeMutableBufferPointer<Float32>.allocate(capacity: 1000)
    output.data.copyBytes(to: probabilities)

    guard let labelPath = Bundle.main.path(
        forResource: "custom_labels",
        ofType: "txt"
    ) else { return }
    let fileContents = try? String(contentsOfFile: labelPath)
    guard let labels = fileContents?.components(separatedBy: "\n") else { return }

    for i in labels.indices {
        print("\(labels[i]): \(probabilities[i])")
    }
} catch let err {
    print(err.localizedDescription)
}

Objective-C

NSError *error = nil;

TFLTensor *output = [interpreter outputTensorAtIndex:0 error:&error];
if (error != nil) { return; }

NSData *outputData = [output dataWithError:&error];
if (error != nil) { return; }

Float32 probabilities[outputData.length / 4];
[outputData getBytes:&probabilities length:outputData.length];

NSString *labelPath = [NSBundle.mainBundle pathForResource:@"custom_labels"
                                                    ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:labelPath
                                                   encoding:NSUTF8StringEncoding
                                                      error:&error];
if (error != nil || fileContents == nil) { return; }

NSArray<NSString *> *labels = [fileContents componentsSeparatedByString:@"\n"];
for (int i = 0; i < labels.count; i++) {
    NSLog(@"%@: %f", labels[i], probabilities[i]);
}