Beispiel #1
0
def augment_image(img, k):
    seq = iaa.Sequential(
        [
            iaa.Affine(rotate=(-180, 180)),
            #iaa.AdditiveGaussianNoise(scale=(10, 60)),
            iaa.Crop(percent=(0, 0.2))
        ],
        random_order=True)
    augmented_images = []
    for i in range(k):
        image_aug = seq.augment_image(img)
        augmented_images.append(image_aug)
    return augmented_images
def train_augment(drawing, label, index):
    up_rand = np.random.random()
    right_rand = np.random.random()
    percent_crop = 0.1
    seq = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Crop(percent=(up_rand*percent_crop, right_rand*percent_crop, \
                      (1-up_rand)*percent_crop, (1-right_rand)*percent_crop), keep_size=True)
    ])

    image = drawing_to_image_with_color_aug(drawing, SIZE, SIZE, seq)
    #     image = drawing_to_image_with_color_v2(drawing, 96, 96)
    return image, label, None
Beispiel #3
0
 def _coerce_augmenter(self, augmenter):
     import netharn as nh
     import imgaug.augmenters as iaa
     if augmenter is True:
         augmenter = 'simple'
     if not augmenter:
         augmenter = None
     elif augmenter == 'simple':
         augmenter = iaa.Sequential(
             [iaa.Crop(percent=(0, .2)),
              iaa.Fliplr(p=.5)])
     elif augmenter == 'medium':
         augmenter = iaa.Sequential([
             iaa.Sometimes(
                 0.2, nh.data.transforms.HSVShift(hue=0.1, sat=1.5,
                                                  val=1.5)),
             iaa.Crop(percent=(0, .2)),
             iaa.Fliplr(p=.5)
         ])
     else:
         raise KeyError('Unknown augmentation {!r}'.format(self.augment))
     return augmenter
Beispiel #4
0
def augumentor(image):
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(rotate=(-10, 10)),
        sometimes(iaa.Crop(percent=(0, 0.1), keep_size=True)),
    ],
                         random_order=True)

    image_aug = seq.augment_image(image)

    return image_aug
Beispiel #5
0
 def crop(dataset):
     new_dataset = []
     for iterm in dataset:
         image = np.array(iterm[0][:])
         image = image.reshape((32, 32, 3)).astype(np.uint8)
         seq = iaa.Sequential(
             [iaa.Pad(px=(0, 4, 4, 0)),
              iaa.Crop(px=(0, 4, 4, 0))])
         # iterm[0] = image.transpose(2,0,1).reshape(-1)
         image = seq(images=image)
         noise_image = (image.reshape(-1)).astype(np.float32)
         new_dataset.append([noise_image, iterm[1]])
     return new_dataset
def train_augmentation() -> iaa.Augmenter:
    seq = iaa.Sequential(
        [
            # iaa.Multiply(mul=(0.8, 1.5)),
            # sometimes(iaa.GammaContrast()),
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5),
            sometimes((iaa.pillike.Autocontrast())),
            sometimes(iaa.Crop(px=(0, 25), keep_size=True))
        ],
        random_order=True
    )
    return seq
Beispiel #7
0
    def __init__(self):
        sometimes = lambda aug: iaa.Sometimes(0.3, aug)

        self.aug = iaa.Sequential(
            iaa.SomeOf(
                (1, 5),
                [
                    # blur
                    sometimes(
                        iaa.OneOf([
                            iaa.GaussianBlur(sigma=(0, 1.0)),
                            iaa.MotionBlur(k=3)
                        ])),

                    # color
                    # sometimes(
                    #     iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)),
                    sometimes(
                        iaa.SigmoidContrast(gain=(3, 10),
                                            cutoff=(0.4, 0.6),
                                            per_channel=True)),
                    sometimes(iaa.Invert(0.25, per_channel=0.5)),
                    sometimes(iaa.Solarize(0.5, threshold=(32, 128))),
                    sometimes(iaa.Dropout2d(p=0.5)),
                    sometimes(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
                    sometimes(iaa.Add((-40, 40), per_channel=0.5)),
                    sometimes(iaa.JpegCompression(compression=(5, 80))),

                    # distort
                    sometimes(
                        iaa.Crop(percent=(0.01, 0.05),
                                 sample_independently=True)),
                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.01))),
                    sometimes(
                        iaa.Affine(
                            scale=(0.7, 1.3),
                            translate_percent=(-0.1, 0.1),
                            #                            rotate=(-5, 5), shear=(-5, 5),
                            order=[0, 1],
                            cval=(0, 255),
                            mode=ia.ALL)),
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))),
                    sometimes(
                        iaa.OneOf([
                            iaa.Dropout(p=(0, 0.1)),
                            iaa.CoarseDropout(p=(0, 0.1),
                                              size_percent=(0.02, 0.25))
                        ])),
                ],
                random_order=True),
            random_order=True)
