结合两个端到端模型的Tensorflow

use*_*665 1 tensorflow

在tensorflow中,通过使用检查点将经过训练的模型加载回tensorflow相当容易。但是,此用例似乎面向希望在检查点模型上运行评估或进行其他培训的用户。

张量流中加载预训练模型并使用它(无需训练)产生结果然后将其用于新模型的最简单方法是什么?

现在,似乎最有前途的方法是tf.get_tensor_by_name()和tf.stop_gradient(),以便从tf.train.import_meta_graph()获得加载的训练模型的输入和输出张量。

什么是最好的做法,设置了这样的事情?

Mat*_*ugi 5

最直接的解决方案是使用以下函数冻结预训练的模型变量:

def freeze_graph(model_dir, output_node_names):
    """Extract the sub graph defined by the output nodes and convert
    all its variables into constant
    Args:
        model_dir: the root folder containing the checkpoint state file
        output_node_names: a string, containing all the output node's names,
                           comma separated
    """
    if not tf.gfile.Exists(model_dir):
        raise AssertionError(
            "Export directory doesn't exist")

    if not output_node_names:
        print("You need to supply the name of the output node)
        return -1

    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_dir)
    input_checkpoint = checkpoint.model_checkpoint_path

    # We precise the file fullname of our freezed graph
    absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])

    # We clear devices to allow TensorFlow to control on which device it will load operations
    clear_devices = True

    # We start a session using a temporary fresh Graph
    with tf.Session(graph=tf.Graph()) as sess:
        # We import the meta graph in the current default Graph
        saver = tf.train.import_meta_graph(args.meta_graph_path, clear_devices=clear_devices)

        # We restore the weights
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constants
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            sess,  # The session is used to retrieve the weights
            tf.get_default_graph().as_graph_def(),  # The graph_def is used to retrieve the nodes
            output_node_names.split(",")  # The output node names are used to select the usefull nodes
        )
    return frozen_graph
Run Code Online (Sandbox Code Playgroud)

然后,您将能够在预先训练的模型之上构建新模型:

# Get the frozen graph
frozen_graph = freeze_graph(YOUR_MODEL_DIR, YOUR_OUTPUT_NODES)

# Set the frozen graph as a default graph
frozen_graph.as_default()

# Get the output tensor from the pre-trained model
pre_trained_model_result = frozen_graph.get_tensor_by_name(OUTPUT_TENSOR_NAME_OF_PRETRAINED_MODEL)

# Let's say you want to get the pre trained model result's square root
my_new_operation_results = tf.sqrt(pre_trained_model_result)
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很棒的解决方案,谢谢。我从没想过导入图形然后将其转换为常量。 (2认同)