在Google Cloud中部署模型:创建版本失败。模型验证失败:SavedModel必须包含一个带有tag:serve的元图

时间:2018-02-09 12:02:29

标签: tensorflow google-cloud-ml

我尝试在Google Cloud中部署本地培训的模型。我训练模型并保存图形.pb并量化模型。但是,现在我尝试部署它,我收到以下错误:

Create Version failed. Model validation failed: SavedModel 
must contain exactly one metagraph with tag: serve

有谁知道如何将标签添加到模型中?

2 个答案:

答案 0 :(得分:1)

    input_node = tf.placeholder(tf.float32,
                            shape=(None, spec.crop_size, spec.crop_size, spec.channels),
                            name='input')
    y_ = tf.placeholder('float', shape=[None, 2])

    # Create builder
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    tensor_info_x = utils.build_tensor_info(input_node)
    tensor_info_y = utils.build_tensor_info(y_)


    prediction_signature = signature_def_utils.build_signature_def(
            inputs={'input': tensor_info_x},
            outputs={'output': tensor_info_y},
            method_name=signature_constants.PREDICT_METHOD_NAME)

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
            prediction_signature
        },
        legacy_init_op=legacy_init_op)

    # Save the SavedModel to disk.
    builder.save()

答案 1 :(得分:0)

由于Zapata的答案仍然使人们难以想象。这是我的代码:

    with tf.Session() as sess:
        # init = tf.initialize_all_variables()
        init = tf.global_variables_initializer()
        input_x = sess.graph.get_tensor_by_name("0:0")  # input
        outputs1 = sess.graph.get_tensor_by_name("add_10:0")
        output_tf_pb = sess.run(
            [outputs1], feed_dict={input_x: np.random.randn(1, 3, 64, 64)}
        )
        # output_tf_pb = sess.run([outputs1, outputs2], feed_dict= 
    {input_x:np.random.randn(1, 3, 224, 224)})
        print("output_tf_pb = {}".format(output_tf_pb))

    # os.removedirs("output2")
    builder = tf.saved_model.builder.SavedModelBuilder("output2")
    prediction_signature = 
    tf.saved_model.signature_def_utils.build_signature_def(
        inputs={"images": tf.saved_model.utils.build_tensor_info(input_x)},
        outputs={"scores": tf.saved_model.utils.build_tensor_info(outputs1)},
        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME,
    )
    tensor_info_x = tf.saved_model.utils.build_tensor_info(input_x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(outputs1)

    classification_signature = tf.saved_model.signature_def_utils.build_signature_def(
        inputs={tf.saved_model.signature_constants.CLASSIFY_INPUTS: tensor_info_x},
        outputs={
            tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES: tensor_info_y
        },
        method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME,
    )

    builder.add_meta_graph_and_variables(
        sess,
        [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            "predict_images": prediction_signature,
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: classification_signature,
        },
        main_op=tf.tables_initializer(),
    )
    builder.save()

完整文件位于:

https://gist.github.com/takotab/ccd131fb06d4cdd2b786b3ef83c62ade