from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer=SummaryWriter("logs")
img=Image.open("data/train/ants_image/0013035.jpg")
print(img)
print('-'*100)
#ToTensor
trans_totensor=transforms.ToTensor()
img_tensor=trans_totensor(img)
print(img_tensor)
print('-'*100)
writer.add_image("ToTensor",img_tensor)
#Normalize
print(img_tensor[0][0][0])
trans_norm=transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm=trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normal",img_norm,2)
#Resize
print(img.size)
print('-'*100)
trans_resize=transforms.Resize((512,512))
img_resize=trans_resize(img)
print(img_resize)
print('-'*100)
img_resize2=trans_totensor(img_resize)
writer.add_image("Resize",img_resize2,0)
print(img_resize2)
#Compost-resize-2
trans_resize_2=transforms.Resize(512)
trans_compose=transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2=trans_compose(img)
writer.add_image("Compose_resize",img_resize_2, 1)
#RandomCrop
trans_random=transforms.RandomCrop(20)
trans_compose_2=transforms.Compose([trans_random,trans_totensor])
for i in range(10):
img_random = trans_compose_2(img)
writer.add_image("randomcrop", img_random, i)
(主要是用来记录自己的学习,各类用法在CSDN上都能找到,大佬们总结得已经非常详细了)
(谢谢各位大佬)
(膜拜膜拜)