दुभाषिया एपीआई के साथ जीपीयू त्वरण प्रतिनिधि

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

प्रतिनिधि और निम्नलिखित एपीआई में से एक:

  • दुभाषिया एपीआई - यह मार्गदर्शिका
  • टास्क लाइब्रेरी एपीआई - गाइड
  • नेटिव (सी/सी++) एपीआई - गाइड

यह पृष्ठ वर्णन करता है कि इंटरप्रेटर एपीआई का उपयोग करके एंड्रॉइड ऐप्स में टेन्सरफ्लो लाइट मॉडल के लिए जीपीयू त्वरण को कैसे सक्षम किया जाए। सर्वोत्तम प्रथाओं और उन्नत तकनीकों सहित, TensorFlow Lite के लिए GPU प्रतिनिधि का उपयोग करने के बारे में अधिक जानकारी के लिए, GPU प्रतिनिधि पृष्ठ देखें।

Google Play सेवाओं के साथ TensorFlow Lite के साथ GPU का उपयोग करें

टेन्सरफ्लो लाइट इंटरप्रेटर एपीआई मशीन लर्निंग एप्लिकेशन बनाने के लिए सामान्य प्रयोजन एपीआई का एक सेट प्रदान करता है। यह अनुभाग बताता है कि Google Play सेवाओं के साथ TensorFlow Lite के साथ इन API के साथ GPU त्वरक प्रतिनिधि का उपयोग कैसे करें।

Google Play सेवाओं के साथ TensorFlow Lite Android पर TensorFlow Lite का उपयोग करने के लिए अनुशंसित पथ है। यदि आपका एप्लिकेशन Google Play नहीं चलाने वाले उपकरणों को लक्षित कर रहा है, तो इंटरप्रेटर एपीआई और स्टैंडअलोन टेन्सरफ्लो लाइट अनुभाग के साथ जीपीयू देखें।

प्रोजेक्ट निर्भरताएँ जोड़ें

GPU प्रतिनिधि तक पहुंच सक्षम करने के लिए, अपने ऐप की build.gradle फ़ाइल में com.google.android.gms:play-services-tflite-gpu जोड़ें:

dependencies {
    ...
    implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
}

GPU त्वरण सक्षम करें

फिर GPU समर्थन के साथ Google Play सेवाओं के साथ TensorFlow Lite को आरंभ करें:

Kotlin

    val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

    val interpreterTask = useGpuTask.continueWith { useGpuTask ->
      TfLite.initialize(context,
          TfLiteInitializationOptions.builder()
          .setEnableGpuDelegateSupport(useGpuTask.result)
          .build())
      }
      

जावा

    Task useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context);

    Task interpreterOptionsTask = useGpuTask.continueWith({ task ->
      TfLite.initialize(context,
      TfLiteInitializationOptions.builder()
        .setEnableGpuDelegateSupport(true)
        .build());
    });
      

आप अंततः InterpreterApi.Options के माध्यम से GpuDelegateFactory को पार करते हुए दुभाषिया को प्रारंभ कर सकते हैं:

Kotlin


    val options = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(GpuDelegateFactory())

    val interpreter = InterpreterApi(model, options)

    // Run inference
    writeToInput(input)
    interpreter.run(input, output)
    readFromOutput(output)
      

जावा


    Options options = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(new GpuDelegateFactory());

    Interpreter interpreter = new InterpreterApi(model, options);

    // Run inference
    writeToInput(input);
    interpreter.run(input, output);
    readFromOutput(output);
      

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

स्टैंडअलोन TensorFlow Lite के साथ GPU का उपयोग करें

यदि आपका एप्लिकेशन उन डिवाइसों को लक्षित करता है जो Google Play नहीं चला रहे हैं, तो आपके एप्लिकेशन में GPU प्रतिनिधि को बंडल करना और इसे TensorFlow Lite के स्टैंडअलोन संस्करण के साथ उपयोग करना संभव है।

प्रोजेक्ट निर्भरताएँ जोड़ें

GPU प्रतिनिधि तक पहुंच सक्षम करने के लिए, अपने ऐप की build.gradle फ़ाइल में org.tensorflow:tensorflow-lite-gpu-delegate-plugin जोड़ें:

dependencies {
    ...
    implementation 'org.tensorflow:tensorflow-lite'
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

GPU त्वरण सक्षम करें

फिर TfLiteDelegate के साथ GPU पर TensorFlow Lite चलाएं। जावा में, आप Interpreter.Options के माध्यम से GpuDelegate निर्दिष्ट कर सकते हैं।

Kotlin

      import org.tensorflow.lite.Interpreter
      import org.tensorflow.lite.gpu.CompatibilityList
      import org.tensorflow.lite.gpu.GpuDelegate

      val compatList = CompatibilityList()

      val options = Interpreter.Options().apply{
          if(compatList.isDelegateSupportedOnThisDevice){
              // if the device has a supported GPU, add the GPU delegate
              val delegateOptions = compatList.bestOptionsForThisDevice
              this.addDelegate(GpuDelegate(delegateOptions))
          } else {
              // if the GPU is not supported, run on 4 threads
              this.setNumThreads(4)
          }
      }

      val interpreter = Interpreter(model, options)

      // Run inference
      writeToInput(input)
      interpreter.run(input, output)
      readFromOutput(output)
      

जावा

      import org.tensorflow.lite.Interpreter;
      import org.tensorflow.lite.gpu.CompatibilityList;
      import org.tensorflow.lite.gpu.GpuDelegate;

      // Initialize interpreter with GPU delegate
      Interpreter.Options options = new Interpreter.Options();
      CompatibilityList compatList = CompatibilityList();

      if(compatList.isDelegateSupportedOnThisDevice()){
          // if the device has a supported GPU, add the GPU delegate
          GpuDelegate.Options delegateOptions = compatList.getBestOptionsForThisDevice();
          GpuDelegate gpuDelegate = new GpuDelegate(delegateOptions);
          options.addDelegate(gpuDelegate);
      } else {
          // if the GPU is not supported, run on 4 threads
          options.setNumThreads(4);
      }

      Interpreter interpreter = new Interpreter(model, options);

      // Run inference
      writeToInput(input);
      interpreter.run(input, output);
      readFromOutput(output);
      

परिमाणित मॉडल

एंड्रॉइड जीपीयू प्रतिनिधि लाइब्रेरीज़ डिफ़ॉल्ट रूप से परिमाणित मॉडल का समर्थन करती हैं। आपको GPU प्रतिनिधि के साथ परिमाणित मॉडल का उपयोग करने के लिए कोई कोड परिवर्तन करने की आवश्यकता नहीं है। निम्नलिखित अनुभाग बताता है कि परीक्षण या प्रयोगात्मक उद्देश्यों के लिए परिमाणित समर्थन को कैसे अक्षम किया जाए।

परिमाणित मॉडल समर्थन अक्षम करें

निम्नलिखित कोड दिखाता है कि परिमाणित मॉडलों के लिए समर्थन को कैसे अक्षम किया जाए

जावा

GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false));

Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
      

GPU त्वरण के साथ क्वांटाइज़्ड मॉडल चलाने के बारे में अधिक जानकारी के लिए, GPU प्रतिनिधि अवलोकन देखें।