Java Tensorflow lite再培训模型:替换我的模型后Android应用程序崩溃

Java Tensorflow lite再培训模型:替换我的模型后Android应用程序崩溃,java,android,tensorflow,tensorflow-lite,Java,Android,Tensorflow,Tensorflow Lite,下面是tensorflow灯光教程: 提供的Android应用程序 在安卓手机上安装和运行时效果良好。但当我替换新创建的花卉模型时,它总是崩溃。以下是日志: 05-31 22:55:46.492 581-581/? I/art: Late-enabling -Xcheck:jni 05-31 22:55:47.484 581-581/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Created a Tensorflow Lite

下面是tensorflow灯光教程:

提供的Android应用程序


在安卓手机上安装和运行时效果良好。但当我替换新创建的花卉模型时,它总是崩溃。以下是日志:

05-31 22:55:46.492 581-581/? I/art: Late-enabling -Xcheck:jni
05-31 22:55:47.484 581-581/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Created a Tensorflow Lite Image Classifier.
05-31 22:55:47.496 581-598/android.example.com.tflitecamerademo D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-31 22:55:47.657 581-598/android.example.com.tflitecamerademo I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build:  (Ifd751822f5)
    OpenGL ES Shader Compiler Version: XE031.06.00.05
    Build Date: 01/26/16 Tue
    Local Branch: AU12_SBA
    Remote Branch: 
    Local Patches: 
    Reconstruct Branch: 
05-31 22:55:47.664 581-598/android.example.com.tflitecamerademo I/OpenGLRenderer: Initialized EGL, version 1.4
05-31 22:55:47.892 581-581/android.example.com.tflitecamerademo I/CameraManagerGlobal: Connecting to camera service
05-31 22:55:48.010 581-581/android.example.com.tflitecamerademo I/CameraManager: Using legacy camera HAL.
05-31 22:55:48.395 581-597/android.example.com.tflitecamerademo I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
05-31 22:55:48.395 581-648/android.example.com.tflitecamerademo I/RequestThread-0: Configure outputs: 1 surfaces configured.
05-31 22:55:48.395 581-648/android.example.com.tflitecamerademo D/Camera: app passed NULL surface
05-31 22:55:48.469 581-581/android.example.com.tflitecamerademo I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
05-31 22:55:48.555 581-597/android.example.com.tflitecamerademo I/CameraDeviceState: Legacy camera service transitioning to state IDLE
05-31 22:55:48.633 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 41
05-31 22:55:48.801 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to run model inference: 169
05-31 22:55:48.853 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 43
05-31 22:55:48.985 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to run model inference: 133
05-31 22:55:48.987 581-597/android.example.com.tflitecamerademo I/RequestQueue: Repeating capture request set.
05-31 22:55:48.993 581-648/android.example.com.tflitecamerademo W/LegacyRequestMapper: convertRequestMetadata - control.awbRegions setting is not supported, ignoring value
    Only received metering rectangles with weight 0.
    Only received metering rectangles with weight 0.
05-31 22:55:49.033 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 40
05-31 22:55:49.159 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to run model inference: 126
05-31 22:55:49.212 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 42
05-31 22:55:49.332 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to run model inference: 121
05-31 22:55:49.385 581-597/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 46
05-31 22:55:49.545 581-597/android.example.com.tflitecamerademo A/libc: Fatal signal 7 (SIGBUS), code 1, fault addr 0xb946ac98 in tid 597 (CameraBackgroun)
这里我使用的图像大小是:-224,架构是:mobilenet_0.50_224

我测试了重新培训的模型,它使用命令运行良好:

python -m scripts.label_image \
--graph=tf_files/retrained_graph.pb  \
--image=/home/ganesh/Documents/Developement/MachineLearning/new_approach/flower_images/flower.jpeg
它给出了正确的结果 然后我使用以下方法将其转换为Tensorflow lite模型:

toco \
--input_file=/tmp/output_graph.pb \
--output_file=/tmp/graph.lite \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--input_shape=1,224,224,3 \
--input_array=input \
--output_array=final_result \
--inference_type=FLOAT \
--input_data_type=FLOAT
已成功生成文件 但是,当我在android应用程序中替换相同的应用程序时,它正在崩溃
由于我能够使用命令测试重新训练的模型,并且它给出了正确的结果,我觉得将其转换为lite格式(适用于Android)有问题。

我以同样的方式创建了自定义模型,这次我尝试了不同的Android应用程序(TFMobile),它起作用了:)这里是教程链接:

问题是,您正在通过
toco--input_data\u type=float
将TF模型转换为
float
tflite模型,而提要
ByteBuffer
输入到模型(它将图像
位图
转换为
ByteBuffer
)。最初它使用了一个量化的mobilenet tflite模型,该模型需要一个
字节
输入。但是,当您将其替换为您的模型时,模型开始期望
浮动
,但应用程序为其提供
字节
。它就这样坠毁了

您提到的另一个应用程序工作正常,因为它将位图转换为
float[]
而不是
ByteBuffer

因此,如果您想回到您的第一个应用程序,您可以首先在tensorflow中量化重新训练的图形,然后告诉
toco
以及您的输入是
量化的
(请参阅),然后重试

(这来自TensorFlow存储库,但不包括在默认安装中)

但是,请注意,在训练后量化图形可能会导致精度损失。所以一定要测量一下。另一种选择是在转换为量化图之前,在训练期间向图中插入伪量化操作。这将确保更少的精度损失,但这是更多的工作


“在安卓手机上安装和运行时工作正常。”->应用程序工作时使用的原始型号是什么?输入和输出类型和形状是否与新模型相同?@PannagSanketi我已更新了详细信息,如果您需要任何其他详细信息,请告诉我。我想我已经找到了问题所在。请听我下面的回答。
toco \
--input_file=/tmp/output_graph.pb \
--output_file=/tmp/graph.lite \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--input_shape=1,224,224,3 \
--input_array=input \
--output_array=final_result \
--inference_type=FLOAT \
--input_data_type=FLOAT
python -m scripts.quantize_graph \
--input=tf_files/optimized_graph.pb \
--output=tf_files/rounded_graph.pb \
--output_node_names=final_result \
--mode=weights_rounded