def make_transform(sz_resize=256, sz_crop=227, mean=None, std=None, rgb_to_bgr=True, is_train=True, intensity_scale=None, rotate=0, jitter=0, hue=0): if std is None: std = [1, 1, 1] if mean is None: mean = [104, 117, 128] return transforms.Compose([ RGBToBGR() if rgb_to_bgr else Identity(), transforms.RandomRotation(rotate) if is_train else Identity(), transforms.RandomResizedCrop(sz_crop) if is_train else Identity(), transforms.Resize(sz_resize) if not is_train else Identity(), transforms.CenterCrop(sz_crop) if not is_train else Identity(), transforms.RandomHorizontalFlip() if is_train else Identity(), #transforms.ColorJitter(brightness=jitter, contrast=jitter, saturation=jitter, hue=hue) if is_train else Identity(), #### New Added #### transforms.RandomPerspective() if is_train else Identity(), transforms.RandomEqualize() if is_train else Identity(), transforms.RandomVerticalFlip() if is_train else Identity(), transforms.RandomAdjustSharpness( sharpness_factor=2) if is_train else Identity(), transforms.RandomAutocontrast() if is_train else Identity(), transforms.RandomErasing() if is_train else Identity(), transforms.RandomAffine(degrees=(-60, 60), translate=(0.1, 0.3), scale=(0.5, 0.75)) if is_train else Identity(), #### New Added #### transforms.ToTensor(), ScaleIntensities( *intensity_scale) if intensity_scale is not None else Identity(), transforms.Normalize( mean=mean, std=std, ) ])
# RandomAutocontrast # ~~~~~~~~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandomAutocontrast` transform # (see also :func:`~torchvision.transforms.functional.autocontrast`) # randomly applies autocontrast to the given image. autocontraster = T.RandomAutocontrast() autocontrasted_imgs = [autocontraster(orig_img) for _ in range(4)] plot(autocontrasted_imgs) #################################### # RandomEqualize # ~~~~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandomEqualize` transform # (see also :func:`~torchvision.transforms.functional.equalize`) # randomly equalizes the histogram of the given image. equalizer = T.RandomEqualize() equalized_imgs = [equalizer(orig_img) for _ in range(4)] plot(equalized_imgs) #################################### # AutoAugment # ~~~~~~~~~~~ # The :class:`~torchvision.transforms.AutoAugment` transform # automatically augments data based on a given auto-augmentation policy. # See :class:`~torchvision.transforms.AutoAugmentPolicy` for the available policies. policies = [T.AutoAugmentPolicy.CIFAR10, T.AutoAugmentPolicy.IMAGENET, T.AutoAugmentPolicy.SVHN] augmenters = [T.AutoAugment(policy) for policy in policies] imgs = [ [augmenter(orig_img) for _ in range(4)] for augmenter in augmenters ]
from matplotlib.image import AxesImage from hearth.vision.datasets import RGBImageDataset from hearth.vision.transforms import RandomFlipOrient _ids = [str(uuid.uuid4()) for i in range(100)] _augmentations = T.RandomApply( [ T.RandomHorizontalFlip(p=0.5), RandomFlipOrient(0.5), T.ColorJitter(brightness=0.1, hue=0.1), T.RandomAdjustSharpness(sharpness_factor=2), T.RandomAutocontrast(), T.RandomEqualize(), ], p=0.8, ) @pytest.fixture(scope='module') def image_dir(tmpdir_factory): d = tmpdir_factory.mktemp('images') dirname = str(d) # compute and save 100 random images of random sizes for _id in _ids: w = random.randint(32, 512) h = random.randint(32, 512) img = torch.rand(3, w, h)
def data_augmentation(ToTensor=False, Resize=None, Contrast=None, Equalize=None, HFlip=None, Invert=None, VFlip=None, Rotation=None, Grayscale=None, Perspective=None, Erasing=None, Crop=None): ''' DataAgumentation 2021/03/23 by Mr.w ------------------------------------------------------------- ToTensor : False/True , 注意转为Tensor,通道会放在第一维 Resize : tuple-->(500,500) Contrast : 0-1 -->图像被自动对比度的可能 Equalize : 0-1 -->图像均衡可能性 HFlip : 0-1 --> 图像水平翻转 Invert : 0-1--> 随机翻转 VFlip : 0-1 --> 图像垂直翻转 Rotation : 0-360 --> 随机旋转度数范围, as : 90 , [-90,90] Grayscale : 0-1 --> 随机转换为灰度图像 Perspective : 0-1 --> 随机扭曲图像 Erasing : 0-1 --> 随机擦除 Crop : tuple --> (500,500) ------------------------------------------------------------- return : transforms.Compose(train_transform) --> 方法汇总 ''' #列表导入Compose train_transform = [] if ToTensor == True: trans_totensor = transforms.ToTensor() train_transform.append(trans_totensor) if Resize != None: trans_Rsize = transforms.Resize(Resize) # Resize=(500,500) train_transform.append(trans_Rsize) if Contrast != None: trans_Rcontrast = transforms.RandomAutocontrast(p=Contrast) train_transform.append(trans_Rcontrast) if Equalize != None: trans_REqualize = transforms.RandomEqualize(p=Equalize) train_transform.append(trans_REqualize) if HFlip != None: train_transform.append(transforms.RandomHorizontalFlip(p=HFlip)) if Invert != None: train_transform.append(transforms.RandomInvert(p=Invert)) if VFlip != None: train_transform.append(transforms.RandomVerticalFlip(p=VFlip)) if Rotation != None: train_transform.append( transforms.RandomRotation(Rotation, expand=False, center=None, fill=0, resample=None)) if Grayscale != None: train_transform.append(transforms.RandomGrayscale(p=Grayscale)) if Perspective != None: train_transform.append( transforms.RandomPerspective(distortion_scale=0.5, p=Perspective, fill=0)) if Erasing != None: train_transform.append( transforms.RandomErasing(p=Erasing, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False)) if Crop != None: train_transform.append( transforms.RandomCrop(Crop, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')) return transforms.Compose(train_transform)