4

I use slim.conv2d to set up VGG-net

with slim.arg_scope([slim.conv2d, slim.max_pool2d], padding='SAME'):
    conv1_1 = slim.conv2d(img, 64, [3, 3], scope='conv1')
    conv1_2 = slim.conv2d(conv1_1, 64, [3, 3], scope='conv1_1')
    pool1 = slim.max_pool2d(conv1_2, [2, 2], 2, scope='pool1_2')

    conv2_1 = slim.conv2d(pool1, 128, [3, 3], 1, scope='conv2_1')
    conv2_2 = slim.conv2d(conv2_1, 128, [3, 3], 1, scope='conv2_2')
    pool2 = slim.max_pool2d(conv2_2, [2, 2], 2, scope='pool2')

    conv3_1 = slim.conv2d(pool2, 256, [3, 3], 1, scope='conv3_1')
    conv3_2 = slim.conv2d(conv3_1, 256, [3, 3], 1, scope='conv3_2')
    conv3_3 = slim.conv2d(conv3_2, 256, [3, 3], 1, scope='conv3_3')
    pool3 = slim.max_pool2d(conv3_3, [2, 2], 2, scope='pool3')

    conv4_1 = slim.conv2d(pool3, 512, [3, 3], scope='conv4_1')
    # print conv4_1.shape
    conv4_2 = slim.conv2d(conv4_1, 512, [3, 3], scope='conv4_2')
    conv4_3 = slim.conv2d(conv4_2, 512, [3, 3], scope='conv4_3')  # 38

If I want to initialize the variables of conv1 or conv2 from existing VGG-model.

How can I do it?

stop-cran
  • 4,229
  • 2
  • 30
  • 47
Frank Mouzrt
  • 57
  • 1
  • 5

3 Answers3

2

You can also use assign_from_values as suggested here: Github - Initialize layers.convolution2d from numpy array

sess = tf.Session()
with sess.as_default():

    init = tf.global_variables_initializer()
    sess.run(init)

    path = pathlib.Path('./assets/classifier_weights.npz')
    if(path.is_file()):
        print("Initilize Weights from Numpy Array")
        init_weights = np.load(path)
        assign_op, feed_dict_init = slim.assign_from_values({
            'conv1/weights' : init_weights['conv1_w'],
        })
        sess.run(assign_op, feed_dict_init)
GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
Jodo
  • 4,515
  • 6
  • 38
  • 50
0

I'm assuming you have a checkpoint of your existing VGG model.

One way to do this using TF Slim is to restore from a checkpoint, but specify a custom mapping between the variable names in the checkpoint and variables in your model. See the comments here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/learning.py#L146

Peter Hawkins
  • 3,201
  • 19
  • 17
0

I've replaced the slim.conv2d with tf.nn.conv2d(input, kernel...), where the kernel was created using tf.get_variable, and assigned using tf.assign.

Tal
  • 127
  • 1
  • 7