Beispiel #8
0
def multi_crop(img_source, gen_times=5, add_black=True):
    if isinstance(img_source, str):
        try:
            image1 = cv2.imread(img_source)
        except:
            # Corrupt JPEG data1: 19 extraneous bytes before marker 0xc4
            raise Exception("image file not found:" + img_source)
    else:
        image1 = img_source

    if image1 is None:
        raise Exception("image file error:" + img_source)

    if add_black:
        image1 = add_black(img_source)

    list_image = [image1]

    # sometimes = lambda aug: iaa.Sometimes(0.96, aug)
    seq = iaa.Sequential([
        iaa.Crop(px=(0, min(image1.shape[0], image1.shape[1]) // 20)),  # crop images from each side by 0 to 16px (randomly chosen)
        # iaa.GaussianBlur(sigma=(0, 3.0)),  # blur images with a sigma of 0 to 3.0,
        # iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),  # sharpen images
        # sometimes(iaa.Crop(percent=(0, 0.1))),  # crop images by 0-10% of their height/width
        # shuortcut for CropAndPad

        # improve or worsen the contrast  If PCH is set to true, the process happens channel-wise with possibly different S.
        # sometimes1(iaa.ContrastNormalization((0.9, 1.1), per_channel=0.5), ),
        # change brightness of images (by -5 to 5 of original value)
        # sometimes1(iaa.Add((-6, 6), per_channel=0.5),),
        # sometimes(iaa.Affine(
        #     # scale={"x": (0.92, 1.08), "y": (0.92, 1.08)},
        #     # scale images to 80-120% of their size, individually per axis
        #     # Translation Shifts the pixels of the image by the specified amounts in the x and y directions
        #     translate_percent={"x": (-0.08, 0.08), "y": (-0.06, 0.06)},
        #     # translate by -20 to +20 percent (per axis)
        #     rotate=(0, 360),  # 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)
        # )),
    ])

    img_results = []

    for i in range(gen_times):
        images_aug = seq.augment_images(list_image)
        img_results.append(images_aug[0])

    return img_results
Beispiel #9
0
def _load_augmentation_aug_geometric():
    return iaa.OneOf([ 
    
    
        iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Flipud(0.2) ] ) , 

        iaa.CropAndPad(
                        percent=(-0.05, 0.1),
                        pad_mode='constant' ,
                        pad_cval=(0, 255)
                    ) , 
        
        iaa.Crop( percent=(0.0,0.1 )) , 

        iaa.Crop( percent=(0.3,0.5 )) , 
        iaa.Crop( percent=(0.3,0.5 )) , 
        iaa.Crop( percent=(0.3,0.5 )) , 

        iaa.Sequential( [ 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
                    mode='constant',
                    cval=(0,255) , 
                    # use any of scikit-image's warping modes
                    # (see 2nd image from the top for examples)
                ) , 


         iaa.Sometimes( 0.3 , iaa.Crop( percent=(0.3,0.5 ))) ]  ) 

    ] )
