Apple प्लेटफ़ॉर्म पर कस्टम TensorFlow Lite मॉडल का उपयोग करें

यदि आपका ऐप कस्टम TensorFlow Lite मॉडल का उपयोग करता है, तो आप अपने मॉडल को तैनात करने के लिए Firebase ML का उपयोग कर सकते हैं। फायरबेस के साथ मॉडल तैनात करके, आप अपने ऐप के शुरुआती डाउनलोड आकार को कम कर सकते हैं और अपने ऐप का नया संस्करण जारी किए बिना अपने ऐप के एमएल मॉडल को अपडेट कर सकते हैं। और, रिमोट कॉन्फिग और ए/बी टेस्टिंग के साथ, आप उपयोगकर्ताओं के विभिन्न समूहों को गतिशील रूप से विभिन्न मॉडल पेश कर सकते हैं।

आवश्यक शर्तें

  • MLModelDownloader लाइब्रेरी केवल स्विफ्ट के लिए उपलब्ध है।
  • TensorFlow Lite केवल iOS 9 और नए संस्करण का उपयोग करने वाले उपकरणों पर चलता है।

टेन्सरफ्लो लाइट मॉडल

TensorFlow Lite मॉडल ML मॉडल हैं जिन्हें मोबाइल उपकरणों पर चलने के लिए अनुकूलित किया गया है। TensorFlow Lite मॉडल प्राप्त करने के लिए:

शुरू करने से पहले

फायरबेस के साथ TensorFlowLite का उपयोग करने के लिए, आपको CocoaPods का उपयोग करना होगा क्योंकि TensorFlowLite वर्तमान में स्विफ्ट पैकेज मैनेजर के साथ इंस्टॉलेशन का समर्थन नहीं करता है। MLModelDownloader इंस्टॉल करने के निर्देशों के लिए CocoaPods इंस्टॉलेशन गाइड देखें।

एक बार इंस्टॉल हो जाने पर, उनका उपयोग करने के लिए Firebase और TensorFlowLite को आयात करें।

तीव्र

import FirebaseMLModelDownloader
import TensorFlowLite

1. अपना मॉडल तैनात करें

फायरबेस कंसोल या फायरबेस एडमिन पायथन और नोड.जेएस एसडीके का उपयोग करके अपने कस्टम टेन्सरफ्लो मॉडल को तैनात करें। कस्टम मॉडल तैनात और प्रबंधित करें देखें।

अपने फायरबेस प्रोजेक्ट में एक कस्टम मॉडल जोड़ने के बाद, आप अपने निर्दिष्ट नाम का उपयोग करके अपने ऐप्स में मॉडल का संदर्भ दे सकते हैं। किसी भी समय, आप एक नया TensorFlow Lite मॉडल तैनात कर सकते हैं और getModel() (नीचे देखें) पर कॉल करके उपयोगकर्ताओं के डिवाइस पर नया मॉडल डाउनलोड कर सकते हैं।

2. डिवाइस पर मॉडल डाउनलोड करें और TensorFlow Lite दुभाषिया आरंभ करें

अपने ऐप में अपने TensorFlow Lite मॉडल का उपयोग करने के लिए, पहले डिवाइस पर मॉडल का नवीनतम संस्करण डाउनलोड करने के लिए Firebase ML SDK का उपयोग करें।

मॉडल डाउनलोड शुरू करने के लिए, मॉडल डाउनलोडर की getModel() विधि को कॉल करें, जिसमें मॉडल को अपलोड करते समय आपने जो नाम निर्दिष्ट किया था, उसे निर्दिष्ट करें, क्या आप हमेशा नवीनतम मॉडल डाउनलोड करना चाहते हैं, और वे शर्तें जिनके तहत आप डाउनलोड करने की अनुमति देना चाहते हैं।

आप तीन डाउनलोड व्यवहारों में से चुन सकते हैं:

