Pytorch对图片下采样到12尺度、14 尺度

Pytorch对图片下采样到12尺度、14 尺度,第1张

Pytorch对图片采样到1/2尺度、1/4 尺度

from PIL import Image as Image
import torchvision.transforms.functional as T
import torch.nn.functional as F
import numpy as np
from torchvision import transforms
import matplotlib.pyplot as plt


path=r'C:UsersAdministratorDesktopPytorchdownsampleimage1.jpg'

# 显示图片
unloader = transforms.ToPILImage()  # reconvert into PIL image

def imshow(tensor, title=None):
    plt.figure()
    image = tensor.cpu().clone()  # we clone the tensor to not do changes on it
    image = image.squeeze(0)      # remove the fake batch dimension
    image = unloader(image)
    plt.imshow(image)
    if title is not None:
        plt.title(title)
    plt.show()

if __name__ == '__main__':
    image = Image.open(path)
    img = T.to_tensor(image)
    print(img.shape)
    x=img.unsqueeze(0)
    print(x.shape)

    imshow(x, title='Image_1')

    # 下采样1/2
    x_2 = F.interpolate(x, scale_factor=0.5)
    print(x_2.shape)
    imshow(x_2,title="Image_1/2")

    # 下采样
    x_4=F.interpolate(x_2,scale_factor=0.5)
    print(x_4.size())
    imshow(x_4,title="Image_1/4")

torch.Size([1, 3, 1080, 1920])


 torch.Size([1, 3, 540, 960])


torch.Size([1, 3, 270, 480])

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4696142.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-07
下一篇2022-11-07

发表评论

登录后才能评论

评论列表(0条)

    保存