Function Differences with torch.nn.Upsample

torch.nn.Upsample

torch.nn.Upsample(
    size=None,
    scale_factor=None,
    mode='nearest',
    align_corners=None
)(input)

For more information, see torch.nn.Upsample.

mindspore.nn.ResizeBilinear

class mindspore.nn.ResizeBilinear(half_pixel_centers=False)(x, size=None, scale_factor=None, align_corners=False)

For more information, see mindspore.nn.ResizeBilinear.

Differences

PyTorch: Multiple modes can be chosen when upsampling data.

MindSpore:Currently only supports bilinear mode to sample data. half_pixel_centers defaults to False, to achieve the same result as PyTorch, it should be set to True.

Code Example

import mindspore as ms
import mindspore.nn as nn
import torch
import numpy as np

# In MindSpore, it is predetermined to use bilinear to resize the input image.
x = np.random.randn(1, 2, 3, 4).astype(np.float32)
resize = nn.ResizeBilinear(half_pixel_centers=True)
tensor = ms.Tensor(x)
output = resize(tensor, (5, 5))
print(output.shape)
# Out:
# (1, 2, 5, 5)

# In torch, parameter mode should be passed to determine which method to apply for resizing input image.
x = np.random.randn(1, 2, 3, 4).astype(np.float32)
resize = torch.nn.Upsample(size=(5, 5), mode='bilinear')
tensor = torch.tensor(x)
output = resize(tensor)
print(output.detach().numpy().shape)
# Out:
# (1, 2, 5, 5)