ani*_*an7 5 android tensorflow tensorflow-lite deeplab
我们正在尝试使用deeplabv3和mobilenetv2在android上运行语义分割模型,并在bazel的帮助下使用TOCO和tflite_convert遵循了官方的tensorflow lite转换过程。
我们能够使用以下命令成功转换模型:
CUDA_VISIBLE_DEVICES =“ 0” toco --output_file = toco256.tflite --graph_def_file = path / to / deeplab / deeplabv3_mnv2_pascal_trainval / frozen_inference_graph.pb --input_arrays = ImageTensor --output_arrays = SemanticPredictions --Input_shape,= 3,mn_2 --inference_type = FLOAT-平均值= 128 --std_dev_values = 127 --allow_custom_ops --post_training_quantize
tflite文件的大小约为2.25 Mb,但是当我们尝试使用官方基准工具测试模型时,它失败并显示以下错误报告:-
bazel run -c opt tensorflow/contrib/lite/tools/benchmark:benchmark_model -- --graph=`realpath toco256.tflite`
INFO: Analysed target //tensorflow/contrib/lite/tools/benchmark:benchmark_model (0 packages loaded).
INFO: Found 1 target...
Target //tensorflow/contrib/lite/tools/benchmark:benchmark_model up-to-date:
bazel-bin/tensorflow/contrib/lite/tools/benchmark/benchmark_model
INFO: Elapsed time: 0.154s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/tensorflow/contrib/lite/tools/benchmark/benchmark_model '--graph=path/to/deeplab/venINFO: Build completed successfully, 1 total action
STARTING!
Num runs: [50]
Inter-run delay (seconds): [-1]
Num threads: [1]
Benchmark name: []
Output prefix: []
Warmup runs: [1]
Graph: path/to/venv/tensorflow/toco256.tflite]
Input layers: []
Input shapes: []
Use nnapi : [0]
Loaded model path/to/venv/tensorflow/toco256.tflite
resolved reporter
Initialized session in 45.556ms
Running benchmark for 1 iterations
tensorflow/contrib/lite/kernels/pad.cc:96 op_context.dims != 4 (3 != 4)
Node number 24 (PAD) failed to prepare.
Failed to invoke!
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
我们还尝试了相同的命令,但不包括'allow_custom_ops'和'post_training_quantize'选项,甚至使用了与1,513,513,3相同的输入大小;但是结果是一样的。
这个问题似乎类似于以下github问题:(https://github.com/tensorflow/tensorflow/issues/21266)。但是,在最新版本的TensorFlow中,该问题应该已解决。
型号:http : //download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz Tensorflow版本:1.11 Bazel版本:0.17.2操作系统:Ubuntu 18.04
另外,Android应用程序无法正确加载模型(tflite解释器)
那么,如何将细分模型正确转换为可在Android设备上进行推理的tflite格式?
更新:-
使用tensorflow 1.12,我们得到了一个新的错误:
$ bazel run -c opt tensorflow/lite/tools/benchmark:benchmark_model -- --graph=`realpath /path/to/research/deeplab/venv/tensorflow/toco256.tflite`
tensorflow/lite/kernels/depthwise_conv.cc:99 params->depth_multiplier * SizeOfDimension(input, 3) != SizeOfDimension(filter, 3) (0 != 32)
Node number 30 (DEPTHWISE_CONV_2D) failed to prepare.
Run Code Online (Sandbox Code Playgroud)
此外,当使用来自Tensorflow DeepLab 模型Zoo的 depth_multiplier = 0.5的同一模型的较新版本(3 Mb .pb文件)时,我们得到了不同的错误:-
F tensorflow/lite/toco/graph_transformations/propagate_fixed_sizes.cc:116] Check failed: dim_x == dim_y (3 vs. 32)Dimensions must match
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们使用了与上述相同的命令进行tflite转换;但是我们甚至无法生成一个'tflite'文件作为输出,这似乎与深度倍增值有关(即使我们尝试将depth_multiplier参数用作参数也是如此)转换时)。
小智 5
我也遇到了这个问题。转换中似乎有两个问题:
对于下面的解决方案,这是在以下方面进行测试的:
我假设您已经使用 deeplab 文件夹中的 export_model.py 文件创建了一个 .pb 文件,并将该文件命名为 deeplab_mobilenet_v2.pb。从这里开始:
python3 optimize_for_inference.py \
--input "path/to/your/deeplab_mobilenet_v2.pb" \
--output "path/to/deeplab_mobilenet_v2_opt.pb" \
--frozen_graph True \
--input_names ImageTensor \
--output_names SemanticPredictions \
--placeholder_type_enum=4
Run Code Online (Sandbox Code Playgroud)
placeholder_type_enum=4 是 uint8 数据类型 (dtypes.uint8.as_datatype_enum)
确保你已经安装了 bazel 并从 github 下载了 tensorflow r1.15 分支。然后从 tensorflow repo 制作 transform_graph 工具:
bazel build tensorflow/tools/graph_transforms:transform_graph
然后运行 transform_graph 工具(确保将形状设置为您用作输入的任何形状):
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph="/path/to/deeplab_mobilenet_v2_opt.pb" \
--out_graph="/path/to/deeplab_mobilenet_v2_opt_flatten.pb" \
--inputs='ImageTensor' \
--outputs='SemanticPredictions' \
--transforms='
strip_unused_nodes(type=quint8, shape="1,400,225,3")
flatten_atrous_conv
fold_constants(ignore_errors=true, clear_output_shapes=false)
fold_batch_norms
fold_old_batch_norms
remove_device
sort_by_execution_order'
Run Code Online (Sandbox Code Playgroud)
运行下面的python 文件,确保将model_filepath、save_folder 和save_name 更改为适合您需要的任何内容。
python3 optimize_for_inference.py \
--input "path/to/your/deeplab_mobilenet_v2.pb" \
--output "path/to/deeplab_mobilenet_v2_opt.pb" \
--frozen_graph True \
--input_names ImageTensor \
--output_names SemanticPredictions \
--placeholder_type_enum=4
Run Code Online (Sandbox Code Playgroud)
tflite_convert \
--graph_def_file="/path/to/deeplab_mobilenet_v2_opt_flatten_static.pb" \
--output_file="/path/to/deeplab_mobilenet_v2_opt_flatten_static.tflite" \
--output_format=TFLITE \
--input_shape=1,400,225,3 \
--input_arrays="ImageTensor" \
--inference_type=FLOAT \
--inference_input_type=QUANTIZED_UINT8 \
--std_dev_values=128 \
--mean_values=128 \
--change_concat_input_ranges=true \
--output_arrays="SemanticPredictions" \
--allow_custom_ops
Run Code Online (Sandbox Code Playgroud)
您现在可以运行您的 tflite 模型
| 归档时间: |
|
| 查看次数: |
1752 次 |
| 最近记录: |