def someAug(): bg = iap.Uniform(16, 18) #Creating a series of augmentations shearXY = iaa.Sequential([ iaa.ShearY(iap.Uniform(-10, 10), cval=bg), iaa.ShearX(iap.Uniform(-10, 10), cval=bg) ]) rotate = iaa.Rotate(rotate=iap.Choice([-30, -15, 15, 30], p=[0.25, 0.25, 0.25, 0.25]), cval=bg) pwAff = iaa.PiecewiseAffine(scale=(0.01, 0.06), cval=bg) affine = iaa.Affine(scale={ "x": iap.Uniform(1.1, 1.2), "y": iap.Uniform(1.1, 1.2) }, cval=bg) noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.025 * 255)) #Using SomeOf to randomly select some augmentations someAug = iaa.SomeOf(iap.Choice([2, 3, 4], p=[1 / 3, 1 / 3, 1 / 3]), [affine, shearXY, pwAff, rotate, noise], random_order=True) return someAug
def _lane_argue(*, image, lane_src): lines_tuple = [[(float(pt['x']), float(pt['y'])) for pt in line_spec] for line_spec in lane_src['Lines']] lss = [ia_LineString(line_tuple_spec) for line_tuple_spec in lines_tuple] lsoi = LineStringsOnImage(lss, shape=image.shape) color_shift = iaa.OneOf([ iaa.GaussianBlur(sigma=(0.5, 1.5)), iaa.LinearContrast((1.5, 1.5), per_channel=False), iaa.Multiply((0.8, 1.2), per_channel=0.2), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(0, iaa.Multiply((0.7, 1.3)))), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(1, iaa.Multiply((0.1, 2)))), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(2, iaa.Multiply((0.5, 1.5)))), ]) posion_shift = iaa.SomeOf(4, [ iaa.Fliplr(), iaa.Crop(percent=([0, 0.2], [0, 0.15], [0, 0], [0, 0.15]), keep_size=True), iaa.TranslateX(px=(-16, 16)), iaa.ShearX(shear=(-15, 15)), iaa.Rotate(rotate=(-15, 15)) ]) aug = iaa.Sequential([ iaa.Sometimes(p=0.6, then_list=color_shift), iaa.Sometimes(p=0.6, then_list=posion_shift) ], random_order=True) batch = ia.Batch(images=[image], line_strings=[lsoi]) batch_aug = list(aug.augment_batches([batch]))[0] # augment_batches returns a generator image_aug = batch_aug.images_aug[0] lsoi_aug = batch_aug.line_strings_aug[0] lane_aug = [[dict(x=kpt.x, y=kpt.y) for kpt in shapely_line.to_keypoints()] for shapely_line in lsoi_aug] return image_aug, dict(Lines=lane_aug)
def rotation(degree, input_path, output_path, image_count): images = [] labels = [] for img_path in range(image_count): img = imageio.imread(input_path + '/images/' + str(img_path) + '.png') images.append(img) lbl = imageio.imread(input_path + '/labels/' + str(img_path) + '.png') labels.append(lbl) seq = iaa.Sequential( [ iaa.Rotate((degree)), ] ) images_aug = seq(images=images) labels_aug = seq(images=labels) path = os.path.join(output_path, 'images') os.mkdir(path) path = os.path.join(output_path, 'labels') os.mkdir(path) for indx, i in enumerate(images_aug): imageio.imwrite(output_path + '/images/' + 'rotat'+ '_' + str(indx) + '.png', i) for indx, i in enumerate(labels_aug): imageio.imwrite(output_path + '/labels/' + 'rotat'+ '_' + str(indx) + '.png', i) print("Rotation results were saved given directory.")
def augmentation(X, y, masks, itterations): ys = np.concatenate( (y[:, :, np.newaxis], y[:, :, np.newaxis], y[:, :, np.newaxis]), axis=2) masks = np.concatenate( (masks[:, :, np.newaxis], masks[:, :, np.newaxis], masks[:, :, np.newaxis]), axis=2) B = np.concatenate((ys, X, masks), axis=2).astype(np.uint8) A = np.zeros((itterations, np.shape(B)[0], np.shape(B)[1], np.shape(B)[2])) for num in range(itterations): aug1 = iaa.Affine(scale={"x": (1, 1.5), "y": (1, 1.5)}) aug2 = iaa.Fliplr(1) aug3 = iaa.Flipud(1) aug4 = iaa.ShearX((-20, 20), mode='reflect') aug5 = iaa.ShearY((-20, 20), mode='reflect') aug6 = iaa.Rotate((-45, 45), mode='reflect') aug7 = iaa.WithChannels((3, 4, 5), iaa.Multiply((0.5, 1.5))) aug_list = [aug1, aug2, aug3, aug4, aug5, aug6, aug7] ID = np.random.randint(0, len(aug_list)) A[num, :, :, :] = aug_list[ID](image=B) aug_y = A[:, :, :, 0] aug_X = A[:, :, :, 3:6] aug_masks = A[:, :, :, 8] return aug_X, aug_y, aug_masks
def __init__(self): """ init Constructor @param self Object. """ self.aug = iaa.Sequential([ # Blur or Sharpness iaa.Sometimes( 0.25, iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 1.0)), iaa.pillike.EnhanceSharpness(factor=(0.8, 1.5)) ])), # Flip horizontally iaa.Fliplr(0.5), # Rotation iaa.Rotate((-20, 20)), # Pixel Dropout iaa.Sometimes( 0.25, iaa.OneOf([ iaa.Dropout(p=(0, 0.1)), iaa.CoarseDropout(0.1, size_percent=0.5) ])), # Color iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True), ])
def augment_3(self, image, depth, lidar, crop_size, degree): # rsz = iaa.Resize({"height": resize_size[0], "width": resize_size[1]}) seq = iaa.Sequential( [ iaa.PadToFixedSize(height=crop_size[0], width=crop_size[1]), # 保证可crop iaa.CropToFixedSize(height=crop_size[0], width=crop_size[1]), # random crop iaa.Fliplr(0.5), # iaa.Flipud(0.5), iaa.Rotate((-degree, degree)), iaa.GammaContrast((0.9, 1.1)), iaa.Multiply((0.9, 1.1)), ], random_order=True) depth, lidar = np.expand_dims(depth, 2), np.expand_dims(lidar, 2) tmp = np.concatenate((depth, lidar), axis=2) tmp = (tmp * 1000).astype(np.int32) # 米单位*1000保留精度 tmp = SegmentationMapsOnImage(tmp, shape=tmp.shape) # image, tmp = rsz(image=image, segmentation_maps=tmp) image, tmp = seq(image=image, segmentation_maps=tmp) tmp = tmp.arr tmp = tmp.astype(np.float32) / 1000 # 再转回米 depth, lidar = tmp[:, :, 0], tmp[:, :, 1] return image, depth, lidar
def myDataAug(X_data, Y_data, its, seed=12): size = len(X_data) ia.seed(seed) seq = iaa.Sequential( [ iaa.GaussianBlur( sigma=(0.0, 4.0)), #Random GaussianBlur from sigma 0 to 4 #iaa.AddToHueAndSaturation((-10, 10), per_channel=True), #color jittering iaa.Affine(translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }), #affine transalte (affects segmaps) iaa.Fliplr(0.5), #50 % to flip horizontally (affects segmaps) iaa.Flipud(0.5), #50 % to flip vertically (affects segmaps) iaa.Rotate( (-45, 45)) # rotate by -45 to 45 degrees (affects segmaps) ], random_order=True) X_data_augs = np.zeros((its * size, 1000, 1000, 3), dtype=np.uint8) Y_data_augs = np.zeros((its * size, 1000, 1000, 3), dtype=np.uint8) for i in range(its): X_data_aug, Y_data_aug = seq(images=X_data, segmentation_maps=Y_data) X_data_augs[i * size:(i + 1) * size] = X_data_aug Y_data_augs[i * size:(i + 1) * size] = Y_data_aug return X_data_augs, Y_data_augs
def preprocess(self, ims, augment): def normalize(batch): return batch.astype(np.float32) / float(255) if augment: augmentations = iaa.Sequential([ iaa.Resize(int(1.1 * hp.img_size)), iaa.Fliplr(0.5), iaa.Sometimes(0.4, iaa.Rotate((-30, 30))), iaa.Sometimes(0.4, iaa.Affine(scale=(0.9, 1.2))), iaa.Sometimes(0.5, iaa.PerspectiveTransform(scale=(0.01, 0.20))), # Crop/resize image to proper dimension iaa.CropToFixedSize(hp.img_size, hp.img_size), iaa.Resize(hp.img_size), iaa.Sometimes(0.3, iaa.SaltAndPepper(0.01)), iaa.CLAHE(to_colorspace='HSV') ]) else: augmentations = ia.Sequential([iaa.CLAHE(to_colorspace='HSV')]) augmented = augmentations.augment_images(ims) for i in augmented: if i.shape != (hp.img_size, hp.img_size, 3): print(i.shape) augmented = np.stack(augmented, axis=0) return normalize(augmented)
def load_augmentation_aug_geometric(): return iaa.Sequential([ iaa.Sometimes(0.5, iaa.Fliplr()), iaa.Sometimes(0.5, iaa.Rotate((-45, 45))), iaa.Sometimes( 0.5, iaa.Affine( scale={ "x": (0.5, 1.5), "y": (0.5, 1.5) }, order=[0, 1], mode='constant', cval=(0, 255), )), iaa.Sometimes( 0.5, iaa.Affine( translate_percent={ "x": (-0.25, 0.25), "y": (-0.25, 0.25) }, order=[0, 1], mode='constant', cval=(0, 255), )), ])
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 get_fg_augmentation(): seq = iaa.Sequential([ iaa.Resize({ 'height': 'keep', 'width': (0.9, 1.1) }), iaa.KeepSizeByResize(iaa.Rotate((-15, 15), fit_output=True)) ]) return seq
def chapter_augmenters_rotate(): fn_start = "geometric/rotate" image = ia.quokka(size=(128, 128)) aug = iaa.Rotate((-45, 45)) run_and_save_augseq( fn_start + ".jpg", aug, [image for _ in range(4*2)], cols=4, rows=2)
def Rotation(self, image, **kwargs): assert len(image.shape) == 3 image_augs = [] List = [0, 30, 50, 70, 90] # Rotation angle for angle in List: seq = iaa.Sequential([iaa.Rotate(rotate=angle)]) image_aug = seq.augment_image(image) image_augs.append(image_aug) return image_augs, List
def _compose_transforms(self): self.aug_functions = list() for transform_name in self._transforms.keys(): if transform_name == 'resize': self.aug_functions.append( iaa.Resize( { "height": self._transforms[transform_name].height, "width": self._transforms[transform_name].width }, name='resize')) continue elif transform_name == 'fliplr': self.aug_functions.append( iaa.Fliplr(self._transforms[transform_name].p, name='fliplr')) continue elif transform_name == 'flipud': self.aug_functions.append( iaa.Flipud(self._transforms[transform_name].p, name='flipud')) continue elif transform_name == 'rotate': self.aug_functions.append( iaa.Rotate(self._transforms[transform_name].degrees, name='rotate')) continue elif transform_name == 'JpegCompression': self.aug_functions.append( iaa.JpegCompression( compression=(self._transforms[transform_name].low, self._transforms[transform_name].high))) continue elif transform_name == 'GaussianBlur': self.aug_functions.append( iaa.GaussianBlur( sigma=(self._transforms[transform_name].low, self._transforms[transform_name].high))) continue elif transform_name == 'CropToFixedSize': self.aug_functions.append( iaa.CropToFixedSize( width=self._transforms[transform_name].width, height=self._transforms[transform_name].height, position=self._transforms[transform_name].position)) continue else: self.logger.info( f'{transform_name} is not support in augment build') self.aug_functions.append(iaa.Noop()) continue iaa_seq = iaa.Sequential( self.aug_functions, name=f'{self.cfg.DATASET.name}_{self.flag}_iaa_seq') return iaa_seq
def __call__(self, image): im = image.copy() pix = np.array(im, dtype='float32') pix = iaa.Fliplr(0.5).augment_image(pix) aug_ratate = iaa.OneOf([ iaa.PerspectiveTransform(scale=(0, 0.015)), iaa.Rotate((-10, 10)) ]) pix = aug_ratate.augment_image(pix) if self.istest: return pix pix = self.rand_brightness(pix) if random.randint(2): self.aumentone(pix) #distort = SSD_Compose(self.pd[:-1]) else: self.aumenttwo(pix) #distort = SSD_Compose(self.pd[1:]) #pix = distort(pix) #pix = pix.astype('uint8') pix = np.where(pix > 255, 255, pix) pix = np.where(pix < 0, 0, pix) #im = Image.fromarray(pix, 'RGB') #self.rand_light_noise(pix) # aug = iaa.Sequential(iaa.SomeOf(2, [ # iaa.AdditiveGaussianNoise(scale=(0, 0.1*255)), # iaa.Dropout(p=(0, 0.1), per_channel=0.5), # iaa.SaltAndPepper(p=(0, 0.1), per_channel=True), # iaa.GaussianBlur(sigma=(0.0, 3.0)), # iaa.MotionBlur(k=15, angle=[-45, 45]), # iaa.AverageBlur(k=((0, 5), (0, 5))) # ], random_order=True)) aug_noise = iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)), iaa.Dropout(p=(0, 0.1), per_channel=0.5), iaa.SaltAndPepper(p=(0, 0.1), per_channel=True), ]) aug_blur = iaa.OneOf([ iaa.GaussianBlur(sigma=(0.0, 1.5)), iaa.MotionBlur(k=7, angle=[-20, 20]), iaa.AverageBlur(k=((0, 2.5), (0, 2.5))) ]) pix = aug_noise.augment_image(pix) pix = aug_blur.augment_image(pix) return pix / 255.0
def __init__(self): self.aug = iaa.Sequential([ iaa.Pad(percent=(0, (0, 0.5), 0, (0, 0.5))), iaa.Resize(size={ "height": 32, "width": "keep-aspect-ratio" }), iaa.Rotate(rotate=(-10, 10)), ])
def make_data_aug(cfg): transf = iaa.Sequential([ iaa.Flipud(p=0.5), iaa.Fliplr(p=.5), iaa.Fliplr(p=.5), iaa.Rotate( [360 / cfg.aug_n_angles * i for i in range(cfg.aug_n_angles)]) ]) return transf
def __init__(self): self.augmentations = augmenters.Sequential([ augmenters.Sometimes(0.5, augmenters.Flipud(0.5)), augmenters.Sometimes(0.5, augmenters.Fliplr(0.5)), augmenters.Sometimes(0.5, augmenters.Rotate((-45, 45))), augmenters.Sometimes(0.25, augmenters.ShearX((-20, 20))), augmenters.Sometimes( 0.1, augmenters.AdditiveGaussianNoise(scale=(0, 0.1 * 255))) ], random_order=True)
def augmentation(): train_images_horiz, train_images_verti, train_labels, test_images_horiz, test_images_verti, test_labels, val_images_horiz, val_images_verti, val_labels = list_input_network( ) seq1 = iaa.Sequential([iaa.Fliplr(1)]) seq2 = iaa.Sequential([iaa.Flipud(1)]) seq3 = iaa.Sequential([iaa.Rotate((-45, 45))]) seq4 = iaa.Sequential([iaa.TranslateX(px=(-20, 20))]) seq5 = iaa.Sequential([iaa.CropToFixedSize(width=30, height=30)]) new_train_images = train_images_horiz new_train_labels = train_labels """ different sequences of data augmentation applied """ images_aug = seq1(images=train_images_horiz) new_train_images = np.concatenate((new_train_images, images_aug)) new_train_labels = np.concatenate((new_train_labels, train_labels)) images_aug = seq2(images=train_images_horiz) new_train_images_horiz = np.concatenate((new_train_images, images_aug)) new_train_labels = np.concatenate((new_train_labels, train_labels)) images_aug = seq3(images=train_images_horiz) new_train_images = np.concatenate((new_train_images, images_aug)) new_train_labels = np.concatenate((new_train_labels, train_labels)) images_aug = seq4(images=train_images_horiz) new_train_images_horiz = np.concatenate((new_train_images, images_aug)) new_train_labels = np.concatenate((new_train_labels, train_labels)) #new_train_images = train_images_verti """ same on vertical images """ """ images_aug = seq1(images=train_images_verti) new_train_images = np.concatenate((new_train_images, images_aug)) images_aug = seq2(images=train_images_verti) new_train_images_verti = np.concatenate((new_train_images, images_aug)) images_aug = seq3(images=train_images_verti) new_train_images = np.concatenate((new_train_images, images_aug)) images_aug = seq4(images=train_images_verti) new_train_images_verti = np.concatenate((new_train_images, images_aug)) """ # images_aug = seq5(images=train_images) # new_train_images = np.concatenate((new_train_images, images_aug)) # new_train_labels = np.concatenate((new_train_labels, train_labels)) #return new_train_images_horiz, new_train_images_verti, new_train_labels return new_train_images_horiz, new_train_labels
def __call__(self, *args, **kwargs) -> typing.Tuple[np.ndarray, typing.List[Polygon]]: if self.is_training: resize = iaa.Resize(size=dict(longer_side=self.long_sizes, width='keep-aspect-ratio')) rotate = iaa.Rotate(rotate=self.angles, fit_output=True) resize_height = iaa.Resize(size=dict(height=self.height_ratios, width='keep')) crop = iaa.CropToFixedSize(width=self.cropped_size[0], height=self.cropped_size[1]) fix_resize = iaa.Resize(size=self.output_size) # blur = iaa.GaussianBlur() # blur = iaa.Sometimes(p=self.blur_prob, # then_list=blur) brightness = iaa.MultiplyBrightness((0.5, 1.5)) brightness = iaa.Sometimes(self.color_jitter_prob, then_list=brightness) saturation = iaa.MultiplySaturation((0.5, 1.5)) saturation = iaa.Sometimes(self.color_jitter_prob, then_list=saturation) contrast = iaa.LinearContrast(0.5) contrast = iaa.Sometimes(self.color_jitter_prob, then_list=contrast) hue = iaa.MultiplyHue() hue = iaa.Sometimes(self.color_jitter_prob, then_list=hue) augs = [resize, rotate, resize_height, crop, fix_resize, brightness, saturation, contrast, hue] ia = iaa.Sequential(augs) else: fix_resize = iaa.Resize(size=self.output_size) ia = iaa.Sequential([fix_resize]) image = args[0] polygons = args[1] polygon_list = [] for i in range(polygons.shape[0]): polygon_list.append(Polygon(polygons[i].tolist())) polygons_on_image = PolygonsOnImage(polygon_list, shape=image.shape) image_aug, polygons_aug = ia(image=image, polygons=polygons_on_image) return image_aug, polygons_aug.polygons
def dictRotateAug(baseImageListFunc, baseMaskListFunc, fullImageListFunc, segmapListFunc): print('Rotation, starting number of images:', len(segmapListFunc)) rotateAug_x00percent = 2 rotateAug = iaa.Rotate((-3, 3), mode="reflect") rotateAug._mode_segmentation_maps = "reflect" alteredImageListFunc, alteredMaskListFunc = expandList(baseImageListFunc, baseMaskListFunc, rotateAug_x00percent) (alteredImageListFunc, alteredMaskListFunc) = rotateAug(images=alteredImageListFunc, segmentation_maps=alteredMaskListFunc) fullImageListFunc.extend(alteredImageListFunc) segmapListFunc.extend(alteredMaskListFunc) return fullImageListFunc, segmapListFunc
def __init__(self, iaalist=None): if iaalist is None: iaalist = iaa.Sequential([ iaa.Sometimes(0.5, iaa.ChannelShuffle(0.3)), iaa.Sometimes(0.5, iaa.MultiplyHue((0.5, 1.5))), iaa.Sometimes(0.5, iaa.AddToHueAndSaturation((-50, 50), per_channel=True)), iaa.Sometimes(0.5, iaa.Fliplr(0.5)), iaa.Sometimes(0.5, iaa.Flipud(0.5)), iaa.Sometimes(0.5, iaa.Rotate((-50, 50))) ], random_order=True) self.transformSet = iaalist self.outscale = random.choice([0.8, 0.85, 0.9, 0.95])
def rotate(magnitude: int) -> iaa.BlendAlphaBoundingBoxes: """ Rotate the bounding box in an image Tensorflow Policy Equivalent: rotate_with_bboxes :type magnitude: int :param magnitude: magnitude of rotation :rtype: iaa.BlendAlphaBoundingBoxes :return: Method to apply rotation """ level = _rotate_mag_to_arg(magnitude) return iaa.Rotate(level)
def make_rotation_ops(increments): ops = [] for increment in increments: suffix = f"{increment}" if increment < 0: suffix = f"Back{-increment}" ops.append( Augment("Rotate" + suffix, iaa.Sequential([ iaa.Grayscale(alpha=1.0), iaa.Rotate(increment) ]), num_repetitions=1) ) return ops
def gen_steps(): STEPS=[ iaa.Rotate((-50,-20)), iaa.Rotate((20,50)), iaa.Affine(shear=(-30,-10)), iaa.Affine(shear=(10,30)), iaa.ShearX(((-10, 10))), iaa.ShearY(((-10, 10))), iaa.Affine(scale=(0.5, 1.5)), iaa.Affine(scale=(1, 2)), iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}), [ iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}), iaa.Affine(shear=(-15,15)), ], [ iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}), iaa.Affine(shear=(-15,15)), ], ] return STEPS
def __init__(self): sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug = iaa.SomeOf( 3, [ sometimes( iaa.AddToHueAndSaturation((-50, 50), per_channel=True)), sometimes( iaa.SigmoidContrast( gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True)), #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.02))), sometimes(iaa.Invert(0.7, per_channel=0.8)), sometimes(iaa.Rotate((-20, 20))) ])
def train(model): """Train the model.""" # Training dataset. dataset_train = CharacterDataset() dataset_train.load_characters("train") dataset_train.prepare() # Validation dataset dataset_val = CharacterDataset() dataset_val.load_characters("val") dataset_val.prepare() #Augmentation aug = iaa.SomeOf(2, [ iaa.AdditiveGaussianNoise(scale=(0, 0.10 * 255)), iaa.MotionBlur(), iaa.GaussianBlur(sigma=(0.0, 2.0)), iaa.RemoveSaturation(mul=(0, 0.5)), iaa.GammaContrast(), iaa.Rotate(rotate=(-45, 45)), iaa.PerspectiveTransform(scale=(0.01, 0.15)), iaa.JpegCompression(compression=(0, 75)), iaa.imgcorruptlike.Spatter(severity=(1, 4)), iaa.Rain(speed=(0.1, 0.3)), iaa.Fog() ]) custom_callbacks = [ ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1), EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1) ] # *** This training schedule is an example. Update to your needs *** # Since we're using a very small dataset, and starting from # COCO trained weights, we don't need to train too long. Also, # no need to train all layers, just the heads should do it. print("Training network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=100, layers='heads', augmentation=aug, custom_callbacks=custom_callbacks)
def balanced_augmentation(X, Y, epochs, img_size): print("START AUGMENTATION...") cluster_df = pd.read_csv('cluster_df.csv', usecols=['cluster']) cluster_size = [0, 0, 0] cluster_size[0] = cluster_df.value_counts()[0].iloc[0] cluster_size[1] = cluster_df.value_counts()[1].iloc[0] cluster_size[2] = cluster_df.value_counts()[2].iloc[0] d_len = cluster_size[0] * epochs[0] + cluster_size[1] * epochs[ 1] + cluster_size[2] * epochs[2] augmented_x = np.zeros((d_len, img_size, img_size, 3), dtype=np.float) augmented_y = np.zeros((d_len, img_size, img_size, 1), dtype=np.bool) seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.GaussianBlur(sigma=(0, 3.0)), iaa.Rotate((-40, 40)) ]) global_cnt = 0 for i in range(X.shape[0]): img_cluster = cluster_df.iloc[i]['cluster'] '''if img_cluster == 2: print(epochs[img_cluster]) cv2.imshow("img", X[i]) cv2.waitKey(0) cv2.destroyAllWindows()''' for e in range(epochs[img_cluster]): img, seg = seq(images=[X[i]], segmentation_maps=[Y[i]]) augmented_x[global_cnt] = img[0] augmented_y[global_cnt] = seg[0] global_cnt += 1 print("AUGMENTED") return augmented_x, augmented_y
def do_all_aug(image): do_aug(image, iaa.Noop(name="origin")) do_aug(image, iaa.Crop((0, 10))) # 切边 do_aug(image, iaa.GaussianBlur((0, 3))) do_aug(image, iaa.AverageBlur(1, 7)) do_aug(image, iaa.MedianBlur(1, 7)) do_aug(image, iaa.Sharpen()) do_aug(image, iaa.BilateralBlur()) # 既噪音又模糊,叫双边 do_aug(image, iaa.MotionBlur()) do_aug(image, iaa.MeanShiftBlur()) do_aug(image, iaa.GammaContrast()) do_aug(image, iaa.SigmoidContrast()) do_aug(image, iaa.Affine(shear={ 'x': (-10, 10), 'y': (-10, 10) }, mode="edge")) # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一 do_aug(image, iaa.Affine(shear={ 'x': (-10, 10), 'y': (-10, 10) }, mode="edge")) # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一 do_aug(image, iaa.Rotate(rotate=(-10, 10), mode="edge")) do_aug(image, iaa.PiecewiseAffine()) # 局部点变形 do_aug(image, iaa.Fog()) do_aug(image, iaa.Clouds()) do_aug(image, iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025))) do_aug( image, iaa.Rain( nb_iterations=1, drop_size=(0.05, 0.1), speed=(0.04, 0.08), )) do_aug( image, iaa.ElasticTransformation(alpha=(0.0, 20.0), sigma=(3.0, 5.0), mode="nearest")) do_aug(image, iaa.AdditiveGaussianNoise(scale=(0, 10))) do_aug(image, iaa.AdditiveLaplaceNoise(scale=(0, 10))) do_aug(image, iaa.AdditivePoissonNoise(lam=(0, 10))) do_aug(image, iaa.Salt((0, 0.02))) do_aug(image, iaa.Pepper((0, 0.02)))
def train(model): """Train the model.""" # Training dataset. dataset_train = PlateDataset() dataset_train.load_plates("train") dataset_train.prepare() # Validation dataset dataset_val = PlateDataset() dataset_val.load_plates("val") dataset_val.prepare() #Augmentation aug = iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 1.0)), iaa.MotionBlur(), iaa.RemoveSaturation((0.0, 0.5)), iaa.GammaContrast(), iaa.Rotate(rotate=(-45, 45)), iaa.PerspectiveTransform(scale=(0.01, 0.15)), iaa.SaltAndPepper(), iaa.JpegCompression(compression=(0, 75)), iaa.imgcorruptlike.Spatter(severity=(1, 4)), iaa.imgcorruptlike.DefocusBlur(severity=1) ]) custom_callbacks = [ ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1), EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1) ] # Since we're using a very small dataset, and starting from # COCO trained weights, we don't need to train too long. Also, # no need to train all layers, just the heads should do it. print("Training network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=100, layers='all', augmentation=aug, custom_callbacks=custom_callbacks)