卷积
卷积函数为:
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 tf
x1 = 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 tf
x1 = 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 tf
x1 = 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)