نماینده شتاب GPU با Interpreter API

استفاده از واحدهای پردازش گرافیکی (GPU) برای اجرای مدل‌های یادگیری ماشینی (ML) می‌تواند عملکرد و تجربه کاربری برنامه‌های دارای ML را به‌طور چشمگیری بهبود بخشد. در دستگاه های اندرویدی می توانید فعال کنید

delegate و یکی از API های زیر:

این صفحه نحوه فعال کردن شتاب GPU را برای مدل‌های TensorFlow Lite در برنامه‌های Android با استفاده از Interpreter API توضیح می‌دهد. برای اطلاعات بیشتر درباره استفاده از نماینده GPU برای TensorFlow Lite، از جمله بهترین شیوه‌ها و تکنیک‌های پیشرفته، به صفحه نمایندگان GPU مراجعه کنید.

از GPU با TensorFlow Lite با خدمات Google Play استفاده کنید

TensorFlow Lite Interpreter API مجموعه‌ای از APIهای عمومی را برای ساخت برنامه‌های یادگیری ماشین ارائه می‌کند. این بخش نحوه استفاده از نماینده شتابدهنده GPU با این APIها با TensorFlow Lite با خدمات Google Play را توضیح می دهد.

TensorFlow Lite با خدمات Google Play مسیر توصیه شده برای استفاده از TensorFlow Lite در اندروید است. اگر برنامه شما دستگاه‌هایی را هدف قرار می‌دهد که از Google Play استفاده نمی‌کنند، به GPU با Interpreter API و بخش TensorFlow Lite مستقل مراجعه کنید.

وابستگی های پروژه را اضافه کنید

برای فعال کردن دسترسی به نماینده GPU، com.google.android.gms:play-services-tflite-gpu به فایل build.gradle برنامه خود اضافه کنید:

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 را فعال کنید

سپس TensorFlow Lite را با سرویس‌های Google Play با پشتیبانی از GPU راه‌اندازی کنید:

کاتلین

    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());
    });
      

در نهایت می توانید مفسری را که یک GpuDelegateFactory را از طریق InterpreterApi.Options ارسال می کند، مقداردهی اولیه کنید:

کاتلین


    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);
      

نماینده GPU همچنین می تواند با اتصال مدل ML در Android Studio استفاده شود. برای اطلاعات بیشتر، به ایجاد رابط های مدل با استفاده از ابرداده مراجعه کنید.

از GPU با TensorFlow Lite مستقل استفاده کنید

اگر برنامه شما دستگاه‌هایی را هدف قرار می‌دهد که Google Play را اجرا نمی‌کنند، می‌توانید نماینده GPU را به برنامه خود اضافه کنید و از آن با نسخه مستقل TensorFlow Lite استفاده کنید.

وابستگی های پروژه را اضافه کنید

برای فعال کردن دسترسی به نماینده GPU، org.tensorflow:tensorflow-lite-gpu-delegate-plugin به فایل build.gradle برنامه خود اضافه کنید:

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

شتاب GPU را فعال کنید

سپس TensorFlow Lite را روی GPU با TfLiteDelegate اجرا کنید. در جاوا، می‌توانید GpuDelegate از طریق Interpreter.Options مشخص کنید.

کاتلین

      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 اندروید به طور پیش فرض از مدل های کوانتیزه شده پشتیبانی می کنند. برای استفاده از مدل های کوانتیزه شده با نماینده GPU، نیازی به تغییر کد ندارید. بخش زیر نحوه غیرفعال کردن پشتیبانی کوانتیزه شده برای اهداف آزمایشی یا آزمایشی را توضیح می دهد.

پشتیبانی از مدل کوانتیزه را غیرفعال کنید

کد زیر نحوه غیرفعال کردن پشتیبانی از مدل های کوانتیزه را نشان می دهد.

جاوا

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

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

برای اطلاعات بیشتر در مورد اجرای مدل های کوانتیزه شده با شتاب GPU، به نمای کلی نماینده GPU مراجعه کنید.