def __init__(self, image_ann: str, class_num: int, anchors: str, in_hw: tuple, out_hw: tuple, validation_split=0.1): self.in_hw = np.array(in_hw) assert self.in_hw.ndim == 2 self.out_hw = np.array(out_hw) assert self.out_hw.ndim == 2 self.validation_split = validation_split # type:float if image_ann == None: self.train_list = None self.test_list = None else: img_ann_list = np.load(image_ann, allow_pickle=True) num = int(len(img_ann_list) * self.validation_split) self.train_list = img_ann_list[num:] # type:np.ndarray self.test_list = img_ann_list[:num] # type:np.ndarray self.train_total_data = len(self.train_list) # type:int self.test_total_data = len(self.test_list) # type:int self.grid_wh = (1 / self.out_hw)[:, [1, 0]] # hw 转 wh 需要交换两列 if class_num: self.class_num = class_num # type:int if anchors: self.anchors = np.load(anchors) # type:np.ndarray self.anchor_number = len(self.anchors[0]) self.output_number = len(self.anchors) self.xy_offset = Helper._coordinate_offset( self.anchors, self.out_hw) # type:np.ndarray self.wh_scale = Helper._anchor_scale( self.anchors, self.grid_wh) # type:np.ndarray self.output_shapes = [ tf.TensorShape([None] + list(self.out_hw[i]) + [len(self.anchors[i]), self.class_num + 5]) for i in range(len(self.anchors)) ] self.iaaseq = iaa.OneOf([ iaa.Fliplr(0.5), # 50% 镜像 iaa.Affine(rotate=(-10, 10)), # 随机旋转 iaa.Affine(translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }) # 随机平移 ]) self.colormap = [ (255, 82, 0), (0, 255, 245), (0, 61, 255), (0, 255, 112), (0, 255, 133), (255, 0, 0), (255, 163, 0), (255, 102, 0), (194, 255, 0), (0, 143, 255), (51, 255, 0), (0, 82, 255), (0, 255, 41), (0, 255, 173), (10, 0, 255), (173, 255, 0), (0, 255, 153), (255, 92, 0), (255, 0, 255), (255, 0, 245), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128), (61, 230, 250), (255, 6, 51), (11, 102, 255), (255, 7, 71), (255, 9, 224), (9, 7, 230), (220, 220, 220), (255, 9, 92), (112, 9, 255), (8, 255, 214), (7, 255, 224), (255, 184, 6), (10, 255, 71), (255, 41, 10), (7, 255, 255), (224, 255, 8), (102, 8, 255), (255, 61, 6), (255, 194, 7), (255, 122, 8), (0, 255, 20), (255, 8, 41), (255, 5, 153), (6, 51, 255), (235, 12, 255), (160, 150, 20), (0, 163, 255), (140, 140, 140), (250, 10, 15), (20, 255, 0), (31, 255, 0), (255, 31, 0), (255, 224, 0), (153, 255, 0), (0, 0, 255), (255, 71, 0), (0, 235, 255), (0, 173, 255), (31, 0, 255), (11, 200, 200) ]
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), ]),
def train(model, dataset_dir, subset): """Train the model.""" # Training dataset. dataset_train = MonusegDataset() dataset_train.load_monuseg(dataset_dir, subset) dataset_train.prepare() dataset_train.read_data_and_mask_arr() # Validation dataset dataset_val = MonusegDataset() dataset_val.load_monuseg(dataset_dir, "val") dataset_val.prepare() dataset_val.read_data_and_mask_arr() # Image augmentation # http://imgaug.readthedocs.io/en/latest/source/augmenters.html def image_channel_suffle(image): img_ = image.copy() r, g, b = img_[:, :, 0], img_[:, :, 1], img_[:, :, 2] idx = [0, 1, 2] np.random.shuffle(idx) img_[:, :, idx[0]], img_[:, :, idx[1]], img_[:, :, idx[2]] = r, g, b return img_ def img_func(images, random_state, parents, hooks): for img in images: img = image_channel_suffle(img) return images def keypoint_func(keypoints_on_images, random_state, parents, hooks): return keypoints_on_images augmentation = iaa.SomeOf((2, 5), [ iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.OneOf([ iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ]), iaa.Affine(scale=(0.6, 2)), iaa.Lambda(img_func, keypoint_func), iaa.Multiply((0.8, 1.5)), iaa.GaussianBlur(sigma=(0.0, 2.0)) ]) # *** This training schedule is an example. Update to your needs *** # If starting from imagenet, train heads only for a bit # since they have random weights print("Train network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=1, augmentation=augmentation, layers='heads') print("Train all layers") model.train(dataset_train, dataset_val, learning_rate=3 * 10e-5, epochs=1, augmentation=augmentation, layers='all')
# n_segments=(20, 200) # ) # ), # Blur each image with varying strength using 使用不同的强度模糊每个图像 # gaussian blur (sigma between 0 and 3.0), # average/uniform blur (kernel size between 2x2 and 7x7) # median blur (kernel size between 3x3 and 11x11). # iaa.OneOf([ # iaa.GaussianBlur((0, 3.0)), # iaa.AverageBlur(k=(2, 7)), # iaa.MedianBlur(k=(3, 11)), # ]), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 5)), iaa.MedianBlur(k=(3, 7)), ]), # Sharpen each image, overlay the result with the original 锐化每个图像,将结果与原始图像叠加 # image using an alpha between 0 (no sharpening) and 1 # (full sharpening effect). iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # Same as sharpen, but for an embossing effect. 和锐化一样,不过是为了浮雕效果。 # iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per
def train(self, augmentation=None, verbose=1): from .nnmrcnn import Dataset import mrcnn.model as modellib from mrcnn import utils from imgaug import augmenters as iaa if verbose: self.CONFIG.display() model = modellib.MaskRCNN(mode="training", config=self.CONFIG, model_dir=self.LOG_DIR) # Select weights file to load assert self.CONFIG.WEIGHTS and type(self.CONFIG.WEIGHTS) is str if self.CONFIG.WEIGHTS.lower() == "coco": weights_path = os.path.join(self.LOG_DIR, "mask_rcnn_coco.h5") # Download weights file if not os.path.exists(weights_path): utils.download_trained_weights(weights_path) elif self.CONFIG.WEIGHTS.lower() == "last": # Find last trained weights weights_path = model.find_last() elif self.CONFIG.WEIGHTS.lower() == "imagenet": # Start from ImageNet trained weights weights_path = model.get_imagenet_weights() else: weights_path = self.CONFIG.WEIGHTS # Load weights if verbose: print("Loading weights ", weights_path) if self.CONFIG.WEIGHTS.lower() == "coco": # Exclude the last layers because they require a matching # number of classes model.load_weights(weights_path, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) else: model.load_weights(weights_path, by_name=True) # Training dataset. if verbose: print("Prepare train data") dataset_train = Dataset() dataset_train.load_numberplate("train", self.CONFIG) dataset_train.prepare() # Validation dataset if verbose: print("Prepare validation data") dataset_val = Dataset() dataset_val.load_numberplate("val", self.CONFIG) dataset_val.prepare() # Image augmentation # http://imgaug.readthedocs.io/en/latest/source/augmenters.html augmentation_default = iaa.SomeOf((0, 2), [ iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.OneOf([ iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ]), iaa.Multiply((0.8, 1.5)), iaa.GaussianBlur(sigma=(0.0, 5.0)) ]) augmentation = augmentation or augmentation_default # *** 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. if verbose: print("Training network") model.train(dataset_train, dataset_val, learning_rate=self.CONFIG.LEARNING_RATE, epochs=self.CONFIG.EPOCHS, augmentation=augmentation, layers=self.CONFIG.LAYERS)
def Augmentors(orgimage, bbs): sometimes = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images # 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 # rotate=(-5, 5), # rotate by -45 to +45 degrees # shear=(5, 5), # 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( 1, [ sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)) ), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur( (0, 1.5 )), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(2, 4) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(3, 7) ), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.15, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 0.5), strength=(0, 0.5)), # 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.Invert(0.05, per_channel=True), # invert color channels iaa.Add( (-5, 5), per_channel=0.5 ), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation( (-5, 5)), # 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.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) try: image_aug, bbs_aug = seq(image=orgimage, bounding_boxes=bbs) return image_aug, bbs_aug except: print("caught") return "caught", "caught"
img_folder = 'JPEGImages' #图片文件夹 dst_folder = 'cropImg' #保存数据的根目录 num_aug = 3 #单张图待增强的数据 if not os.path.exists(dst_folder): os.makedirs(dst_folder) xmls = getXmls(xml_folder) seq = iaa.Sequential([ # iaa.DirectedEdgeDetect(alpha=(0.2, 0.4), direction=(0.0, 1.0)), #噪声 iaa.GammaContrast((0.8, 1.2), per_channel=True), iaa.OneOf([ # iaa.Multiply((1, 1.5), per_channel=0.5), # iaa.MultiplyHueAndSaturation((0.8, 1.2)), iaa.FrequencyNoiseAlpha(exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.LinearContrast((0.5, 2.0))) ]), # 颜色 iaa.Fliplr(0.5), # 水平翻转 iaa.Flipud(0.01), # 上下翻转 iaa.CropAndPad( percent=(-0.05, 0.1), # 依据图像宽高的[-5%, 10%]缩放, # pad_mode=ia.ALL, #若数值过小则填充时会映射出roi区域 pad_mode="constant", pad_cval=(0, 255)), iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, # 原图缩放比例
def data_gen(data, batch_size): # Get total number of samples in the data n = len(data) steps = n // batch_size # Define two numpy arrays for containing batch data and labels batch_data = np.zeros((batch_size, 299, 299, 3), dtype=np.float32) batch_labels = np.zeros((batch_size, 3), dtype=np.float32) # Get a numpy array of all the indices of the input data indices = np.arange(n) # Augmentation sequence seq = iaa.OneOf([ iaa.Fliplr(), # horizontal flips iaa.Affine(rotate=20), # roatation iaa.Multiply((1.2, 1.5)) ]) #random brightness # Initialize a counter i = 0 while True: np.random.shuffle(indices) # Get the next batch count = 0 next_batch = indices[(i * batch_size):(i + 1) * batch_size] for j, idx in enumerate(next_batch): img_name = data.iloc[idx]['image'] label = data.iloc[idx]['label'] # one hot encoding encoded_label = to_categorical(label, num_classes=3) # read the image and resize img_dat = mimg.imread(str(img_name)) img_dat = cv2.resize(img_dat, (299, 299)) if len(img_dat) <= 2: img_dat = np.dstack([img_dat, img_dat, img_dat]) else: img_dat = cv2.cvtColor(img_dat, cv2.COLOR_BGR2RGB) # cv2 reads in BGR mode by default orig_img = cv2.cvtColor(img_dat, cv2.COLOR_BGR2RGB) # normalize the image pixels orig_img = img_dat.astype(np.float32) / 255. batch_data[count] = orig_img batch_labels[count] = encoded_label # generating more samples of the undersampled class if label == 0 and count < batch_size - 2: aug_img1 = seq.augment_image(img_dat) aug_img2 = seq.augment_image(img_dat) aug_img1 = cv2.cvtColor(aug_img1, cv2.COLOR_BGR2RGB) aug_img2 = cv2.cvtColor(aug_img2, cv2.COLOR_BGR2RGB) aug_img1 = aug_img1.astype(np.float32) / 255. aug_img2 = aug_img2.astype(np.float32) / 255. batch_data[count + 1] = aug_img1 batch_labels[count + 1] = encoded_label batch_data[count + 2] = aug_img2 batch_labels[count + 2] = encoded_label count += 2 else: count += 1 if count == batch_size - 1: break i += 1 yield batch_data, batch_labels if i >= steps: i = 0
nb_classes = 10 # # Data Augmentation # Cuando entrenamos redes convulucionales con muy pocas imagenes de entrenamiento como es nuestro caso es muy fácil caer en overfitting, en nuestro caso observavamos que con 10 epochs el modelo de entrenamiento tiene un accuracy del 50% que presumiblemente si aumentamos las epochs mejoraría el accuaracy. En cambio, en las gráficas observavamos que se producía overfitting, ya que no era un modelo exportable en el caso de test donde el modelo actuaba muy mal. # # La técnica de Data augmentation evitará que caigamos en overfitting aumentando las imagenes de muestra con transformaciones aleatorias.De esta manera, nuestro modelo nunca verá más de dos imagenes iguales, esto permitirá generalizar mejor al modelo. # # Cuantos más datos proporcionemos, mejor será el rendimiento (hasta llegar a un límite). Por eso es imporante el aumento de datos. Usaremos imgaug para aumentar nuestras imágenes: # In[18]: # Secuencia de aumentos para cada imagen seq = iaa.OneOf([ iaa.Fliplr(), # Volteos iaa.Affine(rotate=20), # Rotación iaa.Multiply((1.2, 1.5)) ]) # Brillo aleatorio # # Data Generator # In[19]: def data_generator(data, batch_size, is_validation_data=False): n = len(data) # Número de observaciones nb_batches = int(np.ceil(n / batch_size)) indices = np.arange(n) # Concatenamos los datos y las etiquetas
imgs = bp.unpack_ndarray_from_file('../features/train_images_size128_raw.bloscpack') lbls = pd.read_csv('../input/train.csv').iloc[:, 1:4].values trn_imgs = imgs[trn_ndx] trn_lbls = lbls[trn_ndx] vld_imgs = imgs[vld_ndx] vld_lbls = lbls[vld_ndx] # ========================================================================================================================= augs = iaa.Sequential( [ iaa.OneOf( [ iaa.Identity(), iaa.Affine(scale={"x": (0.7, 1.1), "y": (0.7, 1.1)}, rotate=(-15, 15), shear=(-15, 15)), iaa.PerspectiveTransform(scale=.1, keep_size=True), ] ), iaa.SomeOf( (0, 2), [ iaa.PiecewiseAffine(scale=(.02, .03)), iaa.imgcorruptlike.Snow(severity=1), #iaa.CoarseDropout((.1, .2), size_percent=(.12, .2)), ], random_order=True ) ] )
folderOfImagesToBeAugmented = "CameraSet1/Images" folderOfAugmentedImages = "AugmentedSet1" gTruthFileName = "cameraset1.csv" imageFilePathInMatlab = "C:/Users/toran/OneDrive - Universitetet i Agder/MAS500-Swarm/AI/TrainingData/AugmentedSet7/" gTruthPath = folderOfAugmentedImages + '/' + gTruthFileName # https://imgaug.readthedocs.io/en/latest/source/overview_of_augmenters.html # #Set up augmentations seq = iaa.Sequential([ iaa.Sometimes(0.3, iaa.OneOf([ iaa.MotionBlur(k=(5,10), angle=(60, 120)), iaa.imgcorruptlike.DefocusBlur(severity=(1,2)), # iaa.imgcorruptlike.ZoomBlur(severity=1), iaa.GaussianBlur(sigma=(0.3, 0.5)) ]) ), iaa.Sometimes(0.05, iaa.AveragePooling(1) ), # iaa.Sometimes(0.05, # iaa.OneOf([ # iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.05)), # iaa.Cutout(nb_iterations=(1, 3), size=(0.05, 0.15), fill_mode="constant", cval=(0, 255)) # ]), # ),
def get_seq(): 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.5), # vertically flip 50% of all images sometimes( iaa.Affine( scale={ "x": (0.9, 1.6), "y": (0.9, 1.6) }, #>20 will cut part of img translate_percent={ "x": (-0.15, 0.15), "y": (-0.15, 0.15) }, # >20% will also cut part of img rotate=( -10, 10 ), # 45/-45° -> works good with scale + translate to prevent cuts shear=(-5, 5), # shear by -16 to +16 degrees mode=ia.ALL)), iaa.SomeOf( (0, 4), [ sometimes( iaa.Superpixels(p_replace=(0.3, 0.7), n_segments=(10, 100)) ), #superpixel-representation --> better basallamina representation iaa.OneOf([ iaa.GaussianBlur( (0, 0.2 )), #small blur effects --> better representation iaa.AverageBlur(k=(1, 3)), # k must be odd iaa.MedianBlur(k=(1, 3)), # ]), iaa.Sharpen( alpha=(0, 1.0), lightness=(0.9, 1.1)), #cell wall represenation iaa.Emboss(alpha=(0, 0.8), strength=(0, 0.5)), #cell wall represenation #searching for edges or angles --> blobby mask --> better basallamina representation / nuclei iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=( 0.2, 0.4)), #detects edges --> cell wall,.. iaa.DirectedEdgeDetect( alpha=(0.2, 0.4), direction=(0.0, 1.0) ), #direction will make edges from random directions ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.01 * 255), per_channel=0.2), # add gaussian noise to images iaa.OneOf([ iaa.Dropout((0.05, 0.2), per_channel=0.2 ), #rnd remove 5-30% in small pixels iaa.CoarseDropout( (0.05, 0.2), size_percent=(0.01, 0.02), per_channel=0.2), # rnd remove 3% in big pixels ]), iaa.Invert(0.01, per_channel=True), # invert color channels iaa.Add( (-10, 10), per_channel=0.3 ), # change brightness of images (by -10 to 10 of original value) #iaa.AddToHueAndSaturation((-0.1, 0.1)), # 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.9, 1.1), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-1, 0), first=iaa.Multiply((0.9, 1.1), per_channel=True), second=iaa.LinearContrast((0.9, 1.1))) ]), sometimes( iaa.ElasticTransformation(alpha=(0, 0.2), sigma=0.1) ), #still not sure: move pixels locally around sometimes( iaa.PiecewiseAffine(scale=(0.01, 0.03)) ), #still not sure:move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) return seq
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) ] # missing: InColorspace, Lambda, AssertLambda, AssertShape, Convolve 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.Noop(name="Noop"), iaa.Scale({ "width": 64, "height": 64 }, name="Scale"), 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.GaussianBlur((1.0, 3.0)), #first=iaa.MedianBlur((3, 7)), first=iaa.EdgeDetect(1.0), #first=iaa.Affine(rotate=-45), #(-45, 45)), per_channel=False, name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha( #first=iaa.GaussianBlur((1.0, 3.0)), #first=iaa.MedianBlur((3, 7)), first=iaa.EdgeDetect(1.0), #first=iaa.Affine(rotate=-45), #(-45, 45)), 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))
jpg_90 = lambda img: jpg_compress(img, (90, 91)) augs = [ scale_05, scale_08, scale_15, scale_20, gamma_08, gamma_12, jpg_70, jpg_90 ] def random_aug_kaggle(img, p=0.5): if np.random.rand() < p: return random.choice(augs)(img) return img blur = iaa.GaussianBlur(sigma=(0, 2)) sharpen = iaa.Sharpen(alpha=(0, 1), lightness=(0.5, 2)) emboss = iaa.Emboss(alpha=(0, 1), strength=(0, 2)) contrast_normalization = iaa.ContrastNormalization(alpha=(0.7, 1.3)) hard_aug = iaa.OneOf([blur, sharpen, emboss, contrast_normalization]) sometimes_train = iaa.Sometimes(p_hard_aug_train, hard_aug) sometimes_val = iaa.Sometimes(p_hard_aug_val, hard_aug) def aug_train(img): #if min(img.size) > crop_center_size: # return random_flip(random_crop(center_crop(img))) #img_np = np.array(img) #if img_np.shape[0] < crop_center_size and img_np.shape[1] > crop_center_size: # n = np.random.randint(img_np.shape[1] - crop_center_size) # return random_flip(random_crop(Image.fromarray(img_np[:, n:(n + crop_center_size), :]))) #if img_np.shape[1] < crop_center_size and img_np.shape[0] > crop_center_size: # n = np.random.randint(img_np.shape[0] - crop_center_size) # return random_flip(random_crop(Image.fromarray(img_np[n:(n + crop_center_size), :, :]))) return random_flip(random_crop(img))
def draw_single_sequential_images(img_path, idx): ia.seed(44) image = ndimage.imread(img_path) #image = ia.quokka_square(size=(128, 128)) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(1.0), # horizontally flip 50% of all images iaa.Flipud(1.0), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.Affine( scale={ "x": (0.125, 0.75), "y": (0.125, 0.75) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.25, 0.25), "y": (-0.25, 0.25) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-25, 25), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) # 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), [ iaa.OneOf([ iaa.GaussianBlur( (1, 2.0 )), # blur images with a sigma between 0 and 3.0 ]), iaa.Sharpen(alpha=(1.0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.AdditiveGaussianNoise( loc=0, scale=(0.05, 0.1 * 255), per_channel=0.5), # add gaussian noise to images 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.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast sometimes(iaa.PiecewiseAffine(scale=( 0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.075, 0.125))) ], random_order=True) ], random_order=True) #grid = seq.draw_grid(image, cols=8, rows=8) #misc.imsave("examples_grid.jpg", grid) images = [image] * 10 #TODO : convert xml to txt and read it in yolo format and replace the x, y coordinates with them. keypoints = [ ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73) ] # left ear, right ear, mouth keypointsList = [keypoints] * 10 aug_det = seq.to_deterministic() images_aug = aug_det.augment_images(images) row_keypoints = [] image_keypoints = [] for idx in range(len(keypointsList)): onImageKeypoints = [ ia.KeypointsOnImage(keypointsList[idx], shape=image.shape) ] keypoints_aug = aug_det.augment_keypoints(onImageKeypoints) row_keypoints.append(keypoints_aug[0]) for image, keypoints in zip(images_aug, row_keypoints): image_keypoints.append(keypoints.draw_on_image(image, size=5)) for i, image_aug in enumerate(image_keypoints): misc.imsave("image_%05d_%06d.jpg" % (idx, i), image_aug)
def __init__(self, rgb_mean, randomImg, insize): sometimes = lambda aug: iaa.Sometimes(0.7, aug) self.rand_img_dir = randomImg self.rgb_mean = rgb_mean self.inp_dim = insize # self.randomImgList = glob.glob( randomImg + '*.jpg') self.aug = iaa.Sequential([ 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.1, 0.1), "y": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis) rotate=(-25, 25), # rotate by -45 to +45 degrees shear=(-6, 6), # 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) )), iaa.OneOf([ iaa.Fliplr(0.5), iaa.GaussianBlur( sigma=iap.Uniform(0.0, 1.0) ), iaa.BlendAlphaSimplexNoise( foreground=iaa.BlendAlphaSimplexNoise( foreground=iaa.EdgeDetect(1.0), background=iaa.LinearContrast((0.1, .8)), per_channel=True ), background=iaa.BlendAlphaFrequencyNoise( exponent=(-.5, -.1), foreground=iaa.Affine( rotate=(-10, 10), translate_px={"x": (-1, 1), "y": (-1, 1)} ), # background=iaa.AddToHueAndSaturation((-4, 4)), # per_channel=True ), per_channel=True, aggregation_method="max", sigmoid=False ), iaa.BlendAlpha( factor=(0.2, 0.8), foreground=iaa.Sharpen(1.0, lightness=2), background=iaa.CoarseDropout(p=0.1, size_px=8) ), iaa.BlendAlpha( factor=(0.2, 0.8), foreground=iaa.Affine(rotate=(-5, 5)), per_channel=True ), iaa.MotionBlur(k=15, angle=[-5, 5]), iaa.BlendAlphaCheckerboard(nb_rows=2, nb_cols=(1, 4), foreground=iaa.AddToHue((-10, 10))), iaa.BlendAlphaElementwise((0, 1.0), iaa.AddToHue(10)), iaa.BilateralBlur( d=(3, 10), sigma_color=(1, 5), sigma_space=(1, 5)), iaa.AdditiveGaussianNoise(scale=0.02 * 255), iaa.AddElementwise((-5, 5), per_channel=0.5), iaa.AdditiveLaplaceNoise(scale=0.01 * 255), iaa.AdditivePoissonNoise(20), iaa.Cutout(fill_mode="gaussian", fill_per_channel=True), iaa.CoarseDropout(0.02, size_percent=0.1), iaa.SaltAndPepper(0.1, per_channel=True), iaa.JpegCompression(compression=(70, 99)), iaa.ImpulseNoise(0.02), iaa.Dropout(p=(0, 0.04)), iaa.Sharpen(alpha=0.1), ]) # oneof ])
def __get_augmentation(self, mode, rng): if mode == "train": shape_augs = [ # * order = ``0`` -> ``cv2.INTER_NEAREST`` # * order = ``1`` -> ``cv2.INTER_LINEAR`` # * order = ``2`` -> ``cv2.INTER_CUBIC`` # * order = ``3`` -> ``cv2.INTER_CUBIC`` # * order = ``4`` -> ``cv2.INTER_CUBIC`` # ! for pannuke v0, no rotation or translation, just flip to avoid mirror padding 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 -A to +A percent (per axis) translate_percent={ "x": (-0.01, 0.01), "y": (-0.01, 0.01) }, shear=(-5, 5), # shear by -5 to +5 degrees rotate=(-179, 179), # rotate by -179 to +179 degrees order=0, # use nearest neighbour backend="cv2", # opencv for fast processing seed=rng, ), # set position to 'center' for center crop # else 'uniform' for random crop iaa.CropToFixedSize(self.input_shape[0], self.input_shape[1], position="center"), iaa.Fliplr(0.5, seed=rng), iaa.Flipud(0.5, seed=rng), ] input_augs = [ iaa.OneOf([ iaa.Lambda( seed=rng, func_images=lambda *args: gaussian_blur(*args, max_ksize=3), ), iaa.Lambda( seed=rng, func_images=lambda *args: median_blur(*args, max_ksize=3), ), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), ]), iaa.Sequential( [ iaa.Lambda( seed=rng, func_images=lambda *args: add_to_hue( *args, range=(-8, 8)), ), iaa.Lambda( seed=rng, func_images=lambda *args: add_to_saturation( *args, range=(-0.2, 0.2)), ), iaa.Lambda( seed=rng, func_images=lambda *args: add_to_brightness( *args, range=(-26, 26)), ), iaa.Lambda( seed=rng, func_images=lambda *args: add_to_contrast( *args, range=(0.75, 1.25)), ), ], random_order=True, ), ] elif mode == "valid": shape_augs = [ # set position to 'center' for center crop # else 'uniform' for random crop iaa.CropToFixedSize(self.input_shape[0], self.input_shape[1], position="center") ] input_augs = [] return shape_augs, input_augs
def test_determinism(): reseed() images = [ ia.quokka(size=(128, 128)), ia.quokka(size=(64, 64)), ia.imresize_single_image(skimage.data.astronaut(), (128, 256)) ] images.extend([ia.quokka(size=(16, 16))] * 20) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=20, y=10), ia.Keypoint(x=5, y=5), ia.Keypoint(x=10, y=43) ], shape=(50, 60, 3)) ] * 20 augs = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)), iaa.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)), iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)), iaa.EdgeDetect(alpha=(0.1, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)), iaa.Add((-50, 50)), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)), iaa.Multiply((0.6, 1.4)), iaa.MultiplyElementwise((0.6, 1.4)), iaa.Dropout((0.3, 0.5)), iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)), iaa.Invert(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=0.5) ] augs_affect_geometry = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5)) ] for aug in augs: aug_det = aug.to_deterministic() images_aug1 = aug_det.augment_images(images) images_aug2 = aug_det.augment_images(images) aug_det = aug.to_deterministic() images_aug3 = aug_det.augment_images(images) images_aug4 = aug_det.augment_images(images) assert array_equal_lists(images_aug1, images_aug2), \ "Images (1, 2) expected to be identical for %s" % (aug.name,) assert array_equal_lists(images_aug3, images_aug4), \ "Images (3, 4) expected to be identical for %s" % (aug.name,) assert not array_equal_lists(images_aug1, images_aug3), \ "Images (1, 3) expected to be different for %s" % (aug.name,) for aug in augs_affect_geometry: aug_det = aug.to_deterministic() kps_aug1 = aug_det.augment_keypoints(keypoints) kps_aug2 = aug_det.augment_keypoints(keypoints) aug_det = aug.to_deterministic() kps_aug3 = aug_det.augment_keypoints(keypoints) kps_aug4 = aug_det.augment_keypoints(keypoints) assert keypoints_equal(kps_aug1, kps_aug2), \ "Keypoints (1, 2) expected to be identical for %s" % (aug.name,) assert keypoints_equal(kps_aug3, kps_aug4), \ "Keypoints (3, 4) expected to be identical for %s" % (aug.name,) assert not keypoints_equal(kps_aug1, kps_aug3), \ "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
def __init__( self, instances, anchors, # for Feature Pyramid Networks we need 9 anchors, 3 for each scale labels, downsample=32, # ratio between network input's size and network output's size, 32 for YOLOv1-3 max_box_per_image=30, batch_size=1, # min_net_size=224, # max_net_size=224, shuffle=True, jitter=True, norm=None): 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 = 224 self.net_w = 224 # augmentors by 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( [ sometimes(iaa.Affine()), # 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) ), # 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.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.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value) iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast ], random_order=True) ], random_order=True) if shuffle: np.random.shuffle(self.instances)
def test_unusual_channel_numbers(): reseed() images = [(0, create_random_images((4, 16, 16))), (1, create_random_images((4, 16, 16, 1))), (2, create_random_images((4, 16, 16, 2))), (4, create_random_images((4, 16, 16, 4))), (5, create_random_images((4, 16, 16, 5))), (10, create_random_images((4, 16, 16, 10))), (20, create_random_images((4, 16, 16, 20)))] augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.SomeOf(1, [iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), iaa.Noop(name="Noop"), iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize({ "height": 4, "width": 4 }, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: for (nb_channels, images_c) in images: if aug.name != "Resize": images_aug = aug.augment_images(images_c) assert images_aug.shape == images_c.shape image_aug = aug.augment_image(images_c[0]) assert image_aug.shape == images_c[0].shape else: images_aug = aug.augment_images(images_c) image_aug = aug.augment_image(images_c[0]) if images_c.ndim == 3: assert images_aug.shape == (4, 4, 4) assert image_aug.shape == (4, 4) else: assert images_aug.shape == (4, 4, 4, images_c.shape[3]) assert image_aug.shape == (4, 4, images_c.shape[3])
# prepare the validation dataset dataset_val = DetectorDataset(image_fps_val, image_annotations, ORIG_SIZE, ORIG_SIZE) dataset_val.prepare() # Image augmentation (light but constant) augmentation = iaa.Sequential([ iaa.Fliplr(0.1), iaa.OneOf([ ## geometric transform iaa.Affine( scale={ "x": (0.98, 1.02), "y": (0.98, 1.04) }, translate_percent={ "x": (-0.02, 0.02), "y": (-0.04, 0.04) }, rotate=(-2, 2), shear=(-1, 1), ), iaa.PiecewiseAffine(scale=(0.01, 0.025)), ]), iaa.OneOf([ ## brightness or contrast iaa.Multiply((0.9, 1.1)), iaa.ContrastNormalization((0.9, 1.1)), ]), iaa.OneOf([ ## blur or sharpen iaa.GaussianBlur(sigma=(0.0, 0.1)), iaa.Sharpen(alpha=(0.0, 0.1)), ]),
def test_dtype_preservation(): reseed() size = (4, 16, 16, 3) images = [ np.random.uniform(0, 255, size).astype(np.uint8), np.random.uniform(0, 65535, size).astype(np.uint16), np.random.uniform(0, 4294967295, size).astype(np.uint32), np.random.uniform(-128, 127, size).astype(np.int16), np.random.uniform(-32768, 32767, size).astype(np.int32), np.random.uniform(0.0, 1.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float16), np.random.uniform(-1000.0, 1000.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float64) ] default_dtypes = set([arr.dtype for arr in images]) # Some dtypes are here removed per augmenter, because the respective # augmenter does not support them. This test currently only checks whether # dtypes are preserved from in- to output for all dtypes that are supported # per augmenter. # dtypes are here removed via list comprehension instead of # `default_dtypes - set([dtype])`, because the latter one simply never # removed the dtype(s) for some reason def _not_dts(dts): return [dt for dt in default_dtypes if dt not in dts] augs = [ (iaa.Add((-5, 5), name="Add"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AddElementwise((-5, 5), name="AddElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Multiply((0.95, 1.05), name="Multiply"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Dropout(0.01, name="Dropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes), (iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), _not_dts([np.float16])), (iaa.AverageBlur((3, 5), name="AverageBlur"), _not_dts([np.uint32, np.int32, np.float16])), (iaa.MedianBlur((3, 5), name="MedianBlur"), _not_dts([np.uint32, np.int32, np.float16, np.float64])), (iaa.BilateralBlur((3, 5), name="BilateralBlur"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64 ])), (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes), (iaa.Flipud(0.5, name="Flipud"), default_dtypes), (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), _not_dts([np.uint32, np.int32])), (iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), _not_dts([np.uint32, np.int32])), (iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), _not_dts([np.uint32, np.int32])), (iaa.Affine(shear=(-20, 20), name="Affine-shear"), _not_dts([np.uint32, np.int32])), (iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), _not_dts([np.uint32, np.int32])), (iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), default_dtypes), (iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), _not_dts([np.float16])), (iaa.Sequential([iaa.Noop(), iaa.Noop()], name="SequentialNoop"), default_dtypes), (iaa.SomeOf(1, [iaa.Noop(), iaa.Noop()], name="SomeOfNoop"), default_dtypes), (iaa.OneOf([iaa.Noop(), iaa.Noop()], name="OneOfNoop"), default_dtypes), (iaa.Sometimes(0.5, iaa.Noop(), name="SometimesNoop"), default_dtypes), (iaa.Sequential([iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="Sequential"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SomeOf(1, [iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="SomeOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.OneOf([iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Noop(name="Noop"), default_dtypes), (iaa.Alpha((0.0, 0.1), iaa.Noop(), name="AlphaNoop"), default_dtypes), (iaa.AlphaElementwise((0.0, 0.1), iaa.Noop(), name="AlphaElementwiseNoop"), default_dtypes), (iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Superpixels(p_replace=0.01, n_segments=64), _not_dts([np.float16, np.float32, np.float64])), (iaa.Resize({ "height": 4, "width": 4 }, name="Resize"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.Pad(px=(0, 10), name="Pad"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.Crop(px=(0, 10), name="Crop"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])) ] for (aug, allowed_dtypes) in augs: for images_i in images: if images_i.dtype in allowed_dtypes: images_aug = aug.augment_images(images_i) assert images_aug.dtype == images_i.dtype
def __init__(self): super(ImgAugTransform, self).__init__() from imgaug import augmenters as iaa from imgaug import parameters as iap self.seq = iaa.Sequential(children=[ iaa.Sequential(children=[ iaa.Sequential(children=[ iaa.OneOf(children=[ iaa.Sometimes( p=0.95, then_list=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=(-30, 30), shear=(-15, 15), order=iap.Choice( [0, 1, 3], p=[0.15, 0.80, 0.05]), mode="reflect", name="Affine")), iaa.Sometimes(p=0.05, then_list=iaa.PerspectiveTransform( scale=(0.01, 0.1))) ], name="Blur"), iaa.Sometimes(p=0.01, then_list=iaa.PiecewiseAffine( scale=(0.0, 0.01), nb_rows=(4, 20), nb_cols=(4, 20), order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]), mode="reflect", name="PiecewiseAffine")) ], random_order=True, name="GeomTransform"), iaa.Sequential(children=[ iaa.Sometimes(p=0.75, then_list=iaa.Add(value=(-10, 10), per_channel=0.5, name="Brightness")), iaa.Sometimes(p=0.05, then_list=iaa.Emboss(alpha=(0.0, 0.5), strength=(0.5, 1.2), name="Emboss")), iaa.Sometimes(p=0.1, then_list=iaa.Sharpen(alpha=(0.0, 0.5), lightness=(0.5, 1.2), name="Sharpen")), iaa.Sometimes(p=0.25, then_list=iaa.ContrastNormalization( alpha=(0.5, 1.5), per_channel=0.5, name="ContrastNormalization")) ], random_order=True, name="ColorTransform"), iaa.Sequential(children=[ iaa.Sometimes(p=0.5, then_list=iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 10.0), per_channel=0.5, name="AdditiveGaussianNoise")), iaa.Sometimes(p=0.1, then_list=iaa.SaltAndPepper( p=(0, 0.001), per_channel=0.5, name="SaltAndPepper")) ], random_order=True, name="Noise"), iaa.OneOf(children=[ iaa.Sometimes(p=0.05, then_list=iaa.MedianBlur(k=3, name="MedianBlur")), iaa.Sometimes(p=0.05, then_list=iaa.AverageBlur( k=(2, 4), name="AverageBlur")), iaa.Sometimes(p=0.5, then_list=iaa.GaussianBlur( sigma=(0.0, 2.0), name="GaussianBlur")) ], name="Blur"), ], random_order=True, name="MainProcess") ])
def train(self): u_optimizer = tf.train.AdamOptimizer( learning_rate=self.lr, beta1=self.beta1).minimize( self.all_stages_loss, var_list=self.u_vars) # initialize the parameters of the network init_op = tf.global_variables_initializer() self.sess.run(init_op) # initialize the pretrined weights of the 3D cascaded model self.initialize_finetune() # save .log self.log_writer = tf.summary.FileWriter("./logs", self.sess.graph) counter = 1 if self.load_chkpoint(self.chkpoint_dir, self.step): print(" [*] Load checkpoint succeed !") else: print(" [!] Failed to load the checkpoint...") # temporary file to save loss loss_log = open("loss.txt", "w") # lock the graph to be read-only in case we make any mistake when add nodes afterwards self.sess.graph.finalize() # data augment augmentation = iaa.SomeOf((1, 4), [ iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.OneOf([iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270)]), iaa.Multiply((0.8, 1.5)), iaa.GaussianBlur(sigma=(0.0, 4.0)), iaa.Affine(rotate=(-45, 45)) ]) # Initilize the batch generator data_generator = BatchGenerator( batch_size=self.batch_size, shuffle=True, seed=1, volume_path=self.traindata_dir, modalities=self.inputI_chn, resize_r=self.resize_r, rename_map=self.rename_map, patch_dim=self.outputI_size, augmentation=augmentation) for epoch in np.arange(self.epoch): start_time = time.time() # Get the training data batch_img, batch_img2, batch_label, batch_label_stage2, batch_label_stage3 = next( data_generator) # Update Cascaded 3D U-net to get the loss of every step _, cur_train_loss = self.sess.run([u_optimizer, self.all_stages_loss], feed_dict={self.stage1_inputI: batch_img, self.stage1_input_gt: batch_label, self.stage2_inputI: batch_img2, self.stage2_input_gt: batch_label_stage2, self.stage3_inputI: batch_img2, self.stage3_input_gt: batch_label_stage3}) counter += 1 if np.mod(epoch, 2) == 0: print( "Epoch: [%2d] :....time: %4.4f........................train_loss: %.8f" % (epoch, time.time() - start_time, cur_train_loss)) if np.mod(counter, self.save_intval) == 0: # validate and save checkpoints for several iterations # validate on both part of the training data and the validation data self.test( counter=counter, logname="train.log", dataset="train_set", save_pred=False, save_log_single=False, eval_flag=True) self.test( counter=counter, logname="test.log", dataset="test_set", save_pred=False, save_log_single=False, eval_flag=True) self.save_chkpoint(self.chkpoint_dir, self.model_name, counter) loss_log.close()
def __init__(self) -> None: # 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. def sometimes(aug): return iaa.Sometimes(0.5, aug) self.main_seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.15), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad(percent=(0, 0.15), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine( # scale images to 80-120% of their size, individually per axis scale={ "x": (0.5, 1.3), "y": (0.5, 1.5) }, # translate by -20 to +20 percent (per axis) translate_px={ "x": (-10, 10), "y": (-10, 10) }, #translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-30, 30), # rotate by -45 to +45 degrees shear=(-25, 25), # 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=ia.ALL)), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ # convert images into their superpixel representation #sometimes(iaa.Superpixels( # p_replace=(0, 1.0), n_segments=(20, 100))), iaa.OneOf([ # blur images with a sigma between 0 and 3.0 iaa.GaussianBlur((0.9, 1.0)), # blur image using local means with kernel sizes between 2 and 7 #iaa.AverageBlur(k=(1, 3)), # blur image using local medians with kernel sizes between 2 and 7 #iaa.MedianBlur(k=(1, 3)), ]), #iaa.Emboss(alpha=(0.4, 0.8), strength=( # 0.4, 0.8)), # 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.1, .5)), iaa.DirectedEdgeDetect(alpha=(0.1, .50), direction=(0.90, 1.0)), ])), # add gaussian noise to images iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # invert color channels #iaa.Invert(1, per_channel=True), # change brightness of images (by -10 to 10 of original value) iaa.Add((0.8, .1), per_channel=0.5), # change hue and saturation iaa.AddToHueAndSaturation((0, 1)), # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ #iaa.Multiply((0.8, 1.1), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-2, 0), first=iaa.Multiply( (0.6, 1.), per_channel=True), #second=iaa.LinearContrast((0.6, 1.0)) ) ]), # improve or worsen the contrast #iaa.LinearContrast((0.1, .60), per_channel=0.5), #iaa.Grayscale(alpha=(0.1, .5)), # move pixels locally around (with random strengths) #sometimes(iaa.ElasticTransformation( # alpha=(0.7, 1), sigma=0.1)), # sometimes move parts of the image around #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), #sometimes(iaa.PerspectiveTransform(scale=(0.1, 0.5))) ], random_order=True) ], random_order=True)
def get_augmenter(imgs, probs=0.5): # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, sometimes = lambda aug: iaa.Sometimes(probs, aug) # Define our sequence of augmentation steps that will be applied to every image augment_sequences = iaa.Sequential( [ # # apply the following augmenters to most images sometimes(iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis translate_percent={"x": (-0.12, 0.12), "y": (-0.12, 0.12)}, # translate by -20 to +20 percent (per axis) rotate=(-5, 5), # rotate by -45 to +45 degrees shear=(-13, 13), # 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 one 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, 2.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 4)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 5)), # 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 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.05), per_channel=0.5), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.06), 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.LinearContrast((0.5, 2.0)) ) ]), # iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast # iaa.Grayscale(alpha=(0.0, 1.0)), 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 = augment_sequences(images=imgs) return images_aug
def __init__(self, config, split, batch_size, shuffle=True, jitter=False): 'Initialization' self.config = config self.split = split self.batch_size = batch_size self.image_h = config.DATA.IMG_H self.image_w = config.DATA.IMG_W self.n_channels = 3 ## TODO changed to config fold_df = pd.read_csv(self.config.FOLD_DF, engine='python') self.dataset = fold_df.loc[fold_df['split'] == self.split].reset_index(drop=True) if config.DEBUG: self.fold_df = self.fold_df[:100] print(self.split, 'set:', len(self.dataset)) self.shuffle = shuffle self.on_epoch_end() self.jitter = jitter sometimes = lambda aug: iaa.Sometimes(0.5, aug) 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.2), # vertically flip 20% of all images # sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width #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=(-5, 5), # rotate by -45 to +45 degrees # shear=(-5, 5), # 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, 3), [ # 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 # sometimes(iaa.OneOf([ # iaa.EdgeDetect(alpha=(0, 0.7)), # iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), # ])), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # 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.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value) 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 )
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, 3), [ iaa.OneOf([ iaa.GaussianBlur( (0, 2.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(1, 5) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(1, 5) ), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.03 * 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.01, 0.03), per_channel=0.2), ]),
def __init__(self, images, config, shuffle=True, jitter=True, norm=None): self.generator = None self.images = images self.config = config self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [ BoundBox(0, 0, config['ANCHORS'][2 * i], config['ANCHORS'][2 * i + 1]) for i in range(int(len(config['ANCHORS']) // 2)) ] ### augmentors by 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.2), # vertically flip 20% of all images #sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width 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=(-5, 5), # rotate by -45 to +45 degrees #shear=(-5, 5), # 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 #sometimes(iaa.OneOf([ # iaa.EdgeDetect(alpha=(0, 0.7)), # iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), #])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # 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.Multiply( (0.5, 1.5), per_channel=0.5 ), # change brightness of images (50-150% of original value) 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.images)
import imgaug as ia import numpy as np from matplotlib import pyplot as plt def sometimes(aug): return iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.02), iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.3, iaa.GaussianBlur(sigma=(0, 0.5))), iaa.Sometimes(0.3, iaa.Superpixels(0.5, 125)), iaa.Add((-5, 5), True), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-10, 10), shear=(-5, 5)), iaa.OneOf([ iaa.PerspectiveTransform(0.05), iaa.PiecewiseAffine(0.02)]) ], random_order=True) def similar_image(image: np.ndarray, n: int) -> np.ndarray: ret = np.zeros((n,) + image.shape) for i in range(n): ret[i] = seq.augment_image(image) return ret