def chapter_augmenters_cutout(): aug = iaa.Cutout(nb_iterations=2) run_and_save_augseq("arithmetic/cutout_nb_iterations_2.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Cutout(nb_iterations=(1, 5), size=0.2, squared=False) run_and_save_augseq("arithmetic/cutout_non_square.jpg", aug, [ia.quokka(size=(128, 170)) for _ in range(3 * 2)], cols=3, rows=2) aug = iaa.Cutout(fill_mode="constant", cval=255) run_and_save_augseq("arithmetic/cutout_cval_255.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=0.5) run_and_save_augseq("arithmetic/cutout_rgb.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.Cutout(fill_mode="gaussian", fill_per_channel=True) run_and_save_augseq("arithmetic/cutout_gaussian.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2)
def __init__(self,num_of_augms=0): self.num_of_augms=num_of_augms self.aug=iaa.OneOf([ iaa.Sequential([ iaa.LinearContrast(alpha=(0.75, 1.5)), iaa.Fliplr(0.5) ]), iaa.Sequential([ iaa.Grayscale(alpha=(0.1, 0.9)), iaa.Affine( translate_percent={"y": (-0.15, 0.15)} ) ]), iaa.Sequential([ iaa.LinearContrast((0.6, 1.4)), iaa.ShearX((-10, 10)) ]), iaa.Sequential([ iaa.GaussianBlur(sigma=(0, 1)), iaa.ShearY((-10, 10)) ]), iaa.Sequential([ iaa.Cutout(nb_iterations=(1, 2), size=0.1, squared=False), iaa.Multiply((0.8, 1.2), per_channel=0.25), iaa.Fliplr(0.5), ]), iaa.Sequential([ iaa.LinearContrast((0.6, 1.4)), iaa.Affine( translate_percent={"x": (-0.25, 0.25)} ) ]), iaa.Sequential([ iaa.Cutout(nb_iterations=(1, 5), size=0.1, squared=False), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 15), per_channel=0.5), iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, ) ]), iaa.Sequential([ iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)), iaa.GaussianBlur(sigma=(0, 2)), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)} )]) # iaa.Sequential([ # iaa.Cutout(nb_iterations=(1, 5), size=0.05, squared=False), # iaa.Grayscale(alpha=(0.0, 0.50)), # iaa.ScaleX((0.75, 1.25)) # ]), # iaa.Sequential([ # iaa.LinearContrast((0.8, 1.2), per_channel=True), # iaa.PerspectiveTransform(scale=(0.01, 0.15)) # ]) ])
def __call__(self, x, y, choice): if choice: if self.fillcolor: aug = iaa.Cutout(size=0.5, fill_mode='constant', cval=(0, 255), fill_per_channel=0.5) else: aug = iaa.Cutout(size=0.5, fill_mode='constant', cval=0) x = aug.augment_image(x) return x, y
def setup_augmentation(self): # Augmentation # aug = iaa.Sequential([ # #iaa.Sometimes(0.5, iaa.PerspectiveTransform(0.05)), # #iaa.Sometimes(0.5, iaa.CropAndPad(percent=(-0.05, 0.1))), # #iaa.Sometimes(0.5, iaa.Affine(scale=(1.0, 1.2))), # iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.05, size_percent=0.01) ),F # iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())), # iaa.Sometimes(0.5, iaa.Add((-0.1, 0.1), per_channel=0.3)), # iaa.Sometimes(0.3, iaa.Invert(0.2, per_channel=True)), # iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)), # iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))), # iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))], # random_order=False) # aug = iaa.Sequential([ # #iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.25, size_percent=0.02) ), # iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())), # iaa.Sometimes(0.5, iaa.Add((-60, 60), per_channel=0.3)), # iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)), # iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))), # iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))], # random_order=False) aug = iaa.Sequential( [ #iaa.Sometimes(0.5, PerspectiveTransform(0.05)), #iaa.Sometimes(0.5, CropAndPad(percent=(-0.05, 0.1))), iaa.Sometimes(0.5, iaa.Affine(scale=(1.0, 1.2))), #iaa.Sometimes(0.5, iaa.CoarseDropout( p=0.2, size_percent=0.05) ), iaa.Sometimes( 0.5, iaa.SomeOf(2, [ iaa.CoarseDropout(p=0.2, size_percent=0.05), iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=0.5), iaa.Cutout(fill_mode="constant", cval=(255)), iaa.CoarseSaltAndPepper(0.05, size_px=(4, 16)), iaa.CoarseSalt(0.05, size_percent=(0.01, 0.1)) ])), iaa.Sometimes(0.5, iaa.GaussianBlur(1.2 * np.random.rand())), iaa.Sometimes(0.5, iaa.Add((-25, 25), per_channel=0.3)), iaa.Sometimes(0.3, iaa.Invert(0.2, per_channel=True)), iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)), iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))), iaa.Sometimes( 0.5, iaa.ContrastNormalization( (0.5, 2.2), per_channel=0.3)) ], random_order=False) return aug
def main(): aug = iaa.Cutout(fill_mode=["gaussian", "constant"], cval=(0, 255), fill_per_channel=0.5) image = ia.quokka() images_aug = aug(images=[image] * 16) ia.imshow(ia.draw_grid(images_aug, cols=4, rows=4))
def __call__(self, sample): img, annot = sample.image, sample.annotation unique_labels = np.unique( annot[:, 4].astype('int').astype('str')).tolist() bbs = BoundingBoxesOnImage([ BoundingBox(x1=ann[0], y1=ann[1], x2=ann[2], y2=ann[3], label=str(int(ann[4]))) for ann in annot ], shape=img.shape) aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels, foreground=iaa.Cutout( nb_iterations=int(round(self.v)), size=0.05, fill_mode="gaussian")) img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs) annot_aug = np.array( [[bb.x1, bb.y1, bb.x2, bb.y2, np.float32(bb.label)] for bb in bbs_aug]) # the shape has to be at least (0,5) if len(annot_aug) == 0: annot_aug = np.zeros((0, 5)) sample.image = img_aug sample.annotation = annot_aug
def __init__(self): self.aug = iaa.Sequential([ iaa.Sometimes( 0.15, iaa.OneOf([ iaa.GammaContrast(gamma=(0, 1.75)), iaa.pillike.Autocontrast(cutoff=(0, 15.0)) ])), iaa.Sometimes( 0.15, iaa.OneOf([ iaa.HistogramEqualization(), iaa.pillike.Equalize(), ])), iaa.Sometimes(0.1, iaa.Grayscale(alpha=(0.05, 1.0))), iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))), iaa.Sometimes(0.1, iaa.UniformColorQuantizationToNBits(nb_bits=(2, 8))), iaa.Sometimes( 0.3, iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30))), iaa.Sometimes( 0.2, iaa.Cutout( fill_mode="constant", cval=(0, 255), fill_per_channel=0.5)) ], random_order=True)
def cutout_bbox(magnitude: int, **kwargs) -> iaa.BlendAlphaBoundingBoxes: """ Only apply cutout to the bounding box area. Passing the height and width of the image as integers and as keywords will scale the bounding box according to the policy. Note, the cutout location is chosen randomly and will only appear if it falls within the bounding box. :type magnitude: int :param magnitude: magnitude of cutout :param kwargs: height: height of the image as int width: width of the image as int :rtype: iaa.BlendAlphaBoundingBoxes :return: Method to apply cutout only to bounding boxes """ level = int((magnitude / _MAX_MAGNITUDE) * CUTOUT_BBOX) cutout_args = {} if 'height' in kwargs and 'width' in kwargs: size = tuple( np.clip([level / kwargs['height'], level / kwargs['width']], 0.0, 1.0)) cutout_args['size'] = size return iaa.BlendAlphaBoundingBoxes(None, foreground=iaa.Cutout(**cutout_args))
def __call__(self, img, labels, mode=None): aug_labels = labels.copy() if random.random() < self.p: aug = iaa.Cutout(**self.params) img = aug.augment_image(img) return img, labels
def img_aug(img_name, base_dir): image = mpimg.imread(Path(base_dir, 'datasets', 'images', img_name)) ran = random.randint(1, 5) if ran == 1: aug = iaa.Sequential([ iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"), iaa.AddToHueAndSaturation((-60, 60)), iaa.ElasticTransformation(alpha=90, sigma=9), iaa.Cutout() ], random_order=True) image = aug(image=image) elif ran == 2: aug = iaa.BlendAlphaRegularGrid(nb_rows=2, nb_cols=2, foreground=iaa.Multiply(0.0), background=iaa.AveragePooling(8), alpha=[0.0, 0.0, 1.0]) image = aug(image=image) elif ran == 3: image = tf.image.adjust_brightness(image, 0.4) elif ran == 4: image = tf.image.random_flip_left_right(image) image = tf.image.rot90(image) image = tf.image.random_flip_up_down(image) elif ran == 5: image = tf.image.central_crop(image, central_fraction=0.5) return image
def cutout(magnitude: int, **kwargs) -> iaa.Cutout: """ Apply cutout anywhere in the image. Passing the height and width of the image as integers and as keywords will scale the bounding box according to the policy Tensorflow Policy Equivalent: cutout The cutout value in the policies is at a pixel level. The imgaug cutout augmentation method requires the cutout to be a percentage of the image. Passing the image height and width as kwargs will scale the cutout to the appropriate percentage. Otherwise the imgaug default of 20% will be used. :type magnitude: int :param magnitude: magnitude of cutout :param kwargs: height: height of the image as int width: width of the image as int :rtype: iaa.Cutout :return: Method to apply cutout to image """ level = int((magnitude / _MAX_MAGNITUDE) * CUTOUT_CONST) cutout_args = {} if 'height' in kwargs and 'width' in kwargs: size = tuple( np.clip([(level / kwargs['height']) * 2, (level / kwargs['width']) * 2], 0.0, 1.0)) cutout_args['size'] = size return iaa.Cutout(**cutout_args)
def aug_image(image): seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Affine(rotate= (-8, 8), shear = (-8, 8), mode='edge'), iaa.SomeOf((0, 2), [ iaa.GaussianBlur((0, 0.3)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), iaa.AddToHueAndSaturation((-5, 5)), # change hue and saturation iaa.PiecewiseAffine(scale=(0.01, 0.03)), iaa.PerspectiveTransform(scale=(0.01, 0.1)), iaa.JpegCompression(20, 40) ], random_order=True ), iaa.Cutout(nb_iterations=1, size=(0.02, 0.2), squared=False) ]) image = seq.augment_image(image) return image
def __init__(self, agumentation={}): try: self.aug = iaa.Sequential([ iaa.Resize((224, 224)), iaa.Sometimes( float(agumentation['GaussianBlur_P']), iaa.GaussianBlur( sigma=(agumentation["GaussianBlur_sigma"]))), iaa.Sometimes( float(agumentation['MedianBlur_P']), iaa.imgcorruptlike.MotionBlur( severity=agumentation['MedianBlur_Severity'])), iaa.Sometimes( agumentation['DefocusBlur_P'], iaa.imgcorruptlike.DefocusBlur( severity=agumentation['DefocusBlur_Severity'])), iaa.Sometimes( agumentation['Cutout_P'], iaa.Cutout( nb_iterations=agumentation['Cutout_cum_of_cuts']), ), iaa.ChannelShuffle(agumentation['ChannelSuffle_P']), iaa.Sometimes( agumentation['ElasticTransformation_P'], iaa.ElasticTransformation( alpha=agumentation['ElasticTransformation_alpha'], sigma=agumentation['ElasticTransformation_sigma']), ), iaa.Sometimes( agumentation['PerspectiveTransform_P'], iaa.PerspectiveTransform( scale=agumentation['PerspectiveTransform_scale']), ), ], random_order=True) except: self.aug = iaa.Sequential([ iaa.Resize((224, 224)), iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))), iaa.Fliplr(0.5), iaa.Affine(rotate=(-20, 20), mode='symmetric'), iaa.Dropout2d(p=0.5), iaa.Sometimes(0.5, iaa.Cutout(nb_iterations=2)), iaa.Sometimes(0.25, iaa.Invert(0.25, per_channel=0.5)), iaa.Sometimes(0.25, iaa.Add((-40, 40))), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ])
def __init__(self, nb_iterations=(3, 8), size=0.1, determint=False): self.nb_iterations = nb_iterations self.size = size self.determint = determint self.func = iaa.Cutout(nb_iterations=nb_iterations, size=size, squared=False, fill_mode="gaussian")
def generate_cutout(): ia.seed(1) image = ia.quokka((128, 128)) images_aug = [] images_aug.append(image) images_aug.extend(iaa.Cutout()(images=[image] * 7)) images_aug.append(image) images_aug.extend( iaa.Cutout(nb_iterations=2, cval=(0, 255), fill_mode=["constant", "gaussian"], fill_per_channel=0.8)(images=[image] * 7)) _save("cutout.jpg", ia.draw_grid(images_aug, cols=7, rows=2))
def get_preview(images, augmentationList): """ Accepts a list of images and augmentationList as input. Provides a list of augmented images in that order as ouptut. """ augmented = [] for image in images: for augmentation in augmentationList: aug_id = augmentation['id'] params = augmentation['params'] if (aug_id == 1): image = iaa.SaltAndPepper(p=params[0], per_channel=params[1])(image=image) elif (aug_id == 2): image = iaa.imgcorruptlike.GaussianNoise( severity=(params[0], params[1]))(image=image) elif (aug_id == 3): image = iaa.Rain(speed=(params[0], params[1]), drop_size=(params[2], params[3]))(image=image) elif (aug_id == 4): image = iaa.imgcorruptlike.Fog( severity=(params[0], params[1]))(image=image) elif (aug_id == 5): image = iaa.imgcorruptlike.Snow( severity=(params[0], params[1]))(image=image) elif (aug_id == 6): image = iaa.imgcorruptlike.Spatter( severity=(params[0], params[1]))(image=image) elif (aug_id == 7): image = iaa.BlendAlphaSimplexNoise( iaa.EdgeDetect(1))(image=image) elif (aug_id == 8): image = iaa.Rotate(rotate=(params[0], params[1]))(image=image) elif (aug_id == 9): image = iaa.Affine()(image=image) #to be implemented elif (aug_id == 10): image = iaa.MotionBlur(k=params[0], angle=(params[1], params[2]))(image=image) elif (aug_id == 11): image = iaa.imgcorruptlike.ZoomBlur( severity=(params[0], params[1]))(image=image) elif (aug_id == 12): image = iaa.AddToBrightness()(image=image) #to be implemented elif (aug_id == 13): image = iaa.ChangeColorTemperature( kelvin=(params[0], params[1]))(image=image) elif (aug_id == 14): image = iaa.SigmoidContrast()(image=image) #to be implemented elif (aug_id == 15): image = iaa.Cutout(nb_iterations=(params[0], params[1]), size=params[2], squared=params[3])(image=image) else: print("Not implemented") augmented.append(image) return augmented
def fit(self, X: Dict[str, Any], y: Any = None) -> BaseImageAugmenter: if self.use_augmenter: self.augmenter: Augmenter = iaa.Sometimes( self.p, iaa.Cutout(nb_iterations=(1, 10), size=(0.1, 0.5), random_state=self.random_state), name=self.get_properties()['name']) return self
def __init__(self, train_file, class_list, batch_size=1, shuffle=True, transform=None, augment = False, crops_per_snapshot = 8, crop_size = (512,512), debug=False): self.train_file = train_file self.class_list = class_list self.transform = transform #region parse the provided class file try: with open(self.class_list, 'r', newline='') as file: self.classes = self.load_classes(csv.reader(file, delimiter=',')) except ValueError as e: raise (ValueError('invalid CSV class file: {}: {}'.format(self.class_list, e))) self.labels = {} for key, value in self.classes.items(): self.labels[value] = key #endregion #region parse annotations csv file # lines should be of format "img_path, x1, y1, x2, y2, x3, y3, class_name" try: with open(self.train_file, 'r', newline='') as file: self.image_data = self._read_annotations(csv.reader(file, delimiter=','), self.classes) except ValueError as e: raise (ValueError('invalid CSV annotations file: {}: {}'.format(self.train_file, e))) #endregion self.image_names = list(self.image_data.keys()) self.obj_indices = np.arange(len(self.image_names)) self.batch_size = batch_size self.shuffle = shuffle self.augment = augment self.debug = debug self.lock = threading.Lock() # mutex for input path self.yield_lock = threading.Lock() # mutex for generator yielding of batch self.init_count = 0 self.objects_id_generator = threadsafe_iter(get_objects_i_generator(len(self.image_names))) self.crops_per_snapshot = crops_per_snapshot self.crop_size = crop_size self.cache = {} if self.augment: self.seq = iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.GaussianBlur(sigma=(0, 5)), iaa.Affine(rotate=(-180, 180), translate_percent={'x': (-0.1, 0.1), 'y': (-0.1, 0.1)}, shear={'x': (-15, 15), 'y': (-15, 15)})], random_order=True) self.crop = iaa.CropToFixedSize(self.crop_size[0], self.crop_size[1]) self.crop_augmentations = iaa.Sequential([iaa.Cutout(nb_iterations=(1, 3), size=0.3, squared=False, cval=0), iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25))], random_order=True) else: self.seq = iaa.Identity()
def img_pro(old_path, new_path): img_list = os.listdir(old_path) num = 1 for img in img_list: old_img = old_path + img img_save = new_path + img old_img = imageio.imread(old_img) aug = iaa.Cutout(nb_iterations=250, size=[0.03, 0.04], cval=0) img_mask = aug.augment_image(old_img) print("img number:", num, 'processing img_name:', img) num += 1 imageio.imwrite(img_save, img_mask)
def augment_merged(image, N=100): """ Given An Image Create Augment N augmented copies of the image Since we prefer lists or numpy arrays, it is suggested to use OpenCV for opening images """ images = np.array([image for _ in range(N)], dtype=np.uint8) seq = iaa.Sequential( [ #iaa.Fliplr(0.5), # horizontal flips #iaa.Flipud(0.5), #iaa.CropAndPad(percent=(-0.10, 0.10), # pad_mode=["constant", "mean","maximum"], # pad_cval=(0, 128)), # random crops # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. #iaa.Sometimes(0.25, # iaa.GaussianBlur(sigma=(0, 0.5))), #iaa.CoarseDropout((0.0, 0.15), size_percent=(0.02, 0.15)), # Strengthen or weaken the contrast in each image. iaa.ContrastNormalization((0.7, 1.50)), # Add gaussian noise. # For 50% of all images, we sample the noise once per pixel. # For the other 50% of all images, we sample the noise per pixel AND # channel. This can change the color (not only brightness) of the # pixels. iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), #iaa.PiecewiseAffine(scale=(0.01, 0.07)), # Make some images brighter and some darker. # In 20% of all cases, we sample the multiplier once per channel, # which can end up changing the color of the images. iaa.Multiply((0.75, 1.25), per_channel=.35), #iaa.ChannelShuffle(0.5), iaa.Cutout(nb_iterations=(1, 3), size=(0.15, 0.25), squared=False, fill_mode="constant", cval=(0, 255), fill_per_channel=0.5) # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. #iaa.Affine( # scale={"x": (0.75, 1.5), "y": (0.75, 1.5)}, # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # rotate=(-20, 20), # shear=(-8, 8) #) ], random_order=True) # apply augmenters in random order images_aug = seq(images=images) return images_aug
def post_composition_augmentation(image_array): aug = iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=0.5, nb_iterations=(1, 5), size=0.25, squared=False) image_array = aug(image=image_array) enhancer = ImageEnhance.Brightness(Image.fromarray(image_array)) return enhancer.enhance(random.uniform(0.3, 1))
def __init__(self, thresh: float, n: int = 3, size: float = 0.3, squared: bool = False): self.name = "cutout" assert 0.0 <= thresh <= 1.0 assert 0.0 <= size <= 1.0 self.thresh = thresh self.transform = iaa.Cutout(nb_iterations=n, size=size, squared=squared, cval=0)
def init_augmenter(img_mode="color"): """Initializes the augmenters used in the training dataset :param config: the config object that contains all the """ ia.seed(10) if img_mode == 'color': return iaa.Sequential([ sometimes(iaa.Fliplr()), iaa.MultiplyBrightness((0.6, 1.4)), # TODO: try no ChangeColor or Brightness sometimes(iaa.ChangeColorTemperature((5000, 7000))), iaa.Crop(percent=( (0, 0.50), (0, 0.50), (0, 0.50), (0, 0.50) )), sometimes(iaa.OneOf([ iaa.Cutout(nb_iterations=(1, 4), size=0.2, squared=False, cval=(0, 255), fill_mode="constant"), iaa.Cutout(nb_iterations=(1, 4), size=0.2, squared=False, cval=( 0, 255), fill_mode="gaussian", fill_per_channel=True), iaa.AdditiveGaussianNoise(scale=(0, 0.1*255)) ])) ]) else: return iaa.Sequential([ sometimes(iaa.Fliplr()), iaa.Crop(percent=( (0, 0.40), (0, 0.40), (0, 0.40), (0, 0.40) )) ])
def __init__(self, augmentation_ratio=1.0, flip_probability=0.5, contrast_probability=0.2, defocus_probability=0.1, motion_blur_probability=0.2, cutout_probability=0.2, scale_probability=0.1, shift_probability=0.2, fog_probability=0.1): rot = iaa.Rot90(ia.ALL) shift = iaa.Affine(translate_percent=(-0.2, 0.2)) flip = iaa.OneOf([iaa.Fliplr(1), iaa.Flipud(1)]) scale = iaa.Affine(scale=(0.75, 1.25)) motion_blur = iaa.MotionBlur() cutout = iaa.Cutout( nb_iterations=(1, 3), size=(0.01, 0.1), squared=False) fog = iaa.imgcorruptlike.Fog(severity=1) defocus = iaa.imgcorruptlike.DefocusBlur(severity=1) contrast = iaa.imgcorruptlike.Contrast(severity=1) aug_weather = iaa.Sequential([ iaa.Sometimes(augmentation_ratio * fog_probability, fog), ], random_order=True) aug_initial = iaa.Sequential([ rot, iaa.Sometimes(flip_probability, flip), iaa.Sometimes(augmentation_ratio * contrast_probability, contrast) ], random_order=True) aug_camera = iaa.Sequential([ iaa.Sometimes(augmentation_ratio * defocus_probability, defocus), iaa.Sometimes(augmentation_ratio * motion_blur_probability, motion_blur), ], random_order=False) aug_obstacles = iaa.Sequential([ iaa.Sometimes(augmentation_ratio * cutout_probability, cutout), iaa.Sometimes(augmentation_ratio * scale_probability, scale), iaa.Sometimes(shift_probability, shift) ], random_order=False) self.augmentation_pipeline = iaa.Sequential( [aug_weather, aug_initial, aug_camera, aug_obstacles], random_order=False)
def img_aug(image, method): # image = mpimg.imread( # Path(config.base_dir, 'datasets', 'images', img_path)) # print('original image') # ia.imshow(image) # sequential if method == 'seq': aug = iaa.Sequential([ iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"), iaa.AddToHueAndSaturation((-60, 60)), iaa.ElasticTransformation(alpha=90, sigma=9), iaa.Cutout(), ], random_order=True) image = aug(image=image) # print("water-like") # ia.imshow(image) # blend alpha elif method == 'ba': aug = iaa.BlendAlphaVerticalLinearGradient(iaa.AveragePooling(11), start_at=(0.0, 1.0), end_at=(0.0, 1.0)) image = aug(image=image) # print('blend alpha') # ia.imshow(image) # mosaic elif method == 'ba_box': aug = iaa.BlendAlphaRegularGrid(nb_rows=2, nb_cols=2, foreground=iaa.Multiply(0.0), background=iaa.AveragePooling(8), alpha=[0.0, 0.0, 1.0]) image = aug(image=image) # print('blend_alpha with boxes') # ia.imshow(image) return image
def __call__(self, sample): img, annot = sample['img'], sample['annot'] bbs = BoundingBoxesOnImage([ BoundingBox(x1=ann[0], y1=ann[1], x2=ann[2], y2=ann[3], label=str(int(ann[4]))) for ann in annot ], shape=img.shape) aug = iaa.Cutout(nb_iterations=int(round(self.v)), size=0.05, fill_mode="gaussian") img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs) annot_aug = np.array( [[bb.x1, bb.y1, bb.x2, bb.y2, np.float32(bb.label)] for bb in bbs_aug]) # the shape has to be at least (0,5) if len(annot_aug) == 0: annot_aug = np.zeros((0, 5)) return {'img': img_aug, 'annot': annot_aug}
def getImageAug(): seq = iaa.Sequential( [ iaa.SomeOf( (0, 2), [ iaa.Identity(), iaa.AverageBlur(k=((3, 5), (5, 7))), iaa.Rotate((-45, 45)), iaa.Affine(scale=(0.5, 0.95)), iaa.Multiply((0.50, 1.1)) #,iaa.BlendAlphaRegularGrid(nb_rows=(4, 6), nb_cols=(1, 4), # foreground=iaa.Multiply(0.0)) #,iaa.Cartoon() , iaa.Cutout( nb_iterations=(1, 3), size=0.2, squared=False, cval=0), iaa.Affine(shear=(-48, 48)), iaa.Affine(translate_px={ "x": (-42, 42), "y": (-36, 36) }), iaa.KeepSizeByResize( iaa.Resize({ "height": (0.70, 0.90), "width": (0.70, 0.90) })), iaa.CropAndPad(percent=(-0.2, 0.2)) #,iaa.PiecewiseAffine(scale=(0.01, 0.05)) , iaa.PerspectiveTransform(scale=(0.01, 0.1)) #,iaa.WithPolarWarping(iaa.CropAndPad(percent=(-0.1, 0.1))) #,iaa.ElasticTransformation(alpha=(0, 3.0), sigma=0.5) ]) #,iaa.SaveDebugImageEveryNBatches(folder_path, 100) ], random_order=True) return seq
def augment1(txt_path, save_txt_path): """ Augment data from the txt file. """ seq = iaa.Sequential( [iaa.ElasticTransformation(alpha=90, sigma=9), iaa.Cutout()], random_order=False) with open(txt_path, 'r') as fp: lines = fp.readlines() new_lines = [] for line in tqdm.tqdm(lines): new_line = "" cat_id, img_path = line.rstrip().split(' ') new_line += cat_id image = imageio.imread(img_path) images_aug = seq(images=[image])[0] img_path = img_path.replace('cloudy', 'augment').replace( 'dusky', 'augment').replace('foggy', 'augment').replace('sunny', 'augment') new_line += ' ' + img_path + '\n' new_lines.append(new_line) images_aug = Image.fromarray(images_aug) images_aug.save(img_path) with open(save_txt_path, 'a+') as fp: fp.writelines(new_lines) # augment1("data/weather/train.txt", "data/weather/augment.txt") # get_chips_augment(txt_path="data/weather/test.txt", save_name="chips_test", save_txt_path="data/weather/chips_test.txt") # create_gif("C:/Users/18917/Documents/Python Scripts/pytorch/Lab/Pix2Pix-forlab/data-blur/6")
def cutout_fraction(magnitude: int, **kwargs) -> iaa.Cutout: """ Applies cutout to the image according to bbox information. This will apply only to a single bounding box in the image. For the augmentation to apply the policy correctly the image height and width along with the bounding box height and width are required as keyword arguments. Tensorflow Policy Equivalent: bbox_cutout The cutout size is determined as a fraction of the bounding box size. The cutout value in the policies is at a pixel level. The imgaug cutout augmentation method requires the cutout to be a percentage of the image. Passing the image height and width as kwargs will scale the cutout to the appropriate percentage. Otherwise the imgaug default of 20% will be used. Note: the cutout may not always be present in the bounding box dut to randomness in the location of the cutout centre :type magnitude: int :param magnitude: magnitude of cutout :param kwargs: height: height of the image as int width: width of the image as int height_bbox: height of the bounding box as int width_bbox: width of the bounding box as int :rtype: iaa.Cutout :return: Method to apply cutout to bounding boxes """ level = (magnitude / _MAX_MAGNITUDE) * CUTOUT_MAX_PAD_FRACTION cutout_args = {} if all(i in kwargs for i in ['height', 'width', 'height_bbox', 'width_bbox']): size = tuple([(level * kwargs['height_bbox']) / kwargs['height'], (level * kwargs['width_bbox']) / kwargs['width']]) cutout_args['size'] = size return iaa.Cutout(**cutout_args)
def __init__(self, da): assert check_data_aug(da), 'Parameters for custom data augmentation missing. Should have: {}'.format( keys_data_aug) aug_geometric = [iaa.Affine(scale=(da['scalem'], da['scaleM']), translate_percent={'x': (-da['trans'], da['trans']), 'y': (-da['trans'], da['trans'])}, rotate=(-da['rot'], da['rot']), shear=(-da['shear'], da['shear']), cval=FILL_COLOR), iaa.PerspectiveTransform(scale=(0, da['pers']), cval=FILL_COLOR, keep_size=True)] aug_camera = [iaa.GaussianBlur(sigma=(0, da['sigma'])), iaa.MotionBlur(k=(da['mot_km'], da['mot_kM']), angle=(-da['mot_an'], da['mot_an']), direction=(da['mot_dm'], da['mot_dM'])), iaa.JpegCompression(compression=(da['jpegm'], da['jpegM'])), iaa.LinearContrast(alpha=(da['con_alpham'], da['con_alphaM']), per_channel=da['con_chan']), iaa.MultiplyHueAndSaturation(mul=(da['col_mulm'], da['col_mulM']), per_channel=da['col_chan'], from_colorspace='BGR'), iaa.AddToHueAndSaturation((da['col_addm'], da['col_addM']), per_channel=da['col_chan']) ] cutout = iaa.Cutout(nb_iterations=(0, da['co_num']), size=(da['co_sm'], da['co_sM']), squared=False, cval=FILL_COLOR) # Create a mix of all others self.augmenter = iaa.Sequential([iaa.SomeOf((0, 1), aug_geometric), # none or 1 iaa.SomeOf((0, len(aug_camera) - 2), aug_camera), # from none to all-2 cutout], random_order=True) # mix the apply order