【问题标题】:TensorFlow Lite Android Crashes on GPU Compute only when Input Size is >1TensorFlow Lite Android 仅当 Input Size 大于 1 时才会在 GPU Compute 上崩溃
【发布时间】:2020-06-26 18:30:48
【问题描述】:

我一直在开发一个 AndroidStudio 应用程序,它使用 TensorFlow Lite 的 GPU 委托来加快推理速度。它使用一个模型,该模型接受一个大小为 [n]x[384] 的输入数组并输出一个大小为 [n]x[1] 的数组,其中 n 是我希望在给定的情况下输入的 384 大小的输入的数量时间。输出 n 仅取决于输入 n。对于 n=1,我没有问题 - TF Lite 的 CPU 和 GPU 推理都可以正常工作(尽管 GPU 确实需要更长的时间 - 可能是因为输入尺寸较小?)。当我增加 n 使其大于 1 并运行我的模型时,CPU 计算工作正常,但是 GPU 计算使我的程序崩溃。当我使用模拟的 Pixel 3 XL 运行程序时,我收到以下错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mlptest, PID: 10405
java.lang.IllegalArgumentException: Internal error: Failed to apply delegate: OpenCL library not loaded - dlopen failed: library "libOpenCL-pixel.so" not found
Falling back to OpenGL
TfLiteGpuDelegate Init: OpenGL ES 3.1 or above is required to use OpenGL inference.
TfLiteGpuDelegate Prepare: delegate is not initialized
Node number 4 (TfLiteGpuDelegateV2) failed to prepare.

当我在我的个人手机 Motorla Moto G7 Power 上运行 GPU 计算时,我收到以下错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mlptest, PID: 16906
java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: TfLiteGpuDelegate Init: Index is out of range
TfLiteGpuDelegate Prepare: delegate is not initialized
Node number 4 (TfLiteGpuDelegateV2) failed to prepare.

一旦 GPU 委托的解释器运行,就会发生这种崩溃。我正在使用这些代码行创建委托:

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

使用选项初始化解释器然后运行它:

                Interpreter tfliteGPU = new Interpreter(loadedFile, options);

最后在我的计算之后关闭委托:

delegate.close();

我使用的原始 TensorFlow 模型是在 TensorFlow 1.x 中制作的,并使用 tflite_convert 命令从冻结图转换而来。我在 TF Lite 2.2.0 和 TF Lite GPU 2.2.0 上运行应用程序:

    implementation 'org.tensorflow:tensorflow-lite:2.2.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu:2.2.0'

我查看了 TF Lite 的 Android API reference 和他们在 GPU Delegate 上的页面,但没有找到任何相关的解决方案。任何帮助表示赞赏!

【问题讨论】:

  • 有趣!是否有 github repo 可以重现?
  • 有什么方法可以分享模型吗?除非我们可以窥视模型(可能是未经训练/简化的模型?),否则很难判断委托失败的原因。此外,GPU 可能无法在模拟器上运行,因为 AFAIK 所需的驱动程序不存在。
  • 我在一个小型研究小组担任实习生,该小组的任务是让我将他们的 ML 模型放到 Android 上并在其上运行性能测试。目前我不允许分享我们的整个模型,但我会在周三的每周会议上与我的团队交谈,看看我是否能说服他们。感谢您的帮助!
  • 您能否确认您正在导入的 GPU 委托?您是否每晚使用 0.0.0?
  • 我已经用 TF Lite 基础和 GPU 实现信息更新了我的帖子。我只是在使用 2.2.0,但我会每晚尝试并在之后更新。

标签: android-studio tensorflow tensorflow-lite


【解决方案1】:

在推荐试用 TensorFlow Nightly 实施后:

    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
    implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'

我将 build.gradle 中的实现切换为每晚使用 0.0.0,我的问题就消失了。我无法说出最初可能是什么原因造成的,但这就是解决它的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    相关资源
    最近更新 更多