Beispiel #10
0
def chapter_examples_basics_simple():
    import imgaug as ia
    from imgaug import augmenters as iaa

    # Example batch of images.
    # The array has shape (32, 64, 64, 3) and dtype uint8.
    images = np.array([ia.quokka(size=(64, 64)) for _ in range(32)],
                      dtype=np.uint8)

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontal flips
            iaa.Crop(percent=(0, 0.1)),  # 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.GaussianBlur(sigma=(0, 0.5))),
            # Strengthen or weaken the contrast in each image.
            iaa.ContrastNormalization((0.75, 1.5)),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            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=(-8, 8))
        ],
        random_order=True)  # apply augmenters in random order

    ia.seed(1)
    images_aug = seq.augment_images(images)

    # ------------

    save("examples_basics", "simple.jpg", grid(images_aug, cols=8, rows=4))
Beispiel #11
0
def aug(imgs: List) -> List:
    import imgaug.augmenters as iaa

    def sometimes(aug_method: object) -> iaa.Sometimes:
        return iaa.Sometimes(0.5, aug_method)

    seq = iaa.Sequential([
        sometimes(iaa.Crop(percent=(0, 0.01))),
        iaa.Affine(
            scale={
                "x": (0.995, 1.01),
                "y": (0.995, 1.01)
            },
            translate_percent={
                "x": (-0.01, 0.01),
                "y": (-0.01, 0.01)
            },
            rotate=(-3, 3),
            shear=(-3, 3),
        ),
        iaa.SomeOf((0, 5), [
            sometimes(
                iaa.OneOf([
                    iaa.OneOf([
                        iaa.GaussianBlur((1, 1.2)),
                        iaa.AverageBlur(k=(1, 3)),
                        iaa.MedianBlur(k=(1, 3))
                    ]),
                    iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
                    iaa.Grayscale(alpha=(0.0, 1.0)),
                    iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0, 0.7)),
                        iaa.DirectedEdgeDetect(alpha=(0, 0.7),
                                               direction=(0.0, 1.0)),
                    ]),
                ])),
            sometimes(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),
            sometimes(
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.005 * 255), per_channel=0.001)),
            sometimes(iaa.Dropout((0.001, 0.01), per_channel=0.5)),
            sometimes(iaa.Add((-10, 10), per_channel=0.5)),
            sometimes(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
            sometimes(iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=0.05)),
            sometimes(iaa.PiecewiseAffine(scale=(0.001, 0.005)))
        ],
                   random_order=True)
    ],
                         random_order=True)
    return seq.augment_images(imgs)
def generate_images(row):
    path, width, height, xmin, ymin, xmax, ymax = row

    image = cv2.imread(path)

    bbs = ia.BoundingBoxesOnImage([
        ia.BoundingBox(x1=xmin, y1=ymin, x2=xmax, y2=ymax)
    ], shape=image.shape)

    seq = iaa.Sequential([
        iaa.Affine(scale=(0.4, 1.6)),
        iaa.Crop(percent=(0, 0.2)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.AddToHueAndSaturation((-30, 30)),
        iaa.Sometimes(0.5,
                      iaa.Affine(rotate=(-45, 45)),
                      iaa.Affine(shear=(-16, 16))),
        iaa.Sometimes(0.2,
                      iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                                         children=iaa.WithChannels(0, iaa.Add((10, 50)))),
                      ),
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        iaa.GaussianBlur(sigma=(0, 1.0))
    ])

    new_rows = []
    for i in range(0, AUGMENTATION_PER_IMAGE):
        seq_det = seq.to_deterministic()
        image_aug = seq_det.augment_images([image])[0]
        bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
        if not bbs_aug.bounding_boxes[0].is_fully_within_image(image.shape):
            continue
        # Another possibility is:
        # bbs_aug = bbs_aug.remove_out_of_image().cut_out_of_image()
        # if not bbs_aug.bounding_boxes:
        #    continue
        after = bbs_aug.bounding_boxes[0]

        if AUGMENTATION_DEBUG:
            image_aug = bbs_aug.draw_on_image(image_aug, thickness=2, color=[0, 0, 255])

        name, ftype = os.path.splitext(os.path.basename(path))
        new_filename = "{}_aug_{}{}".format(name, i, ftype)
        new_path = os.path.join(TRAIN_FOLDER, new_filename)
        cv2.imwrite(new_path, cv2.resize(image_aug, (IMAGE_SIZE, IMAGE_SIZE)))

        new_rows.append([new_path, *scale_coordinates(width, height, after.x1, after.y1, after.x2, after.y2)])

    return new_rows
