pytorch.torchvision.transforms

torchvision.transforms可以对数据进行以下处理:

  • PIL.Image/numpy.ndarray与Tensor的相互转化;
  • 归一化;
  • 对PIL.Image进行裁剪、缩放等操作。

代码如下:

transform1 = transforms.Compose([
                    transforms.ToTensor()
                    ])
transform2 = transforms.Compose([
                    transforms.Resize(int(256*1.12), Image.BICUBIC), 
                    transforms.RandomCrop(256), 
                    transforms.RandomHorizontalFlip(),
                    transforms.ToTensor(),
                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # (tensor-mean)/std
                    ])

img_path=r"trainA\n02381460_14.jpg"

# numpy.ndarray
img = Image.open(img_path)
print(np.array(img).shape)

img1 = transform1(img)
print("img1=",img1.size())
print("img type", type(img1))
print("img max", torch.max(img1))
print("img min", torch.min(img1))

# Tensor
img = Image.open(img_path).convert('RGB')
img2 = transform2(img)
print("img2=",img2.size())
print("img2 max", torch.max(img2))
print("img2 min", torch.min(img2))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值