赞
踩
PIL Image -> Image.open
tensor -> ToTensor() [Convert a PIL Image or ndarray to tensor]
ndarray -> cv2.imread
(1)ToTensor
将PIL/numpy ndarry转化为tensor
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = "dataset/train/bees/36900412_92b81831ad.jpg"
writer = SummaryWriter("logs")
img = Image.open(img_path)
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
# writer.add_image("Totensor",img_tensor)
# writer.close()
(2)Normalize归一化
trans_norm = transforms.Normalize([0.3,0.3,0.3],[0.1,0.2,0.3])
img_norm = trans_norm(img_tensor)
writer.add_image("Normalize",img_norm)
writer.close()
(3)Resize
trans_resize = transforms.Resize((512,512))
# PIL image => PIL image
img_resize = trans_resize(img)
# PIL image => tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize,0)
writer.close()
(4)Compose 组合多个tranforms
transforms.Compose([
transforms.CenterCrop(10),
transforms.ToTensor()
])
例子
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize",img_resize_2,1)
writer.close()
(5)RandomCrop 随机裁剪
trans_randomCrop = transforms.RandomCrop((100,100))
trans_compose_2 = transforms.Compose([trans_randomCrop,trans_totensor])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop,i)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。