Beispiel #13
0
def data_generation(labels_dir_path, img_dir_path, batch_size=4, inputshape=(224, 224, 3)):
    aug = iaa.Sequential([
        iaa.Crop(px=(0, 40)),  # crop images from each side by 0 to 16px (randomly chosen)
        iaa.Fliplr(0.5),  # horizontally flip 50% of the 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
            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
        )),
    ], random_order=True)
    dataLoaderRGB = DataLoader(batch_size=batch_size, img_dir_path=img_dir_path,
                               labels_dir_path=labels_dir_path, input_shape=inputshape, aug=aug)
    return dataLoaderRGB.generator()
Beispiel #14
0
def augment(batch):
    batch = preprocess_input(batch)
    sometimes = lambda aug: iaa.Sometimes(.5, aug)
    seq = iaa.Sequential([
        iaa.Fliplr(.5),
        sometimes(iaa.Affine(rotate=(-20, 20))),
        sometimes(iaa.AddToHueAndSaturation((-20, 20))),
        sometimes(iaa.GaussianBlur((0, 2.))),
        sometimes(iaa.ContrastNormalization((.5, 1.5), per_channel=True)),
        sometimes(iaa.Sharpen(alpha=(0, 1.), lightness=(.75, 1.5))),
        sometimes(iaa.Emboss(alpha=(0, 1.), strength=(0, 2.))),
        sometimes(iaa.Crop(px=(5, 15))),
    ])
    return seq.augment_images(batch)
    def getImages(self, thisImg, thisBoxes):
        self.randomCenterX = np.random.randint(100, 924)
        self.randomCenterY = np.random.randint(100, 924)
        self.cropTopLeft = iaa.Crop(
            px=(0, 1024 - self.randomCenterX, 1024 - self.randomCenterY, 0),
            keep_size=False,
        )  # top, right, bottom, left
        self.cropTopRight = iaa.Crop(
            px=(0, 0, 1024 - self.randomCenterY, self.randomCenterX),
            keep_size=False)  # top, right, bottom, left
        self.cropBottomLeft = iaa.Crop(
            px=(self.randomCenterY, 1024 - self.randomCenterX, 0, 0),
            keep_size=False)  # top, right, bottom, left
        self.cropBottomRight = iaa.Crop(
            px=(self.randomCenterY, 0, 0, self.randomCenterX),
            keep_size=False)  # top, right, bottom, left
        randomImgs = [thisImg]
        randomBoxes = [thisBoxes]
        for _ in range(3):
            index = np.random.choice(range(len(self.trainPath)))
            path = self.trainPath[index]
            img = cv2.imread(path)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            boxes = self.labels[index]

            b_list = []
            for box in boxes:
                b = bbs.BoundingBox(x1=box[0],
                                    x2=box[0] + box[2],
                                    y1=box[1],
                                    y2=box[1] + box[3])
                b_list.append(b)
            bbox_img = bbs.BoundingBoxesOnImage(b_list, shape=img.shape)

            randomImgs.append(img)
            randomBoxes.append(bbox_img)
        return randomImgs, randomBoxes
def get_augmenter():
    seq = iaa.Sequential([
        iaa.OneOf([
        	iaa.Fliplr(0.5),
        	iaa.Flipud(0.5)
        ]), # horizontal flips | vertical flips
        iaa.Crop(percent=(0, 0.2)), # 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.OneOf([
            	iaa.GaussianBlur(sigma=(0.5, 0.5)),
            	iaa.MedianBlur(k=(5, 5)), 
            	iaa.AverageBlur(k=(3, 3))
            ])
        ),
        # Strengthen or weaken the contrast in each image.
        iaa.LinearContrast((0.75, 1.5)),
        # Add gaussian noise.
        # For 50% of all images, we sample the noise once per pixel.
        # For the other 50% of all images, we sample the noise per pixel AND
        # channel. This can change the color (not only brightness) of the
        # pixels.
        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.3),
        # 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.h
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.

        iaa.Sometimes(
        	0.5, 
        	iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)})
        ),
        
        iaa.Sometimes(
        	0.5,
        	iaa.Affine(translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}),
        ),
        iaa.Sometimes(
        	0.5,
        	iaa.Affine(rotate=(-30, 30))
        ),

		iaa.Sometimes(0.2, iaa.Grayscale(alpha=1.0))
    ], random_order=True) # apply augmenters in random order
    return seq 
