当前位置:   article > 正文

pytorch -- 常见的transform包方法

pytorch -- 常见的transform包方法

1. 基础

PIL Image -> Image.open
tensor -> ToTensor() [Convert a PIL Image or ndarray to tensor]
ndarray -> cv2.imread

2. 常见方法

(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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(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()
  • 1
  • 2
  • 3
  • 4

(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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(4)Compose 组合多个tranforms

transforms.Compose([
    transforms.CenterCrop(10),
    transforms.ToTensor()
])
  • 1
  • 2
  • 3
  • 4

例子

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()
  • 1
  • 2
  • 3
  • 4
  • 5

(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)
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/165764
推荐阅读
相关标签
  

闽ICP备14008679号