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

时间:2018-05-31 17:29:23

标签: java android tensorflow tensorflow-lite

我正在关注这个tensorflow light教程:https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite/#5

Android应用程序提供了here

在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)

如果有人可以提供有关此

的任何意见,那将会很棒

感谢您查看问题并对迟到的回复感到抱歉。以下是我遵循的步骤: 1.使用本教程重新训练模型:https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/index.html?index=..%2F..%2Findex#3

创建模型的命令:

python -m scripts.retrain   
--bottleneck_dir=tf_files/bottlenecks   
--how_many_training_steps=500   
--model_dir=tf_files/models/   
--summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}"   
--output_graph=tf_files/retrained_graph.pb   
--output_labels=tf_files/retrained_labels.txt   
--architecture="${ARCHITECTURE}"   
--image_dir=/home/ganesh/Documents/Developement/MachineLearning/new_approach/flower_photos_subset

我使用的图像尺寸是: - 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)

2 个答案:

答案 0 :(得分:1)

问题是您正在通过float将TF模型转换为toco --input_data_type=FLOAT tflite模型,而tflite poets 2 app Feed ByteBuffer输入到模型(它转换图像BitmapByteBuffer)。最初它使用了量化的 mobilenet tflite模型,该模型期望输入Byte。但是,当您使用模型替换它时,模型开始期待float,但应用程序将其Byte。因此它崩溃了。

您提及的其他应用TFMobile poets 2 app正常工作,因为它将位图转换为float[]而不是ByteBuffer

因此,如果您想要返回第一个应用程序,可以先在tensorflow中量化重新训练的图形,然后告诉toco您的输入是quantized(参见toco commandline examples here )然后再试一次。

(此script来自TensorFlow存储库,但它未包含在默认安装中)。

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
  

但是,请注意,培训后量化图表可能会导致准确性降低。所以一定要衡量一下。另一种选择是在训练期间将伪量化操作插入到图形中,然后转换为量化图形。这将确保更少的准确性损失,但这是更多的工作。

答案 1 :(得分:0)

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