Beispiel #17
0
def augment_img(image_path, amount, width=IMAGE_WIDTH, height=IMAGE_HEIGHT):
    """Returns an array of `amount` generated images"""
    if amount <= 1:
        return [transform_img(image_path, width, height)]
    img = np.array(open_img(image_path))
    # The array has shape (amount, width, height, 3)
    # and dtype uint8.
    images = np.array([img for _ in range(amount)], dtype=np.uint8)

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontal flips
            iaa.Crop(percent=(0, 0.1)),  # 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.GaussianBlur(sigma=(0, 0.5))),
            # Strengthen or weaken the contrast in each image.
            iaa.ContrastNormalization((0.75, 1.5)),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            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=(-8, 8))
        ],
        random_order=True)  # apply augmenters in random order

    images_aug = seq.augment_images(images)
    res = [transform_img(image_path, width, height)]
    for img in images_aug:
        res.append(transform(fromarray(img), width, height))
    return res
def AugmentSteps(elem,l,r):
    
    '''Augments the images in 6 ways
    Inputs:
        - image location
        - directory location
        - exact file location
        '''
    
    # read the image
    image = cv2.imread(elem)
    # image = imageio.imread(elem)
            
    # 1st augmentation --> ROTATE
            
    rotate=iaa.Affine(rotate=(-50, 30))
    rotated_image=rotate.augment_image(image)
    
    cv2.imwrite('{}\\rotated{}.png'.format(l,r), rotated_image) 
            
    # 2nd augmentation --> FLIP HORIZONTALLY
            
    flip_hr=iaa.Fliplr(p=1.0)
    flip_hr_image= flip_hr.augment_image(image)
    cv2.imwrite('{}\\flipped_hor{}.png'.format(l,r), flip_hr_image)
    #plt.imsave('{}\\flipped_hor{}.png'.format(l,r), flip_hr_image, cmap='Greys')
            
    # 3rd augmentation --> FLIP VERTICALLY
            
    flip_vr=iaa.Flipud(p=1.0)
    flip_vr_image= flip_vr.augment_image(image)
    cv2.imwrite('{}\\flipped_ver{}.png'.format(l,r), flip_vr_image)
            
    # 4th augmentation --> CROP
            
    crop = iaa.Crop(percent=(0, 0.3)) # crop image
    crop_image=crop.augment_image(image)
    cv2.imwrite('{}\\cropped{}.png'.format(l,r), crop_image)
            
    # 5th augmentation --> CHANGE CONTRAST
            
    contrast=iaa.GammaContrast(gamma=2.0)
    contrast_image =contrast.augment_image(image)
    cv2.imwrite('{}\\contrast_change{}.png'.format(l,r), contrast_image)
            
    # 6th augmentation --> ADD NOISE
    gaussian_noise=iaa.AdditiveGaussianNoise(10,25)
    noise_image=gaussian_noise.augment_image(image)
    cv2.imwrite('{}\\noise_add{}.png'.format(l,r), noise_image )
Beispiel #19
0
def train(model, epochs=30):
    """Train the model."""
    # Training dataset.
    dataset_train = ProductDataset()
    for class_name in class_list:
        dataset_train.load_products(args.dataset + "/" + class_name, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = ProductDataset()
    for class_name in class_list:
        dataset_val.load_products(args.dataset + "/" + class_name, "val")
    dataset_val.prepare()

    # *** 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.
    # https://imgaug.readthedocs.io/en/latest/source/examples_basics.html
    augmentation = iaa.Sequential(
        [
            iaa.Crop(percent=(
                0, 0.1
            )),  # crop images from each side by 0 to 16px (randomly chosen)
            iaa.Fliplr(0.5),  # horizontally flip 50% of the images
            iaa.Flipud(0.5),  # vertically flip 50% of all images
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       translate_percent={
                           "x": (-0.2, 0.2),
                           "y": (-0.2, 0.2)
                       },
                       rotate=(-45, 45),
                       shear=(-8, 8))
        ],
        random_order=True)
    print("Training network heads")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=epochs,
                layers='heads',
                augmentation=augmentation)
