1

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

  • 保存模型.pb
  • 变量/变量.index
  • 变量/variables.data-00000-of-00001

将变量初始化sesstf.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 网络?

4

1 回答 1

1

由于您有SavedModel,您可以使用tf.saved_model.loader来加载它:

with tf.Session() as sess:
    tf.saved_model.loader.load(sess, ["some_tag"], model_dir)
于 2017-06-12T16:24:47.250 回答