transforms.RandomApply(transforms, p=0.5)
transforms:给定变化的列表, 类型:nn.MModuleList([]) 或者直接一个[]里面填入transforms
transforms.RandomChoice(transforms)
但是,这两个如何组合使用呢,以及当我需要用到我自定义的处理方法时该怎么用呢?直接上代码:
from torchvision import transforms
class AddBlur(object):
# def __init__(self, MAX_HW=1504):
# self.max_hw = MAX_HW
def __call__(self, img):
size = random.choice([1,3,5,7,9,11,13,15,17,19,21])
img = transforms.GaussianBlur(kernel_size=(size, size), sigma = 10)(img)
return img
transform1 = transforms.RandomApply([transforms.ColorJitter(brightness=(0.45,1.4))], p=0.2)
transform2 = transforms.RandomApply([transforms.Compose([AddBlur()])], p=0.2)#对于自定义的预处理方法需要使用transforms.Compose
mytransform = transforms.RandomChoice([transform1,transform2])
image = mytransform(image) #这里的image是需要预处理的图片