def draw_single_sequential_images():
    image = misc.imresize(
        ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))

    st = lambda aug: iaa.Sometimes(0.3, aug)

    seq = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
        st(iaa.Crop(percent=(0, 0.1))),
        st(iaa.GaussianBlur((0, 3.0))),
        st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),
        st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))),
        st(
            iaa.Sometimes(
                0.5,
                iaa.EdgeDetect(alpha=(0, 0.7)),
                iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
            )),
        st(
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)),
        st(iaa.Dropout((0.0, 0.1), per_channel=0.5)),
        st(iaa.Invert(0.25, per_channel=True)),
        st(iaa.Add((-10, 10), per_channel=0.5)),
        st(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)),
        st(iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale")),
        st(
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       translate_px={
                           "x": (-16, 16),
                           "y": (-16, 16)
                       },
                       rotate=(-45, 45),
                       shear=(-16, 16),
                       order=[0, 1],
                       cval=(0, 255),
                       mode=ia.ALL)),
        st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25))
    ],
                         random_order=True)

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
Beispiel #21
0
    def __init__(self,
                 list_paths,
                 labels,
                 resize_dims=(256, 256),
                 augment=False):
        """
        Dataset for training
        :param list_paths: List of image paths
        :param labels: List of keypoints
        :param resize_dims: What size to make images
        :param augment: Whether to augment images or not
        :param device: Which device to load tensors to
        """
        super().__init__()

        self.list_paths = list_paths
        self.labels = labels
        self.resize_dims = resize_dims
        self.keypoint_divisor = np.array([
            resize_dims[0], resize_dims[1], resize_dims[0], resize_dims[1],
            resize_dims[0], resize_dims[1], resize_dims[0], resize_dims[1],
            resize_dims[0], resize_dims[1], resize_dims[0], resize_dims[1],
            resize_dims[0], resize_dims[1], resize_dims[0], resize_dims[1]
        ])
        self.augment = augment

        self.data_transform = transforms.Compose(
            [transforms.Resize(resize_dims),
             transforms.ToTensor()])

        self.seq_basic = iaa.Sequential([iaa.Resize(resize_dims)])

        self.seq1 = iaa.Sequential([
            iaa.Affine(scale=(0.7, 1.0), mode='edge'),  # 'reflect'
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5),
            iaa.Resize(resize_dims)
        ])

        self.seq2 = iaa.Sequential([
            iaa.Affine(rotate=(-60, 60), scale=(0.7, 1.1),
                       mode='edge'),  # 'reflect'
            iaa.Crop(px=(
                0, 25
            )),  # crop images from each side by 0 to 16px (randomly chosen)
            iaa.Fliplr(0.5),  # horizontally flip 50% of the images
            iaa.Flipud(0.5),
            iaa.Resize(resize_dims)
        ])
