使用TensforFlow Benchmark的基准Keras模型 [英] Benchmark Keras model using TensforFlow Benchmark

查看:138
本文介绍了使用TensforFlow Benchmark的基准Keras模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用TensorFlow后端的Keras模型构建的推理阶段中对性能进行基准测试.我当时认为 Tensorflow基准工具是正确的要走的路.

I'm trying to benchmark the performance in the inference phase of my Keras model build with the TensorFlow backend. I was thinking that the the Tensorflow Benchmark tool was the proper way to go.

我已经成功地使用tensorflow_inception_graph.pb在桌面上构建并运行了该示例,一切似乎都正常运行.

I've managed to build and run the example on Desktop with the tensorflow_inception_graph.pb and everything seems to work fine.

我似乎无法弄清楚如何将Keras模型保存为正确的.pb模型.我可以从Keras模型中获取TensorFlow图,如下所示:

What I can't seem to figure out is how to save the Keras model as a proper .pbmodel. I'm able to get the TensorFlow Graph from the Keras model as follows:

import keras.backend as K
K.set_learning_phase(0)

trained_model = function_that_returns_compiled_model()
sess = K.get_session()
sess.graph # This works

# Get the input tensor name for TF Benchmark
trained_model.input
> <tf.Tensor 'input_1:0' shape=(?, 360, 480, 3) dtype=float32>

# Get the output tensor name for TF Benchmark
trained_model.output
> <tf.Tensor 'reshape_2/Reshape:0' shape=(?, 360, 480, 12) dtype=float32>

我现在一直在尝试以几种不同的方式保存模型.

I've now been trying to save the model in a couple of different ways.

import tensorflow as tf
from tensorflow.contrib.session_bundle import exporter

model = trained_model
export_path = "path/to/folder"  # where to save the exported graph
export_version = 1  # version number (integer)

saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=model.input, scores_tensor=model.output)
model_exporter.init(sess.graph.as_graph_def(), default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)

哪个会生成一个文件夹,其中包含一些我不知道如何处理的文件.

Which produces a folder with some files I don't know what to do with.

我现在将使用类似这样的工具运行基准测试工具

I would now run the Benchmark tool with something like this

bazel-bin/tensorflow/tools/benchmark/benchmark_model \
  --graph=tensorflow/tools/benchmark/what_file.pb \
  --input_layer="input_1:0" \
  --input_layer_shape="1,360,480,3" \
  --input_layer_type="float" \
  --output_layer="reshape_2/Reshape:0"

但是无论我要使用哪个文件作为what_file.pb,我都会得到一个Error during inference: Invalid argument: Session was not created with a graph before Run()!

But no matter which file I'm trying to use as the what_file.pb I'm getting a Error during inference: Invalid argument: Session was not created with a graph before Run()!

推荐答案

所以我可以使用它.只需要将张量流图中的所有变量转换为常量,然后保存图定义.

So I got this to work. Just needed to convert all variables in the tensorflow graph to constants and then save graph definition.

这是一个小例子:

import tensorflow as tf

from keras import backend as K
from tensorflow.python.framework import graph_util

K.set_learning_phase(0)
model = function_that_returns_your_keras_model()
sess = K.get_session()

output_node_name = "my_output_node" # Name of your output node

with sess as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    graph_def = sess.graph.as_graph_def()
    output_graph_def = graph_util.convert_variables_to_constants(
                                                                 sess,
                                                                 sess.graph.as_graph_def(),
                                                                 output_node_name.split(","))
    tf.train.write_graph(output_graph_def,
                         logdir="my_dir",
                         name="my_model.pb",
                         as_text=False)

现在只需使用my_model.pb作为图形调用TensorFlow Benchmark工具.

Now just call the TensorFlow Benchmark tool with my_model.pb as the graph.

这篇关于使用TensforFlow Benchmark的基准Keras模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