TensorFlow Lite Android 仅当输入大小 >1 时才会在 GPU 计算上崩溃
TensorFlow Lite Android Crashes on GPU Compute only when Input Size is >1
我一直在开发一个 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.
当我在个人 phone 上 运行 GPU 计算时,Motorla Moto G7 Power,我收到此错误消息:
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 and their page on the GPU Delegate,但没有找到任何相关的解决方案。感谢您的帮助!
在推荐试用 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-nightly,我的问题就消失了。我不能说最初可能是什么原因造成的,但这就是解决它的原因。
我一直在开发一个 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.
当我在个人 phone 上 运行 GPU 计算时,Motorla Moto G7 Power,我收到此错误消息:
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 and their page on the GPU Delegate,但没有找到任何相关的解决方案。感谢您的帮助!
在推荐试用 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-nightly,我的问题就消失了。我不能说最初可能是什么原因造成的,但这就是解决它的原因。