Mai*_*Son 3 python interpolation image-processing pytorch
我用来torch.nn.functional.interpolate()调整图像大小。
首先,我将transforms.ToTensor()图像转换为张量,其大小为(3, 252, 252),(252, 252)是导入图像的大小。我想做的是使用interpolate()函数创建一个大小为 (3, 504, 504) 的张量。
我设置了 para scale_factor=2,但它返回了 (3, 252, 504) 张量。然后我将其设置为scale_factor=(1,2,2)并收到如下尺寸冲突错误:
size shape must match input shape. Input is 1D, size is 3
那么我应该如何设置参数才能接收 (3, 504, 504) 张量呢?
如果您正在使用,scale_factor则需要提供批量图像而不是单个图像。因此,您需要使用以下方式添加一批,unsqueeze(0)然后使其发挥interpolate作用:
import torch
import torch.nn.functional as F
img = torch.randn(3, 252, 252) # torch.Size([3, 252, 252])
img = img.unsqueeze(0) # torch.Size([1, 3, 252, 252])
out = F.interpolate(img, scale_factor=(2, 2), mode='nearest')
print(out.size()) # torch.Size([1, 3, 504, 504])
Run Code Online (Sandbox Code Playgroud)