Main Content

resize2dLayer

2-D resize layer

Since R2020b

Description

A 2-D resize layer resizes 2-D input by a scale factor, to a specified height and width, or to the size of a reference input feature map. Use of this layer requires Deep Learning Toolbox™.

Creation

Description

example

layer = resize2dLayer("Scale",scale) creates a 2-D resize layer and sets the Scale property as the scale factor specified by scale.

example

layer = resize2dLayer("OutputSize",outputSize) creates a 2-D resize layer and sets the OutputSize property with the height and width specified by outputSize.

example

layer = resize2dLayer("EnableReferenceInput",tf) creates a 2-D resize layer and sets the EnableReferenceInput property with the boolean specified by tf. When you specify the value as true, the layer adds an additional input that accepts a reference feature map and resizes the input to the size of the reference feature map.

example

layer = resize2dLayer(___,Name,Value) sets the optional Method, GeometricTransformMode, NearestRoundingMode, and Name properties using name-value arguments. You can specify multiple name-value arguments.

Example: layer = resize2dLayer("OutputSize",[128 128],"Method","bilinear") creates a 2-D resize layer that resizes input to 128-by-128 pixels using bilinear interpolation

Properties

expand all

Resize

Scale factor to resize input, specified as 2-element row vector of positive numbers. The scale factors are for the row and column dimensions, respectively. When creating the layer, you can specify Scale as a scalar to use the same value for both dimensions.

Output size of resized input, specified as a 2-element row vector of positive integers of the form [nrows ncols]. You can specify one element as NaN, in which case the layer computes the value automatically to preserve the aspect ratio of the input.

Add reference feature map as input to the layer, specified as a numeric or logical 0 (false) or 1 (true). When you specify the value as true, the layer resizes the height and width of the input to match the height and width of the reference feature map. The resizing operation does not change the number of channels of the input.

When you enable a reference feature map, the inputs to the layer have the names 'in1' and 'ref', where 'ref' is the name of the reference feature map. Use the input names when connecting or disconnecting the layer by using connectLayers (Deep Learning Toolbox) or disconnectLayers (Deep Learning Toolbox).

Interpolation method, specified as "nearest" for nearest neighbor interpolation or "bilinear" for bilinear interpolation.

Geometric transformation mode to map points from input space to output space, specified as"half-pixel" or "asymmetric".

Rounding mode for nearest neighbor interpolation, specified as one of the following.

  • "round" — use the same rounding behavior as the MATLAB® round function.

  • "floor" — use the same rounding behavior as the MATLAB floor function.

  • "onnx-10" — reproduce the resizing behavior of the ONNX™ (Open Neural Network Exchange) opset 10 Resize operator.

This property is valid when the Method property is "nearest".

Layer

Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet (Deep Learning Toolbox) and dlnetwork (Deep Learning Toolbox) functions automatically assign names to layers with the name "".

The Resize2DLayer object stores this property as a character vector.

Data Types: char | string

Number of inputs of the layer, specified as 1 when the EnableReferenceInput property is false or 2 when the EnableReferenceInput property is true.

Data Types: double

Input names of the layer, specified as {'in'} when the EnableReferenceInput property is false or {'in','ref'} when the EnableReferenceInput property is true.

Data Types: cell

This property is read-only.

Number of outputs from the layer, returned as 1. This layer has a single output only.

Data Types: double

This property is read-only.

Output names, returned as {'out'}. This layer has a single output only.

Data Types: cell

Examples

collapse all

Create a 2-D resize layer with a horizontal scale factor of 2 and a vertical scale factor of 4.

layer = resize2dLayer('Scale',[2 4])
layer = 
  Resize2DLayer with properties:

                      Name: ''
                     Scale: [2 4]
                OutputSize: []
      EnableReferenceInput: 0
                    Method: 'nearest'
    GeometricTransformMode: 'half-pixel'
       NearestRoundingMode: 'round'

   Learnable Parameters
    No properties.

   State Parameters
    No properties.

Use properties method to see a list of all properties.

Create a 2-D resize layer named 'resize224' with an output size of [224 224].

layer = resize2dLayer('OutputSize',[224 224],'Name','resize224')
layer = 
  Resize2DLayer with properties:

                      Name: 'resize224'
                     Scale: []
                OutputSize: [224 224]
      EnableReferenceInput: 0
                    Method: 'nearest'
    GeometricTransformMode: 'half-pixel'
       NearestRoundingMode: 'round'

   Learnable Parameters
    No properties.

   State Parameters
    No properties.

Use properties method to see a list of all properties.

Create a dlnetwork object.

net = dlnetwork;

Create an array of layers that includes a 2-D resize layer that accepts a reference input feature map. Add the layers to the network.

layers = [
    imageInputLayer([32 32 3],Name="image")
    resize2dLayer(EnableReferenceInput=true,Name="resize")]
layers = 
  2x1 Layer array with layers:

     1   'image'    Image Input   32x32x3 images with 'zerocenter' normalization
     2   'resize'   Resize        nnet.cnn.layer.Resize2DLayer
net = addLayers(net,layers);

Connect the "ref" input of the 2-D resize layer to the output of a layer that provides a reference feature map by using the connectLayers function. This example shows a trivial connection in which the "ref" input is also connected to the output of the image input layer.

net = connectLayers(net,"image","resize/ref");

Create a 2-D resize layer named 'rescale0.5' with a uniform scale factor of 0.5. Specify the interpolation method as bilinear interpolation.

layer = resize2dLayer('Scale',0.5,'Method','bilinear','Name','rescale0.5')
layer = 
  Resize2DLayer with properties:

                      Name: 'rescale0.5'
                     Scale: [0.5000 0.5000]
                OutputSize: []
      EnableReferenceInput: 0
                    Method: 'bilinear'
    GeometricTransformMode: 'half-pixel'
       NearestRoundingMode: 'round'

   Learnable Parameters
    No properties.

   State Parameters
    No properties.

Use properties method to see a list of all properties.

References

[1] Open Neural Network Exchange. https://github.com/onnx/.

[2] ONNX. https://onnx.ai/.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2020b

expand all

See Also

| | (Deep Learning Toolbox) | (Deep Learning Toolbox) | (Deep Learning Toolbox)

Topics