pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
83.82k stars 22.6k forks source link

Strange behaviour of F.interpolate with bicubic mode. #34544

Open gsygsy96 opened 4 years ago

gsygsy96 commented 4 years ago

🐛 Bug

Input: X with shape (2,3,256,256) RUN: Y = F.interpolate(X, [256,256], mode='bicubic', align_corners=True) Z = F.interpolate(Y, [256,256], mode='bicubic', align_corners=True) ISSUE: Y[1,:,:,:] = zeros and Z[1,:,:,:] = zeros.

Following is my code:


def interpolate_torch(x, s=256, mode='bicubic', align_corners=True):
    return F.interpolate(x, [s, s], mode=mode, align_corners=True)
def get_transform(resize_crop, colorJitter, horizen_flip, inputsize=1024):
    transform_list =[]
    if resize_crop:
        transform_list.append(transforms.RandomResizedCrop(inputsize, scale=(0.8, 1.0), interpolation=PIL.Image.BICUBIC))
    else:
        if inputsize != 1024:
            transform_list.append(transforms.Resize(inputsize, interpolation=PIL.Image.BICUBIC))
    if colorJitter:
        transform_list.append(transforms.ColorJitter())
    if horizen_flip:
        transform_list.append(transforms.RandomHorizontalFlip())
    transform_list += [transforms.ToTensor(),
                       transforms.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))]
    return transforms.Compose(transform_list)

def img2tensor(name):
    transf = get_transform(resize_crop=False, colorJitter=False,
                           horizen_flip=False, inputsize=256)
    img = Image.open(name)
    img_torch = transf(img)
    return img_torch

y2 = img2tensor('content.png').unsqueeze(0)
y1 = img2tensor('content.png').unsqueeze(0)
y = torch.cat([y2, y1],0)
print(y[1,:,:,:])
y_bicubic = interpolate_torch(y, s=256, mode='bicubic')
print(y_bicubic[1,:,:,:])
y_bicubic_bicubic = interpolate_torch(y_bicubic, s=128, mode='bicubic')

Environment: pytorch 1.1.0 torchvision 0.2.2.post3 scikit-image 0.15.0 python3

gsygsy96 commented 4 years ago

When I use bilinear, everything is ok. Help someone can given me the answer.

Enealor commented 4 years ago

Maybe related to #32918. To make sure though: is content.png a 256x256 image?

gsygsy96 commented 4 years ago

Maybe related to #32918. To make sure though: is content.png a 256x256 image?

The content.png is an 1024 x 1024 image.

gsygsy96 commented 4 years ago

Also, in my training script, if I first interpolate images(Bx3x256x256) with bicubic mode and scale_factor=1 and then interpolate the results again using bicubic and scale_factor != 1, the final results will be left rotated 90 degrees. PS: if the first interpolation is bilinear or the seconde interpolation use scale_factor = 1, everything will be ok.