Example 5

This project is just for exploring how convolution computation is done in tensorflow, and check how to use different padding methods(same, valid), that should have an effect for the dimension of the output matrix/tensor

Also, there is some background knowledge about the reshape function. That is tricky to make convolution, since sometimes it will affect how it works(or dimension mismatch error will occur if not in the right way)


In [40]:
import tensorflow as tf
import numpy as np

In [41]:
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=1)
    return tf.Variable(initial, tf.int32)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding = 'SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

In [47]:
patch1 = tf.Variable([[0., 1., 2.], [3., 4., 5.], [1., 2., 3.]], tf.float32)
#conv_weight = tf.Variable([[-1., 0.], [0., 1.]], tf.float32)

W_conv1 = weight_variable([2, 2, 1, 1])
x_image = tf.reshape(patch1, [-1, 3, 3, 1])

In [62]:
a = np.random.randint(2, size=(3,3))
k = [[1,1],[1,1]]

tensor_a = tf.constant(a, tf.float32)
tensor_k = tf.constant(k, tf.float32)

# first is 3x3, first 1 dimension is the remaining, last 1 is the color channels, there is only 1 
# for tensor_k, first two dimension is width x height, and first 1 is the input channel, last one is the output channel
tensor_res = tf.nn.convolution(tf.reshape(tensor_a, [1, 3, 3, 1]), tf.reshape(tensor_k, [2, 2, 1, 1]), padding='VALID')

"""
result for `VALID` padding, that should only contain the pixel inside it

('X_a: ', array([[ 1.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 1.,  1.,  0.]], dtype=float32))
('X_k: ', array([[ 1.,  1.],
       [ 1.,  1.]], dtype=float32))
('X_res: ', array([[[[ 1.],
         [ 0.]],

        [[ 2.],
         [ 1.]]]], dtype=float32))
"""


Out[62]:
"\nresult for `VALID` padding, that should only contain the pixel inside it\n\n('X_a: ', array([[ 1.,  0.,  0.],\n       [ 0.,  0.,  0.],\n       [ 1.,  1.,  0.]], dtype=float32))\n('X_k: ', array([[ 1.,  1.],\n       [ 1.,  1.]], dtype=float32))\n('X_res: ', array([[[[ 1.],\n         [ 0.]],\n\n        [[ 2.],\n         [ 1.]]]], dtype=float32))\n"

In [57]:
a = np.random.randint(2, size=(3,3))
k = [[1,1],[1,1]]

tensor_a = tf.constant(a, tf.float32)
tensor_k = tf.constant(k, tf.float32)

tensor_res = tf.nn.convolution(tf.reshape(tensor_a, [1, 3, 3, 1]), tf.reshape(tensor_k, [2, 2, 1, 1]), padding='SAME')

"""
result for `SAME` padding, that can be outside, so dimension is the same as input layer


('X_a: ', array([[ 1.,  1.,  0.],
       [ 1.,  1.,  0.],
       [ 0.,  1.,  1.]], dtype=float32))
('X_k: ', array([[ 1.,  1.],
       [ 1.,  1.]], dtype=float32))
('X_res: ', array([[[[ 4.],
         [ 2.],
         [ 0.]],

        [[ 3.],
         [ 3.],
         [ 1.]],

        [[ 1.],
         [ 2.],
         [ 1.]]]], dtype=float32))
"""

In [58]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    result = conv2d(x_image, W_conv1)
    #print(sess.run(x_image))
    #print(sess.run(W_conv1))
    #print(sess.run(result))
    print("X_a: ", sess.run(tensor_a))
    print("X_k: ", sess.run(tensor_k))
    print("X_res: ", sess.run(tensor_res))


('X_a: ', array([[ 1.,  1.,  0.],
       [ 1.,  1.,  0.],
       [ 0.,  1.,  1.]], dtype=float32))
('X_k: ', array([[ 1.,  1.],
       [ 1.,  1.]], dtype=float32))
('X_res: ', array([[[[ 4.],
         [ 2.],
         [ 0.]],

        [[ 3.],
         [ 3.],
         [ 1.]],

        [[ 1.],
         [ 2.],
         [ 1.]]]], dtype=float32))

In [ ]:

Another example for using reshape and convolution


In [ ]:
patch1 = tf.Variable([[0., 1., 2.], [3., 4., 5.], [1., 2., 3.]], tf.float32)
#conv_weight = tf.Variable([[-1., 0.], [0., 1.]], tf.float32)

conv_weight = tf.Variable([[-1., 0.], [0., 1.]], tf.float32)
x_image = tf.reshape(patch1, [-1, 3, 3, 1])
W_resize = tf.reshape(conv_weight, [2, 2, 1, 1])

In [ ]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print("x_image: ", sess.run(x_image))
    print("W_rezie: ", sess.run(W_resize))
    print("Result: ", sess.run(tf.nn.conv2d(x_image, W_resize, strides = [1, 1, 1, 1], padding = 'VALID')))

Results for the result:

('x_image: ', array([[[[ 0.],
         [ 1.],
         [ 2.]],

        [[ 3.],
         [ 4.],
         [ 5.]],

        [[ 1.],
         [ 2.],
         [ 3.]]]], dtype=float32))
('W_rezie: ', array([[[[-1.]],

        [[ 0.]]],


       [[[ 0.]],

        [[ 1.]]]], dtype=float32))
('Result: ', array([[[[ 4.],
         [ 4.]],

        [[-1.],
         [-1.]]]], dtype=float32))

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:

Appendix

Reshapes a tensor.

Given tensor, this operation returns a tensor that has the same values as tensor with shape shape.

If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.

If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.


In [59]:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
                        [4, 5, 6],
                        [7, 8, 9]]

# tensor 't' is [[[1, 1], [2, 2]],
#                [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
                        [3, 3, 4, 4]]

# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]

# -1 can also be used to infer the shape

# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
                              [2, 2, 2],
                              [3, 3, 3]],
                             [[4, 4, 4],
                              [5, 5, 5],
                              [6, 6, 6]]]

# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7


  File "<ipython-input-59-daf3c3904a8e>", line 3
    reshape(t, [3, 3]) ==> [[1, 2, 3],
                         ^
SyntaxError: invalid syntax

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: