Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.UpSampling2D

UpSampling2D enlarges the two spatial dimensions of a 4D image or feature-map tensor while preserving channels. It supports separate row and column factors plus several interpolation modes.

How it works

UpSampling2D resizes a four-dimensional image or feature-map tensor by independently multiplying its two spatial dimensions. It keeps the channel dimension unchanged and can either repeat pixels with nearest-neighbor behavior or use one of the documented interpolation methods.

Syntax

keras.layers.UpSampling2D(size=(2, 2), data_format=None, interpolation="nearest", **kwargs)

Parameters

NameTypeDescription
sizeint or tuple of 2 integersUpsampling factors for rows and columns; an integer applies the same factor to both, while a two-integer tuple can specify them separately.
data_formatstring or NoneInput dimension ordering: channels_last or channels_first. If omitted, the configured Keras image data format is used.
interpolationstringResize algorithm. Supported values are bicubic, bilinear, lanczos3, lanczos5, and nearest.
**kwargskeyword argumentsAdditional arguments accepted by the base Layer class.

Returns

A 4D tensor whose row and column dimensions are enlarged according to size, with layout determined by data_format.

Key points

  • The class is designed for two-dimensional image or feature-map data.
  • size controls the spatial scaling independently along the row and column axes.
  • interpolation accepts the documented resize modes, including bicubic, bilinear, lanczos3, lanczos5, and nearest.
  • Nearest-neighbor interpolation provides pixel or feature-value repetition rather than a smoothed resize.
  • The layer expects a rank-four input tensor.
  • With channels_last, the output places the enlarged row and column dimensions before the unchanged channel dimension.
  • With channels_first, channels precedes the two enlarged spatial dimensions in the output.

Common mistakes

  • If data_format is omitted, the layer follows the image layout configured globally, so input and output shape expectations can depend on Keras configuration.

Example: Resize a 2D feature map

import keras
from keras import layers

inputs = keras.Input(shape=(16, 16, 3))
x = layers.UpSampling2D(size=(2, 3), interpolation="bilinear")(inputs)
model = keras.Model(inputs, x)
print(model.output_shape)  # (None, 32, 48, 3)

This model doubles the input height, triples its width, and preserves the channel count using bilinear interpolation.

When to use it

Increase feature-map resolution

Decoder networks and autoencoders

Restore spatial resolution before producing a pixel-level output

Image segmentation

Apply deterministic spatial enlargement without a trainable convolution

Feature-map resizing

Interview questions

QWhat does UpSampling2D do to an input tensor?

UpSampling2D takes a 4D tensor and enlarges its rows and columns according to size. The channel dimension is retained, and the resulting spatial dimensions are the corresponding input dimensions multiplied by the upsampling factors.

Follow-up: How does layout affect the tensor shape? With channels_last the layout is batch, rows, columns, channels; with channels_first it is batch, channels, rows, columns.

QHow is the size argument interpreted?

size specifies the enlargement factor for rows and columns. It may be one integer, applying the same factor to both spatial dimensions, or a two-integer tuple for separate factors.

Follow-up: What happens when size is (2, 3)? The output height is doubled and the output width is tripled, while channels remain unchanged.

QWhich interpolation modes are supported?

The interpolation argument selects the resizing method. The documented choices are bicubic, bilinear, lanczos3, lanczos5, and nearest; nearest can be used to repeat rows and columns.

Follow-up: Which method is appropriate when exact repetition is wanted? Use interpolation="nearest", because the documentation explicitly associates it with repeating rows and columns.

  • UpSampling1D
  • UpSampling3D
  • Conv2D
  • MaxPooling2D

Source: UpSampling2D layer - Keras. Checked 2026-10-15.