def augment_masked_images(data_path, save_path, img_path, msk_path, train_df_path):
    seq = iaa.Sequential([
        iaa.Affine(rotate=(-25, 25)),
        iaa.Crop(percent=(0, 0.1)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.ElasticTransformation(alpha=3, sigma=1)
    ], random_order=True)
    
    train_df = pd.read_csv(train_df_path)
    aug_train_df_path = train_df_path.split(".csv")[-2]+'_aug.csv'
    os.system(f'cat {train_df_path} > {aug_train_df_path}')
    total_imgs = train_df.shape[0]
    train_df = train_df[train_df['mask_pxl']>0]
    masked_imgs = train_df.shape[0]
    number_of_augmentation = 2 #int(total_imgs/masked_imgs)+1
    print('train images:',total_imgs)
    aug_path = os.path.join(save_path, 'augmented')
    if not os.path.exists(aug_path):
        os.mkdir(aug_path)
    if not os.path.exists(os.path.join(aug_path,img_path)):
        os.mkdir(os.path.join(aug_path,img_path))
    if not os.path.exists(os.path.join(aug_path,msk_path)):
        os.mkdir(os.path.join(aug_path,msk_path))
        
    for _, row in tqdm(train_df.iterrows()):
        image, meta = readtiff(os.path.join(save_path, row['dataset_folder'],img_path,row['name']+'_'+row['position']+'.tiff'))
        segmap = imageio.imread(os.path.join(save_path, row['dataset_folder'],msk_path,row['name']+'_'+row['position']+'.png'))
        
        images = np.zeros((number_of_augmentation, image.shape[0], image.shape[1], image.shape[2]))
        segmaps = np.zeros((number_of_augmentation, image.shape[0], image.shape[1], 1))
        for n in range(number_of_augmentation):
            images[n] = image
            segmaps[n] = segmap.reshape((image.shape[0], image.shape[1], 1))
        
        images = images.astype(np.uint8)
        segmaps = segmaps.astype(np.uint8)
        images_aug, segmaps_aug = seq(images=images, segmentation_maps=segmaps)
        
        for n in range(number_of_augmentation):
            img = images_aug[n].reshape((image.shape[0],image.shape[1],image.shape[2]))
            msk  =segmaps_aug[n].reshape((image.shape[0],image.shape[1]))
            with rs.open(os.path.join(aug_path,img_path,row['name']+'_'+str(n)+'_'+row['position']+'.tiff'), 'w', **meta) as dst:
                for ix in range(img.shape[2]):
                    dst.write(img[:, :, ix], ix + 1)
            dst.close() 
            imageio.imwrite(os.path.join(aug_path,msk_path,row['name']+'_'+str(n)+'_'+row['position']+'.png'), msk)
            row_info = pd.DataFrame(['augmented',  row['name']+'_'+str(n), row['position'], int(msk.sum()/255)]).T
            row_info.to_csv(f'{aug_train_df_path}', header=None, index=None, mode='a')
Beispiel #23
0
def process_grey_blur(image_file, image_name, gt_lables, output_dir):

    image = cv2.imread(image_file, 1)

    line = gt_lables[image_name]
    parts = line.split(" ")
    image_name = parts[0]
    image_box = list()
    for j in range(int((len(parts) - 1) / 5)):
        bbox = list()
        bbox.append(float(parts[2 + 5 * j]))
        bbox.append(float(parts[2 + 5 * j + 1]))
        bbox.append(float(parts[2 + 5 * j + 2]))
        bbox.append(float(parts[2 + 5 * j + 3]))
        x1, y1, x2, y2 = get_corrdinate_from_bbox(bbox)
        image_box.append(
            ia.BoundingBox(x1=x1,
                           y1=y1,
                           x2=x2,
                           y2=y2,
                           label=int(parts[2 + 5 * j - 1])))

    bbs = ia.BoundingBoxesOnImage(image_box, shape=image.shape)

    # bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=23, y1=56, x2=276, y2=287)], shape=image.shape)

    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            # iaa.Grayscale(1),
            # iaa.GaussianBlur(2)
            iaa.Crop(px=(100, 200))
        ],
        random_order=True)

    seq_det = seq.to_deterministic()
    image_aug = seq_det.augment_images([image])[0]
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

    image_before = bbs.draw_on_image(image, thickness=2)
    image_after = bbs_aug.draw_on_image(image_aug,
                                        thickness=2,
                                        color=[0, 0, 255])

    # misc.imsave(output_img_name(output_dir, image_name), image_aug)

    image_show(image_before, image_after)

    return bbox_output(bbs_aug, image_name)
