【问题标题】:loading pre-trained vgg-16 on tensorflow在张量流中加载预训练的 vgg16
【发布时间】:2018-08-15 14:30:53
【问题描述】:

我正在尝试使用 tensorflow r1.1 加载预训练的 vgg-16 网络。网络在 3 个文件中提供:

  • saved_model.pb
  • 变量/variables.index
  • variables/variables.data-00000-of-00001

将变量sess初始化为tf.Session()

我使用以下脚本加载网络并提取一些特定层:

vgg_path='./'
model_filename = os.path.join(vgg_path, "saved_model.pb")
export_dir = os.path.join(vgg_path, "variables/")

with gfile.FastGFile(model_filename, 'rb') as f:
    data = compat.as_bytes(f.read())
    sm = saved_model_pb2.SavedModel()
    sm.ParseFromString(data)
    image_input, l7, l4, l3 = tf.import_graph_def(sm.meta_graphs[0].graph_def, 
            name='',return_elements=["image_input:0", "layer7_out:0",
            "layer4_out:0", "layer3_out:0"])

tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, image_input)
tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, l7)
tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, l4)
tf.add_to_collection(tf.GraphKeys.GLOBAL_VARIABLES, l3)

saver = tf.train.Saver(tf.global_variables())
print("load data")
saver.restore(sess, export_dir)

初始化变量saver时,脚本终止并出现以下错误:

TypeError: 要保存的变量不是变量:Tensor("image_input:0", shape=(?, ?, ?, 3), dtype=float32)

如何修复我的脚本并恢复预训练的 vgg 网络?

【问题讨论】:

标签: tensorflow vgg-net


【解决方案1】:

既然你有一个SavedModel,你可以使用tf.saved_model.loader来加载它:

with tf.Session() as sess:
    tf.saved_model.loader.load(sess, ["some_tag"], model_dir)

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多