What’s Transposed Convolutional Layer?

0
2


A transposed convolutional layer is an upsampling layer that generates the output function map better than the enter function map. It’s just like a deconvolutional layer. A deconvolutional layer reverses the layer to a customary convolutional layer. If the output of the usual convolution layer is deconvolved with the deconvolutional layer then the output would be the identical as the unique worth, Whereas in transposed convolutional worth is not going to be the identical, it could possibly reverse to the identical dimension,

Transposed convolutional layers are utilized in a wide range of duties, together with picture era, picture super-resolution, and picture segmentation. They’re significantly helpful for duties that contain upsampling the enter information, reminiscent of changing a low-resolution picture to a high-resolution one or producing a picture from a set of noise vectors. 

The operation of a transposed convolutional layer is just like that of a standard convolutional layer, besides that it performs the convolution operation in the wrong way. As a substitute of sliding the kernel over the enter and performing element-wise multiplication and summation, a transposed convolutional layer slides the enter over the kernel and performs element-wise multiplication and summation. This ends in an output that’s bigger than the enter, and the dimensions of the output will be managed by the stride and padding parameters of the layer.

Transposed Convolutional  with stride 2 - Geeksforgeeks

Transposed Convolutional  with stride 2

In a transposed convolutional layer, the enter is a function map of dimension  I_h times I_w    , the place  I_h     and  I_w  are the peak and width of the enter and the kernel dimension is  K_h times K_w, the place K_h and  K_w  are the peak and width of the kernel. 

 If the stride form is (s_h,s_w)  and the padding is p, The stride of the transposed convolutional layer determines the step dimension for the enter indices p and q, and the padding determines the variety of pixels so as to add to the sides of the enter earlier than performing the convolution. Then the output of the transposed convolutional layer will probably be

O_h = (I_h -1) times s_h + K_h -2p  O_w = (I_w -1) times s_w + K_h -2p

the place O_h  and O_w are the peak and width of the output.

Instance 1: 

Suppose we’ve a grayscale picture of dimension 2 X 2, and we need to upsample it utilizing a transposed convolutional layer with a kernel dimension of 2 x 2, a stride of 1, and 0 padding (or no padding). The enter picture and the kernel for the transposed convolutional layer could be as follows:

Input = begin{bmatrix} 0 & 1 2 & 3 end{bmatrix}

Kernel = begin{bmatrix} 4 & 1 2 & 3 end{bmatrix}

The output will probably be:

Transposed Convolutional  Stride = 1 -Geeksforgeeks

Transposed Convolutional  Stride = 1

Technique 1: Manually with TensorFlow

Code Explanations:

  • Import needed libraries (TensorFlow and NumPy)
  • Outline Enter tensor and customized kernel
  • Apply Transpose convolution with kernel dimension =2, stride = 1.
  • Write the customized features for transpose convolution
  • Apply Transpose convolution on enter information.

Python3

import tensorflow as tf

import numpy as np

  

Enter = tf.fixed([[0.0, 1.0], [2.0, 3.0]], dtype=tf.float32)

Kernel = tf.fixed([[4.0, 1.0], [2.0, 3.0]], dtype=tf.float32)

  

  

def trans_conv(Enter, Kernel):

    h, w = Kernel.form

    Y = np.zeros((Enter.form[0] + h - 1, Enter.form[1] + w - 1))

  

    for i in vary(Enter.form[0]):

        for j in vary(Enter.form[1]):

            Y[i: i + h, j: j + w] += Enter[i, j] * Kernel

    return tf.fixed(Y)

  

  

trans_conv(Enter, Kernel)

Output:

<tf.Tensor: form=(3, 3), dtype=float64, numpy=
array([[ 0.,  4.,  1.],
       [ 8., 16.,  6.],
       [ 4., 12.,  9.]])>

The output form will be calculated as :

 begin{aligned}O_h &= (I_h -1) times s_h + K_h -2p  &= (2-1)times 1 + 2 -2times0  &= 1times 1 + 2-0  &=3end{aligned}  begin{aligned}O_w &= (I_w -1) times s_w + K_w -2p  &= (2-1)times 1 + 2 -2times0  &= 1times 1 + 2-0  &=3end{aligned}

Technique 2: With PyTorch:

Code Explanations:

  • Import needed libraries (torch and nn from torch)
  • Outline Enter tensor and customized kernel
  • Redefine the form in 4 dimensions as a result of PyTorch takes 4D shapes in inputs.
  • Apply Transpose convolution with enter and output channel =1,1, kernel dimension =2, stride = 1, padding = 0 means legitimate padding.
  • Set the shopper kernel weight by utilizing Transpose.weight.information
  • Apply Transpose convolution on enter information.

Python3

import torch

from torch import nn

  

Enter = torch.tensor([[0.0, 1.0], [2.0, 3.0]])

Kernel = torch.tensor([[4.0, 1.0], [2.0, 3.0]])

  

Enter = Enter.reshape(1, 1, 2, 2)

Kernel = Kernel.reshape(1, 1, 2, 2)

  

Transpose = nn.ConvTranspose2d(in_channels =1

                               out_channels =1,

                               kernel_size=2

                               stride = 1

                               padding=0

                               bias=False)

  

Transpose.weight.information = Kernel

Transpose(Enter)

Output:

tensor([[[[ 0.,  4.,  1.],
          [ 8., 16.,  6.],
          [ 4., 12.,  9.]]]], grad_fn=<ConvolutionBackward0>)

Transposed convolutional layers are sometimes used along with different kinds of layers, reminiscent of pooling layers and absolutely linked layers, to construct deep convolutional networks for numerous duties.

Instance 2: Legitimate Padding

In legitimate padding, no further layer of zeros will probably be added.

Python3

import tensorflow as tf

  

input_tensor = tf.fixed([[

    [[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]],

    [[17, 18, 19], [21, 22, 23], [25, 26, 27], [29, 30, 31]],

    [[33, 34, 35], [37, 38, 39], [41, 42, 43], [45, 46, 47]],

    [[49, 50, 51], [53, 54, 55], [57, 58, 59], [61, 62, 63]]

]], dtype=tf.float32)

  

  

transposed_conv_layer = tf.keras.layers.Conv2DTranspose(

    filters=1, kernel_size=3, strides=2, padding='legitimate')

  

output = transposed_conv_layer(input_tensor)

  

print(output.form)

Output:

(1, 9, 9, 1)

Instance 3: Identical Padding

In identical padding, an Additional layer of zeros (often called the padding layer) will probably be added.

Python3

import tensorflow as tf

  

input_tensor = tf.fixed([[

    [[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]],

    [[17, 18, 19], [21, 22, 23], [25, 26, 27], [29, 30, 31]],

    [[33, 34, 35], [37, 38, 39], [41, 42, 43], [45, 46, 47]],

    [[49, 50, 51], [53, 54, 55], [57, 58, 59], [61, 62, 63]]

]], dtype=tf.float32)

  

  

transposed_conv_layer = tf.keras.layers.Conv2DTranspose(

    filters=1, kernel_size=3, strides=2, padding='identical')

  

output = transposed_conv_layer(input_tensor)

  

print(output.form)

Output:

(1, 8, 8, 1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here