Increase feature-map resolution
Decoder networks and autoencoders
Keras · Class
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.
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.
keras.layers.UpSampling2D(size=(2, 2), data_format=None, interpolation="nearest", **kwargs)| Name | Type | Description |
|---|---|---|
size | int or tuple of 2 integers | Upsampling factors for rows and columns; an integer applies the same factor to both, while a two-integer tuple can specify them separately. |
data_format | string or None | Input dimension ordering: channels_last or channels_first. If omitted, the configured Keras image data format is used. |
interpolation | string | Resize algorithm. Supported values are bicubic, bilinear, lanczos3, lanczos5, and nearest. |
**kwargs | keyword arguments | Additional arguments accepted by the base Layer class. |
A 4D tensor whose row and column dimensions are enlarged according to size, with layout determined by data_format.
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.
Decoder networks and autoencoders
Image segmentation
Feature-map resizing
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.
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.
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.
UpSampling1DUpSampling3DConv2DMaxPooling2D