votes up 5

Got 3D input, but trilinear mode needs 5D input

Package:
torch
github stars 50580
Exception Class:
NotImplementedError

Raise code

    if input.dim() == 4 and mode == "bicubic":
        assert align_corners is not None
        return torch._C._nn.upsample_bicubic2d(input, output_size, align_corners, scale_factors)

    if input.dim() == 3 and mode == "bilinear":
        raise NotImplementedError("Got 3D input, but bilinear mode needs 4D input")
    if input.dim() == 3 and mode == "trilinear":
        raise NotImplementedError("Got 3D input, but trilinear mode needs 5D input")
    if input.dim() == 4 and mode == "linear":
        raise NotImplementedError("Got 4D input, but linear mode needs 3D input")
    if input.dim() == 4 and mode == "trilinear":
        raise NotImplementedError("Got 4D input, but trilinear mode needs 5D input")
    if input.dim() == 5 and mode == "linear":
        raise NotImplementedError("Got 5D input, but linear mode needs 3D input")
    if input.dim() == 5 and mode == "bilinear":

Ways to fix

votes up 5 votes down

When the torch.nn.Upsample initialized in 'trilinear' mode a 5D input is expected.

Step to reproduce the error:

import torch

input_3x3 = torch.zeros(3, 3).view(1,3, 3) #Here a 3D tensor is provided 
m = torch.nn.Upsample(scale_factor=2, mode='trilinear')
m(input_3x3)

Fixed version of the code

The tensor should be reshaped to a 5D.

from torch import nn
# here the input is reshaped to 5D tensor. this fixes the error
input_5x5 = torch.zeros(3, 3).view(1,1, 1, 3, 3) 
m = nn.Upsample(scale_factor=2, mode='trilinear')
m(input_5x5)

Jun 03, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix