best_model_paths = [] for cv in range(CV_NUM): cur_seed = SEED + cv opt = keras.optimizers.Adam(lr=start_lr) model = build_model(backbone= backbone, use_imagenet=use_imagenet,input_shape = input_shape, num_classes=num_classes, base_freeze = True,opt = opt, NUM_GPU=NUM_GPU) xx_train, xx_val, yy_train, yy_val = train_test_split(x_train, y_train, test_size=0.15, random_state=cur_seed,stratify=y_train) print('shape:',xx_train.shape,'val shape:',xx_val.shape) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ iaa.SomeOf((0, 3),[ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images sometimes(iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=['reflect'] )), sometimes( iaa.OneOf([ iaa.Affine(rotate=0), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ])), sometimes(iaa.Affine( scale={"x": (0.1, 1.1), "y": (0.9, 1.1)}, translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-5, 5), order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) mode=['reflect'] ))
def preprocess(src): gs = gs = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) gs = cv2.GaussianBlur(gs, ksize=(9, 9), sigmaX=10) swimg = cv2.resize(gs, (256, 256)) pimg = cv2.Laplacian(swimg, cv2.CV_32F, ksize=5) return pimg if len(sys.argv) < 3: print 'not enough input' exit() seq = iaa.Sequential([iaa.Scale((0.5, 0.5))]) seq = iaa.Scale((0.1, 0.5)) seq2 = iaa.CropAndPad((-100, 100), pad_mode='linear_ramp', keep_size=False, sample_independently=True) seq2 = iaa.Crop((0, 50), keep_size=False, sample_independently=True) imgdir = sys.argv[1] expdir = sys.argv[2] if not os.path.isdir(expdir): os.mkdir(expdir) maxN = 2 if len(sys.argv) > 3: maxN = int(sys.argv[3]) edgeOnly = 0 if len(sys.argv) > 4: edgeOnly = int(sys.argv[4])
def create_image_augmentation_fn(num_augs=4): """ create an augmentation pipeline and return a function which applies that pipeline to a pair of images, yielding the augmentations uses imgaug to perform augmentations, which depends on opencv https://imgaug.readthedocs.io/ num_augs: number of augmentations to perform in the generator """ # Define our augmentation pipeline. sometimes = lambda aug: iaa.Sometimes(0.5, aug) 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 images by -5% to 10% of their height/width sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=["constant"], pad_cval=0)), sometimes( iaa.Affine( scale={ "x": (0.7, 1.3), "y": (0.7, 1.3) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.20, 0.20), "y": (-0.20, 0.20) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=0, # if mode is constant, use a cval between 0 and 255 mode=[ "constant" ] # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # 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), [ # 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)), # 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 # don't do dropouts # 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 ), # change brightness of images (by -10 to 10 of original value) # 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.ContrastNormalization((0.5, 2.0)) # ) # ]), iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast # iaa.Grayscale(alpha=(0.0, 1.0)), # sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) # sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around # sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) # seq = iaa.Sequential([ # iaa.Dropout([0.05, 0.2]), # drop 5% or 20% of all pixels # iaa.Sharpen((0.0, 1.0)), # sharpen the image # iaa.Affine(rotate=(-45, 45)), # rotate by -45 to 45 degrees (affects segmaps) # iaa.ElasticTransformation(alpha=50, sigma=5) # apply water effect (affects segmaps) # ], random_order=True) def augmentation(X, y): for aug_id, _ in enumerate(range(num_augs)): det = seq.to_deterministic() # create the inputs aug_x = [det.augment_image(x) for x in X] # make the mask rgb, then convert back and binarize aug_y = [] for i in y: _y = np.stack((i[:, :, 0], ) * 3, axis=-1) _aug_y = det.augment_image(_y) _aug_y = _aug_y.mean(axis=-1)[..., np.newaxis] aug_y.append((_aug_y > _aug_y.mean()).astype(np.uint8) * 255) # logger.debug("yielding: '%s'/'%s'" % (str(i_x.shape), str(i_y.shape))) yield ([x.astype(np.float) / 255.0 for x in aug_x], [y.astype(np.float) / 255.0 for y in aug_y]) if num_augs == 0: return None return augmentation
def augmentation_sequence(self): # Create an augmentation instance def fixFillMode(x, mode="reflect"): clsName = x.__class__.__name__ # clsName = type(x).__name__ if clsName == "CropAndPad": x._pad_mode_segmentation_maps = mode elif clsName == "Affine": x._mode_segmentation_maps = mode return x seq = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Flipud(0.2), iaa.Sometimes( 0.5, fixFillMode( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode="reflect", pad_cval=(0, 255) ) ), ), iaa.Sometimes( 0.5, fixFillMode( 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=(-25, 25), shear=(-16, 16), order=[0, 1], cval=(0, 255), mode="reflect", ) ), ), iaa.SomeOf( (0, 3), [ 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.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.contrast.LinearContrast((0.75, 1.20), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.Sometimes( 0.5, iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))), iaa.Sometimes(0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1))), ], random_order=True, ), ], random_order=True, ) return seq
from imgaug import augmenters as iaa sometimes_5 = lambda aug: iaa.Sometimes(0.5, aug) sometimes_1 = lambda aug: iaa.Sometimes(0.1, aug) sometimes_8 = lambda aug: iaa.Sometimes(0.8, aug) sometimes_2 = lambda aug: iaa.Sometimes(0.2, aug) augmentations = iaa.Sequential([ sometimes_2( iaa.CropAndPad(percent=(-0.02, 0.02), pad_mode=["edge"], pad_cval=(0, 255))), iaa.Sequential([iaa.size.Scale(0.6), iaa.size.Scale(1 / 0.6)]), #sometimes_8(iaa.OneOf([iaa.Sequential([iaa.size.Scale(0.6),iaa.size.Scale(1/0.6)]), # iaa.Sequential([iaa.size.Scale(0.8),iaa.size.Scale(1/0.8)]), # iaa.Sequential([iaa.size.Scale(0.9),iaa.size.Scale(1/0.9)]),])), #This inverts completely inverts the text ( make black white etc.) sometimes_1(iaa.Invert(1, per_channel=True)), #This does some affine transformations sometimes_2( iaa.Affine( scale={ "x": (0.8, 1), "y": (0.8, 1) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0, 0), "y": (-0.1, 0.1) }, # translate by -20 to +20 percent (per axis)
def image_segmentation_generator(self, images_path, segs_path, batch_size, n_classes, input_height, input_width, output_height, output_width, augument_image=True, with_auxiliary_loss=True): img_seg_pairs = self.get_pairs_from_paths(images_path, segs_path) random.shuffle(img_seg_pairs) zipped = itertools.cycle(img_seg_pairs) seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.5), # vertically flip 50% of all images # crop images by -5% to 10% of their height/width iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode='constant', pad_cval=(0, 255)), 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'), ], random_order=True) while True: X = [] Y = [] for _ in range(batch_size): im, seg = next(zipped) im = cv2.imread(im, 1) seg = cv2.imread(seg, 1) if augument_image: # データ拡張 aug_det = seq.to_deterministic() image_aug = aug_det.augment_image(im) segmap = ia.SegmentationMapsOnImage(seg, shape=im.shape) segmap_aug = aug_det.augment_segmentation_maps(segmap) segmap_aug = segmap_aug.get_arr() im = image_aug seg = segmap_aug X.append(self.get_image_array(im, input_width, input_height)) Y.append( self.get_segmentation_array(seg, n_classes, output_width, output_height)) if with_auxiliary_loss: yield np.array(X), [np.array(Y), np.array(Y)] else: yield np.array(X), [np.array(Y)]
失去多线程的优势 """ import augment as agmt import preprocess from imgaug import augmenters as ia # %% agmtgen = agmt.AugmentGenerator(path="../data/tmp") seq = ia.Sequential([ ia.Fliplr(0.5), ia.CropAndPad( percent=(0, 0.2), pad_mode=["constant", "edge"], pad_cval=(0)), ia.Sometimes( 1, ia.OneOf([ ia.GaussianBlur((0, 5.0)), ia.AverageBlur(k=(2, 11)), ia.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 1.5)) ])), ia.OneOf([ ia.AdditiveGaussianNoise(scale=(0.0, 0.1 * 255)), ia.CoarseDropout(0.1, size_percent=0.2) ]), ia.OneOf([ia.Add((-50, 0)), ia.Multiply((1, 1.5))]), ia.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2)
def main(): image = ia.quokka(size=0.5) kps = [ ia.KeypointsOnImage([ ia.Keypoint(x=245, y=203), ia.Keypoint(x=365, y=195), ia.Keypoint(x=313, y=269) ], shape=(image.shape[0] * 2, image.shape[1] * 2)) ] kps[0] = kps[0].on(image.shape) print("image shape:", image.shape) augs = [ iaa.CropAndPad(px=50, name="pad-by-50px"), iaa.CropAndPad(px=(10, 20, 30, 40), name="pad-by-10-20-30-40px"), iaa.CropAndPad(percent=0.1, name="pad-by-01percent"), iaa.CropAndPad(percent=(0.01, 0.02, 0.03, 0.04), name="pad-by-001-002-003-004percent"), iaa.CropAndPad(px=-20, name="crop-by-20px"), iaa.CropAndPad(px=(-10, -20, -30, -40), name="crop-by-10-20-30-40px"), iaa.CropAndPad(percent=-0.1, name="crop-by-01percent"), iaa.CropAndPad(percent=(-0.01, -0.02, -0.03, -0.04), name="crop-by-001-002-003-004percent") ] augs_many = [ iaa.Crop(px=(0, 50), name="native-crop-0-to-50px"), iaa.Crop(px=iap.DiscreteUniform(0, 50), name="native-crop-0-to-50px-iap"), iaa.Pad(px=(0, 50), pad_mode="linear_ramp", pad_cval=(0, 255), name="native-pad-0-to-50px-pad-modes"), iaa.CropAndPad(px=(0, 50), sample_independently=False, name="pad-by-0-to-50px-same"), iaa.CropAndPad(px=(0, 50), name="pad-by-0-to-50px"), iaa.CropAndPad(px=(0, 50), pad_mode=ia.ALL, pad_cval=(0, 255), name="pad-by-0-to-50px-random-pad-modes-cvals"), iaa.CropAndPad(px=((0, 50), (0, 50), (0, 50), (0, 50)), name="pad-by-0-to-50px-each"), iaa.CropAndPad(percent=(0, 0.1), sample_independently=False, name="pad-by-0-to-01percent-same"), iaa.CropAndPad(percent=(0, 0.1), name="pad-by-0-to-01percent"), iaa.CropAndPad(percent=(0, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255), name="pad-by-0-to-01percent-random-pad-modes-cvals"), iaa.CropAndPad(percent=((0, 0.1), (0, 0.1), (0, 0.1), (0, 0.1)), name="pad-by-0-to-01percent-each"), iaa.CropAndPad(px=(-50, 0), name="crop-by-50-to-0px"), iaa.CropAndPad(px=((-50, 0), (-50, 0), (-50, 0), (-50, 0)), name="crop-by-50-to-0px-each"), iaa.CropAndPad(percent=(-0.1, 0), name="crop-by-01-to-0percent"), iaa.CropAndPad(percent=((-0.1, 0), (-0.1, 0), (-0.1, 0), (-0.1, 0)), name="crop-by-01-to-0percent-each"), iaa.CropAndPad(px=(-50, 50), name="pad-and-crop-by-50px") ] print("original", image.shape) ia.imshow(kps[0].draw_on_image(image)) print("-----------------") print("Same aug per image") print("-----------------") for aug in augs: img_aug = aug.augment_image(image) kps_aug = aug.augment_keypoints(kps)[0] img_aug_kps = kps_aug.draw_on_image(img_aug) print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1] / img_aug_kps.shape[0]) ia.imshow(img_aug_kps) print("-----------------") print("Random aug per image") print("-----------------") for aug in augs_many: images_aug = [] for _ in range(64): aug_det = aug.to_deterministic() img_aug = aug_det.augment_image(image) kps_aug = aug_det.augment_keypoints(kps)[0] img_aug_kps = kps_aug.draw_on_image(img_aug) img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=255) #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) images_aug.append(img_aug_kps) print(aug.name) ia.imshow(ia.draw_grid(images_aug))
def build_augmentation_pipeline(self, height=None, width=None, apply_prob=0.5): sometimes = lambda aug: iaa.Sometimes(apply_prob, aug) pipeline = iaa.Sequential(random_order=False) cfg = self.cfg if cfg.get('fliplr', False): opt = cfg.get('fliplr', False) if type(opt) == int: pipeline.add(sometimes(iaa.Fliplr(opt))) else: pipeline.add(sometimes(iaa.Fliplr(0.5))) if cfg.get('rotation', False): opt = cfg.get('rotation', False) if type(opt) == int: pipeline.add(sometimes(iaa.Affine(rotate=(-opt, opt)))) else: pipeline.add(sometimes(iaa.Affine(rotate=(-10, 10)))) if cfg.get('motion_blur', False): opts = cfg.get('motion_blur', False) if type(opts) == list: opts = dict(opts) pipeline.add(sometimes(iaa.MotionBlur(**opts))) else: pipeline.add(sometimes(iaa.MotionBlur(k=7, angle=(-90, 90)))) if cfg.get('covering', False): pipeline.add( sometimes( iaa.CoarseDropout(0.02, size_percent=0.3, per_channel=0.5))) if cfg.get('elastic_transform', False): pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5))) if cfg.get('gaussian_noise', False): opt = cfg.get('gaussian_noise', False) if type(opt) == int or type(opt) == float: pipeline.add( sometimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, opt), per_channel=0.5))) else: pipeline.add( sometimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5))) if cfg.get('grayscale', False): pipeline.add(sometimes(iaa.Grayscale(alpha=(0.5, 1.0)))) if cfg.get('hist_eq', False): pipeline.add(sometimes(iaa.AllChannelsHistogramEqualization())) if height is not None and width is not None: if not cfg.get('crop_by', False): crop_by = 0.15 else: crop_by = cfg.get('crop_by', False) pipeline.add( iaa.Sometimes( cfg.cropratio, iaa.CropAndPad(percent=(-crop_by, crop_by), keep_size=False))) pipeline.add(iaa.Resize({"height": height, "width": width})) return pipeline
def train_transformation(image, size): #convert image to grayscale and find croping position np_img = np.asarray(image) grayImg = cv2.cvtColor(np_img, cv2.COLOR_BGR2GRAY) h, w = grayImg.shape medianImg = cv2.medianBlur(grayImg, 11) threshold = medianImg.max() / 8 mask = np.zeros((h, w), dtype=np.uint8) mask = (medianImg > threshold) * 1.0 one_mask = np.argwhere(mask == 1) max_pos = np.max(one_mask, axis=0) min_pos = np.min(one_mask, axis=0) top, bottom, left, right = min_pos[0], max_pos[0], min_pos[1], max_pos[1] np_img = np_img[top:bottom, left:right, :] # Transform image sometimes = lambda aug: iaa.Sometimes(0.5, aug) train_seq = iaa.Sequential([ iaa.Resize({ "height": size, "width": size }), sometimes(iaa.Fliplr(1)), sometimes(iaa.Flipud(1)), sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=["constant", "edge"], pad_cval=0)), sometimes( iaa.Affine(scale=(0.8, 1.2), translate_percent=(-0.2, 0.2), rotate=(-45, 45), order=[0, 1], cval=0, mode=["constant", "edge"])), iaa.SomeOf((0, 4), [ iaa.OneOf([ iaa.WithChannels(0, iaa.Add((5, 50))), iaa.WithChannels(1, iaa.Add((5, 20))), iaa.WithChannels(2, iaa.Add((5, 20))), ]), iaa.OneOf([ iaa.GaussianBlur(sigma=(0.0, 2.0)), iaa.MedianBlur(k=(3, 5)), iaa.AverageBlur(k=(2, 4)) ]), iaa.contrast.LinearContrast((0.5, 1.5)), iaa.Sharpen(alpha=(0.0, 0.5), lightness=1.0), iaa.Multiply((0.5, 1.5)), ], random_order=True) ], random_order=False) np_img = train_seq.augment_images(np_img.reshape(1, *np_img.shape)) # Convert to Pillow image im = Image.fromarray(np_img[0]) # Convert to Tensor convert_to_tensor = [transforms.ToTensor()] # if normalize: # convert_to_tensor.append(transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)) tensor_tranform = transforms.Compose(convert_to_tensor) return tensor_tranform(im)
voc_obj.init_voc_datastruct() sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. 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 50% 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={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) cval=(0, 255), # if mode is constant, use a cval between 0 and 255 mode=["constant"] # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # 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), [
def augment(images, boxes, batch_idx, size=556): from imgaug import parameters as iap boxes_augs = [] for box1 in boxes: for box in box1: boxes_augs.append( ia.BoundingBox(x1=box[0], y1=box[1], x2=box[2], y2=box[3])) bbs = ia.BoundingBoxesOnImage(boxes_augs, shape=images[0].shape) seq = iaa.Sequential( [ iaa.OneOf([ iaa.Fliplr(0.5), # horizontal flips iaa.Flipud(0.5), # horizontal flips iaa.CropAndPad(percent=(-0.15, 0.15)), # 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.5, iaa.Add((-40, 40)), iaa.GaussianBlur(sigma=(0, 0.5)), # Invert each image's chanell 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), # iaa.ContrastNormalization((0.75, 1.5)), # Improve or worsen the contrast of images. # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. ), # Strengthen or weaken the contrast in each image. # 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. # 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. # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. iaa.OneOf([ 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=(-4, 4)), iaa.Multiply((0.5, 1.5)), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Sequential([ iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add((50, 100))), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB") ]), iaa.Superpixels(n_segments=100), iaa.Invert(0.2), iaa.CoarseSaltAndPepper(size_percent=0.05), iaa.ElasticTransformation(2), iaa.SimplexNoiseAlpha(first=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True)), iaa.FrequencyNoiseAlpha(first=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True)), iaa.Grayscale(alpha=(0.0, 1.0)), # iaa.PiecewiseAffine(scale=(0.01, 0.05)) ]), ], random_order=True) # apply augmenters in random order seq_det = seq.to_deterministic( ) # Call this once PER BATCH, otherwise you will always get the to get random images_data = images # for i,img in enumerate(images): # images_data[i,:,:,:]=img[:,:,:] aug_images = seq_det.augment_images(images_data) bbs_augs = seq_det.augment_bounding_boxes([bbs]) for i, aug_bb in enumerate(bbs_augs): idx_i = batch_idx + i imgid = train_dataset[idx_i] save_augs(JPEG_dir, anno_dir, idx_i, aug_images[i], aug_bb, imgid + "_" + str(idx_i)) return
def __init__(self, data_dir, image_size=128, augmentations=50, crop_percent=(-0.07, 0.1), affine_scale=(0.8, 1.2), hue_range=(0, 20), translate_percent=(-0.1, 0.1), rotation=(-10, 10)): """ Args: data_dir: image_size: jpeg_pics: different_crops: crop_diff_w: crop_diff_h: keep_close_aspect_ratio: If None will randomly keep aspect ratio """ self.data_dir = data_dir self._data = [] self.image_size = image_size self.num_augmentations = augmentations self.seq = iaa.Sequential( [ iaa.OneOf([ # Crop images to -7% to 10% of their width/height sometimes(iaa.CropAndPad(percent=crop_percent, ), chance=0.2), # Scale image between 80% to 120% of original size # Translate the picture -10% to 10% on both axes sometimes(iaa.Affine( scale=affine_scale, translate_percent=translate_percent, ), chance=0.4) ]), # Rotate and shear image sometimes(iaa.Affine( rotate=rotation, shear=(-3, 3), mode=ia.ALL), chance=0.2), # Changes gamma contrast sometimes(iaa.GammaContrast(gamma=(0.8, 1.3)), chance=0.3), # Change to HSV and add hue then transfer back to RGB sometimes([ iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add(hue_range)), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB") ], chance=0.2), # Add one type of blur sometimes(iaa.OneOf([ iaa.GaussianBlur(sigma=(0.1, 2)), iaa.AverageBlur(k=(1, 6)), iaa.MedianBlur(k=(1, 7)), iaa.BilateralBlur( d=(1, 7), sigma_color=250, sigma_space=250) ]), chance=0.4), sometimes(iaa.Sharpen(alpha=(0, 0.4)), chance=0.4) ], random_order=True)
def main(): quokka = ia.quokka(size=0.5) h, w = quokka.shape[0:2] c = 1 segmap = np.zeros((h, w, c), dtype=np.int32) segmap[70:120, 90:150, 0] = 1 segmap[30:70, 50:65, 0] = 2 segmap[20:50, 55:85, 0] = 3 segmap[120:140, 0:20, 0] = 4 segmap = ia.SegmentationMapsOnImage(segmap, quokka.shape) print("Affine...") aug = iaa.Affine(translate_px={"x": 20}, mode="constant", cval=128) quokka_aug = aug.augment_image(quokka) segmaps_aug = aug.augment_segmentation_maps([segmap])[0] segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("Affine with mode=edge...") aug = iaa.Affine(translate_px={"x": 20}, mode="edge") quokka_aug = aug.augment_image(quokka) segmaps_aug = aug.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("PiecewiseAffine...") aug = iaa.PiecewiseAffine(scale=0.04) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("PerspectiveTransform...") aug = iaa.PerspectiveTransform(scale=0.04) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("ElasticTransformation alpha=3, sig=0.5...") aug = iaa.ElasticTransformation(alpha=3.0, sigma=0.5) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("ElasticTransformation alpha=10, sig=3...") aug = iaa.ElasticTransformation(alpha=10.0, sigma=3.0) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("ElasticTransformation alpha=200, sig=20...") aug = iaa.ElasticTransformation(alpha=200.0, sigma=20.0) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("CopAndPad mode=constant...") aug = iaa.CropAndPad(px=(-10, 10, 15, -15), pad_mode="constant", pad_cval=128) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("CropAndPad mode=edge...") aug = iaa.CropAndPad(px=(-10, 10, 15, -15), pad_mode="edge") aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn])) print("Resize...") aug = iaa.Resize(0.5, interpolation="nearest") aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(ia.draw_grid([segmaps_drawn, segmaps_aug_drawn], cols=2)) print("Alpha...") aug = iaa.Alpha(0.7, iaa.Affine(rotate=20)) aug_det = aug.to_deterministic() quokka_aug = aug_det.augment_image(quokka) segmaps_aug = aug_det.augment_segmentation_maps(segmap) segmaps_drawn = segmap.draw_on_image(quokka)[0] segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0] ia.imshow(np.hstack([segmaps_drawn, segmaps_aug_drawn]))
def build_augmentation_pipeline(self, height=None, width=None, apply_prob=0.5): sometimes = lambda aug: iaa.Sometimes(apply_prob, aug) pipeline = iaa.Sequential(random_order=False) cfg = self.cfg if cfg.mirror: opt = cfg.mirror # fliplr if type(opt) == int: pipeline.add(sometimes(iaa.Fliplr(opt))) else: pipeline.add(sometimes(iaa.Fliplr(0.5))) if cfg.rotation > 0: pipeline.add( iaa.Sometimes( cfg.rotratio, iaa.Affine(rotate=(-cfg.rotation, cfg.rotation)) ) ) if cfg.motion_blur: opts = cfg.motion_blur_params pipeline.add(sometimes(iaa.MotionBlur(**opts))) if cfg.covering: pipeline.add( sometimes(iaa.CoarseDropout(0.02, size_percent=0.3, per_channel=0.5)) ) if cfg.elastic_transform: pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5))) if cfg.get("gaussian_noise", False): opt = cfg.get("gaussian_noise", False) if type(opt) == int or type(opt) == float: pipeline.add( sometimes( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, opt), per_channel=0.5 ) ) ) else: pipeline.add( sometimes( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5 ) ) ) if cfg.get("grayscale", False): pipeline.add(sometimes(iaa.Grayscale(alpha=(0.5, 1.0)))) if cfg.get("hist_eq", False): pipeline.add(sometimes(iaa.AllChannelsHistogramEqualization())) if height is not None and width is not None: if not cfg.get("crop_by", False): crop_by = 0.15 else: crop_by = cfg.get("crop_by", False) pipeline.add( iaa.Sometimes( cfg.get("cropratio", 0.4), iaa.CropAndPad(percent=(-crop_by, crop_by), keep_size=False), ) ) pipeline.add(iaa.Resize({"height": height, "width": width})) return pipeline
import torch from torch.utils.data import Dataset import imgaug as ia import imgaug.augmenters as iaa import torchvision.transforms as transforms __all__ = ['TrainDataset'] augmenter = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Sometimes(0.5, iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode='median' )), iaa.SomeOf((0, 5), [ iaa.Sometimes(0.5, 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)),
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( "--only", default=None, help= "If this is set, then only the results of an augmenter with this name will be shown. " "Optionally, comma-separated list.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), ia.imresize_single_image(data.astronaut(), (128, 128)) ] keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=50, y=40), ia.Keypoint(x=70, y=38), ia.Keypoint(x=62, y=52) ], shape=images[0].shape), ia.KeypointsOnImage([ ia.Keypoint(x=55, y=32), ia.Keypoint(x=42, y=95), ia.Keypoint(x=75, y=89) ], shape=images[1].shape) ] bounding_boxes = [ ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[0].shape), ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[1].shape) ] augmenters = [ iaa.Sequential([ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="Sequential"), iaa.SomeOf(2, children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="SomeOf"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=0.1 * 255), name="Sometimes"), iaa.WithColorspace("HSV", children=[iaa.Add(20)], name="WithColorspace"), iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddToHueAndSaturation"), iaa.Identity(name="Identity"), iaa.Resize({ "width": 64, "height": 64 }, name="Resize"), iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"), iaa.Pad(px=(0, 8), name="Pad-px"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=10, name="BilateralBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"), iaa.Salt(p=0.05, name="Salt"), iaa.Pepper(p=0.05, name="Pepper"), iaa.CoarseSaltAndPepper(p=0.05, size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1), name="CoarsePepper"), 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=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=False, name="Alpha"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=True, name="AlphaPerChannel"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaAffine"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=False, name="AlphaElementwise"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=True, name="AlphaElementwisePerChannel"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaElementwiseAffine"), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="FrequencyNoiseAlpha") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name in [ v.strip() for v in args.only.split(",") ]: print("Augmenter: %s" % (augmenter.name, )) grid = [] for image, kps, bbs in zip(images, keypoints, bounding_boxes): aug_det = augmenter.to_deterministic() imgs_aug = aug_det.augment_images( np.tile(image[np.newaxis, ...], (16, 1, 1, 1))) kps_aug = aug_det.augment_keypoints([kps] * 16) bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16) imgs_aug_drawn = [ kps_aug_one.draw_on_image(img_aug) for img_aug, kps_aug_one in zip(imgs_aug, kps_aug) ] imgs_aug_drawn = [ bbs_aug_one.draw_on_image(img_aug) for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug) ] grid.append(np.hstack(imgs_aug_drawn)) ia.imshow(np.vstack(grid))
iaa.SomeOf(3, [ sometimes(iaa.Fliplr(0.99)), sometimes(iaa.Flipud(0.99)), sometimes(iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, backend="cv2")), sometimes(iaa.Affine(translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, order=1, backend="cv2")), sometimes(iaa.Affine(rotate=(-90, 90), order=1, backend="cv2")), sometimes(iaa.Affine(shear=(-25, 25), order=1, backend="cv2")), sometimes(iaa.ElasticTransformation(alpha=50.0, sigma=5.0)), # radomly crop some part of image to add more BG class in data sometimes(iaa.CropToFixedSize(width=512, height=512, position='uniform')), iaa.OneOf([ sometimes(iaa.KeepSizeByResize( iaa.Crop(percent=(0.05, 0.25), keep_size=False), interpolation='linear')), sometimes(iaa.KeepSizeByResize( iaa.CropAndPad(percent=(0.05, 0.25), pad_mode=["constant", "edge"], pad_cval=(0, 255)), interpolation="linear")) ]), ], random_order=True), ], random_order=True) ''' Noisy Data Aug''' seq_2 = iaa.Sequential( [ iaa.OneOf( [ # Blur each image using a median over neihbourhoods that have a random size between 3x3 and 7x7 sometimes(iaa.MedianBlur(k=(3, 7))), # blur images using gaussian kernels with random value (sigma) from the interval [a, b] sometimes(iaa.GaussianBlur(sigma=(0.0, 1.0))), sometimes(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)) ]
smooth_factor = 0.1 if exclusion_loss_ratio == 0: model_save_dir = '/tmp3/2019_9_9/bigclass_30_param{}_{}/'.format(inter_class_ratio, positive_weight_ratio) else: model_save_dir = '/tmp3/2019_9_9/bigclass_30_param{}_{}_exclu_{}/'.format(inter_class_ratio, positive_weight_ratio, exclusion_loss_ratio) BATCH_SIZE_TRAIN = 32 BATCH_SIZE_VALID = 64 from imgaug import augmenters as iaa sometimes = lambda aug: iaa.Sometimes(0.96, aug) imgaug_train_seq = iaa.Sequential([ iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.Flipud(0.2), # horizontally flip 50% of the images iaa.CropAndPad(percent=(-0.03, 0.03)), # iaa.GaussianBlur(sigma=(0, 3.0)), # blur images with a sigma of 0 to 3.0, # iaa.Sometimes(0.9, iaa.ContrastNormalization((0.9, 1.1))), # iaa.Sometimes(0.9, iaa.Add((-6, 6))), sometimes(iaa.Affine( # scale={"x": (0.92, 1.08), "y": (0.92, 1.08)}, translate_percent={"x": (-0.04, 0.04), "y": (-0.04, 0.04)}, # translate by -20 to +20 percent (per axis) rotate=(-15, 15), # rotate by -10 to +10 degrees )), ]) dict_lr_traintop = collections.OrderedDict() dict_lr_traintop['0'] = 3e-4 dict_lr_traintop['1'] = 1e-5 # dict_lr_rate_traintop['2'] = 1e-5
# 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. sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine( scale={ "x": (0.85, 1.15), "y": (0.85, 1.5) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.15, 0.15), "y": (-0.15, 0.15) }, # translate by -20 to +20 percent (per axis) rotate=(-30, 30), # rotate by -45 to +45 degrees shear=(-5, 5), # shear by -16 to +16 degrees order=[ 0, 1
def load_aug(): def sometimes(aug): return iaa.Sometimes(0.2, aug) seq[0] = iaa.Sequential( [ # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-5, 5), 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=ia.ALL)), iaa.Sometimes(0.1, iaa.MotionBlur(k=15, angle=[-45, 45])), # 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), [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 5)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images # add gaussian noise to images iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # 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.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.LinearContrast((0.5, 2.0))) ]), # improve or worsen the contrast iaa.LinearContrast((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), ], random_order=True) ], random_order=True)
def Augmentation(input_image, label): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, './npy') sometimes = lambda aug: iaa.Sometimes(0.5, aug) aug_name = input_image.split("/")[-1].split(".")[0] minsize = 35 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 44 image_size = 200 nb_batches = 16 aug_label = [label] * nb_batches aug_faces = [] batches = [] seq = iaa.Sequential( [ iaa.Fliplr(0.5), sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine(scale={ "x": (0.8, 1.0), "y": (0.8, 1.0) }, translate_percent={ "x": (-0.2, 0.2), "y": (0, 0.2) }, rotate=(-10, 10), shear=(-16, 16), order=[0, 1], cval=(0, 255))), iaa.SomeOf( (0, 4), [ 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)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 1.0)), # emboss images iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.2, 0.5)), iaa.DirectedEdgeDetect(alpha=(0.2, 0.5), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation((-20, 20)), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 2), sigma=0.25)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) img = misc.imread(input_image) if img.ndim < 2: print("Unable !") elif img.ndim == 2: img = facenet.to_rgb(img) img = img[:, :, 0:3] batches.append(np.array([img for _ in range(nb_batches)], dtype=np.uint8)) aug_images = seq.augment_images(batches[0]) for aug_img in aug_images: bounding_boxes, _ = detect_face.detect_face(aug_img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([ (det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0] ]) offset_dist_squared = np.sum(np.power(offsets, 2.0), 0) index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) det = det[index, :] det = np.squeeze(det) bb_temp = np.zeros(4, dtype=np.int32) bb_temp[0] = det[0] bb_temp[1] = det[1] bb_temp[2] = det[2] bb_temp[3] = det[3] cropped_temp = aug_img[bb_temp[1]:bb_temp[3], bb_temp[0]:bb_temp[2], :] scaled_temp = misc.imresize(cropped_temp, (image_size, image_size), interp='bilinear') aug_faces.append(scaled_temp) return (aug_label, aug_faces)
type=int, default=0, help='model 을 load 할때 1로 설정됩니다.') config = args.parse_args() sometimes = lambda aug: iaa.Sometimes(0.5, aug) lesssometimes = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential( [ iaa.SomeOf( (0, 3), [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images sometimes( iaa.CropAndPad(percent=(-0.1, 0.2), pad_mode=['reflect'])), sometimes( iaa.OneOf([ iaa.Affine(rotate=0), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ])), sometimes( iaa.Affine( scale={ "x": (0.7, 1.3), "y": (0.7, 1.3) }, translate_percent={ "x": (-0.1, 0.1),
import numpy as np import imgaug as ia from imgaug import augmenters as iaa import sys try: from future_builtins import zip except ImportError: pass ia.seed(1) augmentations = iaa.SomeOf( 1, [ iaa.CropAndPad(px=((0, 10), (0, 10), (0, 10), (0, 10)), pad_mode=ia.ALL, pad_cval=(0, 128)), iaa.CoarseDropout(p=(0.05, 0.2), size_percent=(0.15, 0.20)), # iaa.WithColorspace(from_colorspace='RGB', to_colorspace='HSV', children=iaa.WithChannels(2, iaa.Add((0,10)))), iaa.Add((-50, 50)), iaa.ContrastNormalization((0.2, 0.5)), # iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)), iaa.AverageBlur(k=((4, 8), (1, 3))), iaa.PerspectiveTransform(scale=(0.01, 0.2)), iaa.Affine(rotate=(-15, 15), scale=(0.75, 1.25)) ], random_order=True) flipseq = iaa.Fliplr(1.0)
def test_heavy_aug(images): sometimes = lambda aug: iaa.Sometimes(0.5, aug) 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 images by -5% to 10% of their height/width 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) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=( 0, 255 ), # if mode is constant, use a cval between 0 and 255 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # 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), [ 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)), # 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((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) 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.ContrastNormalization((0.5, 2.0))) ]), iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05)) ), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) images_aug = seq.augment_images(images) show_image(image=images[0], wait=0) show_image(image=images_aug[0], wait=0) return True, []
args = parser.parse_args() if args.augment: aug = iaa.OneOf([ iaa.Flipud(0.5), iaa.Fliplr(0.5), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AdditiveGaussianNoise(scale=0.05 * 255), iaa.ElasticTransformation(sigma=5.0), iaa.ContrastNormalization((0.5, 1.5)), iaa.Affine(translate_percent={ 'x': (-0.2, 0.2), 'y': (-0.2, 0.2) }), iaa.Affine(rotate=(-30, 30)), iaa.CropAndPad(percent=(-0.25, 0.25)) ]) augment = True # GAMMA CORRECTION else: aug = None augment = False # GAMMA CORRECTION ############################################################ # Configurations ############################################################ with open(args.json_file, 'r') as f: obj = json.load(f) # Configurations if args.command == "train": config = BagsConfig(len(obj['classes']), args.num_gpus, args.nsteps,
def get_augmentations(aug_names, img_size=224): sometimes = lambda aug: iaa.Sometimes(0.5, aug) none = iaa.Fliplr(0.0) flip_lr = iaa.Fliplr(0.5) flip_ud = iaa.Flipud(0.2) crop_n_pad = sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))) affine = sometimes( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=(0, 255), # if mode is constant, use a cval between 0 and 255 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )) superpixels = sometimes( iaa.Superpixels(p_replace=(0, 0.3), n_segments=(20, 200))) blur = 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)), ]) sharpen = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) emboss = iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)) simplex_noise = iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])) gaussian_noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5) dropout = 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), ]) invert = iaa.Invert(0.25, per_channel=True) add = iaa.Add((-10, 10), per_channel=0.5) hue_saturation = iaa.AddToHueAndSaturation((-20, 20)) multiply = 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))) ]) linear_contrast = iaa.LinearContrast((0.5, 2.0), per_channel=0.5) grayscale = iaa.Grayscale(alpha=(0.5, 1.0)) elastic_transform = iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) piecewise_affine = sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) perspective_transform = sometimes( iaa.PerspectiveTransform(scale=(0.01, 0.1))) augmentations = { 'Original': none, 'Flip left to right': flip_lr, 'Flip upside down': flip_ud, 'Crop and pad': crop_n_pad, 'Affine transformations': affine, 'Superpixels': superpixels, 'Blur': blur, 'Sharpen': sharpen, 'Emboss': emboss, 'Simplex noise': simplex_noise, 'Gaussian noise': gaussian_noise, 'Dropout': dropout, 'Invert': invert, 'Add': add, 'Hue saturation': hue_saturation, 'Multiply': multiply, 'Linear contrast': linear_contrast, 'Grayscale': grayscale, 'Elastic transform': elastic_transform, 'Piecewise affine': piecewise_affine, 'Perspective transform': perspective_transform } aug_list = [iaa.CropToFixedSize(img_size, img_size)] aug_list2 = [] aug_names1 = [] aug_names2 = [] for aug_name in aug_names: if aug_name in [ 'Flip left to right', 'Flip upside down', 'Crop and pad', 'Affine transformations' ]: aug_names1.append(aug_name) else: aug_names2.append(aug_name) for aug_name in aug_names1: aug_list.append(augmentations[aug_name]) aug_list.append(iaa.SomeOf((0, 5), aug_list2)) aug = iaa.Sequential(aug_list) #augDet = aug.to_deterministic() return aug
def __init__( self, instances, anchors, labels, downsample, # ratio between network input's size and network output's size, 32 for YOLOv3 max_box_per_image, batch_size, min_net_size, max_net_size, net_size, shuffle, jitter, norm): self.instances = instances self.batch_size = batch_size self.labels = labels self.downsample = downsample self.max_box_per_image = max_box_per_image self.min_net_size = (min_net_size // self.downsample) * self.downsample self.max_net_size = (max_net_size // self.downsample) * self.downsample self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [ BoundBox(0, 0, anchors[2 * i], anchors[2 * i + 1]) for i in range(len(anchors) // 2) ] self.net_h = net_size self.net_w = net_size self.aug = True #Augment using imaug pipeline https://github.com/aleju/imgaug sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.aug_pipe = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.5), # 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=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-40, 40), # rotate by -45 to +45 degrees shear=(-10, 10), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=( 0, 255 ), # if mode is constant, use a cval between 0 and 255 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # 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), [ 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) ), # 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( (-10, 10), per_channel=0.5 ), # change brightness of images (by -10 to 10 of original value) 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.ContrastNormalization((0.5, 2.0))) ]), iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)), #sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around ], random_order=True) ], random_order=True) if shuffle: np.random.shuffle(self.instances)
def processor(self): return iaa.CropAndPad(self.px, self.percent, self.pad_mode, self.pad_cval, self.keep_size)
ia.imshow(image_aug) images = [image, image, image, image] images_aug = rotate(images = images) ia.imshow(np.hstack(images_aug)) #%% #组合增广 #输入images的图片尺寸可以不同 seq = iaa.Sequential([ #iaa.Affine(rotate=(-25, 25)), #iaa.AdditiveGaussianNoise(scale=(10, 60)), #iaa.Crop(percent=(0.1, 0.2))#上下各裁剪0.1, 左右各裁剪0.2, keep_size = False, 这两个值必须相等 iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"), # crop and pad images iaa.AddToHueAndSaturation((-60, 60)), # change their color iaa.ElasticTransformation(alpha=90, sigma=9), # water-like effect iaa.Cutout() # replace one squared area within the image by a constant intensity value ], random_order=True#表示顺序随机 ) images_aug = seq(images = images) ia.imshow(np.hstack(images_aug)) #%% #不常用操作 #在多个cpu上进行增广https://nbviewer.jupyter.org/github/aleju/imgaug-doc/blob/master/notebooks/A03%20-%20Multicore%20Augmentation.ipynb