导出张量流服务的pb图

时间:2016-12-30 12:21:40

标签: python tensorflow tensorflow-serving

我正在尝试导出我的再培训初始图(pb文件)for tensorflow serve,因为我无法找到导出pb文件的任何片段我必须创建自己的但显然我做错了,因为我'我得到“没有变量保存”错误,我将不胜感激任何帮助

更新:经过更多的研究我认为我需要元图来提供变量,但是reconin.py不会给我元图,任何想法?

import tensorflow as tf,sys

label_lines = [line.rstrip() for line
                   in tf.gfile.GFile("retrained_labels.txt")]

graph = None

with tf.gfile.FastGFile("retrained_graph.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    graph = tf.import_graph_def(graph_def, name='')

with tf.Session(graph=graph) as sess:
    output_tensor = sess.graph.get_tensor_by_name('final_result:0')
    input_tensor = sess.graph.get_tensor_by_name('DecodeJpeg/contents:0')

    mapping_string = tf.constant( label_lines )
    indices = tf.constant( [0,len(label_lines)-1], tf.int64 )

    prediction_classes = tf.contrib.lookup.index_to_string( indices, mapping=mapping_string )
    export_path = sys.argv[1]

    print('Exporting trained model to %s' % export_path)

    init_op = tf.group(tf.initialize_all_tables(), name='init_op')
    saver = tf.train.Saver(sharded=True)
    model_exporter = exporter.Exporter(saver)
    model_exporter.init(
        sess.graph.as_graph_def(),
        init_op=init_op,
        default_graph_signature=exporter.classification_signature(
            input_tensor=input_tensor,
            classes_tensor=prediction_classes,
            scores_tensor=output_tensor),
        named_graph_signatures={
            'inputs': exporter.generic_signature({'images': 'final_result:0'}),
            'outputs': exporter.generic_signature({'scores': 'DecodeJpeg/contents:0'})})

    model_exporter.export(export_path, tf.constant(1), sess)
    print('Done exporting!')

0 个答案:

没有答案