डाउनलोड प्रकार विवरण
localModel डिवाइस से स्थानीय मॉडल प्राप्त करें. यदि कोई स्थानीय मॉडल उपलब्ध नहीं है, तो यह latestModel की तरह व्यवहार करता है। यदि आप मॉडल अपडेट की जाँच में रुचि नहीं रखते हैं तो इस डाउनलोड प्रकार का उपयोग करें। उदाहरण के लिए, आप मॉडल नाम पुनर्प्राप्त करने के लिए रिमोट कॉन्फ़िगरेशन का उपयोग कर रहे हैं और आप हमेशा नए नामों (अनुशंसित) के तहत मॉडल अपलोड करते हैं।
localModelUpdateInBackground डिवाइस से स्थानीय मॉडल प्राप्त करें और पृष्ठभूमि में मॉडल को अपडेट करना प्रारंभ करें। यदि कोई स्थानीय मॉडल उपलब्ध नहीं है, तो यह latestModel की तरह व्यवहार करता है।
latestModel नवीनतम मॉडल प्राप्त करें. यदि स्थानीय मॉडल नवीनतम संस्करण है, तो स्थानीय मॉडल लौटाता है। अन्यथा, नवीनतम मॉडल डाउनलोड करें. नवीनतम संस्करण डाउनलोड होने तक यह व्यवहार अवरुद्ध रहेगा (अनुशंसित नहीं)। इस व्यवहार का उपयोग केवल उन मामलों में करें जहां आपको स्पष्ट रूप से नवीनतम संस्करण की आवश्यकता है।

आपको मॉडल-संबंधित कार्यक्षमता को अक्षम करना चाहिए - उदाहरण के लिए, अपने यूआई के हिस्से को ग्रे-आउट करना या छिपाना - जब तक आप पुष्टि नहीं करते कि मॉडल डाउनलोड हो गया है।

तीव्र

let conditions = ModelDownloadConditions(allowsCellularAccess: false)
ModelDownloader.modelDownloader()
    .getModel(name: "your_model",
              downloadType: .localModelUpdateInBackground,
              conditions: conditions) { result in
        switch (result) {
        case .success(let customModel):
            do {
                // Download complete. Depending on your app, you could enable the ML
                // feature, or switch from the local model to the remote model, etc.

                // The CustomModel object contains the local path of the model file,
                // which you can use to instantiate a TensorFlow Lite interpreter.
                let interpreter = try Interpreter(modelPath: customModel.path)
            } catch {
                // Error. Bad model file?
            }
        case .failure(let error):
            // Download was unsuccessful. Don't enable ML features.
            print(error)
        }
}

कई ऐप्स अपने इनिशियलाइज़ेशन कोड में डाउनलोड कार्य शुरू करते हैं, लेकिन आप मॉडल का उपयोग करने से पहले किसी भी समय ऐसा कर सकते हैं।

3. इनपुट डेटा पर अनुमान लगाएं

अपने मॉडल के इनपुट और आउटपुट आकार प्राप्त करें

TensorFlow Lite मॉडल दुभाषिया इनपुट के रूप में लेता है और आउटपुट के रूप में एक या अधिक बहुआयामी सरणियाँ उत्पन्न करता है। इन सरणियों में या तो byte , int , long या float मान होते हैं। इससे पहले कि आप किसी मॉडल में डेटा पास कर सकें या उसके परिणाम का उपयोग कर सकें, आपको अपने मॉडल द्वारा उपयोग किए जाने वाले सरणियों की संख्या और आयाम ("आकार") पता होना चाहिए।

यदि आपने स्वयं मॉडल बनाया है, या यदि मॉडल का इनपुट और आउटपुट प्रारूप प्रलेखित है, तो आपके पास यह जानकारी पहले से ही हो सकती है। यदि आप अपने मॉडल के इनपुट और आउटपुट के आकार और डेटा प्रकार को नहीं जानते हैं, तो आप अपने मॉडल का निरीक्षण करने के लिए TensorFlow Lite दुभाषिया का उपयोग कर सकते हैं। उदाहरण के लिए:

अजगर

import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path="your_model.tflite")
interpreter.allocate_tensors()

# Print input shape and type
inputs = interpreter.get_input_details()
print('{} input(s):'.format(len(inputs)))
for i in range(0, len(inputs)):
    print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype']))

# Print output shape and type
outputs = interpreter.get_output_details()
print('\n{} output(s):'.format(len(outputs)))
for i in range(0, len(outputs)):
    print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))

उदाहरण आउटपुट:

1 input(s):
[  1 224 224   3] <class 'numpy.float32'>

1 output(s):
[1 1000] <class 'numpy.float32'>

दुभाषिया चलाएँ

