def test_in_memory_distortions(): im, tmpdir = create_colour_temp_image((800, 800), "JPEG") r_d = Operations.Distort(probability=1, grid_width=8, grid_height=8, magnitude=8) tmp_im = r_d.perform_operation([im]) assert tmp_im is not None assert tmp_im[0].size == (800, 800) im_bw, tmpdir_bw = create_greyscale_temp_image((800, 800), "PNG") r_d_bw = Operations.Distort(probability=1, grid_width=8, grid_height=8, magnitude=8) tmp_im_bw = r_d_bw.perform_operation([im_bw]) assert tmp_im_bw is not None assert tmp_im_bw[0].size == (800, 800) assert isinstance(tmp_im_bw[0], Image.Image) shutil.rmtree(tmpdir) shutil.rmtree(tmpdir_bw)
def get_augmentation_group(data_aug_group, input_size, center=True, resize=True): resize_prob = 1 if resize else 0 center_prob = 1 if center else 0 DATA_AUGMENTATION_GROUPS = [ # GROUP 0 (NO DATA AUGMENTATION) [ # Center crop CropCenter(probability=center_prob), # Resize the image to the desired input size Operations.Resize(probability=resize_prob, width=input_size[0], height=input_size[1], resample_filter="BICUBIC") ], # GROUP 1 (Common transformations: rotations, flips, crops, shears) [ # Rotate the image by 90 degrees randomly Operations.Rotate(probability=0.5, rotation=-1), # Flip top/bottom Operations.Flip(probability=0.5, top_bottom_left_right="TOP_BOTTOM"), # Flip left/right Operations.Flip(probability=0.5, top_bottom_left_right="LEFT_RIGHT"), # Random crops Operations.CropPercentage(probability=0.5, percentage_area=0.85, centre=center, randomise_percentage_area=True), # Resize the image to the desired input size Operations.Resize(probability=resize_prob, width=input_size[0], height=input_size[1], resample_filter="BICUBIC") ], # GROUP 2 (Pixel intensity transformations) [ # Rotate the image by 90 degrees randomly Operations.Rotate(probability=0.5, rotation=-1), # Flip top/bottom Operations.Flip(probability=0.5, top_bottom_left_right="TOP_BOTTOM"), # Flip left/right Operations.Flip(probability=0.5, top_bottom_left_right="LEFT_RIGHT"), # Random change brightness of the image Operations.RandomBrightness(probability=0.5, min_factor=0.5, max_factor=1.5), # Random change saturation of the image Operations.RandomColor(probability=0.5, min_factor=0.5, max_factor=1.5), # Random change saturation of the image Operations.RandomContrast(probability=0.5, min_factor=0.5, max_factor=1.5), # Random crops Operations.CropPercentage(probability=0.5, percentage_area=0.85, centre=center, randomise_percentage_area=True), # Resize the image to the desired input size Operations.Resize(probability=resize_prob, width=input_size[0], height=input_size[1], resample_filter="BICUBIC") ], # GROUP 3 (Perspective transformations) [ # Rotate the image by 90 degrees randomly Operations.Rotate(probability=0.5, rotation=-1), # Flip top/bottom Operations.Flip(probability=0.5, top_bottom_left_right="TOP_BOTTOM"), # Flip left/right Operations.Flip(probability=0.5, top_bottom_left_right="LEFT_RIGHT"), # Shear Image Operations.Shear(probability=0.5, max_shear_left=20, max_shear_right=20), # Random Tilt up down Operations.Skew(probability=0.5, skew_type="TILT_TOP_BOTTOM", magnitude=1.0), # Random Tilt left right Operations.Skew(probability=0.5, skew_type="TILT_LEFT_RIGHT", magnitude=1.0), # Random Skew CORNER Operations.Skew(probability=0.5, skew_type="CORNER", magnitude=1.3), # Random crops Operations.CropPercentage(probability=0.5, percentage_area=0.85, centre=center, randomise_percentage_area=True), # Resize the image to the desired input size Operations.Resize(probability=resize_prob, width=input_size[0], height=input_size[1], resample_filter="BICUBIC") ], # GROUP 4 (Noise transformations) [ # Center crop CropCenter(probability=1), # Rotate the image by 90 degrees randomly Operations.Rotate(probability=0.5, rotation=-1), # Flip top/bottom Operations.Flip(probability=0.5, top_bottom_left_right="TOP_BOTTOM"), # Flip left/right Operations.Flip(probability=0.5, top_bottom_left_right="LEFT_RIGHT"), # Random distortions Operations.Distort(probability=0.5, grid_width=8, grid_height=8, magnitude=15), # Random erasing Operations.RandomErasing(probability=0.5, rectangle_area=0.25), # Random crops Operations.CropPercentage(probability=0.5, percentage_area=0.85, centre=center, randomise_percentage_area=True), # Resize the image to the desired input size Operations.Resize(probability=resize_prob, width=input_size[0], height=input_size[1], resample_filter="BICUBIC") ] ] return DATA_AUGMENTATION_GROUPS[data_aug_group]