【问题标题】:Initializing weights from inception_v3.ckpt in tensorflow在张量流中从 inception_v3.ckpt 初始化权重
【发布时间】:2018-01-29 17:48:59
【问题描述】:

在 tensorflow 中,我需要从 inception_v3 预训练模型中加载权重,以便在以下代码中使用:

with tf.variable_scope(scope, "InceptionV3", [images]) as scope:
            with slim.arg_scope(
                [slim.conv2d, slim.fully_connected],
                weights_regularizer=weights_regularizer,
                trainable=False):
                with slim.arg_scope(
                    [slim.conv2d],
                    weights_initializer=tf.truncated_normal_initializer(stddev=stddev),
                    activation_fn=tf.nn.relu,
                    normalizer_fn=slim.batch_norm,
                    normalizer_params=batch_norm_params):
                    net, end_points = inception_v3_base(images, scope=scope)
                with tf.variable_scope("logits"):
                    shape = net.get_shape()
                    net = slim.avg_pool2d(net, shape[1:3], padding="VALID", scope="pool")
                    net = slim.dropout(
                            net,
                            keep_prob=dropout_keep_prob,
                            is_training=False,
                            scope="dropout")
                    net = slim.flatten(net, scope="flatten")

    image_embeddings = tf.contrib.layers.fully_connected(
                    inputs=net,
                                num_outputs=512,
                                activation_fn=None,
                                weights_initializer=initializer,
                                biases_initializer=None,
                                scope=scope)

怎么可能做到这一点?可以举个简单的例子吗?

上面的代码中有两个权重初始化器。我不知道我必须在哪一个初始化模型中的权重,以及如何初始化?

谢谢,

【问题讨论】:

  • 您只有一个.ckpt 文件还是更多? (例如,.meta.pbtxt/.pb
  • 我只有一个 .ckpt 文件,@GPhilo。
  • 您能否发布您获得检查点的页面的链接?它是来自 Tensorflow 模型动物园的预训练模型之一吗?
  • 好的,该检查点的匹配 Tensorflow 模型在检查点附带的python file 中定义。我假设您已经下载了(如果没有,请下载并将其放在与您的脚本相同的文件夹中)

标签: python tensorflow


【解决方案1】:

TL;DR:阅读下面列表中的第三点。

关于如何恢复模型的冗长通用说明

当您需要从检查点加载权重时,您需要匹配的模型定义才能在尝试恢复权重之前定义图形。这是必要的,因为检查点文件只包含变量的值,它没有关于图形本身结构的信息

模型结构可以通过不同方式检索:

  • 检查点带有匹配的.meta 文件。在这种情况下,导入元图,然后通过以下方式恢复权重:

    new_saver = tf.train.import_meta_graph('my-save-dir/my-model-10000.meta')
    new_saver.restore(sess, 'my-save-dir/my-model-10000')
    
  • 检查点附带一个匹配的.pb/.pbtxt 文件,其中包含序列化的GraphDef。在这种情况下,从其定义中加载图形,然后恢复权重:

    • 对于.pbtxt

      with open('graph.pbtxt', 'r') as f:
          graph_def = tf.GraphDef()
          file_content = f.read()
          text_format.Merge(file_content, graph_def)
          tf.import_graph_def(graph_def, name='')
      saver = tf.train.Saver() # note: it is important that this is defined AFTER you import the graph definition or it won't find any variables in the graph to restore
      saver.restore(sess, "/tmp/model.ckpt")
      
    • 对于.pb

      with gfile.FastGFile('graph.pb','rb') as f:
          graph_def = tf.GraphDef()
          graph_def.ParseFromString(f.read())
          tf.import_graph_def(graph_def, name='')
      saver = tf.train.Saver() # note: it is important that this is defined AFTER you import the graph definition or it won't find any variables in the graph to restore
      saver.restore(sess, "/tmp/model.ckpt")
      
  • 检查点附带一个包含模型定义的匹配 python 文件。在这种情况下,请通读该文件的文档并找到定义模型所需调用的函数。然后,在您的脚本中,导入函数,在定义 saver 之前调用它,然后从检查点恢复变量的值:

    from inception_v3 import inception_v3
    
    logits, endpoints = inception_v3()
    saver = tf.train.Saver() # as above, it is important that this is defined after you define the graph, or it won't find any variables.
    saver.restore(sess, 'inception_v3.ckpt')
    

    注意:对于这种情况,您需要在保存检查点时调用函数完全(除非您有选择地恢复某些变量),否则还原将失败并出现错误。

【讨论】:

  • 很好,但这不是我问题的答案。我想在我给出的代码中使用这个权重作为初始化。我需要嵌入图像。
  • 但这你的问题的答案。恢复权重后,所有变量都将初始化为从检查点获取的值。之后,您可以对它们做任何您需要的事情。只需 sess.run 恢复权重后要计算其值的张量,您将拥有嵌入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多