अपने मॉडल के इनपुट और आउटपुट का प्रारूप निर्धारित करने के बाद, अपना इनपुट डेटा प्राप्त करें और डेटा पर कोई भी परिवर्तन करें जो आपके मॉडल के लिए सही आकार का इनपुट प्राप्त करने के लिए आवश्यक हो।

उदाहरण के लिए, यदि आपका मॉडल छवियों को संसाधित करता है, और आपके मॉडल में [1, 224, 224, 3] फ़्लोटिंग-पॉइंट मानों के इनपुट आयाम हैं, तो आपको निम्न उदाहरण के अनुसार छवि के रंग मानों को फ़्लोटिंग-पॉइंट रेंज में स्केल करना पड़ सकता है :

तीव्र

let image: CGImage = // Your input image
guard let context = CGContext(
  data: nil,
  width: image.width, height: image.height,
  bitsPerComponent: 8, bytesPerRow: image.width * 4,
  space: CGColorSpaceCreateDeviceRGB(),
  bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
) else {
  return false
}

context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
guard let imageData = context.data else { return false }

var inputData = Data()
for row in 0 ..&lt; 224 {
  for col in 0 ..&lt; 224 {
    let offset = 4 * (row * context.width + col)
    // (Ignore offset 0, the unused alpha channel)
    let red = imageData.load(fromByteOffset: offset+1, as: UInt8.self)
    let green = imageData.load(fromByteOffset: offset+2, as: UInt8.self)
    let blue = imageData.load(fromByteOffset: offset+3, as: UInt8.self)

    // Normalize channel values to [0.0, 1.0]. This requirement varies
    // by model. For example, some models might require values to be
    // normalized to the range [-1.0, 1.0] instead, and others might
    // require fixed-point values or the original bytes.
    var normalizedRed = Float32(red) / 255.0
    var normalizedGreen = Float32(green) / 255.0
    var normalizedBlue = Float32(blue) / 255.0

    // Append normalized values to Data object in RGB order.
    let elementSize = MemoryLayout.size(ofValue: normalizedRed)
    var bytes = [UInt8](repeating: 0, count: elementSize)
    memcpy(&amp;bytes, &amp;normalizedRed, elementSize)
    inputData.append(&amp;bytes, count: elementSize)
    memcpy(&amp;bytes, &amp;normalizedGreen, elementSize)
    inputData.append(&amp;bytes, count: elementSize)
    memcpy(&ammp;bytes, &amp;normalizedBlue, elementSize)
    inputData.append(&amp;bytes, count: elementSize)
  }
}

फिर, अपने इनपुट NSData दुभाषिया में कॉपी करें और इसे चलाएं:

तीव्र

try interpreter.allocateTensors()
try interpreter.copy(inputData, toInputAt: 0)
try interpreter.invoke()

आप दुभाषिया के output(at:) विधि को कॉल करके मॉडल का आउटपुट प्राप्त कर सकते हैं। आप आउटपुट का उपयोग कैसे करते हैं यह उस मॉडल पर निर्भर करता है जिसका आप उपयोग कर रहे हैं।

उदाहरण के लिए, यदि आप वर्गीकरण कर रहे हैं, तो अगले चरण के रूप में, आप परिणाम की अनुक्रमणिका को उनके द्वारा दर्शाए गए लेबल पर मैप कर सकते हैं:

तीव्र

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: "retrained_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])")
}

परिशिष्ट: मॉडल सुरक्षा

चाहे आप अपने TensorFlow Lite मॉडल को Firebase ML के लिए कैसे भी उपलब्ध कराएं, Firebase ML उन्हें स्थानीय भंडारण में मानक क्रमबद्ध प्रोटोबफ़ प्रारूप में संग्रहीत करता है।

सैद्धांतिक रूप से, इसका मतलब यह है कि कोई भी आपके मॉडल की नकल कर सकता है। हालाँकि, व्यवहार में, अधिकांश मॉडल इतने एप्लिकेशन-विशिष्ट होते हैं और अनुकूलन द्वारा अस्पष्ट होते हैं कि जोखिम आपके कोड को अलग करने और पुन: उपयोग करने वाले प्रतिस्पर्धियों के जोखिम के समान होता है। फिर भी, अपने ऐप में कस्टम मॉडल का उपयोग करने से पहले आपको इस जोखिम के बारे में पता होना चाहिए।