Tensorflow 反卷积

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)

输出:

transpose_conv_1

[[[[ 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的卷积过程见下图:

Tensorflow 反卷积_第1张图片

transpose_conv_2

[[[[ 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.]]]]

transpose_conv_3

[[[[ 1.]
[ 2.]
[ 2.]
[ 1.]]

[[ 2.]
[ 4.]
[ 4.]
[ 2.]]

[[ 2.]
[ 4.]
[ 4.]
[ 2.]]

[[ 1.]
[ 2.]
[ 2.]
[ 1.]]]]

stride=1,padding=VALID的卷积过程见下图:

Tensorflow 反卷积_第2张图片

transpose_conv_4

[[[[ 4.]
[ 4.]]

[[ 4.]
[ 4.]]]]


参考资料:
1. http://deeplearning.net/software/theano_versions/dev/tutorial/conv_arithmetic.html#transposed-convolution-arithmetic

你可能感兴趣的:(tensorflow)