exporting pb graph for tensorflow serve

829 views Asked by At

I'm trying to export my retrained inception graph (pb file) for tensorflow serve since I was not able to find any snippets for exporting pb file I had to create my own but obviously I'm doing something wrong since I'm getting "No variables to save" error, I would appreciate any help

Update: after researching more I think I need meta graph to supply variables but retrain.py doesn't give me meta graph, any ideas ?

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

There are 0 answers