【问题标题】:How to restore tensorflow inceptions checkpoint file (ckpt)?如何恢复 tensorflow 初始检查点文件(ckpt)?
【发布时间】:2017-04-28 17:43:32
【问题描述】:

我有 inception_resnet_v2_2016_08_30.ckpt 文件,这是一个预训练的初始模型。我想恢复这个模型使用

saver.restore(sess, ckpt_filename)

但为此,我需要编写在训练此模型时使用的变量集。我在哪里可以找到这些(脚本或详细说明)?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    首先,您已经了解了内存中的网络架构。网络架构可以从here获取

    一旦你有了这个程序,使用以下方法来使用模型:

    from inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope
    
    height = 299
    width = 299
    channels = 3
    
    X = tf.placeholder(tf.float32, shape=[None, height, width, channels])
    with slim.arg_scope(inception_resnet_v2_arg_scope()):
         logits, end_points = inception_resnet_v2(X, num_classes=1001,is_training=False)
    

    这样你就拥有了内存中的所有网络,现在你可以使用 tf.train.saver 使用检查点文件(ckpt)初始化网络:

    saver = tf.train.Saver()
    sess = tf.Session()
    saver.restore(sess, "/home/pramod/Downloads/inception_resnet_v2_2016_08_30.ckpt")
    

    如果你想做瓶子提取,很简单,比如你想从最后一层获取特征,那么你只需声明predictions = end_points["Logits"] 如果你想为其他中间层获取它,你可以从上面的程序 inception_resnet_v2.py 中获取这些名称

    之后您可以拨打:output = sess.run(predictions, feed_dict={X:batch_images})

    【讨论】:

    • 您需要import tensorflow.contrib.slim as slim 才能使用slim.arg_scope。
    【解决方案2】:

    我相信MetaGraph mechanism 是您所需要的。

    编辑:另外,看看tf.train.NewCheckpointReader——它有一个get_variable_to_shape_map() 方法。见unit test。

    【讨论】:

    • 谢谢,但 MetaGraph 有助于加载模型定义。我目前只有一个检查点模型,它只包含训练过的权重和偏差的值(没有关于模型架构等的信息)
    猜你喜欢
    • 2017-07-30
    • 2021-04-03
    • 2019-04-03
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多