5

操作系统平台和发行版:Linux Ubuntu 14.04 TensorFlow 版本:来自二进制的 tensorflow (1.4.0),CUDA/cuDNN 版本:cuda 8.0

我已经使用 tensorflow 训练了一个自定义模型,并且我正在尝试将其作为移动应用程序的 tensorflow lite 模型。我的模型定义如下:

def P_Net(inputs,label=None,bbox_target=None,landmark_target=None,training=True):
    #define common param
    with slim.arg_scope([slim.conv2d],
                        activation_fn=prelu,
                        weights_initializer=slim.xavier_initializer(),
                        biases_initializer=tf.zeros_initializer(),
                        weights_regularizer=slim.l2_regularizer(0.0005), 
                        padding='valid'):
        print inputs.get_shape()
        net = slim.conv2d(inputs, 28, 3, stride=1,scope='conv1')
......
        conv4_1 = slim.conv2d(net,num_outputs=2,kernel_size=[1,1],stride=1,scope='conv4_1',activation_fn=tf.nn.softmax)
        #conv4_1 = slim.conv2d(net,num_outputs=1,kernel_size=[1,1],stride=1,scope='conv4_1',activation_fn=tf.nn.sigmoid)
        
        print conv4_1.get_shape()
        #batch*H*W*4
        bbox_pred = slim.conv2d(net,num_outputs=4,kernel_size=[1,1],stride=1,scope='conv4_2',activation_fn=None)
        print bbox_pred.get_shape()

其中 conv4_1 和 conv4_2 是输出层。

我冻结模型:

freeze_graph.freeze_graph('out_put_model/model.pb', '', False, model_path, 'Squeeze,Squeeze_1', '', '', 'out_put_model/frozen_model.pb', '', '')

之后,我可以使用 tensorboard 来查看图表。当我读回来仔细检查时,我得到了与检查点模型相同的信息。

然后,我尝试将 frozen_model.pb 保存到 tensorflow lite 模型。虽然 tensorflow 1.4.0 没有 tensorflow lite 模块,但我从 github 和 bazel run toco 签出了 tensorflow,如下所示:

bazel run --config=opt   //tensorflow/contrib/lite/toco:toco --   --input_file='/home/sens/mtcnn_cat/MTCNN-Tensorflow/test/out_put_model/frozen_model.pb'    --output_file='/home/sens/mtcnn_cat/MTCNN-Tensorflow/test/out_put_model/pnet.tflite'    --inference_type=FLOAT   --input_shape=1,128,128,3   --input_array=image_height,image_width,input_image   --output_array=Squeeze,Squeeze_1  --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --dump_graphviz=/tmp

但是,我收到有关未找到输出数组的错误:

INFO: Running command line: bazel-bin/tensorflow/contrib/lite/toco/toco '--input_file=/home/sens/mtcnn_cat/MTCNN-Tensorflow/test/out_put_model/frozen_model.pb' '--output_file=/home/sens/mtcnn_cat/MTCNN-Tensorflow/test/out_put_model/pnet.tflite' '--inference_type=FLOAT' '--input_shape=1,128,128,3' '--input_array=image_height,image_width,input_image' '--output_array=Squeeze,Squeeze_1' '--input_format=TENSORFLOW_GRAPHDEF' '--output_format=TFLITE' '--dump_graphviz=/tmp'
2018-04-03 11:17:37.412589: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1172] Converting unsupported operation: Abs
2018-04-03 11:17:37.412660: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1172] Converting unsupported operation: Abs
2018-04-03 11:17:37.412699: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1172] Converting unsupported operation: Abs
2018-04-03 11:17:37.412880: F tensorflow/contrib/lite/toco/tooling_util.cc:686] Check failed: model.HasArray(output_array) Output array not found: Squeeze,Squeeze_1

问题:

  1. 如何设置--output_array=Squeeze,Squeeze_1参数?我认为它与张量板中的输出节点相同freeze_graph(),我确实找到了“Squeeze”和“Squeeze_1”节点在此处输入图像描述

  2. 如何设置--input_shape=1,128,128,3 --input_array=image_height,image_width,input_image参数?我检查并发现手机确实有固定大小的图像输入,但在我的模型中,输入图像的大小和完全卷积输入没有固定大小,例如:

         self.image_op = tf.placeholder(tf.float32, name='input_image')
         self.width_op = tf.placeholder(tf.int32, name='image_width')
         self.height_op = tf.placeholder(tf.int32, name='image_height')
         image_reshape = tf.reshape(self.image_op, [1, self.height_op, self.width_op, 3])
    

并重塑为 1高*3 在此处输入图像描述

那么,如何将其写为输入形状?

4

4 回答 4

3

对于输入数组

[node.op.name for node in model.inputs]

对于输出数组

[node.op.name for node in model.outputs]
于 2018-11-09T08:14:22.413 回答
1

我在尝试重新训练然后转换为 tflite 时遇到了这个问题。

这是对我有用的解决方案:

使用 1.9 及更高版本(可能还有 1.8,尚未测试。)您需要删除并将参数--input_format field更改为--input_file--graph_def_file

所以你最终得到一个看起来有点像的命令:

toco \
  --graph_def_file=tf_files/retrained_graph.pb \
  --output_file=tf_files/optimized_graph.lite \
  --output_format=TFLITE \
  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \
  --input_array=input \
  --output_array=final_result \
  --inference_type=FLOAT \
  --inference_input_type=FLOAT

然后我能够完成诗人的例子,并让我的 tflite 文件在 android 上工作。

来源: https ://github.com/googlecodelabs/tensorflow-for-poets-2/issues/68

于 2018-08-02T12:09:47.113 回答
1

多亏了 tensorflow,将冻结模型转换为 tf_lite 从来都不是一件容易的事。希望这段代码可以帮助您总结图表并帮助您找到输出和输入数组

bazel-bin/tensorflow/tools/graph_transforms/summarize_graph --in_graph={PATH_TO_FROZEN_GRAPH}/optimized_best.pb`
于 2018-07-18T01:17:43.823 回答
1

您可以使用以下工具来确定输入和输出数组和模型大小以及其他用于 tflite 转换的参数。这也为 tensorflow 冻结图创建了一个很好的可视化。 该工具的 Github 链接

于 2020-05-21T19:07:08.387 回答