TensorFlow لايت يدعم العديد من مسرعات الأجهزة. يصف هذا المستند كيفية استخدام الواجهة الخلفية لوحدة معالجة الرسومات باستخدام واجهات برمجة تطبيقات تفويض TensorFlow Lite على Android و iOS.
تم تصميم وحدات معالجة الرسومات للحصول على إنتاجية عالية لأحمال العمل القابلة للتوازي بشكل كبير. وبالتالي ، فهي مناسبة تمامًا للشبكات العصبية العميقة ، والتي تتكون من عدد كبير من المشغلين ، كل منهم يعمل على بعض موتر (موترات) الإدخال التي يمكن تقسيمها بسهولة إلى أعباء عمل أصغر ويتم تنفيذها بشكل متوازٍ ، مما يؤدي عادةً إلى تقليل زمن الوصول. في أفضل السيناريوهات ، قد يعمل الاستدلال على وحدة معالجة الرسومات الآن بسرعة كافية لتطبيقات الوقت الفعلي التي لم تكن متاحة من قبل.
على عكس وحدات المعالجة المركزية (CPU) ، تحسب وحدات معالجة الرسومات بأرقام فاصلة عائمة 16 بت أو 32 بت ولا تتطلب تكميمًا للحصول على الأداء الأمثل. المندوب لا يقبل النماذج الكمية 8 بتات ، ولكن سيتم إجراء الحساب بأرقام الفاصلة العائمة. الرجوع إلى الوثائق المتقدمة للحصول على التفاصيل.
فائدة أخرى لاستدلال GPU هي كفاءتها في الطاقة. تقوم وحدات معالجة الرسومات (GPU) بإجراء العمليات الحسابية بطريقة فعالة ومحسّنة للغاية ، بحيث تستهلك طاقة أقل وتولد حرارة أقل مما يحدث عند تشغيل نفس المهمة على وحدات المعالجة المركزية.
دروس التطبيق التجريبي
أسهل طريقة لتجربة مفوض GPU هي اتباع البرامج التعليمية أدناه ، والتي تمر عبر إنشاء تطبيقاتنا التجريبية للتصنيف مع دعم GPU. رمز GPU هو ثنائي فقط في الوقت الحالي ؛ سيكون مفتوح المصدر قريبًا. بمجرد أن تفهم كيفية تشغيل عروضنا التوضيحية ، يمكنك تجربة ذلك على نماذجك المخصصة.
Android (مع Android Studio)
لتعليمي خطوة بخطوة، ومشاهدة GPU مندوب لالروبوت الفيديو.
الخطوة 1. استنساخ كود مصدر TensorFlow وافتحه في Android Studio
git clone https://github.com/tensorflow/tensorflow
الخطوة 2. تحرير app/build.gradle
لاستخدام AAR GPU ليلا
إضافة tensorflow-lite-gpu
حزمة جنبا إلى جنب مع القائمة tensorflow-lite
الحزمة في القائمة dependencies
كتلة.
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite:2.3.0'
implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0'
}
الخطوة 3. البناء والتشغيل
تشغيل → تشغيل "التطبيق". عند تشغيل التطبيق ، سترى زرًا لتمكين وحدة معالجة الرسومات. قم بالتغيير من النموذج الكمي إلى النموذج العائم ثم انقر فوق GPU للتشغيل على وحدة معالجة الرسومات.
iOS (مع XCode)
لتعليمي خطوة بخطوة، ومشاهدة GPU مندوب عن دائرة الرقابة الداخلية الفيديو.
الخطوة الأولى. احصل على الكود المصدري التجريبي وتأكد من تجميعه.
اتبع دينا دائرة الرقابة الداخلية تجريبي التطبيقات البرنامج التعليمي . سيوصلك هذا إلى نقطة حيث يعمل العرض التوضيحي لكاميرا iOS غير المعدلة على هاتفك.
الخطوة 2. قم بتعديل Podfile لاستخدام TensorFlow Lite GPU CocoaPod
بدءًا من الإصدار 2.3.0 ، يتم استبعاد مفوض GPU افتراضيًا من البود لتقليل الحجم الثنائي. يمكنك تضمينها عن طريق تحديد نوع فرعي. ل TensorFlowLiteSwift
جراب:
pod 'TensorFlowLiteSwift/Metal', '~> 0.0.1-nightly',
أو
pod 'TensorFlowLiteSwift', '~> 0.0.1-nightly', :subspecs => ['Metal']
يمكنك القيام بالمثل ل TensorFlowLiteObjC
أو TensorFlowLitC
إذا كنت تريد استخدام الهدف-C (من 2.4.0 الافراج) أو C API.
قبل الإصدار 2.3.0
حتى TensorFlow Lite 2.0.0
لقد قمنا ببناء CocoaPod ثنائي يتضمن مندوب GPU. لتبديل المشروع لاستخدامه ، قم بتعديل ملف `tensorflow / tensorflow / lite / أمثلة / ios / camera / Podfile` لاستخدام حجرة` TensorFlowLiteGpuExperimental` بدلاً من `TensorFlowLite`.
target 'YourProjectName'
# pod 'TensorFlowLite', '1.12.0'
pod 'TensorFlowLiteGpuExperimental'
حتى TensorFlow Lite 2.2.0
من TensorFlow Lite 2.1.0 إلى 2.2.0 ، يتم تضمين مفوض GPU في حجرة "TensorFlowLiteC". يمكنك الاختيار بين "TensorFlowLiteC" و "TensorFlowLiteSwift" حسب اللغة.
الخطوة 3. قم بتمكين مفوض GPU
لتمكين التعليمات البرمجية التي سوف تستخدم مندوب GPU، وسوف تحتاج إلى تغيير TFLITE_USE_GPU_DELEGATE
0-1 في CameraExampleViewController.h
.
#define TFLITE_USE_GPU_DELEGATE 1
الخطوة 4. إنشاء التطبيق التجريبي وتشغيله
بعد اتباع الخطوة السابقة ، يجب أن تكون قادرًا على تشغيل التطبيق.
الخطوة 5. وضع الإصدار
أثناء تشغيلك في الخطوة 4 في وضع التصحيح ، للحصول على أداء أفضل ، يجب عليك التغيير إلى إصدار مع الإعدادات المعدنية المثلى المناسبة. على وجه الخصوص، لتحرير هذه الإعدادات انتقل إلى Product > Scheme > Edit Scheme...
. اختر Run
. على Info
التبويب، وتغير Build Configuration
، من Debug
لل Release
، ازل Debug executable
.
ثم انقر على Options
التبويب وتغيير GPU Frame Capture
إلى Disabled
و Metal API Validation
ل Disabled
.
أخيرًا ، تأكد من تحديد الإصدار فقط يبني على بنية 64 بت. تحت Project navigator -> tflite_camera_example -> PROJECT -> tflite_camera_example -> Build Settings
مجموعة Build Active Architecture Only > Release
إلى نعم.
تجربة مندوب GPU على الطراز الخاص بك
ذكري المظهر
هناك طريقتان لاستدعاء تسارع نموذجي اعتمادا على إذا كنت تستخدم الروبوت ستوديو ML نموذج ملزم أو TensorFlow لايت مترجم.
مترجم TensorFlow Lite
انظر إلى العرض التوضيحي لمعرفة كيفية إضافة المفوض. في التطبيق الخاص بك، إضافة AAR على النحو الوارد أعلاه، استيراد org.tensorflow.lite.gpu.GpuDelegate
حدة، واستخدام addDelegate
وظيفة لتسجيل مندوب GPU للمترجم:
كوتلن
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);
iOS
سويفت
import TensorFlowLite // Load model ... // Initialize TensorFlow Lite interpreter with the GPU delegate. let delegate = MetalDelegate() if let interpreter = try Interpreter(modelPath: modelPath, delegates: [delegate]) { // Run inference ... }
ج موضوعية
// Import module when using CocoaPods with module support @import TFLTensorFlowLite; // Or import following headers manually #import "tensorflow/lite/objc/apis/TFLMetalDelegate.h" #import "tensorflow/lite/objc/apis/TFLTensorFlowLite.h" // Initialize GPU delegate TFLMetalDelegate* metalDelegate = [[TFLMetalDelegate alloc] init]; // Initialize interpreter with model path and GPU delegate TFLInterpreterOptions* options = [[TFLInterpreterOptions alloc] init]; NSError* error = nil; TFLInterpreter* interpreter = [[TFLInterpreter alloc] initWithModelPath:modelPath options:options delegates:@[ metalDelegate ] error:&error]; if (error != nil) { /* Error handling... */ } if (![interpreter allocateTensorsWithError:&error]) { /* Error handling... */ } if (error != nil) { /* Error handling... */ } // Run inference ... ```
C (حتى 2.3.0)
#include "tensorflow/lite/c/c_api.h" #include "tensorflow/lite/delegates/gpu/metal_delegate.h" // Initialize model TfLiteModel* model = TfLiteModelCreateFromFile(model_path); // Initialize interpreter with GPU delegate TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate(); TfLiteDelegate* delegate = TFLGPUDelegateCreate(nil); // default config TfLiteInterpreterOptionsAddDelegate(options, metal_delegate); TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options); TfLiteInterpreterOptionsDelete(options); TfLiteInterpreterAllocateTensors(interpreter); NSMutableData *input_data = [NSMutableData dataWithLength:input_size * sizeof(float)]; NSMutableData *output_data = [NSMutableData dataWithLength:output_size * sizeof(float)]; TfLiteTensor* input = TfLiteInterpreterGetInputTensor(interpreter, 0); const TfLiteTensor* output = TfLiteInterpreterGetOutputTensor(interpreter, 0); // Run inference TfLiteTensorCopyFromBuffer(input, inputData.bytes, inputData.length); TfLiteInterpreterInvoke(interpreter); TfLiteTensorCopyToBuffer(output, outputData.mutableBytes, outputData.length); // Clean up TfLiteInterpreterDelete(interpreter); TFLGpuDelegateDelete(metal_delegate); TfLiteModelDelete(model);
## Supported Models and Ops
With the release of the GPU delegate, we included a handful of models that can
be run on the backend:
* [MobileNet v1 (224x224) image classification](https://ai.googleblog.com/2017/06/mobilenets-open-source-models-for.html) [[download]](https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/mobilenet_v1_1.0_224.tflite)
<br /><i>(image classification model designed for mobile and embedded based vision applications)</i>
* [DeepLab segmentation (257x257)](https://ai.googleblog.com/2018/03/semantic-image-segmentation-with.html) [[download]](https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/deeplabv3_257_mv_gpu.tflite)
<br /><i>(image segmentation model that assigns semantic labels (e.g., dog, cat, car) to every pixel in the input image)</i>
* [MobileNet SSD object detection](https://ai.googleblog.com/2018/07/accelerated-training-and-inference-with.html) [[download]](https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/mobile_ssd_v2_float_coco.tflite)
<br /><i>(image classification model that detects multiple objects with bounding boxes)</i>
* [PoseNet for pose estimation](https://github.com/tensorflow/tfjs-models/tree/master/posenet) [[download]](https://storage.googleapis.com/download.tensorflow.org/models/tflite/gpu/multi_person_mobilenet_v1_075_float.tflite)
<br /><i>(vision model that estimates the poses of a person(s) in image or video)</i>
To see a full list of supported ops, please see the
[advanced documentation](gpu_advanced).
## Non-supported models and ops
If some of the ops are not supported by the GPU delegate, the framework will
only run a part of the graph on the GPU and the remaining part on the CPU. Due
to the high cost of CPU/GPU synchronization, a split execution mode like this
will often result in slower performance than when the whole network is run on
the CPU alone. In this case, the user will get a warning like:
```none
WARNING: op code #42 cannot be handled by this delegate.
```
لم نقدم رد اتصال لهذا الفشل ، لأن هذا ليس فشلًا حقيقيًا في وقت التشغيل ، ولكنه شيء يمكن للمطور ملاحظته أثناء محاولة تشغيل الشبكة على المفوض.
نصائح للتحسين
التحسين للأجهزة المحمولة
قد يكون لبعض العمليات البسيطة على وحدة المعالجة المركزية تكلفة عالية لوحدة معالجة الرسومات على الأجهزة المحمولة. عمليات إعادة تشكيل غالية ولا سيما لتشغيل، بما في ذلك BATCH_TO_SPACE
، SPACE_TO_BATCH
، SPACE_TO_DEPTH
، وهكذا دواليك. يجب عليك فحص استخدام عمليات إعادة التشكيل عن كثب ، واعتبار ذلك قد تم تطبيقه فقط لاستكشاف البيانات أو للتكرارات المبكرة لنموذجك. يمكن أن تؤدي إزالتها إلى تحسين الأداء بشكل ملحوظ.
في GPU ، يتم تقسيم بيانات الموتر إلى 4 قنوات. وهكذا، فإن حساب على موتر من شكل [B,H,W,5]
سوف تؤدي عن نفسه على موتر من شكل [B,H,W,8]
ولكن ما هو أسوأ بكثير من [B,H,W,4]
. وبهذا المعنى ، إذا كانت أجهزة الكاميرا تدعم إطارات الصور في RGBA ، فإن تغذية المدخلات ذات 4 قنوات تكون أسرع بشكل ملحوظ حيث يمكن تجنب نسخ الذاكرة (من 3 قنوات RGB إلى 4 قنوات RGBX).
للحصول على أفضل أداء ، يجب أن تفكر في إعادة تدريب المصنف بهندسة شبكة محسّنة للجوّال. يمكن أن يؤدي تحسين الاستنتاج على الجهاز إلى تقليل وقت الاستجابة واستهلاك الطاقة بشكل كبير من خلال الاستفادة من ميزات الأجهزة المحمولة.
تقليل وقت التهيئة باستخدام التسلسل
تسمح لك ميزة مفوض GPU بالتحميل من كود kernel المترجمة مسبقًا وبيانات النموذج المتسلسلة وحفظها على القرص من عمليات التشغيل السابقة. يتجنب هذا النهج إعادة التجميع ويقلل من وقت بدء التشغيل بنسبة تصل إلى 90٪. للحصول على إرشادات حول كيفية تطبيق التسلسل إلى المشروع الخاص بك، انظر GPU مندوب التسلسل .