der_radler
der_radler

Reputation: 579

Computing outputs from conv2d in tensorflow

I would like to intuitively understand the tf.nn.conv2d() function in tensorflow. Reason i post this since when the inputs are single dimensional array the convolution is tricky to interpret. Here is an example.

Say i have a [1x5] input feature vector.

d = [x for x in range(1,6)]
print('Inputs:', d)

Now reshape and flatten into the tensor.

d = tf.reshape(d, shape=[-1,1,5,1])
print ("New shape : ",d.get_shape().as_list())

New shape : [1, 1, 5, 1]

# convert inputs to float32
d = tf.cast(d,tf.float32)

Applying filters with size [2x2] and then convolve with strides as 1.

w1 = np.random.randint(5, size=(2,2))
w1 = tf.reshape(w1, shape=[2,2,1,1])
w1 = tf.cast(w1, tf.float32)

# Apply conv layer   
d_conv = tf.nn.conv2d(d, w1, strides=[1,1,1,1], padding='SAME')  

# run session
with tf.Session() as sess:
 sess.run(tf.global_variables_initializer())
 print('\nFilter: ', w1.eval())
 print('\nShape after convolution:',d_conv.eval().shape)
 print('\nConv output:',d_conv.eval()) 

Outputs.

Inputs: [1, 2, 3, 4, 5]

Filter:  [[[[ 3.]]

  [[ 1.]]]


 [[[ 2.]]

  [[ 3.]]]]

Shape after convolution: (1, 1, 5, 1)

Conv output: [[[[  5.]
   [  9.]
   [ 13.]
   [ 17.]
   [ 15.]]]]

Analysis

The output is a result of convolving filter [[[ 3.]] [[ 1.]]]] with the input d with stride 1 and the last element after 5 is zero padded. Looks like the filter elements [[[ 2.]] [[ 3.]]]] where never applied.

Would be nice if someone can explain what is going on. Best would be to increase the filter size w1 and figure out, ie, w1 = np.random.randint(10, size=(3,3)), w1 = tf.reshape(w1, shape=[3,3,1,1]) but i still get weird outputs.

Thanks a lot.

Upvotes: 0

Views: 900

Answers (1)

dm0_
dm0_

Reputation: 2156

According to the documentation padding SAME involves the following:

out_height = ceil(float(in_height) / float(strides[1]))
out_width  = ceil(float(in_width) / float(strides[2]))

if (in_height % strides[1] == 0):
  pad_along_height = max(filter_height - strides[1], 0)
else:
  pad_along_height = max(filter_height - (in_height % strides[1]), 0)
if (in_width % strides[2] == 0):
  pad_along_width = max(filter_width - strides[2], 0)
else:
  pad_along_width = max(filter_width - (in_width % strides[2]), 0)

pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left

In your example

filter_height = 2
filter_width = 2
in_height = 1 
in_width = 5
strides = [1 1 1 1]

This results in following paddings:

pad_top = 0
pad_bottom = 1
pad_left = 0
pad_right = 1

Zero-padded input (channel and batch dimensions omitted):

1 2 3 4 5 0
0 0 0 0 0 0

Convolving with kernel (channels dimensions omitted)

3 1
2 3

Gives the following:

[ 1 ( 2 ][ 3 )( 4 ][ 5 ) 0 ]
[ 0 ( 0 ][ 0 )( 0 ][ 0 ) 0 ]

[ 3   1 ][ 3    1 ][ 3   1 ]
[ 2   3 ][ 2    3 ][ 2   3 ]

    ( 3    1 )( 3    1 )
    ( 2    3 )( 2    3 )

[5] (9)  [13] (17) [15]

Here the same type of braces show location in the input data convolved with the kernel. For example:

[ 1  2 ]   [ 3  1 ]   
         *           =  [5]
[ 0  0 ]   [ 2  3 ]


( 2  3 )   ( 3  1 )  
         *           =  (9)
( 0  0 )   ( 2  3 )

And so on.

Upvotes: 1

Related Questions