def augmentation(mode, target_size, prob=0.5, aug_m=2): ''' description: augmentation mode: 'train' 'test' target_size: int or list, the shape of image , aug_m: Strength of transform ''' high_p = prob low_p = high_p / 2.0 M = aug_m first_size = [int(x / 0.7) for x in target_size] if mode == 'train': return composition.Compose([ transforms.Resize(first_size[0], first_size[1], interpolation=3), transforms.Flip(p=0.5), composition.OneOf([ RandomCenterCut(scale=0.1 * M), transforms.ShiftScaleRotate(shift_limit=0.05 * M, scale_limit=0.1 * M, rotate_limit=180, border_mode=cv2.BORDER_CONSTANT, value=0), albumentations.imgaug.transforms.IAAAffine( shear=(-10 * M, 10 * M), mode='constant') ], p=high_p), transforms.RandomBrightnessContrast( brightness_limit=0.1 * M, contrast_limit=0.03 * M, p=high_p), transforms.HueSaturationValue(hue_shift_limit=5 * M, sat_shift_limit=15 * M, val_shift_limit=10 * M, p=high_p), transforms.OpticalDistortion(distort_limit=0.03 * M, shift_limit=0, border_mode=cv2.BORDER_CONSTANT, value=0, p=low_p), composition.OneOf([ transforms.Blur(blur_limit=7), albumentations.imgaug.transforms.IAASharpen(), transforms.GaussNoise(var_limit=(2.0, 10.0), mean=0), transforms.ISONoise() ], p=low_p), transforms.Resize(target_size[0], target_size[1], interpolation=3), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0) ], p=1) else: return composition.Compose([ transforms.Resize(target_size[0], target_size[1], interpolation=3), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0) ], p=1)
def AlbumentationTrainTransform(self): tf = tc.Compose([ ta.HorizontalFlip(), ta.Cutout(num_holes=1, max_h_size=16, max_w_size=16), tp.ToTensor(dict(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) ]) return lambda img: tf(image=np.array(img))["image"]
def AlbumentationTestTransform(self): tf = tc.Compose([ tp.ToTensor( dict(mean=(0.4802, 0.4481, 0.3975), std=(0.2302, 0.2265, 0.2262))) ]) return lambda img: tf(image=np.array(img))["image"]
def AlbumentationTrainTransform(self): tf = tc.Compose([ta.PadIfNeeded(4, 4, always_apply=True), ta.RandomCrop(height=32, width=32, always_apply=True), ta.Cutout(num_holes = 1, max_h_size=8, max_w_size=8, always_apply=True), ta.HorizontalFlip(), tp.ToTensor(dict (mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) ]) return lambda img: tf(image = np.array(img))["image"]
def model10_resnet_train_transforms(): transforms = C.Compose([ A.HorizontalFlip(), #A.RandomCrop(height=30, width=30, p=5.0), A.Cutout(num_holes=1, max_h_size=16, max_w_size=16), P.ToTensor(dict (mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) ]) return lambda img: transforms(image = np.array(img))["image"]
def model12_train_transforms(): transform = C.Compose([ A.PadIfNeeded(min_height=70, min_width=70, border_mode=cv2.BORDER_CONSTANT, value=0.5), A.RandomCrop(height=64, width=64), A.HorizontalFlip(p=0.5), A.Cutout(num_holes=1, max_h_size=32, max_w_size=32, p=1), P.ToTensor(dict (mean=(0.4802, 0.4481, 0.3975), std=(0.2302, 0.2265, 0.2262))) ]) return lambda img: transform(image = np.array(img))["image"]
def model11_davidnet_train_transforms(): transform = C.Compose([ A.PadIfNeeded(min_height=36, min_width=36, border_mode=cv2.BORDER_CONSTANT, value=0.5), A.RandomCrop(height=32, width=32, p=1), A.HorizontalFlip(p=0.5), A.Cutout(num_holes=1, max_h_size=8, max_w_size=8, p=1), P.ToTensor(dict (mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) ]) return lambda img: transform(image = np.array(img))["image"]
def AlbumentationTrainTransform(self): tf = tc.Compose([ta.HorizontalFlip(p=0.5), ta.Rotate(limit=(-20, 20)), # ta.VerticalFlip(p=0.5), # ta.Cutout(num_holes=3, max_h_size=8, max_w_size=8, p=0.5), # ta.Blur(), # ta.ChannelShuffle(), # ta.InvertImg(), ta.RandomCrop(height=30, width=30, p=5.0), ta.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), tp.ToTensor() ]) return lambda img: tf(image = np.array(img))["image"]
def AlbumentationTestTransform(self): tf = tc.Compose([ta.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), tp.ToTensor() # tp.ToTensor(dict(mean=(0.4914, 0.4822, 0.4465), std=(0.247, 0.2435, 0.2616))) ]) return lambda img: tf(image = np.array(img))["image"]
def model12_test_transforms(): transform = C.Compose([ P.ToTensor(dict (mean=(0.4802, 0.4481, 0.3975), std=(0.2302, 0.2265, 0.2262))) ]) return lambda img: transform(image = np.array(img))["image"]
def model9_resnet_test_transforms(): transforms = C.Compose([ P.ToTensor(dict (mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) ]) return lambda img: transforms(image = np.array(img))["image"]