【问题标题】:Tensorflow lite retrained model : Android application crashes after replacing my modelTensorflow lite 重新训练模型:Android 应用程序在替换我的模型后崩溃
【发布时间】:2018-05-31 17:29:23
【问题描述】:

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

Android 应用提供here

当我在 android mobile 上安装和运行时工作正常。但是当我更换新创建的花卉模型时,它总是崩溃。以下是日志:

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)

【问题讨论】:

  • "当我在 android mobile 上安装和运行时工作正常。" -> 应用程序运行时使用的原始模型是什么?输入输出类型和形状是否与新模型相同?
  • @PannagSanketi 我已经更新了详细信息,如果您需要任何其他详细信息,请告诉我
  • 谢谢。我想我找到了问题所在。 PTAL 在我下面的回答中。

标签: java android tensorflow tensorflow-lite


【解决方案1】:

问题是您通过toco --input_data_type=FLOAT 将TF 模型转换为float tflite 模型,而tflite poets 2 appByteBuffer 输入提供给模型(它将图像Bitmap 转换为ByteBuffer )。最初它使用 quantized 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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多