简体   繁体   中英

How to have multiple outputs in a tensorflow SavedModel?

So the whole story is that I am trying to convert a.pb frozen inference graph to a tflite model, and to do that I am first trying to create a SavedModel. Here is the code that I am trying to use below:

with tf.Session(graph=tf.Graph()) as sess:
    # name="" is important to ensure we don't get spurious prefixing
    tf.import_graph_def(graph_def, name="")
    g = tf.get_default_graph()
    inp = g.get_tensor_by_name("image_tensor:0")

    out = {{g.get_tensor_by_name('num_detections:0')}, {g.get_tensor_by_name('detection_boxes:0')},{g.get_tensor_by_name('detection_scores:0')},{g.get_tensor_by_name('detection_classes:0')}}


    sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
        tf.saved_model.signature_def_utils.predict_signature_def(
            {"inputs": inp}, {"outputs": out})

    builder.add_meta_graph_and_variables(sess,
                                         [tag_constants.SERVING],
                                         signature_def_map=sigs)

builder.save()

I am not doing the 'out' correctly, however I do not know how to include more outputs for a SavedModel signature, or if it is even possible?

I think instead of feeding a collection to the output dictionary, you need to specify all the individual output tensors in that one dictionary. Something like this worked for me.

with tf.Session(graph=tf.Graph()) as sess:
    # name="" is important to ensure we don't get spurious prefixing
    tf.import_graph_def(graph_def, name="")
    g = tf.get_default_graph()
    inp = g.get_tensor_by_name("input_image:0")
    out1 = g.get_tensor_by_name("output_1:0")
    out2 = g.get_tensor_by_name("output_2:0")
    out3 = g.get_tensor_by_name("output_3:0")
    sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
        tf.saved_model.signature_def_utils.predict_signature_def(
            {"in": inp}, {"out1": out1, "out2": out2, "out3": out3 })

    builder.add_meta_graph_and_variables(sess,
                                         [tag_constants.SERVING],
                                         signature_def_map=sigs)

    builder.save()

I am still learning TF as well, but hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM