1

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

此处提供的 Android 应用程序

当我在 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 格式存在问题(对于安卓)

4

2 回答 2

1

问题是您正在将 TF 模型转换为floattflite 模型,toco --input_data_type=FLOATtflite Pots 2 应用程序ByteBuffer输入提供给模型(它将图像转换Bitmap为 a ByteBuffer)。最初它使用了一个量化的mobilenet tflite 模型,该模型期望Byte输入。然而,当你用你的模型替换它时,模型开始期待float,但应用程序喂了它Byte。因此它崩溃了。

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

所以如果你想回到你的第一个应用程序,你可以先在 tensorflow 中量化你重新训练的图,然后告诉toco你的输入是quantized(参见这里的 toco 命令行示例),然后再试一次。

(此脚本来自 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

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

于 2018-06-04T19:11:48.527 回答
0

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

于 2018-06-03T16:43:16.197 回答