-
-
Save kkk324/9f994afe450690e31c2c78f59dcad6c0 to your computer and use it in GitHub Desktop.
tf tensorflow atrous convolution aka dilated convolution test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| import numpy as np | |
| dim = 256 | |
| kernel_dim = 3 | |
| dilation_rate = np.array([2, 2]) | |
| input_img_np = np.random.random((1, dim, dim, 1)).astype(np.float32) | |
| kernel = np.random.random((kernel_dim,kernel_dim,1,1)).astype(np.float32) | |
| with tf.Session() as sess: | |
| concrete_input_op = tf.constant(input_img_np) | |
| concrete_output_op = tf.nn.convolution(concrete_input_op, kernel, padding='SAME', dilation_rate=dilation_rate) | |
| concrete_output = sess.run(concrete_output_op) | |
| print('convolution + CONCRETE + SAME') | |
| print('concrete_input_op: ', concrete_input_op.get_shape()) | |
| print('concrete_output_op: ', concrete_output_op.get_shape()) | |
| print('concrete_output:', concrete_output.shape) | |
| assert(concrete_input_op.get_shape() == concrete_output_op.get_shape()) | |
| undef_input_op = tf.placeholder(tf.float32, shape=(None, dim, dim, 1)) | |
| undef_output_op = tf.nn.convolution(undef_input_op, kernel, padding='SAME', dilation_rate=dilation_rate) | |
| undef_output = sess.run(undef_output_op, feed_dict={undef_input_op: input_img_np}) | |
| print('convolution + UNDEF + SAME') | |
| print('undef_input_op: ', undef_input_op.get_shape()) | |
| print('undef_output_op: ', undef_output_op.get_shape()) | |
| print('undef_output:', undef_output.shape) | |
| # This assert will correctly fail even though the shapes are ok because shapes are only partially known | |
| # assert(undef_input_op.get_shape() == undef_output_op.get_shape()) | |
| valid_concrete_input_op = tf.constant(input_img_np) | |
| valid_concrete_output_op = tf.nn.convolution(valid_concrete_input_op, kernel, padding='VALID', dilation_rate=dilation_rate) | |
| valid_concrete_output = sess.run(valid_concrete_output_op) | |
| print('convolution + CONCRETE + VALID') | |
| print('valid_concrete_input_op: ', valid_concrete_input_op.get_shape()) | |
| print('valid_concrete_output_op: ', valid_concrete_output_op.get_shape()) | |
| print('valid_concrete_output:', valid_concrete_output.shape) | |
| valid_undef_input_op = tf.placeholder(tf.float32, shape=(None, dim, dim, 1)) | |
| valid_undef_output_op = tf.nn.convolution(valid_undef_input_op, kernel, padding='VALID', dilation_rate=dilation_rate) | |
| valid_undef_output = sess.run(valid_undef_output_op, feed_dict={valid_undef_input_op: input_img_np}) | |
| print('convolution + UNDEF + VALID') | |
| print('valid_undef_input_op: ', valid_undef_input_op.get_shape()) | |
| print('valid_undef_output_op: ', valid_undef_output_op.get_shape()) | |
| print('valid_undef_output:', valid_undef_output.shape) | |
| # This assert will correctly fail even though the shapes are ok because shapes are only partially known | |
| # assert(undef_input_op.get_shape() == undef_output_op.get_shape()) | |
| ############################################################################ | |
| # Now atrous | |
| concrete_input_op = tf.constant(input_img_np) | |
| concrete_output_op = tf.nn.atrous_conv2d(concrete_input_op, kernel, padding='SAME', rate=2) | |
| concrete_output = sess.run(concrete_output_op) | |
| print('atrous_conv2d + CONCRETE + SAME') | |
| print('concrete_input_op: ', concrete_input_op.get_shape()) | |
| print('concrete_output_op: ', concrete_output_op.get_shape()) | |
| print('concrete_output_op: ', concrete_output_op.get_shape()) | |
| print('concrete_output:', concrete_output.shape) | |
| assert(concrete_input_op.get_shape() == concrete_output_op.get_shape()) | |
| undef_input_op = tf.placeholder(tf.float32, shape=(None, dim, dim, 1)) | |
| undef_output_op = tf.nn.atrous_conv2d(undef_input_op, kernel, padding='SAME', rate=2) | |
| undef_output = sess.run(undef_output_op, feed_dict={undef_input_op: input_img_np}) | |
| print('atrous_conv2d + UNDEF + SAME') | |
| print('undef_input_op: ', undef_input_op.get_shape()) | |
| print('undef_output_op: ', undef_output_op.get_shape()) | |
| print('undef_output:', undef_output.shape) | |
| # This assert will correctly fail even though the shapes are ok because shapes are only partially known | |
| # assert(undef_input_op.get_shape() == undef_output_op.get_shape()) | |
| valid_concrete_input_op = tf.constant(input_img_np) | |
| valid_concrete_output_op = tf.nn.atrous_conv2d(valid_concrete_input_op, kernel, padding='VALID', rate=2) | |
| valid_concrete_output = sess.run(valid_concrete_output_op) | |
| print('atrous_conv2d + CONCRETE + VALID') | |
| print('valid_concrete_input_op: ', valid_concrete_input_op.get_shape()) | |
| print('valid_concrete_output_op: ', valid_concrete_output_op.get_shape()) | |
| print('valid_concrete_output:', valid_concrete_output.shape) | |
| valid_undef_input_op = tf.placeholder(tf.float32, shape=(None, dim, dim, 1)) | |
| valid_undef_output_op = tf.nn.atrous_conv2d(valid_undef_input_op, kernel, padding='VALID', rate=2) | |
| valid_undef_output = sess.run(valid_undef_output_op, feed_dict={valid_undef_input_op: input_img_np}) | |
| print('atrous_conv2d + UNDEF + VALID') | |
| print('valid_undef_input_op: ', valid_undef_input_op.get_shape()) | |
| print('valid_undef_output_op: ', valid_undef_output_op.get_shape()) | |
| print('valid_undef_output:', valid_undef_output.shape) | |
| # This assert will correctly fail even though the shapes are ok because shapes are only partially known | |
| # assert(undef_input_op.get_shape() == undef_output_op.get_shape()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| convolution + CONCRETE + SAME | |
| ('concrete_input_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('concrete_output_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('concrete_output:', (1, 256, 256, 1)) | |
| convolution + UNDEF + SAME | |
| ('undef_input_op: ', TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('undef_output_op: ', TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('undef_output:', (1, 256, 256, 1)) | |
| convolution + CONCRETE + VALID | |
| ('valid_concrete_input_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('valid_concrete_output_op: ', TensorShape([Dimension(1), Dimension(252), Dimension(252), Dimension(1)])) | |
| ('valid_concrete_output:', (1, 252, 252, 1)) | |
| convolution + UNDEF + VALID | |
| ('valid_undef_input_op: ', TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('valid_undef_output_op: ', TensorShape([Dimension(None), Dimension(252), Dimension(252), Dimension(1)])) | |
| ('valid_undef_output:', (1, 252, 252, 1)) | |
| atrous_conv2d + CONCRETE + SAME | |
| ('concrete_input_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('concrete_output_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('concrete_output_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('concrete_output:', (1, 256, 256, 1)) | |
| atrous_conv2d + UNDEF + SAME | |
| ('undef_input_op: ', TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('undef_output_op: ', TensorShape([Dimension(None), Dimension(None), Dimension(None), Dimension(1)])) | |
| ('undef_output:', (1, 256, 256, 1)) | |
| atrous_conv2d + CONCRETE + VALID | |
| ('valid_concrete_input_op: ', TensorShape([Dimension(1), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('valid_concrete_output_op: ', TensorShape([Dimension(1), Dimension(252), Dimension(252), Dimension(1)])) | |
| ('valid_concrete_output:', (1, 252, 252, 1)) | |
| atrous_conv2d + UNDEF + VALID | |
| ('valid_undef_input_op: ', TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])) | |
| ('valid_undef_output_op: ', TensorShape([Dimension(None), Dimension(None), Dimension(None), Dimension(1)])) | |
| ('valid_undef_output:', (1, 252, 252, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment