transform:数据预处理代码,可以自定义数据增广等

from __future__ import absolute_import

from torchvision.transforms import *
from PIL import Image
import random
import numpy as np

class Random2DTranslation(object):
    """
    With a probability, first increase image size to (1 + 1/8), and then perform random crop.

    Args:
        height (int): target height.
        width (int): target width.
        p (float): probability of performing this transformation. Default: 0.5.
    """
    def __init__(self, height, width, p=0.5, interpolation=Image.BILINEAR):
        self.height = height
        self.width = width
        self.p = p
        self.interpolation = interpolation

    def __call__(self, img):
        """
        Args:
            img (PIL Image): Image to be cropped.

        Returns:
            PIL Image: Cropped image.
        """
        if random.random() < self.p:
            return img.resize((self.width, self.height), self.interpolation)
        new_width, new_height = int(round(self.width * 1.125)), int(round(self.height * 1.125))
        resized_img = img.resize((new_width, new_height), self.interpolation)
        x_maxrange = new_width - self.width
        y_maxrange = new_height - self.height
        x1 = int(round(random.uniform(0, x_maxrange)))
        y1 = int(round(random.uniform(0, y_maxrange)))
        croped_img = resized_img.crop((x1, y1, x1 + self.width, y1 + self.height))
        return croped_img
if __name__ == '__main__':
    img = Image.open('/home/ubuntu/reid/AlignedReID/data/market1501/bounding_box_train/0002_c1s1_000451_03.jpg')
    transform = Random2DTranslation(256, 128, 0.5)
    img_t = transform(img)
    import matplotlib.pyplot as plt
    
    plt.figure(12)
    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(img_t)
    plt.show()   

在这里插入图片描述

if __name__ == '__main__':
    img = Image.open('/home/ubuntu/reid/AlignedReID/data/market1501/bounding_box_train/0002_c1s1_000451_03.jpg')
    transform = transforms.Compose(
        [
            Random2DTranslation(256, 128, 0.5),
            transforms.RandomHorizontalFlip(),
            #transforms.ToTensor(),
        ]
    )
    img_t = transform(img)
    import matplotlib.pyplot as plt
    
    plt.figure(12)
    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(img_t)
    plt.show()

有概率翻转

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值