【问题标题】:How to verify optimized model in tensorflow如何在张量流中验证优化模型
【发布时间】:2018-09-05 08:58:03
【问题描述】:

我正在关注tutorial from codelabs。他们使用这个脚本来优化模型

python -m tensorflow.python.tools.optimize_for_inference \
  --input=tf_files/retrained_graph.pb \
  --output=tf_files/optimized_graph.pb \
  --input_names="input" \
  --output_names="final_result"

他们使用此脚本验证optimized_graph.pb

python -m scripts.label_image \
    --graph=tf_files/optimized_graph.pb \
    --image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg

问题是我尝试将optimize_for_inference 用于我自己的代码,而不是用于图像分类。

以前,在优化之前,我使用此脚本通过对样本数据进行测试来验证我的模型:

import tensorflow as tf
from tensorflow.contrib import predictor
from tensorflow.python.platform import gfile
import numpy as np

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name="prefix")

    input_name = graph.get_operations()[0].name+':0'
    output_name = graph.get_operations()[-1].name+':0'

    return graph, input_name, output_name

def predict(model_path, input_data):
    # load tf graph
    tf_model,tf_input,tf_output = load_graph(model_path)

    x = tf_model.get_tensor_by_name(tf_input)
    y = tf_model.get_tensor_by_name(tf_output) 

    model_input = tf.train.Example(
        features=tf.train.Features(feature={
        "thisisinput": tf.train.Feature(float_list=tf.train.FloatList(value=input_data)),
    }))
    model_input = model_input.SerializeToString()

    num_outputs = 3
    predictions = np.zeros(num_outputs)
    with tf.Session(graph=tf_model) as sess:
        y_out = sess.run(y, feed_dict={x: [model_input]})
        predictions = y_out

    return predictions

if __name__=="__main__":
    input_data = [4.7,3.2,1.6,0.2] # my model recieve 4 inputs
    print(np.argmax(predict("not_optimized_model.pb",x)))

但优化模型后,我的测试脚本不起作用。它引发了一个错误:

ValueError: 节点导入/ParseExample/ParseExample 的输入 0 是 从导入/输入张量传递的浮点数:0 与预期不兼容 字符串。

所以我的问题是优化模型后如何验证我的模型?我不能像教程那样使用--image 命令。

【问题讨论】:

  • 找到解决方案后,您可以提交自己问题的答案。

标签: python tensorflow tensorflow-estimator


【解决方案1】:

我已通过在导出模型时将占位符的类型更改为 tf.float32 解决了该错误:

def my_serving_input_fn():
    input_data = {
        "featurename" : tf.placeholder(tf.float32, [None, 4], name='inputtensors')
    }
    return tf.estimator.export.ServingInputReceiver(input_data, input_data)

然后把上面的prediction函数改成:

def predict(model_path, input_data):
    # load tf graph
    tf_model, tf_input, tf_output = load_graph(model_path)

    x = tf_model.get_tensor_by_name(tf_input)
    y = tf_model.get_tensor_by_name(tf_output) 

    num_outputs = 3
    predictions = np.zeros(num_outputs)
    with tf.Session(graph=tf_model) as sess:
        y_out = sess.run(y, feed_dict={x: [input_data]})
        predictions = y_out

    return predictions

冻结模型后,上面的预测代码将起作用。但不幸的是it raises another error when trying to load pb directly after exporting the model

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2018-08-25
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    相关资源
    最近更新 更多