Google Cloud Vision 对象检测模型在 Android 上崩溃
Google Cloud Vision Object Detection Model Crashes on Android
我最近在 Google Cloud Vision 上训练了一个对象检测模型。我导出了元数据 jason 文件、标签文本文件和训练模型的模型 tflite 文件,我打算 运行 它在 Android 上。但是,我无法使用 Android 演示应用程序 运行 这个模型,因为它每次都会崩溃。
使用的演示应用与本地训练和转换的 tflite 模型兼容,但与从 Google 云导出的模型不兼容。
这里可能出了什么问题,如何解决?
谢谢
参考:
演示应用程序:https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection
部分日志:
2020-01-24 11:29:11.628 18071-18071/org.tensorflow.lite.examples.detection E/libc: Access denied finding property "persist.camera.privapp.list"
2020-01-24 11:29:11.732 18071-18101/org.tensorflow.lite.examples.detection I/tensorflow: CameraConnectionFragment: Opening camera preview: 640x480
2020-01-24 11:29:11.769 18071-18102/org.tensorflow.lite.examples.detection D/vndksupport: Loading /vendor/lib/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-01-24 11:29:11.770 18071-18102/org.tensorflow.lite.examples.detection D/vndksupport: Loading /vendor/lib/hw/gralloc.msm8937.so from current namespace instead of sphal namespace.
2020-01-24 11:29:11.803 18071-18071/org.tensorflow.lite.examples.detection I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5ab1c5e time:332335506
2020-01-24 11:29:12.198 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 0 at size 307200
2020-01-24 11:29:12.201 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 1 at size 153599
2020-01-24 11:29:12.203 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 2 at size 153599
2020-01-24 11:29:12.204 18071-18101/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Preparing image 1 for detection in bg thread.
2020-01-24 11:29:12.311 18071-18100/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Running detection on image 1
2020-01-24 11:29:12.475 18071-18100/org.tensorflow.lite.examples.detection E/AndroidRuntime: FATAL EXCEPTION: inference
Process: org.tensorflow.lite.examples.detection, PID: 18071
java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 307200 bytes and a Java Buffer with 270000 bytes.
at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:332)
at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:305)
at org.tensorflow.lite.Tensor.setTo(Tensor.java:123)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:148)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:296)
at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:193)
at org.tensorflow.lite.examples.detection.DetectorActivity.run(DetectorActivity.java:183)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.os.HandlerThread.run(HandlerThread.java:65)
============================================= ============
更新:现在我们知道这是因为输入模型的图像和模型的输入形状不匹配。从 Google Cloud Vision 训练的模型的 input/output 形状似乎并不一致。我最近得到了 [1 320 320 3] 和 [ 1 20 4] 中的一个,还有 [ 1 512 512 3] 中和 [ 1 20 4] 中的另一个。
演示应用程序用于处理 [1 300 300 3] 输入和 [ 1 10 4] 输出的模型。
如何在 Google Cloud Vision 训练之前分配模型的形状,或者如何使演示应用程序能够处理特定形状的模型?
============================================= ============
为了让演示应用程序能够处理特定形状的模型,我将 TF_OD_API_INPUT_SIZE
从 300 更改为 320,这似乎已经解决了输入数据形状问题。然而,问题出在输出端。
新的错误日志说:
java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 20, 4] and a Java object with shape [1, 10, 4].
将 TEXT_SIZE_DIP
从 10 更改为 20 没有帮助。
崩溃的原因是输入的形状与模型不匹配,解决后,由于输出形状不匹配导致崩溃。
解决方案是根据 AutoML 在 Google Cloud 上提供的模型元数据调整演示应用程序的 I/O 形状。
我最近在 Google Cloud Vision 上训练了一个对象检测模型。我导出了元数据 jason 文件、标签文本文件和训练模型的模型 tflite 文件,我打算 运行 它在 Android 上。但是,我无法使用 Android 演示应用程序 运行 这个模型,因为它每次都会崩溃。
使用的演示应用与本地训练和转换的 tflite 模型兼容,但与从 Google 云导出的模型不兼容。
这里可能出了什么问题,如何解决?
谢谢
参考: 演示应用程序:https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection
部分日志:
2020-01-24 11:29:11.628 18071-18071/org.tensorflow.lite.examples.detection E/libc: Access denied finding property "persist.camera.privapp.list"
2020-01-24 11:29:11.732 18071-18101/org.tensorflow.lite.examples.detection I/tensorflow: CameraConnectionFragment: Opening camera preview: 640x480
2020-01-24 11:29:11.769 18071-18102/org.tensorflow.lite.examples.detection D/vndksupport: Loading /vendor/lib/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2020-01-24 11:29:11.770 18071-18102/org.tensorflow.lite.examples.detection D/vndksupport: Loading /vendor/lib/hw/gralloc.msm8937.so from current namespace instead of sphal namespace.
2020-01-24 11:29:11.803 18071-18071/org.tensorflow.lite.examples.detection I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5ab1c5e time:332335506
2020-01-24 11:29:12.198 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 0 at size 307200
2020-01-24 11:29:12.201 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 1 at size 153599
2020-01-24 11:29:12.203 18071-18101/org.tensorflow.lite.examples.detection D/tensorflow: CameraActivity: Initializing buffer 2 at size 153599
2020-01-24 11:29:12.204 18071-18101/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Preparing image 1 for detection in bg thread.
2020-01-24 11:29:12.311 18071-18100/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Running detection on image 1
2020-01-24 11:29:12.475 18071-18100/org.tensorflow.lite.examples.detection E/AndroidRuntime: FATAL EXCEPTION: inference
Process: org.tensorflow.lite.examples.detection, PID: 18071
java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 307200 bytes and a Java Buffer with 270000 bytes.
at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:332)
at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:305)
at org.tensorflow.lite.Tensor.setTo(Tensor.java:123)
at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:148)
at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:296)
at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:193)
at org.tensorflow.lite.examples.detection.DetectorActivity.run(DetectorActivity.java:183)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.os.HandlerThread.run(HandlerThread.java:65)
============================================= ============
更新:现在我们知道这是因为输入模型的图像和模型的输入形状不匹配。从 Google Cloud Vision 训练的模型的 input/output 形状似乎并不一致。我最近得到了 [1 320 320 3] 和 [ 1 20 4] 中的一个,还有 [ 1 512 512 3] 中和 [ 1 20 4] 中的另一个。
演示应用程序用于处理 [1 300 300 3] 输入和 [ 1 10 4] 输出的模型。
如何在 Google Cloud Vision 训练之前分配模型的形状,或者如何使演示应用程序能够处理特定形状的模型?
============================================= ============
为了让演示应用程序能够处理特定形状的模型,我将 TF_OD_API_INPUT_SIZE
从 300 更改为 320,这似乎已经解决了输入数据形状问题。然而,问题出在输出端。
新的错误日志说:
java.lang.IllegalArgumentException: Cannot copy between a TensorFlowLite tensor with shape [1, 20, 4] and a Java object with shape [1, 10, 4].
将 TEXT_SIZE_DIP
从 10 更改为 20 没有帮助。
崩溃的原因是输入的形状与模型不匹配,解决后,由于输出形状不匹配导致崩溃。
解决方案是根据 AutoML 在 Google Cloud 上提供的模型元数据调整演示应用程序的 I/O 形状。