def rotate_images(tmpdir, rot): original_dimensions = (800, 800) im_tmp = tmpdir.mkdir("subfolder").join('test.JPEG') im = Image.new('RGB', original_dimensions) im.save(str(im_tmp), 'JPEG') r = Operations.Rotate(probability=1, rotation=rot) im = [im] im_r = r.perform_operation(im) assert im_r is not None assert im_r[0].size == original_dimensions
def test_rotate_images_custom_temp_files(): original_dimensions = (800, 800) tmpdir = tempfile.mkdtemp() tmp = create_temp_file(tmpdir, '.JPEG') im = Image.new('RGB', original_dimensions) im.save(tmp.name, 'JPEG') r = Operations.Rotate(probability=1, rotation=90) im = [im] im_r = r.perform_operation(im) assert im_r is not None assert im_r[0].size == original_dimensions shutil.rmtree(tmpdir)
def test_rotate_images_custom_temp_files(): original_dimensions = (800, 800) tmpdir = tempfile.mkdtemp() tmp = tempfile.NamedTemporaryFile(dir=tmpdir, suffix='.JPEG') im = Image.new('RGB', original_dimensions) im.save(tmp.name, 'JPEG') r = Operations.Rotate(probability=1, rotation=90) im_r = r.perform_operation(im) assert im_r is not None assert im_r.size == original_dimensions tmp.close() shutil.rmtree(tmpdir)
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]