1

我有一个关于从 tensorflow.contrib.slim.nets 的 vgg 中排除前两层的问题。

如您所知 tensorflow.contrib.slim.nets.vgg,

 def vgg16(inputs):
    with slim.arg_scope([slim.conv2d, slim.fully_connected],
                      activation_fn=tf.nn.relu,
                      weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),
                      weights_regularizer=slim.l2_regularizer(0.0005)):
    net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1')
    net = slim.max_pool2d(net, [2, 2], scope='pool1')
    net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2')
    net = slim.max_pool2d(net, [2, 2], scope='pool2')
    net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3')
    net = slim.max_pool2d(net, [2, 2], scope='pool3')
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4')
    net = slim.max_pool2d(net, [2, 2], scope='pool4')
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5')
    net = slim.max_pool2d(net, [2, 2], scope='pool5')
    net = slim.fully_connected(net, 4096, scope='fc6')
    net = slim.dropout(net, 0.5, scope='dropout6')
    net = slim.fully_connected(net, 4096, scope='fc7')
    net = slim.dropout(net, 0.5, scope='dropout7')
    net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc8')
  return net

我想要的是net = slim.fully_connected(net, 4096, scope='fc7')的输出,而不是net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc8')

那么无论如何要从中删除 dropout7 和 fc8 吗?

这是一个使用 tf.slim 的 vgg 的简单代码;请让我知道如何在此示例中删除这些内容。

import tensorflow as tf
import tensorflow.contrib.slim.nets as nets
import cv2
import numpy as np

slim = tf.contrib.slim
vgg = nets.vgg

image = cv2.imread('girl.jpg')
image = cv2.resize(image, (224, 224))
image = np.reshape(image, (1, 224, 224, 3)).astype(float)
predictions, _ = vgg.vgg_16(image) # the 'predictions' is not I want.
4

1 回答 1

1

可以使用end_pointsslim 框架中定义的来实现:

定义 VGG 图

vgg_graph = tf.Graph()
with vgg_graph.as_default():

   images = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])

   with slim.arg_scope(vgg.vgg_arg_scope()):
      outputs, end_points = vgg.vgg_16(images, is_training=False)

   fc7 = end_points['vgg_16/fc7'] 

将权重加载到图表中

with tf.Session(graph=vgg_graph) as sess:
   saver = tf.train.Saver()
   saver.restore(sess, '/home/vijay/datasets/pre_trained_models/vgg_16.ckpt')

   # pass images through the network
   fc7_output = sess.run(fc7, feed_dict={images:np.random.normal(size=(1, 224, 224, 3))})

   print(np.squeeze(fc7_output).shape) 
   #Output:(4096,)
于 2018-05-24T17:12:16.470 回答