

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:\Users\Administrator\Desktop\Pytorch\downsample\image\1.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])

|