我正在尝试使用 TensorFlow 后端对 Keras 模型构建的推理阶段的性能进行基准测试。我当时想的是张量流基准测试工具是正确的方法。
我已经成功地在桌面上构建并运行了示例tensorflow_inception_graph.pb
一切似乎都运行良好。
我似乎无法弄清楚如何将 Keras 模型保存为正确的模型.pb
模型。我可以从 Keras 模型获取 TensorFlow Graph,如下所示:
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>
我现在一直在尝试以几种不同的方式保存模型。
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)
这会生成一个文件夹,其中包含一些我不知道如何处理的文件。
我现在将运行基准测试工具,如下所示
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()!