PyTorch's torch.nn.functional.interpolate behaving strangely

Viewed 2328

I'm having issues with PyTorch's tensor-resizing options. In the following code, x is a dataset of 888 64x64 RGB images of pokemon. xs is to be a dictionary of the same dataset at different resolutions.

def load_data():
    pokemon = []
    for png in os.listdir("pokemon"):
        pokemon.append(imageio.imread("pokemon/" + png))
    pokemon = np.array(pokemon)
    s = pokemon.shape
    pokemon = pokemon.reshape((s[0], s[-1], s[1], s[2]))
    pokemon = (pokemon.astype(np.float32) - 127.5)/127.5
    return(pokemon)
x = load_data()

ss = [2, 1, 1/2, 1/4, 1/8, 1/16]
xs = {s : nn.functional.interpolate(torch.from_numpy(x), scale_factor = s, mode='nearest') for s in ss}

As expected, the output shapes include all 888 RGB images with new heights and widths.

print("Data shape:")
for x in xs.values():
    print(" ", x.shape)

> Data shape:
>  torch.Size([888, 3, 128, 128])
>  torch.Size([888, 3, 64, 64])
>  torch.Size([888, 3, 32, 32])
>  torch.Size([888, 3, 16, 16])
>  torch.Size([888, 3, 8, 8])
>  torch.Size([888, 3, 4, 4])

However, the images weren't sensibly resized.

  • When s = 1, the pokemon are in their ordinary 64x64 format, as expected:

  • When s = 2, the pokemon become Pollocks, overlapping in different colors:

  • When s = 1/2, the pokemon are cut into pieces and rearranged:

Is there an alternative PyTorch tool for resizing? I encounter roughly the same problem using AvgPool2d and Upsample instead. I will be resizing non-image tensors, too, so PIL isn't always an option. In any case, using the GPU instead of CPU would be helpful.

1 Answers

I have solved my problem. I was unused to transitioning images from (Batch, Height, Width, Channels) to (Batch, Channels, Height, Width) for PyTorch. Replacing the reshape lines with np.moveaxis fixed the issue. Thanks, everyone, for your help.

Related