5

让我们从头开始。到目前为止,我自己已经在 Tensorflow 中创建并训练了小型网络。在训练期间,我保存我的模型并在我的目录中获取以下文件:

model.ckpt.meta
model.ckpt.index
model.ckpt.data-00000-of-00001

稍后,我加载保存的模型network_dir进行一些分类并提取模型的可训练变量。

saver = tf.train.import_meta_graph(network_dir + ".meta")
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="NETWORK")

现在我想使用更大的预训练模型,如 VGG16 或 ResNet,并希望使用我的代码来做到这一点。我想加载预训练模型,如我自己的网络,如上所示。

在这个网站上,我发现了许多预训练模型:

https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models

我下载了 VGG16 检查点,发现这些只是训练好的参数。

我想知道如何或在哪里可以获得这些预训练网络的保存模型或图形结构?例如,如何使用不带model.ckpt.meta,model.ckpt.indexmodel.ckpt.data-00000-of-00001文件的 VGG16 检查点?

4

1 回答 1

2

在权重链接旁边,有指向定义模型的代码的链接。例如,对于 VGG16:代码。使用代码创建模型并从检查点恢复变量:

import tensorflow as tf

slim = tf.contrib.slim

image = ...  # Define your input somehow, e.g with placeholder
logits, _ = vgg.vgg_16(image)
predictions = tf.argmax(logits, 1)
variables_to_restore = slim.get_variables_to_restore()

saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
    saver.restore(sess, "/path/to/model.ckpt")

因此,vgg.py中包含的代码将为您创建所有变量。使用 tf-slim 帮助程序,您可以获得列表。然后,只需按照通常的程序。对此有一个类似的问题

于 2019-02-08T15:02:10.877 回答