def init_augmenter(img_mode="color"):
    """Initializes the augmenters used in the training dataset
    :param config: the config object that contains all the 
    """
    ia.seed(10)

    if img_mode == 'color':
        return iaa.Sequential([
            sometimes(iaa.Fliplr()),
            iaa.MultiplyBrightness((0.6, 1.4)),
            # TODO: try no ChangeColor or Brightness
            sometimes(iaa.ChangeColorTemperature((5000, 7000))),
            iaa.Crop(percent=(
                (0, 0.50),
                (0, 0.50),
                (0, 0.50),
                (0, 0.50)
            ))
            # sometimes(iaa.OneOf([
            #     iaa.Cutout(nb_iterations=(1, 4), size=0.2,
            #                squared=False, cval=(0, 255), fill_mode="constant"),
            #     iaa.Cutout(nb_iterations=(1, 4), size=0.2, squared=False, cval=(
            #         0, 255), fill_mode="gaussian", fill_per_channel=True),
            #     iaa.AdditiveGaussianNoise(scale=(0, 0.1*255))
            # ]))
        ])
    else:
        return iaa.Sequential([
            sometimes(iaa.Fliplr()),
            iaa.Crop(percent=(
                (0, 0.40),
                (0, 0.40),
                (0, 0.40),
                (0, 0.40)
            ))
        ])
Beispiel #25
0
def aug_sroie_dynamic_1():
    """Perform image augmentation for dynamic input shape"""
    aug_list = []
    aug_list.append(
        augmenters.PadToFixedSize(width=1, height=1536, pad_cval=255))
    aug_list.append(augmenters.Affine(translate_px=(-16, 16), cval=255))
    aug_list.append(augmenters.Crop(percent=(0.2, 0.3), keep_size=False))
    aug_list.append(
        augmenters.Resize(size={
            "height": 768,
            "width": "keep-aspect-ratio"
        }))
    aug_list.append(augmenters.Fliplr(0.33, name="horizontal_flip"))
    aug_list.append(augmenters.Flipud(0.33, name="vertical_flip"))
    return aug_list
Beispiel #26
0
def augmenter(x):
    # Basic transformations, can change accordingly
    # Among the given augmentations it always applies any 2 randomly chosen augmentations.
    seq = iaa.Sequential([
        iaa.SomeOf((0, 2), [
            iaa.Crop(px=(0, 16)),
            iaa.Fliplr(0.5),
            iaa.GaussianBlur(sigma=(0, 1.0)),
            iaa.Affine(translate_px=(-15, 15)),
            iaa.Affine(rotate=(-15, 15)),
            iaa.Dropout(p=(0, 0.2))
        ],
                   random_order=True)
    ])
    return seq.augment_images(x)
def augment_images_function(images, p):
    """
    Augmentations with probability p
    """
    augs = iaa.SomeOf((2, 4),
                      [
                          iaa.Crop(px=(0, 4)),  # crop images from each size 0-4px (randomly chosen)
                          iaa.Affine(scale={"x": (0.8,1.2), "y":(0.8, 1.2)}),
                          iaa.Affine(rotate=(-45, 45)),  # rotate by -45 to +45 degrees
                          iaa.Affine(shear=(-10, 10))  # shear by -10 to +10 degrees
                      ])

    sequential = iaa.Sequential([iaa.Sometimes(p, augs)])
    result = sequential.augment_images(images)
    return result
Beispiel #28
0
def _setter(filename, label):
    """
    images :image path
    """
    # image_string=tf.read_file(filename)
    # image=tf.image.decode_jpeg(image_string)
    image = cv2.imread(filename)
    image = cv2.cvtColor(gray, cv2.COLOR_BGR2RGB)
    seq = iaa.Sequential([
        iaa.Crop(px=(0, 16)),
        iaa.Fliplr(0.5),
        iaa.GaussianBlur(sigma=(0, 3.0))
    ])
    image_aug = seq.augment_images(image)
    return image, label
Beispiel #29
0
    def augmentor(self, images):

        seq = iaa.Sequential([
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       rotate=(-10, 10)),
            iaa.Crop(percent=(0, 0.1))
        ],
                             random_order=True)

        images_aug = seq(images=images)

        return images_aug
Beispiel #30
0
def create_sequence():
    sequence = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
        iaa.Multiply((0.7, 1.3)),
        iaa.Grayscale(alpha=(0.0, 1.0)),
        iaa.AddToHueAndSaturation((-15, 15)),
        iaa.Affine(translate_percent={
            "x": (-0.15, 0.15),
            "y": (-0.15, 0.15)
        }),
        iaa.Crop(percent=((0, 0.1), (0, 0.2), (0, 0.1), (0, 0.2))),
    ],
                              random_order=False)
    return sequence