Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualizing filter weights in tf.layers.conv2d

Tags:

tensorflow

I am using tf.layers.conv2d in TensorFlow V1.0 to do convolution.

An example is as follows :

conv1 = tf.layers.conv2d(batch_images, filters=96,
                                     kernel_size=7,
                                     strides=2,
                                     activation=tf.nn.relu,
                                     kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d(uniform=False),
                                     bias_initializer=tf.contrib.layers.xavier_initializer(uniform=False),
                                     kernel_regularizer=tf.nn.l2_loss,
                                     bias_regularizer=tf.nn.l2_loss,
                                     name='conv1')

I then try to collect the filter weights as follows :-

l1weights = tf.get_collection(tf.GraphKeys.WEIGHTS, 'conv1')

However although the network is getting trained, I get [] on evaluating l1weights inside a session.

How do I extract the filter weights and visualize them using tf.summary.image ?

like image 670
Ujjwal Avatar asked Apr 18 '17 19:04

Ujjwal


1 Answers

I managed to get the weights using the following

conv1 = tf.layers.conv2d(
    inputs=input_layer,
    filters=32,
    kernel_size=[5, 5],
    padding="same",
    activation=tf.nn.relu, name='conv1')

kernel = tf.get_collection(tf.GraphKeys.VARIABLES, 'conv1/kernel')[0]
bias = tf.get_collection(tf.GraphKeys.VARIABLES, 'conv1/bias')[0]

Hope it helps.

like image 50
azmath Avatar answered Oct 03 '22 11:10

azmath