如何将尺寸为 (512, 512, 3) 的图像更改为尺寸为 ([1, 3, 224, 224]) 的张量

fea*_*ght 2 computer-vision image-segmentation pytorch

我试图实现一篇论文,其中输入维度是大小为 ([1, 3, 224, 224]) 的张量。我当前的图像尺寸是(512, 512, 3)。

如何调整大小和转换以输入模型?

小智 5

假设图像已经转换为torch.Tensor并具有 shape (512, 512, 3),可能的方法之一:

from torchvision.transforms import Resize

image = image.permute((2, 0, 1))  # convert to (C, H, W) format

image = image.unsqueeze(0)  # add fake batch dimension

resize = Resize((224, 224))

new_image = resize(image)
Run Code Online (Sandbox Code Playgroud)

现在new_image.shape等于(1, 3, 224, 224)