卷積
卷積函數為:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
input為一個4-D的輸入,fileter為濾波器(卷積核),4-D,通常為[height, width, input_dim, output_dim],height, width分別表示卷積核的高,寬.input_dim, output_dim分別表式輸入維度,輸出維度.
import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov, x2_cov = sess.run([y1, y2])print(x1_cov.shape)print(x2_cov.shape)
反卷積
反卷積函數為:
tf.nn.conv2d_transpose(value, filter, output_shape, strides, padding="SAME", data_format="NHWC", name=None)
output_shape為輸出shape,由於是卷積的反過程,因此這裡filter的輸入輸出為維度位置調換,[height, width, output_channels, in_channels].
import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3], strides=[1,2,2,1],padding="SAME")y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3], strides=[1,2,2,1],padding="SAME")sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov, x2_cov,y1_decov,y2_decov = sess.run([y1, y2,y3,y4])print(x1_cov.shape)print(x2_cov.shape)print(y1_decov.shape)print(y2_decov.shape)
注意output_shape需要根據input shape,filter shape,以及output_dim指定,但不可以隨意指定,例如
y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,5,5,3], strides=[1,2,2,1],padding="SAME")
得到y4 shape為(1, 5, 5, 3),但是若設定output_shape=[1,7,7,3],
y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,7,7,3], strides=[1,2,2,1],padding="SAME")
則出錯:
InvalidArgumentError (see above for traceback): Conv2DSlowBackpropInput: Size of out_backprop doesn’t match computed: actual = 3, computed = 4
[[Node: conv2d_transpose_1 = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](conv2d_transpose_1/output_shape, Const_2, Conv2D_1)]] [[Node: conv2d_transpose/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_23_conv2d_transpose", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
空洞卷積(dilated convolution):
空洞卷積函數為:
tf.nn.atrous_conv2d(value, filters, rate, padding, name=None)
fileter為濾波器(卷積核),格式與卷積相同,為[height, width, input_dim, output_dim].rate為對輸入的採樣步長(sample stride).
x1 = tf.constant(1.0, shape=[1, 5, 5, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')
y5.shape為(1, 5, 5, 1).
完整調用代碼為:
import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3], strides=[1,2,2,1],padding="SAME")y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3], strides=[1,2,2,1],padding="SAME")y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov, x2_cov,y1_decov,y2_decov,y5_dicov = sess.run([y1, y2,y3,y4,y5])print(x1_cov.shape)print(x2_cov.shape)print(y1_decov.shape)print(y2_decov.shape)print(y5_dicov.shape)