def DA_Superpixels(inputimg): assert inputimg.ndim in [2, 3], "input invalid! Please check input" AUG_OPS_NAME = ["Superpixels_25_64", "Superpixels_50_64", "Superpixels_25_128", "Superpixels_50_128"] Superpixels_param = [(0.25, 64), (0.5, 64), (0.25, 128), (0.5, 128)] ret = [] for i in np.arange(len(Superpixels_param)): VALUE = Superpixels_param[i] Name = AUG_OPS_NAME[i] if Name == "Superpixels_25_64": aug_img = iaa.Superpixels(p_replace=VALUE[0], n_segments=VALUE[1]).augment_image(inputimg) ret.append((Name, str(VALUE[0]) + "_" + str(VALUE[1]), aug_img)) elif Name == "Superpixels_50_64": aug_img = iaa.Superpixels(p_replace=VALUE[0], n_segments=VALUE[1]).augment_image(inputimg) ret.append((Name, str(VALUE[0]) + "_" + str(VALUE[1]), aug_img)) elif Name == "Superpixels_25_128": aug_img = iaa.Superpixels(p_replace=VALUE[0], n_segments=VALUE[1]).augment_image(inputimg) ret.append((Name, str(VALUE[0]) + "_" + str(VALUE[1]), aug_img)) elif Name == "Superpixels_50_128": aug_img = iaa.Superpixels(p_replace=VALUE[0], n_segments=VALUE[1]).augment_image(inputimg) ret.append((Name, str(VALUE[0]) + "_" + str(VALUE[1]), aug_img)) else: print "invalid ops in DA_Superpixels" print "DA_Superpixels aug size is ", len(ret) assert len(ret) == 4, "DA_Superpixels Fails" return ret
def chapter_augmenters_superpixels(): aug = iaa.Superpixels(p_replace=0.5, n_segments=64) run_and_save_augseq("segmentation/superpixels_50_64.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2) aug = iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128)) run_and_save_augseq("segmentation/superpixels.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2) #ps = [1/8*i for i in range(8)] ps = np.linspace(0, 1.0, num=8) run_and_save_augseq( "segmentation/superpixels_vary_p.jpg", [iaa.Superpixels(p_replace=p, n_segments=64) for p in ps], [ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1) ns = [16 * i for i in range(1, 9)] run_and_save_augseq( "segmentation/superpixels_vary_n.jpg", [iaa.Superpixels(p_replace=1.0, n_segments=n) for n in ns], [ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1)
def test_other_dtypes_float(self): # currently, no float dtype is actually accepted for dtype in []: def _allclose(a, b): atol = 1e-4 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) isize = np.dtype(dtype).itemsize for value in [0, 1.0, 10.0, 1000 ** (isize - 1)]: v1 = (-1) * value v2 = value aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [v1, v1, v2, v2], [v1, v1, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [v2, v2, v2, v2], [v1, v2, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, (7/8)*v2 + (1/8)*v1)
def augment(): with open(CONFIG, "r") as file: config = json.loads(file.read()) if config[ImageAugmentation.AUGMENT_DATA]: matrix = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) return iaa.Sometimes(ImageAugmentation.AUG_PERCENTAGE, [ iaa.GaussianBlur(sigma=2.0), iaa.Sequential([iaa.Affine(rotate=45), iaa.Sharpen(alpha=1.0)]), iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 0, iaa.Add((10, 50)))), iaa.AdditiveGaussianNoise(scale=0.2 * 255), iaa.Add(50, per_channel=True), iaa.Sharpen(alpha=0.5), iaa.WithChannels(0, iaa.Add((10, 100))), iaa.WithChannels(0, iaa.Affine(rotate=(0, 45))), iaa.Noop(), iaa.Superpixels(p_replace=0.5, n_segments=64), iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128)), iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add((50, 100))), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB"), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(2, 11)), iaa.AverageBlur(k=((5, 11), (1, 3))), iaa.MedianBlur(k=(3, 11)), iaa.Convolve(matrix=matrix), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)), iaa.EdgeDetect(alpha=(0.0, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.0, 1.0), direction=(0.0, 1.0)), iaa.Add((-40, 40)), iaa.Add((-40, 40), per_channel=0.5), iaa.AddElementwise((-40, 40)), iaa.AddElementwise((-40, 40), per_channel=0.5), iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)), iaa.AdditiveGaussianNoise(scale=0.05 * 255), iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.Dropout(p=(0, 0.2)), iaa.Dropout(p=(0, 0.2), per_channel=0.5), iaa.CoarseDropout(0.02, size_percent=0.5), iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)), iaa.CoarseDropout(0.02, size_percent=0.15, per_channel=0.5), iaa.Invert(0.25, per_channel=0.5), iaa.Invert(0.5), iaa.ContrastNormalization((0.5, 1.5)), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25) ]) else: return None
def __init__(self): self._img_allowed_scales = [0.25, 0.5, .75, 1, 1.25, 1.5, 2] sometimes = lambda aug: iaa.Sometimes(0.5, aug) self._aug_pipe_object = iaa.Sequential( [ iaa.Fliplr(0.5), sometimes(iaa.Affine( rotate=(-20, 20), )), iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0.20, 0.5), n_segments=(20, 100))), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), sometimes(iaa.PerspectiveTransform(scale=(0.025, 0.075))), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), ], random_order=True ) ], random_order=True ) self._aug_pipe_sample = iaa.Sequential( [ iaa.SomeOf((0, 3), [ sometimes(iaa.Superpixels(p_replace=(0.20, 0.5), n_segments=(20, 100))), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), ], random_order=True ), iaa.Add((-25, 25), per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), ], random_order=True )
def matsushita_aug(): return iaa.SomeOf((1, 4), [ iaa.Noop(), iaa.Superpixels(p_replace=0.5, n_segments=64), iaa.Add((-50, 50), per_channel=True), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.GaussianBlur(sigma=(0.1, 1.0)), iaa.ContrastNormalization((0.5, 2), per_channel=0.5), iaa.AdditiveGaussianNoise(scale=0.075 * 255, per_channel=0.5), iaa.CoarseDropout( (0.1, 0.25), size_percent=(0.02, 0.25), per_channel=0.5), iaa.OneOf([ iaa.Emboss(alpha=0.25), iaa.Sharpen(alpha=0.5), iaa.Invert(1, per_channel=1.0), ]), iaa.Affine(scale={ 'x': (0.84, 1.16), 'y': (0.84, 1.16) }, mode=['edge', 'wrap']), iaa.Affine(translate_percent={ 'x': (-0.16, 0.16), 'y': (-0.16, 0.16) }, mode=['edge', 'wrap']), iaa.Affine(rotate=(-16, 16), mode=['edge', 'wrap']), iaa.Affine(shear=(-16, 16), mode=['edge', 'wrap']), iaa.Fliplr(1) ])
def light_augmentation(img): sometimes = lambda aug: iaa.Sometimes(0.5, aug) sometimes2 = lambda aug: iaa.Sometimes(0.2, aug) seq = iaa.Sequential([ # sometimes(iaa.Crop(px=(0, 16))), iaa.Fliplr(0.5), iaa.Flipud(0.5), sometimes( iaa.OneOf([ iaa.GaussianBlur( (0, 2.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(2, 7) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(3, 11) ), # blur image using local medians with kernel sizes between 2 and 7 ]), ), sometimes( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }, # translate by -20 to +20 percent (per axis) rotate=(-5, 5), # rotate by -45 to +45 degrees shear=(-5, 5), # shear by -16 to +16 degrees )), sometimes2(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))) ]) return seq.augment_image(img)
def augment_image(img_array): """Augment images randomly Parameters ---------- img_array : array-like shape(N, W, H, C) Returns ---------- img_array : array-like Augmented images """ def st(aug): return iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ st(iaa.Multiply(mul=(0.25, 1.50))), # Brightness st(iaa.Crop(percent=(0, 0.1))), # Random Crop st(iaa.Dropout((0, 0.7))), # Pixel Dropout st(iaa.Invert(1)), st(iaa.Affine(translate_percent=(0, 0.3), shear=(-45, 45))), # Shear st(iaa.Superpixels(p_replace=(0, 0.25), n_segments=100)) ], random_order=True) # Visualize Augmentation # idx = np.random.choice(np.arange(len(img_array))) # seq.show_grid(img_array[idx], cols=8, rows=8) # assert 0 return seq.augment_images(img_array)
def augment(img, steering_angle): # Flip - odbicie lustrzane if random.random() > 0.5: img = img[:, ::-1, :] steering_angle = -steering_angle #blur - rozmazanie blurer = iaa.GaussianBlur(iap.Uniform(0.1, 1.0)) img = blurer.augment_image(img) #shuffle ColorShuffle = iaa.ChannelShuffle(p=0.7) img = ColorShuffle.augment_image(img) #SuperPixels superpixel = iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)) img = superpixel.augment_image(img) #Fog Clouds = iaa.Clouds() img = Clouds.augment_image(img) #Snowflakes # Snowflakes = iaa.Snowflakes(flake_size=(0.1, 0.4), speed=(0.01, 0.05)) # img = Snowflakes.augment_image(img) #Translate tx = random.randint(-20,20) translater = iaa.Affine(translate_px = {"x":tx}, mode = 'edge') img = translater.augment_image(img) steering_angle += tx*0.02 return img, steering_angle
def customizedImgAug(input_img): rarely = lambda aug: iaa.Sometimes(0.1, aug) sometimes = lambda aug: iaa.Sometimes(0.25, aug) often = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), often( iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.12, 0) }, rotate=(-10, 10), shear=(-8, 8), order=[0, 1], cval=(0, 255), )), iaa.SomeOf((0, 4), [ rarely(iaa.Superpixels(p_replace=(0, 0.3), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), iaa.AverageBlur(k=(2, 4)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 0.3), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.5)), rarely( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.3)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.0, 0.05), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.05), size_percent=(0.01, 0.05), per_channel=0.2), ]), rarely(iaa.Invert(0.05, per_channel=True)), often(iaa.Add((-40, 40), per_channel=0.5)), iaa.Multiply((0.7, 1.3), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))), sometimes(iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25)), ], random_order=True), iaa.Fliplr(0.5), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ], random_order=True) # apply augmenters in random order output_img = seq.augment_image(input_img) return output_img
def augmentation2(image, mask): sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), iaa.SomeOf( (0, 5), [ sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur( (0, 3.0) ), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout( (0.01, 0.1), per_channel=0.5 ), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation( (-20, 20)), # change hue and saturation iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), ]), iaa.Grayscale(alpha=(0.0, 1.0)), ], random_order=True) ], random_order=True) image_heavy, mask_heavy = seq(images=image, segmentation_maps=mask) return image_heavy, mask_heavy
def generateAugSeq(): sometimes = lambda aug: iaa.Sometimes(0.5, aug) return iaa.Sequential([ sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, order=[0, 1], cval=(0, 255), mode=ia.ALL)), iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation((-20, 20)), iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha(exponent=(-4, 0), first=iaa.Multiply( (0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization( (0.5, 2.0))) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), ], random_order=True) ], random_order=True)
def logic(self, image): for param in self.augmentation_params: self.augmentation_data.append([ str(param.augmentation_value), iaa.Superpixels( p_replace=param.augmentation_value, n_segments=100).to_deterministic().augment_image(image), param.detection_tag ])
def test_p_replace_stochastic_parameter_n_segments_2(self): aug = iaa.Superpixels( p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(self.base_img) assert ( np.allclose(observed, self.base_img) or self._array_equals_tolerant( observed, self.base_img_superpixels, 2) )
def test_other_dtypes_bool(self): aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [False, False, True, True], [False, False, True, True] ], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug == img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [True, True, True, True], [False, True, True, True] ], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug)
def test_failure_on_invalid_datatype_for_n_segments(self): # note that assertRaisesRegex does not exist in 2.7 got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception
def superpixel(input): image11 = cv2.imread(input) #label = input.rsplit('/', 1)[1].rsplit('.', 1)[0] h, w = image11.shape[0:2] seq11 = st(iaa.Superpixels(p_replace=0.63)) y = seq11.name.rsplit('Unnamed')[1] images_aug11 = seq11.draw_grid(image11, cols=1, rows=1) input = input.rsplit('.')[0] misc.imsave( '/home/ahmed/Pictures/cogedis/24072017/all_augmented/' + str(input) + '_superpixel.png', images_aug11)
def test_get_parameters(self): aug = iaa.Superpixels( p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest"
def gen(self, batch_size): valid_target_len = self.valid_target_len with open(self.annotation_path, 'r') as ann_file: lines = ann_file.readlines() random.shuffle(lines) for l in lines: img_path = l.strip().split()[0] lex = l.strip().split(img_path + ' ')[-1] try: img_bw, word = self.read_data(img_path, lex) if random.random() < 0.5: augMethodsNumber = int(math.floor(random.random() * 2) + 1) seq = iaa.SomeOf(augMethodsNumber, [ iaa.CropAndPad( px=((0, 30), (0, 10), (0, 30), (0, 10)), pad_mode=["constant", "edge"], pad_cval=(0, 128), ), iaa.Add((-100, 100)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=random.random()*0.1, n_segments=4), # iaa.Grayscale(alpha=(0.0, 0.1)), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(1, 4)), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Dropout(p=(0, 0.2)), iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)) ], random_order=True) img_bw = seq.augment_images(img_bw) if valid_target_len < float('inf'): word = word[:valid_target_len + 1] width = img_bw.shape[-2] # TODO:resize if > 320 b_idx = min(width, self.bucket_max_width) bs = self.bucket_data[b_idx].append(img_bw, word, os.path.join(self.data_root,img_path)) if bs >= batch_size: b = self.bucket_data[b_idx].flush_out( self.bucket_specs, valid_target_length=valid_target_len, go_shift=1) if b is not None: yield b else: assert False, 'no valid bucket of width %d'%width except IOError: pass # ignore error images #with open('error_img.txt', 'a') as ef: # ef.write(img_path + '\n') self.clear()
def test_other_dtypes_uint_int(self): for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: min_value, center_value, max_value = \ iadt.get_value_range_of_dtype(dtype) if np.dtype(dtype).kind == "i": values = [int(center_value), int(0.1 * max_value), int(0.2 * max_value), int(0.5 * max_value), max_value-100] values = [((-1)*value, value) for value in values] else: values = [(0, int(center_value)), (10, int(0.1 * max_value)), (10, int(0.2 * max_value)), (10, int(0.5 * max_value)), (0, max_value), (int(center_value), max_value)] for v1, v2 in values: aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [v1, v1, v2, v2], [v1, v1, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.array_equal(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [v2, v2, v2, v2], [v1, v2, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.all(img_aug == int(np.round((7/8)*v2 + (1/8)*v1)))
def __init__(self): self.seq = iaa.Sequential([ iaa.SomeOf(2, [ iaa.Superpixels(p_replace=0.5, n_segments=100), iaa.GaussianBlur(0, 2.0), iaa.BilateralBlur(5, sigma_color=250, sigma_space=250), iaa.Sharpen(alpha=1), iaa.Emboss(alpha=1), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25), iaa.ContrastNormalization((0.5, 2.0)) ], random_order=True) ])
def draw_single_sequential_images(): image = misc.imresize( ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) st = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Flipud(0.5), st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), st(iaa.Crop(percent=(0, 0.1))), st(iaa.GaussianBlur((0, 3.0))), st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), st( iaa.Sometimes( 0.5, iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), )), st( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), st(iaa.Invert(0.25, per_channel=True)), st(iaa.Add((-10, 10), per_channel=0.5)), st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), st(iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale")), st( iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=[0, 1], cval=(0, 255), mode=ia.ALL)), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) ], random_order=True) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
def __call__(self, sample): image, mask = sample if np.random.uniform(0, 1) > 0.5: seq = iaa.Sequential( [ iaa.SomeOf( (1, None), [ iaa.AdditiveGaussianNoise(scale=(0, 0.2 * 255)), # 添加高斯噪音 iaa.SomeOf( (1, None), [ iaa.Sharpen(alpha=(0.1, 0.3), lightness=(0.7, 1.3)), # 锐化 iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # 浮雕效果 iaa.Invert(0.05, per_channel=True), # 5%的概率反转像素的强度,即原来的强度为v那么现在的就是255-v ], random_order=True # 随机的顺序把这些操作用在图像上 ), # 用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法 iaa.OneOf( [ iaa.GaussianBlur(sigma=(0, 1.0)), # 高斯模糊 iaa.MedianBlur(k=(3, 11)), # 中值模糊 iaa.AverageBlur(k=(2, 7)), # 均值模糊。 ] ), iaa.OneOf( [ iaa.Multiply((0.5, 1.5), per_channel=0.5), # 像素乘上0.5或者1.5之间的数字. iaa.Add((-10, 10), per_channel=0.5), # 每个像素随机加减-10到10之间的数 ] ), sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)), # 超像素的表示 ), sometimes( # 把像素移动到周围的地方。这个方法在mnist数据集增强中有见到 iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), iaa.Grayscale(alpha=(0.0, 1.0)), # 将RGB变成灰度图然后乘alpha加在原图上 ], random_order=True # 随机的顺序把这些操作用在图像上 ), ] ) image = seq.augment_image(image) return image, mask
def main(): image = data.astronaut()[..., ::-1] # rgb2bgr print(image.shape) cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image) cv2.waitKey(TIME_PER_STEP) for n_segments in cycle(reversed(np.arange(1, 200, SEGMENTS_PER_STEP))): aug = iaa.Superpixels(p_replace=0.75, n_segments=n_segments) time_start = time.time() img_aug = aug.augment_image(image) print("augmented %d in %.4fs" % (n_segments, time.time() - time_start)) img_aug = ia.draw_text(img_aug, x=5, y=5, text="%d" % (n_segments, )) cv2.imshow("aug", img_aug) cv2.waitKey(TIME_PER_STEP)
def __init__(self,with_mask=True): self.with_mask = with_mask self.seq = iaa.Sequential( [ iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 0.5), n_segments=(100, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images #iaa.OneOf([ # iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels # iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), #]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-5, 5), per_channel=0.5), # change brightness of images iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.LinearContrast((0.5, 2.0)) ) ]), iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)) ], random_order=True ) ], random_order=True )
def test_p_replace_050_n_segments_2(self): aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(self.base_img) if self._array_equals_tolerant(observed, self.base_img, 2): seen["none"] = True elif self._array_equals_tolerant( observed, self.base_img_superpixels_left, 2): seen["left"] = True elif self._array_equals_tolerant( observed, self.base_img_superpixels_right, 2): seen["right"] = True elif self._array_equals_tolerant( observed, self.base_img_superpixels, 2): seen["both"] = True else: raise Exception( "Generated superpixels image does not match any " "expected image.") if all(seen.values()): break assert all(seen.values())
def _load_augmentation_aug_all(): """ Load image augmentation model """ def sometimes(aug): return iaa.Sometimes(0.5, aug) return iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode='constant', pad_cval=(0, 255))), sometimes( iaa.Affine( # scale images to 80-120% of their size, individually per axis scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # translate by -20 to +20 percent (per axis) translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees # use nearest neighbour or bilinear interpolation (fast) order=[0, 1], # if mode is constant, use a cval between 0 and 255 cval=(0, 255), # use any of scikit-image's warping modes # (see 2nd image from the top for examples) mode='constant')), # execute 0 to 5 of the following (less important) augmenters per # image don't execute all of them, as that would often be way too # strong iaa.SomeOf( (0, 5), [ # convert images into their superpixel representation sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), iaa.OneOf([ # blur images with a sigma between 0 and 3.0 iaa.GaussianBlur((0, 3.0)), # blur image using local means with kernel sizes # between 2 and 7 iaa.AverageBlur(k=(2, 7)), # blur image using local medians with kernel sizes # between 2 and 7 iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.BlendAlphaSimplexNoise( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), # add gaussian noise to images iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ # randomly remove up to 10% of the pixels iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), # invert color channels iaa.Invert(0.05, per_channel=True), # change brightness of images (by -10 to 10 of original value) iaa.Add((-10, 10), per_channel=0.5), # change hue and saturation iaa.AddToHueAndSaturation((-20, 20)), # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.BlendAlphaFrequencyNoise( exponent=(-4, 0), foreground=iaa.Multiply( (0.5, 1.5), per_channel=True), background=iaa.contrast.LinearContrast((0.5, 2.0))) ]), # improve or worsen the contrast iaa.contrast.LinearContrast((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), # move pixels locally around (with random strengths) sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # sometimes move parts of the image around sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True)
def run_seq(): # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second # image. # if not isinstance(images,list): # images=[images] sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image. seq = iaa.Sequential( [ # # Apply the following augmenters to most images. # iaa.Fliplr(0.5), # horizontally flip 50% of all images # iaa.Flipud(0.2), # vertically flip 20% of all images # crop some of the images by 0-10% of their height/width sometimes(iaa.Crop(percent=(0, 0.1))), # sometimes(iaa.Crop(percent=(0, 0.05))), # Apply affine transformations to some of the images # - scale to 80-120% of image height/width (each axis independently) # - translate by -20 to +20 relative to height/width (per axis) # - rotate by -45 to +45 degrees # - shear by -16 to +16 degrees # - order: use nearest neighbour or bilinear interpolation (fast) # - mode: use any available mode to fill newly created pixels # see API or scikit-image for which modes are available # - cval: if the mode is constant, then use a random brightness # for the newly created pixels (e.g. sometimes black, # sometimes white) sometimes( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # rotate=(-45, 45), rotate=(-5, 5), # shear=(-16, 16), shear=(-5, 5), order=[0, 1], cval=(0, 255), mode=ia.ALL)), # # Execute 0 to 5 of the following (less important) augmenters per # image. Don't execute all of them, as that would often be way too # strong. # iaa.SomeOf( (0, 5), [ # Convert some images into their superpixel representation, # sample between 20 and 200 superpixels per image, but do # not replace all superpixels with their average, only # some of them (p_replace). sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # Blur each image with varying strength using # gaussian blur (sigma between 0 and 3.0), # average/uniform blur (kernel size between 2x2 and 7x7) # median blur (kernel size between 3x3 and 11x11). iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), # Sharpen each image, overlay the result with the original # image using an alpha between 0 (no sharpening) and 1 # (full sharpening effect). iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # Same as sharpen, but for an embossing effect. iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # Search in some images either for all edges or for # directed edges. These edges are then marked in a black # and white image and overlayed with the original image # using an alpha of 0 to 0.7. sometimes( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per # pixel (i.e. brightness change). iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # Either drop randomly 1 to 10% of all pixels (i.e. set # them to black) or drop them on an image with 2-5% percent # of the original size, leading to large dropped # rectangles. iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), # Invert each image's channel with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Add((-10, 10), per_channel=0.5), # Change brightness of images (50-150% of original value). iaa.Multiply((0.5, 1.5), per_channel=0.5), # Improve or worsen the contrast of images. iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. iaa.Grayscale(alpha=(0.0, 1.0)), # In some images move pixels locally around (with random # strengths). sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # In some images distort local areas with varying strength. sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) ], # do all of the above augmentations in random order random_order=True) ], # do all of the above augmentations in random order random_order=True) # images_aug = seq(images=images) return seq
from keras.models import load_model from keras.optimizers import SGD from keras.preprocessing import image as keras_image from keras.callbacks import ModelCheckpoint from tqdm import tqdm as tqdm BATCH_SIZE = 3 * 3 SPLIT_NUM = 4 H_CHANNEL = 4 S_CHANNEL = 4 seq = iaa.Sequential([ iaa.Fliplr(0.02), iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.3, iaa.GaussianBlur(sigma=(0, 0.5))), iaa.Sometimes(0.3, iaa.Superpixels(0.5, 125)), iaa.Add((-5, 5), True), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-10, 10), shear=(-5, 5)), iaa.OneOf([ iaa.PerspectiveTransform(0.05), iaa.PiecewiseAffine(0.02)]) ], random_order=True) def get_image_hist(image_path): image = cv2.imread(image_path) num_row = image.shape[0]
def __init__(self, p_replace=0.1, n_segments=100, p=0.5): super(IAASuperpixels, self).__init__(p) self.processor = iaa.Superpixels(p_replace=p_replace, n_segments=n_segments)