def return_train_transforms(self): tfms = transforms.Compose([ transforms.Lambda(self.pad_image), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.RandomAdjustSharpness(2), transforms.RandomAutocontrast(), transforms.RandomRotation([-90, 90]), transforms.RandomAffine((-90, 90)), transforms.Resize([self.img_size[0], self.img_size[1]], transforms.transforms.InterpolationMode.BICUBIC), transforms.ToTensor(), transforms.RandomErasing(0.25), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) return tfms
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, ) ])
# ~~~~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandomSolarize` transform # (see also :func:`~torchvision.transforms.functional.solarize`) # randomly solarizes the image by inverting all pixel values above # the threshold. solarizer = T.RandomSolarize(threshold=192.0) solarized_imgs = [solarizer(orig_img) for _ in range(4)] plot(solarized_imgs) #################################### # RandomAdjustSharpness # ~~~~~~~~~~~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandomAdjustSharpness` transform # (see also :func:`~torchvision.transforms.functional.adjust_sharpness`) # randomly adjusts the sharpness of the given image. sharpness_adjuster = T.RandomAdjustSharpness(sharpness_factor=2) sharpened_imgs = [sharpness_adjuster(orig_img) for _ in range(4)] plot(sharpened_imgs) #################################### # 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
from torchvision.utils import save_image import torchvision.transforms as T # noqa: N812 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)
# target_size = 224, # transform = T.Compose([ # T.GaussianBlur(kernel_size = 3), # T.RandomAutocontrast(p = 0.5), # T.RandomAdjustSharpness(1.2, p = 0.5) # ])) # next(iter(val_data)) # type(img) # np.shape(img) # print(img) # plt.imshow(sample['image'], cmap = 'gray') transform_img = T.Compose([ T.GaussianBlur(kernel_size = 3), T.RandomAutocontrast(p = 0.5), T.RandomAdjustSharpness(1.2, p = 0.5)]) train_data = BrixiaDataset(annot_train, IMG_DIR, target_size = 224, transform = transform_img) test_data = BrixiaDataset(annot_test, IMG_DIR, target_size = 224) train_dl = DataLoader(train_data, BATCH_SIZE, shuffle = True, num_workers = 4) test_dl = DataLoader(test_data, BATCH_SIZE, shuffle = False, num_workers = 4) dataloaders = {'train': train_dl, 'test': test_dl} data_sizes = {'train': len(train_data), 'test': len(test_data)} data_sizes ## Creating an Instance of DataLoader! # train_dl = DataLoader(data, batch_size = 8, shuffle = True) # X, y = next(iter(val_dl)) ## 43s! After multiple runs. # ## With Four Workers: 1m 15s. First run! # print(np.shape(X))