שימוש ביחידות עיבוד גרפיות (GPU) להפעלת מודלים של למידת מכונה (ML) שלך יכול לשפר באופן דרמטי את הביצועים וחווית המשתמש של היישומים התומכים ב-ML שלך. במכשירי אנדרואיד, אתה יכול להפעיל
נציג ואחד מממשקי ה-API הבאים:
דף זה מתאר כיצד להפעיל האצת GPU עבור דגמי TensorFlow Lite באפליקציות אנדרואיד באמצעות Interpreter API. למידע נוסף על השימוש ב-GPU Delegate עבור 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()) }
Java
TaskuseGpuTask = 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)
Java
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 Delegate, הוסף 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
. ב-Java, אתה יכול לציין את 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)
Java
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);
דגמים כמותיים
ספריות נציגי Android GPU תומכות בדגמים כמותיים כברירת מחדל. אינך צריך לבצע שינויים כלשהם בקוד כדי להשתמש במודלים כמותיים עם נציג ה-GPU. הסעיף הבא מסביר כיצד להשבית תמיכה כמותית למטרות בדיקה או ניסוי.
השבת את התמיכה במודלים כמותיים
הקוד הבא מראה כיצד להשבית תמיכה עבור מודלים כמותיים.
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
למידע נוסף על הפעלת דגמים קוונטיים עם האצת GPU, ראה סקירת נציגי GPU .