Updated March 16, 2023
Introduction to UpSampling2d
Upsampling2d creates an upsampling layer, also known as image interpolation. To repeat the column and rows by size zero and one, the upsampling2d function is used. The layer of inputs defined in 2d is upsampling2d. The layer of upsampling2d is effective and simple, but it does not perform learning and does not fill in useful details about the operation of upsampling.
Key Takeaways
- The conv2d transpose layer is more complex as compared to the upsampling2d layer. Both layers is performing the operation of upsampling and also interpret the upsampling data.
- This layer will combine the conv2d and upsampling layers into a single layer. We need to import libraries of UpSampling2d before using it.
What is UpSampling2d?
Each layer of upsampling2d is followed by a conv2d layer that will learn to interpret the input and train to translate the useful details. GAN is a neural network architecture that was used to train the model. The architecture will be made up of the discriminator and generator models, which will be implemented using a convolutional deep neural network. The discriminator is responsible for image classification. The generator is also responsible for generating new examples of the problem domain.
The generator will work by taking the random point by using latent space as input and output for completing the image. The convolutional neural network for the classification of the image is used to pool the layers for the input image down sampling. The convolutional layer is performing sown sampling by applying the filter as per the input image or feature maps. The activation which was resulting is a map of the output feature which contains the smaller border effects.
How to Use UpSampling2d?
To use upsampling2d we need to follow below steps as follows:
We need to import the different modules while using upsampling2d by using the import keyword.
1. In the below example we can see that we are importing the asarray, sequential, upsampling2d, and keras models by using the import keyword as follows.
Code:
from keras.layers import UpSampling2D
from numpy import asarray
import tensorflow as tf
from keras.models import Sequential
Output:
2. Now we are defining the input data for using upsampling2d also we are printing the context of input data as follows.
Code:
ip_data = asarray ([[1, 2], [3, 4]])
print(ip_data)
Output:
3. Now we are reshaping the data in a single sample. We are taking the input data for sampling as follows.
Code:
ip_data = ip_data.reshape((1, 2, 2, 1))
Output:
4. Now we are defining the model of it by using the model’s name as sequential as follows.
Code:
mod = Sequential()
mod.add(UpSampling2D (input_shape = (2, 2, 1)))
mod.summary()
Output:
5. Now in this step we are making the prediction of input data by using upsampling2d as follows.
Code:
pred = mod.predict(ip_data)
Output:
6. Now in this step we are reshaping the output for removing the channel, also we are summarizing the output as follows.
Code:
pred = pred.reshape((4, 4))
print (pred)
Output:
tf.keras.UpSampling2D and Conv2DTranspose Layers
The tensor space model is pretrained by using initialization also we are configuring the same by using different ways. Below is the syntax of keras upsampling2d as follows:
Syntax:
tf.layers.upsampling2d(arguments)
We have defined multiple arguments with the function of upsampling2d when using it. Below example shows how we can define upsampling2d as follows:
Code:
from keras.layers import UpSampling2D
from numpy import asarray
import tensorflow as tf
import numpy as np
ip_sh = (2, 2, 1, 3)
ip = np.arange (np.prod (ip_sh)).reshape(ip_sh)
print (ip)
op = tf.keras.layers.UpSampling2D (size=(11, 22))(ip)
print (op)
Output:
Con2DTranspose Layer
This is also known as the transpose convolution layer. The need for this layer generally arises from the need to shape and output of convolution for maintaining the pattern of connectivity that was compatible with the convolution. We must provide keyword arguments as input shapes by using the model’s first layer.
The conv2d transpose layer is shown in the example below:
Code:
from keras.layers import Conv2DTranspose
from numpy import asarray
import tensorflow as tf
import numpy as np
ip_sh = (2, 2, 1, 3)
ip = np.arange (np.prod(ip_sh)).reshape(ip_sh)
print(ip)
op = tf.keras.layers.Conv2DTranspose
(
filters,
kernel_size,
strides = (2, 2),
padding = "valid",
output_padding = None,
data_format = None,
dilation_rate = (4, 4),
activation = None,
use_bias = True,
bias_regularizer = None,
bias_constraint = None,
)
print (op)
Output:
Upsampling2d Arguments
Below are the arguments for upsampling as follows. It will accept the object of the field as follows.
- Size – The type of size argument is float. Row and column are an upsampling factor. It contains the numbers array.
- Shape – The type of shape argument is int. It contains the instruction as an output shape. This argument is used to create the input layer which was used for inserting the same before the layer.
- Name – The type of name argument is a string. This is defined as the name of the layer.
- Color – The type of color argument is a color format. This argument is defined as layer color.
- Close button – The type of close button argument is dict. Close button will appear as a control dict.
- Init status – The type of init status argument is a string. The layer initial status of upsampling2d is open or closed.
- Animetime – The type of anime time argument is int. It will define the speed of open and closed animation.
- Dataformat – These arguments will determine the ordering of input data format in an input dimension.
- Interpolation – It will define the mechanism of interpolation. The value of this argument is nearest.
- Batch size – This argument is defined in the absence of batch input shape.
- Dtype – The suppose layer is used as an input layer then this field is used as the data type.
- Weights – This is defined as tensor which was defining the value of initial weights.
- Batch input shape – It will define array numbers. This arguments is used in absence of batch size.
Examples of UpSampling2d
Given below are the examples mentioned:
Example #1
In the below example, we are using the function of upsampling2d as follows.
Code:
from keras.layers import UpSampling2D
from numpy import asarray
import tensorflow as tf
from keras.models import Sequential
ip = asarray([[11, 21],
[31, 41]])
print(ip)
ip = ip.reshape((1, 2, 2, 1))
conv = Sequential()
conv.add(UpSampling2D (input_shape = (2, 2, 1)))
conv.summary()
pred_2d = conv.predict(ip)
pred_2d = pred_2d.reshape((4, 4))
print(pred_2d)
Output:
Example #2
In below example we are defining the keras upsampling as follows.
Code:
from keras.layers import UpSampling2D
from numpy import asarray
import tensorflow as tf
import numpy as np
ip_spape = (2, 2, 1, 1)
ip = np.arange (np.prod (ip_spape)).reshape(ip_spape)
print (ip)
op = tf.keras.layers.UpSampling2D (size=(1, 2))(ip)
print (op)
Output:
Conclusion
Generator will work by taking the random point by using latent space as input and output for completing the image. It is creating a layer of upsampling, which is also known as image interpolation. The upsampling2d function is used to repeat the column and rows by size zero and one.
Recommended Articles
This is a guide to UpSampling2d. Here we discussed the introduction, how to use UpSampling2d? arguments and examples. You may also have a look at the following articles to learn more –