TensorFlow - 使用toco

时间:2018-05-12 09:02:48

标签: android tensorflow tensorflow-lite

我使用以下示例创建张量流模型:http://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/ 您可以从此处下载代码:https://github.com/sankit1/cv-tricks.com/tree/master/Tensorflow-tutorials/tutorial-2-image-classifier 我也使用" 2。冻结图表"来自http://cv-tricks.com/how-to/freeze-tensorflow-models/的部分 创建我的模型的* .pb文件。 我尝试使用toco命令行工具转换* .pb文件,如"将TensorFlow SavedModel转换为TensorFlow Lite"在https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/cmdline_examples.md#savedmodel 并得到他跟随错误:

(venv)user @ user-desktop:〜/ PycharmProjects / tensorflow_tutorial / tensorflow $ bazel run -c opt tensorflow / contrib / lite / toco:toco - --savedmodel_directory = / home / user / PycharmProjects / tensorflow_tutorial / tutorial -2-image-classifier --output_file = / home / user / PycharmProjects / tensorflow_tutorial / tutorial-2-image-classifier / dogs-cats-model.tflite 警告:/home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/external/protobuf_archive/WORKSPACE:1:在/home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/external/protobuf_archive/WORKSPACE工作区名(@com_google_protobuf)与存储库定义(@protobuf_archive)中给出的名称不匹配;这将导致未来版本中的构建错误 信息:分析目标// tensorflow / contrib / lite / toco:toco(已加载0个包)。 信息:找到1个目标...... 警告:未能为前缀&#39; bazel创建一个或多个便利符号链接 - &#39;: 无法创建符号链接bazel-out - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-out(文件存在) 无法创建符号链接bazel-out - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-out(文件存在) 无法创建符号链接bazel-tensorflow - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-tensorflow(文件存在) 无法创建符号链接bazel-bin - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out/k8-opt/bin:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-bin(文件存在) 无法创建符号链接bazel-testlogs - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out/k8-opt/testlogs:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-testlogs(文件存在) 无法创建符号链接bazel-genfiles - &gt; /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out/k8-opt/genfiles:/ home / user / PycharmProjects / tensorflow_tutorial / tensorflow / bazel-genfiles(File exists) 目标// tensorflow / contrib / lite / toco:toco是最新的: /home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/contrib/lite/toco/toco 信息:经过时间:0.271s,关键路径:0.00s 信息:0进程。 信息:构建成功完成,总共1次操作 信息:运行命令行:/home/user/.cache/bazel/_bazel_user/e21a56d90e65395c94952f8aa3d0c4bc/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/contrib/lite/toco/toco' - savedmodel_directory = /家庭/用户/ PycharmProjects / tensorflow_tutorial /教程-2-图像分类器&#39; &#39; - OUTPUT_FILE = /家庭/用户/ PycharmProjects / tensorflow_tutorial /教程-2-图像分类器/狗-CATS-model.tflite&#39; 2018-05-07 01:33:13.776954:F tensorflow / contrib / lite / toco / toco_saved_model.cc:34] 检查失败:tensorflow :: MaybeSavedModelDirectory(model_path)模型未以受支持的SavedModel格式保存。< /强>

抛出此错误的函数是MaybeSavedModelDirectory at https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/toco_saved_model.cc, 我看了一下它的实现 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/cc/saved_model/loader.cc 实际上它在模型目录中寻找* .pb或* .pbtxt文件,我在请求的位置获得了这个文件,为什么我会收到这个错误?

机器细节: 操作系统平台和分发 - ubuntu x64, TensorFlow从 - pip安装, TensorFlow版本 - cpu版本1.8.0, Bazel版 - 0.13.0, CUDA / cuDNN版本 - 没有cuda, GPU模型和内存 - 没有gpu, 准确的命令重现 - 没有必要, python版本 - 3.5.2

2 个答案:

答案 0 :(得分:0)

TensorFlow 1.8支持两种格式:

  1. SavedModels
  2. 通过freeze_graph.py生成的冻结GraphDefs

在您的情况下,如果您已经使用过freeze_graph.py,则应该遵循有关GraphDefs的文档。可从here获得TensorFlow Lite的最新文档。

从(TensorFlow 1.9的文档)复制:

以下示例将基本的TensorFlow GraphDef(由freeze_graph.py冻结)转换为TensorFlow Lite FlatBuffer以执行浮点推理。冻结图包含以Const ops形式存储在Checkpoint文件中的变量。

curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_0.50_128_frozen.tgz \
  | tar xzv -C /tmp
tflite_convert \
  --output_file=/tmp/foo.tflite \
  --graph_def_file=/tmp/mobilenet_v1_0.50_128/frozen_graph.pb \
  --input_arrays=input \
  --output_arrays=MobilenetV1/Predictions/Reshape_1

input_shapes的值会在可能的情况下自动确定。

答案 1 :(得分:0)

而不是使用toco,而是使用Colab将您的.pb转换为.lite,如下所述:

https://stackoverflow.com/a/58583419/11517841

解释为什么这样做会更简单。