我们有一个预训练好(pre-trained)的VGG16模型。这个模型是在1000类的ImageNet数据集上训练的。但是,我们想要将它应用到只有20类的Pascal VOC数据集上。为了达到这个目的,我们可以用预训练好的模型的参数来初始化我们的新模型(除了最后一层):
# Load the Pascal VOC data
image, label = MyPascalVocDataLoader(...)
images, labels = tf.train.batch([image, label], batch_size=32)
# Create the model
predictions = vgg.vgg_16(images)
train_op = slim.learning.create_train_op(...)
# Specify where the Model, trained on ImageNet, was saved.
model_path = '/path/to/pre_trained_on_imagenet.checkpoint'
# Specify where the new model will live:
log_dir = '/path/to/my_pascal_model_dir/'
# Restore only the convolutional layers:
variables_to_restore = slim.get_variables_to_restore(exclude=['fc6', 'fc7', 'fc8']) #除了全连接层
init_fn = slim.assign_from_checkpoint_fn(model_path, variables_to_restore)
# Start training.
slim.learning.train(train_op, log_dir, init_fn=init_fn)