Tenssorflow中提供了tf.nn.conv2d_tranpose()接口实现反卷积。
关于反卷积的实现:
import tensorflow as tf
filter=tf.constant(1.0, shape=[3, 3, 1, 1])
x1=tf.constant(1.0,shape=[1,5,5,1])
x2=tf.constant(1.0,shape=[1,2,2,1])
conv_1=tf.nn.conv2d(x1, filter, strides=[1, 2, 2, 1], padding='SAME')
transpose_conv_1=tf.nn.conv2d_transpose(conv_1, filter, output_shape=[1, 5, 5, 1],
strides=[1,2,2,1], padding='SAME')
transpose_conv_2=tf.nn.conv2d_transpose(conv_1, filter, output_shape=[1, 7, 7, 1],
strides=[1,2,2,1], padding='VALID')
transpose_conv_3=tf.nn.conv2d_transpose(x2,filter,output_shape=[1,4,4,1],
strides=[1,1,1,1],padding='VALID')
transpose_conv_4=tf.nn.conv2d_transpose(x2,filter,output_shape=[1,2,2,1],
strides=[1,1,1,1],padding='SAME')
with tf.Session() as sess:
tf.initialize_all_variables()
print sess.run(conv_1)
print 'transpose_conv_1'
print sess.run(transpose_conv_1)
print 'transpose_conv_2'
print sess.run(transpose_conv_2)
print 'transpose_conv_3'
print sess.run(transpose_conv_3)
print 'transpose_conv_4'
print sess.run(transpose_conv_4)
[[[[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]][[ 2.]
[ 4.]
[ 2.]
[ 4.]
[ 2.]][[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]][[ 2.]
[ 4.]
[ 2.]
[ 4.]
[ 2.]][[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]]]]
stride=2,padding=SAME的卷积过程见下图:
[[[[ 1.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]][[ 1.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]][[ 2.]
[ 2.]
[ 4.]
[ 2.]
[ 4.]
[ 2.]
[ 2.]][[ 1.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]][[ 2.]
[ 2.]
[ 4.]
[ 2.]
[ 4.]
[ 2.]
[ 2.]][[ 1.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]][[ 1.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]]]]
[[[[ 1.]
[ 2.]
[ 2.]
[ 1.]][[ 2.]
[ 4.]
[ 4.]
[ 2.]][[ 2.]
[ 4.]
[ 4.]
[ 2.]][[ 1.]
[ 2.]
[ 2.]
[ 1.]]]]
stride=1,padding=VALID的卷积过程见下图:
[[[[ 4.]
[ 4.]][[ 4.]
[ 4.]]]]
参考资料:
1. http://deeplearning.net/software/theano_versions/dev/tutorial/conv_arithmetic.html#transposed-convolution-arithmetic