How does "TensorFlow" tf.nn.conv2d_transpose achieve deconvolution?

Source: Internet
Author: User

Three months has not updated Ah, come back a more ~ ~

CSDN mainly on some coding process encountered in the functions, problems, solutions. Partial practice

In addition, if you want to see some of the theoretical aspects, welcome to add me to know the homepage

Csdn DMS Almost do not look, there are problems Exchange can send a mailbox: hzmaoxiaofeng@163.com or know DMS, see I will be the first time to reply to you

In the previous article, someone and I asked the question of the location of the parameter, thank you very much. Before I used the TensorFlow version is 0.8, in 1.0 major changes after many parameters exchange position, so I updated the version to 1.1.0RC1, please see the code when you notice the version of the problem

/************************************************************************************************************** *****************************************/

Today to introduce the tensorflow inside the deconvolution operation, online deconvolution of the use of less introduction, I hope this tutorial can help you

Deconvolution is derived from this paper: Deconvolutional Networks, interested students to understand

First of all, no matter how you understand deconvolution, keep in mind that deconvolution is the reverse of convolution.

If you always remember the emphasis above, then you basically understand half, and then through the introduction of some functions to strengthen this concept

Conv2d_transpose (value, filter, Output_shape, strides, padding= "SAME", data_format= "NHWC", Name=none)
Drop the name parameter to specify the name of the operation, with a total of six parameters related to the method: the
first argument value: the input image that needs to do the deconvolution, which requires a tensor
second parameter filter: The convolution kernel, which requires a tensor, With a shape such as [Filter_height, Filter_width, Out_channels, In_channels], the specific meaning is [the height of the convolution nucleus, the width of the convolution nucleus, the number of convolution cores, the number of image channels]
The third parameter output_shape: deconvolution operation output shape, careful classmate will find that convolution operation is not this parameter, then this parameter is what use here. The following explains the
fourth parameter strides: Deconvolution in the image of each dimension of the step, which is a one-dimensional vector, length 4
fifth parameter padding:string type of quantity, can only be "SAME", "VALID" one of them, This value determines the number of data_format:string types for the sixth parameter of the different convolution modes
, ' NHWC ' and ' NCHW ', which is the new parameter in the new version of TensorFlow, which describes the data format of the value parameter. ' NHWC ' refers to the TensorFlow standard data format [batch, height, width, in_channels], ' nchw ' refers to the Theano data format, [batch, in_channels,height, Width], Of course the default value is ' NHWC '

before starting to understand the process of convolution, refer to my another article: http://blog.csdn.net/mao_xiao_feng/article/details/53444333
First, define a single channel graph and 3 convolution cores.
X1 = Tf.constant (1.0, shape=[1,3,3,1])
kernel = tf.constant (1.0, shape=[3,3,3,1])


Don't worry about it first. Instead of using the deconvolution function directly, we define some graphs
x2 = tf.constant (1.0, shape=[1,6,6,3])
x3 = tf.constant (1.0, shape=[1,5,5,3])


X2 is the 6x6 3-channel graph, X3 is the 5x5 3-channel graph
Okay, let's do a convolution operation on X3.
y2 = tf.nn.conv2d (x3, kernel, strides=[1,2,2,1), padding= "SAME")

So the return y2 is a single channel diagram, and if you understand the convolution process, it's easy to see that the results of the tensor,y2 of Y2 [1,3,3,1] are as follows:
[[[[[[]]
   [[A]]

  [[[
   ]] [A.] [A.]
   ]

  [[[A] [[]]
   [12.]]]]

Another important part of it. The filter parameters in the tf.nn.conv2d are in the form of [Filter_height, Filter_width, In_channels, Out_channels], and tf.nn.conv2d_ The filter parameters in the transpose are in the form of [Filter_height, Filter_width, Out_channels,in_channels], noting that in_channels and Out_channels are reversed. Because the two are reversed, the input and output are reversed.
Since Y2 is the return value of the convolution operation, then of course we can do deconvolution, and the tensor returned by the deconvolution operation should be the same as the X3 shape (not difficult to understand, because it is the reverse process of convolution).
Y3 = Tf.nn.conv2d_transpose (y2,kernel,output_shape=[1,5,5,3], strides=[1,2,2,1],padding= "SAME")


OK, now the return of the y3 is indeed [1,5,5,3] tensor, the result is as follows:
[[[[A]  .  .]
   [  .  ]
   [A.  [.]  .  ]
   [A.  .]]

  [[  .  ]
   [A  .  [A.]  .]
   [A  .  [.]  ]  ]

  [[A  .  [A.]  .]
   [A  .  .]
   [  .]
   [A.  ]

  [[.  ]] [ .  ]
   [A  .  [A.]  .]
   [A  .  [.]  ]  ]

  [[A]  .  .]
   [  .  ]
   [A.  [.]  .  ]
   [A.  12.]]]


How the result came about. You can use an action diagram to illustrate, picture source: The true meaning of deconvolution

It seems that Tf.nn.conv2d_transpose's output_shape seems superfluous, because the original image, convolution core, the step is obviously can be exported to the size of the picture, then why to specify the Output_shape it.
Look at a situation like this:
Y4 = tf.nn.conv2d (x2, Kernel, strides=[1,2,2,1), padding= "SAME")


We also do the X2 on the top and get the y4 with shape [1,3,3,1] as follows:
[[[[]]
   [A.]]

  [[
   A.] [A.] [A.]
   ]

  [[[A.]
   [
   A.] [12.]]]]


The [1,6,6,3] and [1,5,5,3] graphs get the same size through the convolution, [1,3,3,1]
So let's look back and see what the [1,3,3,1] graph does after deconvolution. Two situations have been created. Therefore, it is meaningful to specify output_shape here, of course, the optional output_shape is not allowed, the following situation will be an error:
Y5 = Tf.nn.conv2d_transpose (x1,kernel,output_shape=[1,10,10,3],strides=[1,2,2,1],padding= "SAME")
The above is the case of Stride 2, is similar to 1 o'clock, when the volume of core larger than the original image, the default valid way (with same meaningless) reference to the following figure:

List of programs:
Import TensorFlow as tf

x1 = tf.constant (1.0, shape=[1,3,3,1])

x2 = tf.constant (1.0, shape=[1,6,6,3])

x3 = tf . Constant (1.0, shape=[1,5,5,3])

kernel = tf.constant (1.0, shape=[3,3,3,1])



y1 = tf.nn.conv2d_transpose (x1, kernel,output_shape=[1,6,6,3],
    strides=[1,2,2,1],padding= "SAME")

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)







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.