初始Resnet V2模型冻结

时间:2017-08-30 09:19:38

标签: python tensorflow

我使用InceptionResNet v2模型来训练使用(转移学习)的图像分类模型。我的模型运作良好。问题在于冻结模型。 目前,我有:

  • model.ckpt.meta
  • model.ckpt.index
  • model.ckpt

我使用this教程通过将output_node_names设置为 InceptionResnetV2 / Logits / Predictions 来冻结模型,并且正确生成了模型。我现在有一个名为model.pb的新文件

用于构建以冻结模型的已使用代码:

import os

import tensorflow as tf
from tensorflow.python.framework import graph_util

dir = os.path.dirname(os.path.realpath(__file__))


def freeze_graph(model_folder, output_node_names):
    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path

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

    # Before exporting our graph, we need to precise what is our output node
    # This is how TF decides what part of the Graph he has to keep and what part it can dump
    # NOTE: this variable is plural, because you can have multiple output nodes
    # output_node_names = "Accuracy/predictions"

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

    # We import the meta graph and retrieve a Saver
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    # We retrieve the protobuf graph definition
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    # We start a session and restore the graph weights
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constants
        output_graph_def = graph_util.convert_variables_to_constants(
            sess,  # The session is used to retrieve the weights
            input_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
        )

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))

当我想用输入提供此模型时,问题出现了。

首先,我使用以下方法加载模型图:

def load_graph(frozen_graph_filename):
    # We load the protobuf file from the disk and parse it to retrieve the
    # unserialized graph_def
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Then, we can use again a convenient built-in function to import a graph_def into the
    # current default Graph
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def,
            input_map=None,
            return_elements=None,
            name="prefix",
            op_dict=None,
            producer_op_list=None
        )
    return graph

然后,当我探索图操作时,我找不到输入占位符

for op in graph.get_operations():
    print(op.name)

第一个输入显示为:

前缀/批次/ fifo_queue 前缀/批次/ n的 前缀/批 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 /权重 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 /重量/读 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 /卷积 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 / BatchNorm /测试 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 / BatchNorm /测试/读 前缀/ InceptionResnetV2 / Conv2d_1a_3x3 / BatchNorm /瞬间/平均/ reduction_indices 。 。 。 的前缀/ InceptionResnetV2 / Logits /预测

使用以下方式输入图像时出现的错误:

    img_path = 'img.jpg'

    img_data = imread(img_path)
    img_data = imresize(img_data, (299, 299, 3))
    img_data = img_data.astype(np.float32)
    img_data = np.expand_dims(img_data, 0)

    # print('Starting Session, setting the GPU memory usage to %f' % args.gpu_memory)
    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory)
    # sess_config = tf.ConfigProto(gpu_options=gpu_options)
    persistent_sess = tf.Session(graph=graph)  # , config=sess_config)

    input_node = graph.get_tensor_by_name('prefix/batch/fifo_queue:0')
    output_node = graph.get_tensor_by_name('prefix/InceptionResnetV2/Logits/Predictions:0')

    predictions = persistent_sess.run(output_node, feed_dict={input_node: [img_data]})
    print(predictions)
    label_predicted = np.argmax(predictions[0])
    print(label_predicted)

错误:

 File /ImageClassification_TransferLearning System/ModelTraining/model/model_frezzing.py", line 96, in <module>
    predictions = persistent_sess.run(output_node, feed_dict={input_node: [img_data]})
  File "\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1078, in _run
    subfeed_dtype = subfeed_t.dtype.as_numpy_dtype
  File "\Anaconda3\lib\site-packages\tensorflow\python\framework\dtypes.py", line 122, in as_numpy_dtype
    return _TF_TO_NP[self._type_enum]
KeyError: 20

1 个答案:

答案 0 :(得分:0)

我发现了问题!! 我不得不从名为前缀/批次:0

的输入操作中提供模型
 input_node = graph.get_tensor_by_name('prefix/batch:0')
相关问题