Upsample#
- classtorch.nn.modules.upsampling.Upsample(size=None,scale_factor=None,mode='nearest',align_corners=None,recompute_scale_factor=None)[source]#
Upsamples a given multi-channel 1D (temporal), 2D (spatial) or 3D (volumetric) data.
The input data is assumed to be of the formminibatch x channels x [optional depth] x [optional height] x width.Hence, for spatial inputs, we expect a 4D Tensor and for volumetric inputs, we expect a 5D Tensor.
The algorithms available for upsampling are nearest neighbor and linear,bilinear, bicubic and trilinear for 3D, 4D and 5D input Tensor,respectively.
One can either give a
scale_factoror the target outputsizetocalculate the output size. (You cannot give both, as it is ambiguous)- Parameters
size (int orTuple[int] orTuple[int,int] orTuple[int,int,int],optional) – output spatial sizes
scale_factor (float orTuple[float] orTuple[float,float] orTuple[float,float,float],optional) – multiplier for spatial size. Has to match input size if it is a tuple.
mode (str,optional) – the upsampling algorithm: one of
'nearest','linear','bilinear','bicubic'and'trilinear'.Default:'nearest'align_corners (bool,optional) – if
True, the corner pixels of the inputand output tensors are aligned, and thus preserving the values atthose pixels. This only has effect whenmodeis'linear','bilinear','bicubic', or'trilinear'.Default:Falserecompute_scale_factor (bool,optional) – recompute the scale_factor for use in theinterpolation calculation. Ifrecompute_scale_factor is
True, thenscale_factor must be passed in andscale_factor is used to compute theoutputsize. The computed outputsize will be used to infer new scales forthe interpolation. Note that whenscale_factor is floating-point, it may differfrom the recomputedscale_factor due to rounding and precision issues.Ifrecompute_scale_factor isFalse, thensize orscale_factor willbe used directly for interpolation.
- Shape:
Input:, or
Output:,or, where
Warning
With
align_corners=True, the linearly interpolating modes(linear,bilinear,bicubic, andtrilinear) don’t proportionallyalign the output and input pixels, and thus the output values can dependon the input size. This was the default behavior for these modes up toversion 0.3.1. Since then, the default behavior isalign_corners=False. See below for concrete examples on how thisaffects the outputs.Note
If you want downsampling/general resizing, you should use
interpolate().Examples:
>>>input=torch.arange(1,5,dtype=torch.float32).view(1,1,2,2)>>>inputtensor([[[[1., 2.], [3., 4.]]]])>>>m=nn.Upsample(scale_factor=2,mode='nearest')>>>m(input)tensor([[[[1., 1., 2., 2.], [1., 1., 2., 2.], [3., 3., 4., 4.], [3., 3., 4., 4.]]]])>>>m=nn.Upsample(scale_factor=2,mode='bilinear')# align_corners=False>>>m(input)tensor([[[[1.0000, 1.2500, 1.7500, 2.0000], [1.5000, 1.7500, 2.2500, 2.5000], [2.5000, 2.7500, 3.2500, 3.5000], [3.0000, 3.2500, 3.7500, 4.0000]]]])>>>m=nn.Upsample(scale_factor=2,mode='bilinear',align_corners=True)>>>m(input)tensor([[[[1.0000, 1.3333, 1.6667, 2.0000], [1.6667, 2.0000, 2.3333, 2.6667], [2.3333, 2.6667, 3.0000, 3.3333], [3.0000, 3.3333, 3.6667, 4.0000]]]])>>># Try scaling the same data in a larger tensor>>>input_3x3=torch.zeros(3,3).view(1,1,3,3)>>>input_3x3[:,:,:2,:2].copy_(input)tensor([[[[1., 2.], [3., 4.]]]])>>>input_3x3tensor([[[[1., 2., 0.], [3., 4., 0.], [0., 0., 0.]]]])>>>m=nn.Upsample(scale_factor=2,mode='bilinear')# align_corners=False>>># Notice that values in top left corner are the same with the small input (except at boundary)>>>m(input_3x3)tensor([[[[1.0000, 1.2500, 1.7500, 1.5000, 0.5000, 0.0000], [1.5000, 1.7500, 2.2500, 1.8750, 0.6250, 0.0000], [2.5000, 2.7500, 3.2500, 2.6250, 0.8750, 0.0000], [2.2500, 2.4375, 2.8125, 2.2500, 0.7500, 0.0000], [0.7500, 0.8125, 0.9375, 0.7500, 0.2500, 0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])>>>m=nn.Upsample(scale_factor=2,mode='bilinear',align_corners=True)>>># Notice that values in top left corner are now changed>>>m(input_3x3)tensor([[[[1.0000, 1.4000, 1.8000, 1.6000, 0.8000, 0.0000], [1.8000, 2.2000, 2.6000, 2.2400, 1.1200, 0.0000], [2.6000, 3.0000, 3.4000, 2.8800, 1.4400, 0.0000], [2.4000, 2.7200, 3.0400, 2.5600, 1.2800, 0.0000], [1.2000, 1.3600, 1.5200, 1.2800, 0.6400, 0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])