tf代码之tf.nn.conv2d_transpose

摘自博客:https://blog.csdn.net/mao_xiao_feng/article/details/71713358

conv2d_transpose(value, filter, output_shape, strides, padding="SAME", data_format="NHWC", name=None)

除去name参数用以指定该操作的name,与方法有关的一共六个参数:

  • 第一个参数value:指需要做反卷积的输入图像,它要求是一个Tensor
  • 第二个参数filter:卷积核,它要求是一个Tensor,具有[filter_height, filter_width, out_channels, in_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,卷积核个数,图像通道数]
  • 第三个参数output_shape:反卷积操作输出的shape,细心的同学会发现卷积操作是没有这个参数的,那这个参数在这里有什么用呢?下面会解释这个问题
  • 第四个参数strides:反卷积时在图像每一维的步长,这是一个一维的向量,长度4
  • 第五个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式,默认为SAME
  • 第六个参数data_format:string类型的量,'NHWC'和'NCHW'其中之一,这是tensorflow新版本中新加的参数,它说明了value参数的数据格式。'NHWC'指tensorflow标准的数据格式[batch, height, width, in_channels],'NCHW'指Theano的数据格式,[batch, in_channels,height, width],当然默认值是'NHWC'

tf.nn.conv2d      中的filter参数,是[filter_height, filter_width, in_channels, out_channels]

tf.nn.conv2d_transpose    中的filter参数,是[filter_height, filter_width, out_channels,in_channels]

 

x1 = tf.constant(1.0, shape=[1,3,3,1])
x2 = tf.constant(1.0, shape=[1,6,6,3]) #6×6的3通道图
x3 = tf.constant(1.0, shape=[1,5,5,3]) #5×5的3通道图
kernel = tf.constant(1.0, shape=[3,3,3,1])

y2 = tf.nn.conv2d(x3, kernel, strides=[1,2,2,1], padding="SAME")
y3 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,5,5,3], strides=[1,2,2,1],padding="SAME")
y4 = tf.nn.conv2d(x2, kernel, strides=[1,2,2,1], padding="SAME")

'''
Wrong!!This is impossible
y5 = tf.nn.conv2d_transpose(x1,kernel,output_shape=[1,10,10,3],strides=[1,2,2,1],padding="SAME")
'''
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)
x1_decov, x3_cov, y2_decov, x2_cov=sess.run([y1,y2,y3,y4])
print(x1_decov.shape)
print(x3_cov.shape)
print(y2_decov.shape)
print(x2_cov.shape)

返回的y2是[1,3,3,1]的Tensor,是一个单通道的图

对y2做conv2d_transpose,返回的Tensor和x3的shape是一样的[1,5,5,3]

x2做卷积,获得y4的shape也为[1,3,3,1]。

说明[1,3,3,1]的图反卷积产生了两种情况。所以这里指定output_shape是有意义的,当然随意指定output_shape是不允许的如y5

 

 

 

 

 

 


 

 

猜你喜欢

转载自blog.csdn.net/infinita_LV/article/details/86130485
今日推荐