Exemplo n.º 1
0
def someAug():
    bg = iap.Uniform(16, 18)
    #Creating a series of augmentations
    shearXY = iaa.Sequential([
        iaa.ShearY(iap.Uniform(-10, 10), cval=bg),
        iaa.ShearX(iap.Uniform(-10, 10), cval=bg)
    ])
    rotate = iaa.Rotate(rotate=iap.Choice([-30, -15, 15, 30],
                                          p=[0.25, 0.25, 0.25, 0.25]),
                        cval=bg)

    pwAff = iaa.PiecewiseAffine(scale=(0.01, 0.06), cval=bg)

    affine = iaa.Affine(scale={
        "x": iap.Uniform(1.1, 1.2),
        "y": iap.Uniform(1.1, 1.2)
    },
                        cval=bg)

    noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.025 * 255))
    #Using SomeOf to randomly select some augmentations
    someAug = iaa.SomeOf(iap.Choice([2, 3, 4], p=[1 / 3, 1 / 3, 1 / 3]),
                         [affine, shearXY, pwAff, rotate, noise],
                         random_order=True)
    return someAug
Exemplo n.º 2
0
def augmentation(X, y, masks, itterations):
    ys = np.concatenate(
        (y[:, :, np.newaxis], y[:, :, np.newaxis], y[:, :, np.newaxis]),
        axis=2)
    masks = np.concatenate(
        (masks[:, :, np.newaxis], masks[:, :, np.newaxis], masks[:, :,
                                                                 np.newaxis]),
        axis=2)
    B = np.concatenate((ys, X, masks), axis=2).astype(np.uint8)
    A = np.zeros((itterations, np.shape(B)[0], np.shape(B)[1], np.shape(B)[2]))

    for num in range(itterations):
        aug1 = iaa.Affine(scale={"x": (1, 1.5), "y": (1, 1.5)})
        aug2 = iaa.Fliplr(1)
        aug3 = iaa.Flipud(1)
        aug4 = iaa.ShearX((-20, 20), mode='reflect')
        aug5 = iaa.ShearY((-20, 20), mode='reflect')
        aug6 = iaa.Rotate((-45, 45), mode='reflect')
        aug7 = iaa.WithChannels((3, 4, 5), iaa.Multiply((0.5, 1.5)))
        aug_list = [aug1, aug2, aug3, aug4, aug5, aug6, aug7]
        ID = np.random.randint(0, len(aug_list))
        A[num, :, :, :] = aug_list[ID](image=B)
    aug_y = A[:, :, :, 0]
    aug_X = A[:, :, :, 3:6]
    aug_masks = A[:, :, :, 8]

    return aug_X, aug_y, aug_masks
Exemplo n.º 3
0
def initialise_augmenter():
    # Horizontal and Vertical Flips (set to 1 as the SomeOf function will choose when to apply these itself)
    horizontal_flip = iaa.Fliplr(1)  #0.5)
    vertical_flip = iaa.Flipud(1)  #0.5)

    # 90, 180 and 270 degree rotations
    rotate_90 = iaa.Affine(rotate=90)
    rotate_180 = iaa.Affine(rotate=180)
    rotate_270 = iaa.Affine(rotate=270)

    # Translations of -10% to 10% of the image's pixels
    translate_x = iaa.TranslateX(percent=(-0.1, 0.1))
    translate_y = iaa.TranslateY(percent=(-0.1, 0.1))

    # Scale the image between 0.75 and 1.1 of the original size
    scale_x = iaa.ScaleX((0.75, 1.1))
    scale_y = iaa.ScaleY((0.75, 1.1))

    # Shear the image between -20 and 20 degrees
    shear_x = iaa.ShearX((-20, 20))
    shear_y = iaa.ShearY((-20, 20))

    augmentation = iaa.SomeOf((0, None), [
        horizontal_flip, vertical_flip,
        iaa.OneOf([rotate_90, rotate_180, rotate_270]), translate_x,
        translate_y, scale_x, scale_y, shear_x, shear_y
    ],
                              random_order=True)

    return augmentation
Exemplo n.º 4
0
 def shear(image):
     # shear image randomly between -25 and 25 degree
     aug_x = iaa.ShearX((-25, 25))
     aug_y = iaa.ShearY((-25, 25))
     image_aug = aug_x(image=image)
     image_aug = aug_y(image=image_aug)
     return image_aug
Exemplo n.º 5
0
def shearY(shear_amount, input_path, output_path, image_count):
  images = []
  labels = []
  ia.seed(1)
  for img_path in range(image_count):
    img = imageio.imread(input_path + '/images/' + str(img_path) + '.png')
    images.append(img) 

    lbl = imageio.imread(input_path + '/labels/' + str(img_path) + '.png')
    labels.append(lbl)
  
  seq = iaa.Sequential(
      [

          iaa.ShearY((shear_amount)),

      ]
  )

  images_aug = seq(images=images)
  labels_aug = seq(images=labels)

  path = os.path.join(output_path, 'images') 
  os.mkdir(path) 

  path = os.path.join(output_path, 'labels') 
  os.mkdir(path)

  for indx, i in enumerate(images_aug):
      imageio.imwrite(output_path + '/images/'  + 'shear'+ '_' + str(indx) + '.png', i)

  for indx, i in enumerate(labels_aug):
      imageio.imwrite(output_path + '/labels/'  + 'shear'+ '_' + str(indx) + '.png', i)

  print("Shear results were saved given directory.")
Exemplo n.º 6
0
def augment_geometric(
    d_scale=0.05,
    d_translate=0.02,
    d_rotate=3,
    d_shear_x=5,
    d_shear_y=1,
):
    return iaa.Sequential(
        [
            iaa.Sometimes(
                0.5,
                iaa.Affine(
                    scale=(1 / (1 + d_scale), (1 + d_scale)),
                    translate_percent=dict(
                        x=(-d_translate, d_translate),
                        y=(-d_translate, d_translate),
                    ),
                    rotate=(-d_rotate, d_rotate),
                ),
            ),
            iaa.Sometimes(0.5, iaa.ShearX(shear=(-d_shear_x, d_shear_x))),
            iaa.Sometimes(0.5, iaa.ShearY(shear=(-d_shear_y, d_shear_y))),
        ],
        random_order=True,
    )
Exemplo n.º 7
0
def chapter_augmenters_sheary():
    fn_start = "geometric/sheary"

    image = ia.quokka(size=(128, 128))

    aug = iaa.ShearY((0.5, 1.5))
    run_and_save_augseq(
        fn_start + ".jpg", aug,
        [image for _ in range(4*2)], cols=4, rows=2)
Exemplo n.º 8
0
 def __init__(self,num_of_augms=0):
     self.num_of_augms=num_of_augms
     self.aug=iaa.OneOf([
         iaa.Sequential([
             iaa.LinearContrast(alpha=(0.75, 1.5)),
             iaa.Fliplr(0.5)
         ]),
         iaa.Sequential([
             iaa.Grayscale(alpha=(0.1, 0.9)),
             iaa.Affine(
                 translate_percent={"y": (-0.15, 0.15)}
             )
         ]),
         iaa.Sequential([
             iaa.LinearContrast((0.6, 1.4)),
             iaa.ShearX((-10, 10))
         ]),
         iaa.Sequential([
             iaa.GaussianBlur(sigma=(0, 1)),
             iaa.ShearY((-10, 10))
         ]),
         iaa.Sequential([
             iaa.Cutout(nb_iterations=(1, 2), size=0.1, squared=False),
             iaa.Multiply((0.8, 1.2), per_channel=0.25),
             iaa.Fliplr(0.5),
         ]),
         iaa.Sequential([
             iaa.LinearContrast((0.6, 1.4)),
             iaa.Affine(
                 translate_percent={"x": (-0.25, 0.25)}
             )
         ]),
         iaa.Sequential([
             iaa.Cutout(nb_iterations=(1, 5), size=0.1, squared=False),
             iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 15), per_channel=0.5),
             iaa.Affine(
                 scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
             )
         ]),
         iaa.Sequential([
             iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
             iaa.GaussianBlur(sigma=(0, 2)),
             iaa.Affine(
                 scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}
             )])
         # iaa.Sequential([
         #     iaa.Cutout(nb_iterations=(1, 5), size=0.05, squared=False),
         #     iaa.Grayscale(alpha=(0.0, 0.50)),
         #     iaa.ScaleX((0.75, 1.25))
         # ]),
         # iaa.Sequential([
         #     iaa.LinearContrast((0.8, 1.2), per_channel=True),
         #     iaa.PerspectiveTransform(scale=(0.01, 0.15))
         # ])
     ])
def dictShearY(baseImageListFunc, baseMaskListFunc, fullImageListFunc, segmapListFunc):
    print('ShearY, starting number of images:', len(segmapListFunc))
    shearY_x00percent = 2
    shearY = iaa.ShearY((-3, 3), mode="reflect")
    shearY._mode_segmentation_maps = "reflect"
    alteredImageListFunc, alteredMaskListFunc = expandList(baseImageListFunc, baseMaskListFunc, shearY_x00percent)
    (alteredImageListFunc, alteredMaskListFunc) = shearY(images=alteredImageListFunc,
                                                         segmentation_maps=alteredMaskListFunc)

    fullImageListFunc.extend(alteredImageListFunc)
    segmapListFunc.extend(alteredMaskListFunc)
    return fullImageListFunc, segmapListFunc
Exemplo n.º 10
0
def shear_y(magnitude: int) -> iaa.ShearY:
    """
    Apply y shear image and boxes

    Tensorflow Policy Equivalent: shear_y

    :type magnitude: int
    :param magnitude: magnitude of y shear
    :rtype: iaa.ShearY
    :return: Method to y shear bounding boxes
    """
    level = _shear_mag_to_arg(magnitude)
    return iaa.ShearY(level)
Exemplo n.º 11
0
def shear_y_bbox(magnitude: int) -> iaa.BlendAlphaBoundingBoxes:
    """
    Apply y shear only to bboxes

    Tensorflow Policy Equivalent: shear_y_only_bboxes

    :type magnitude: int
    :param magnitude: magnitude of y shear
    :rtype: iaa.BlendAlphaBoundingBoxes
    :return: Method to y shear bounding boxes
    """
    level = _shear_mag_to_arg(magnitude)
    return iaa.BlendAlphaBoundingBoxes(
        None,
        foreground=iaa.ShearY(level),
    )
Exemplo n.º 12
0
 def __init__(self,num_of_augms=0):
     self.num_of_augms=num_of_augms
     self.aug=iaa.OneOf([
         iaa.Sequential([
             iaa.LinearContrast(alpha=(0.75, 1.5)),
             iaa.Fliplr(0.5)
         ]),
         iaa.Sequential([
             iaa.Grayscale(alpha=(0.1, 0.9)),
             iaa.Affine(
             translate_percent={"y": (-0.15, 0.15)}
         )
         ]),
         iaa.Sequential([
             iaa.Solarize(0.5, threshold=(0, 256)),
             iaa.ShearX((-10, 10))
         ]),
         iaa.Sequential([
             iaa.GaussianBlur(sigma=(0, 1)),
             iaa.ShearY((-10, 10))
         ]),
         iaa.Sequential([
             iaa.Multiply((0.5, 1.5), per_channel=0.25),
             iaa.Fliplr(0.5),
         ]),
         iaa.Sequential([
             iaa.HistogramEqualization(),
             iaa.Affine(
             translate_percent={"x": (-0.25, 0.25)},
                 shear=(-8, 8)
         )
         ]),
         iaa.Sequential([
             iaa.Crop(percent=(0.01, 0.1)),
             iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
             iaa.Affine(
             scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
         )
         ]),
         iaa.Sequential([
             iaa.GaussianBlur(sigma=(0, 0.9)),
             iaa.Affine(
             scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}
         )
         ])
     ])
Exemplo n.º 13
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels,
                                          foreground=iaa.ShearY(self.v))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        masks = []
        if sample.masks_and_category is not None:
            for index in sample.masks_and_category:
                masks.append(index[0])

        mask_aug = []
        if masks is not None:
            for mask in masks:
                mask = SegmentationMapsOnImage(mask, shape=img.shape).arr
                segmap, _ = aug(image=mask, bounding_boxes=bbs)

                mask_aug.append(segmap)
            # back to 2D array
            mask_result = []
            for mask in mask_aug:
                mask_result.append(mask[:, :, 0])

            for i, index in enumerate(sample.masks_and_category):
                index[0] = mask_result[i]

        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        sample.image = img_aug
        sample.annotation = annot_aug
Exemplo n.º 14
0
    def np_func(img, ann):
        # convert to imgaug keypoints
        keypoints = annotation_to_keypoints(img, ann)
        img = img.astype(np.uint8)

        p = 0.1
        seq = iaa.Sequential([
            iaa.Sometimes(p, iaa.Sequential([iaa.ShearY((-20, 20))])),
            iaa.Sometimes(p, iaa.ChangeColorTemperature((3500, 8000))),
            iaa.Sometimes(p, iaa.AddToBrightness((-15, 15))),
            iaa.Sometimes(p, iaa.AdditiveGaussianNoise(scale=(0, 0.03 * 255), per_channel=True))
        ])

        img, keypoints = seq(image=img, keypoints=keypoints)

        # convert from imgaug keypoints
        ann = keypoints_to_annotation(img, ann, keypoints)

        return img.astype(np.float32), ann
Exemplo n.º 15
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.ShearY(self.v)
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        return {'img': img_aug, 'annot': annot_aug}
def get_aug(aug_cfg):
    seq = iaa.Sequential(
        [
            iaa.Sometimes(
                aug_cfg["Shear"]["p"],
                iaa.OneOf([
                    iaa.ShearX(aug_cfg["Shear"]["X"]),
                    iaa.ShearY(aug_cfg["Shear"]["Y"]),
                ]),
            ),
            iaa.Sometimes(
                aug_cfg["GaussianBlur"]["p"],
                iaa.GaussianBlur(sigma=aug_cfg["GaussianBlur"]["sigma"]),
            ),
            iaa.OneOf([
                iaa.LinearContrast(aug_cfg["LinearContrast"]["alpha"]),
                iaa.AdditiveGaussianNoise(
                    loc=aug_cfg["AdditiveGaussianNoise"]["loc"],
                    scale=aug_cfg["AdditiveGaussianNoise"]["scale"],
                    per_channel=aug_cfg["AdditiveGaussianNoise"]
                    ["per_channel"],
                ),
                iaa.Multiply(
                    aug_cfg["Multiply"]["mul"],
                    per_channel=aug_cfg["Multiply"]["per_channel"],
                ),
            ]),
            iaa.Sometimes(
                aug_cfg["Affine"]["p"],
                iaa.Affine(
                    translate_percent=iap.Normal(
                        *aug_cfg["Affine"]["translate_percent"]),
                    rotate=iap.Normal(*aug_cfg["Affine"]["rotate"]),
                    scale=None,
                ),
            ),
        ],
        random_order=True,
    )
    return seq
def gen_steps():

    STEPS=[
        iaa.Rotate((-50,-20)),
        iaa.Rotate((20,50)),
        iaa.Affine(shear=(-30,-10)),
        iaa.Affine(shear=(10,30)),
        iaa.ShearX(((-10, 10))),
        iaa.ShearY(((-10, 10))),
        iaa.Affine(scale=(0.5, 1.5)),
        iaa.Affine(scale=(1, 2)),
        iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}),
        [
            iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}),
            iaa.Affine(shear=(-15,15)),
        ],
        [
            iaa.Affine(translate_px={"x": (1, random.randint(300,800)), "y": (1, random.randint(300,800))}),
            iaa.Affine(shear=(-15,15)),
        ],
    ] 

    return STEPS
Exemplo n.º 18
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels,
                                          foreground=iaa.ShearY(self.v))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        return {'img': img_aug, 'annot': annot_aug}
Exemplo n.º 19
0
                # ),
                # iaa.Add((-15, 15), per_channel=0.5),
                # iaa.Multiply((0.8, 1.2), per_channel=0.5),
                # iaa.imgcorruptlike.Contrast(severity=1),
                # iaa.imgcorruptlike.Brightness(severity=2),
                iaa.ContrastNormalization((0.1, 1.5), per_channel=0.5),
                iaa.WithHueAndSaturation([
                    iaa.WithChannels(0, iaa.Add((-15, 15))),
                    iaa.WithChannels(1, iaa.Add((-20, 20))),
                ]),
                iaa.GammaContrast((0.3, 1.5)),
                iaa.WithBrightnessChannels(iaa.Add((-30, 70))),
                iaa.ScaleX((0.5, 1.5)),
                iaa.ScaleY((0.5, 1.5)),
                iaa.ShearX((-10, 10)),
                iaa.ShearY((-10, 10)),
            ],
            random_order=True)
    ],
    random_order=True)


def augment_pair(fg, label):
    # print('Augment start')
    label_i, segmaps_aug_i = seq(images=fg, segmentation_maps=label)
    # print('Augment ok')
    return label_i, segmaps_aug_i


#res = [1080, 960]
res = [int(1080 / 5), int(960 / 5)]
Exemplo n.º 20
0
        iaa.LinearContrast((0.80, 1.1)),
        iaa.Multiply((0.9, 1.5)),
        # iaa.pillike.Autocontrast((10, 20), per_channel=True),
        iaa.Sometimes(0.50,
            iaa.OneOf([
                iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03*255)),
                iaa.PiecewiseAffine(scale=(0.01, 0.05))
                ]),
        ),
    ]),

    iaa.Sometimes(0.8,
        iaa.OneOf([
            iaa.OneOf([
                iaa.ShearX((-5,5)),
                iaa.ShearY((-5,5))
            ]),
            iaa.OneOf([
                iaa.Affine(
                    scale={"x": (0.8, 1), "y": (0.8, 1)}
                ),
                iaa.Affine(
                    translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}
                ),
                iaa.Affine(
                    rotate=(-4, 4)
                )
            ]),
        ]),
    ),
], random_order=True) # apply augmenters in random order
Exemplo n.º 21
0
critical_1 = [
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.ScaleX(0.7)
]

critical_2 = [
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.Flipud(1)
]

critical_3 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.ShearY(10)
]

critical_4 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.Add(20)
]

critical_5 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.Multiply(1.3)
]

critical_6 =[
Exemplo n.º 22
0
def da_policy(image, label):

    img_size = 224

    #image = sample[0]
    policy = np.random.randint(4)

    #policy = 2
    if policy == 0:

        p = np.random.random()
        if p <= 0.6:
            aug = iaa.TranslateX(px=(-60, 60), cval=128)
            image = aug(image=image)

        p = np.random.random()
        if p <= 0.8:
            aug = iaa.HistogramEqualization()
            image = aug(image=image)

    elif policy == 1:

        p = np.random.random()
        if p <= 0.2:
            aug = iaa.TranslateY(px=(int(-0.18 * img_size),
                                     int(0.18 * img_size)),
                                 cval=128)
            image = aug(image=image)

        p = np.random.random()
        if p <= 0.8:
            square_size = np.random.randint(48)
            aug = iaa.Cutout(nb_iterations=1,
                             size=square_size / img_size,
                             squared=True)
            image = aug(image=image)

    elif policy == 2:
        p = np.random.random()
        if p <= 1:
            aug = iaa.ShearY(shear=(int(-0.06 * img_size),
                                    int(0.06 * img_size)),
                             order=1,
                             cval=128)
            image = aug(image=image)

        p = np.random.random()
        if p <= 0.6:
            aug = iaa.TranslateX(px=(-60, 60), cval=128)
            image = aug(image=image)

    elif policy == 3:
        p = np.random.random()
        if p <= 0.6:
            aug = iaa.Rotate(rotate=(-30, 30), order=1, cval=128)
            image = aug(image=image)

        p = np.random.random()
        if p <= 1:
            aug = iaa.MultiplySaturation((0.54, 1.54))
            image = aug(image=image)

    #Para EFFICIENTNET NO es necesario NORMALIZAR
    return (tf.cast(image, tf.float32), tf.cast(label, tf.int64))
Exemplo n.º 23
0
class Dataset(object):
    # Dataset preprocess implementation
    def __init__(self, dataset_type, TEST_INPUT_SIZE=TEST_INPUT_SIZE):
        self.annot_path  = TRAIN_ANNOT_PATH if dataset_type == 'train' else TEST_ANNOT_PATH
        self.input_sizes = TRAIN_INPUT_SIZE if dataset_type == 'train' else TEST_INPUT_SIZE
        self.batch_size  = TRAIN_BATCH_SIZE if dataset_type == 'train' else TEST_BATCH_SIZE
        self.data_aug    = TRAIN_DATA_AUG   if dataset_type == 'train' else TEST_DATA_AUG

        self.train_input_sizes = TRAIN_INPUT_SIZE
        self.strides = np.array(YOLO_STRIDES)
        self.classes = read_class_names(TRAIN_CLASSES)
        self.num_classes = len(self.classes)
        self.anchors = (np.array(YOLO_ANCHORS).T/self.strides).T
        self.anchor_per_scale = YOLO_ANCHOR_PER_SCALE
        self.max_bbox_per_scale = YOLO_MAX_BBOX_PER_SCALE

        self.annotations = self.load_annotations(dataset_type)
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0


    def load_annotations(self, dataset_type):
        final_annotations = []
        with open(self.annot_path, 'r') as f:
            txt = f.readlines()
            annotations = [line.strip() for line in txt if len(line.strip().split()[1:]) != 0]
        np.random.shuffle(annotations)
        
        for annotation in annotations:
            # fully parse annotations
            line = annotation.split()
            image_path, index = "", 1
            for i, one_line in enumerate(line):
                if not one_line.replace(",","").isnumeric():
                    if image_path != "": image_path += " "
                    image_path += one_line
                else:
                    index = i
                    break
            if not os.path.exists(image_path):
                raise KeyError("%s does not exist ... " %image_path)
            if TRAIN_LOAD_IMAGES_TO_RAM:
                image = cv2.imread(image_path)
            else:
                image = ''
            final_annotations.append([image_path, line[index:], image])
        return final_annotations

    def __iter__(self):
        return self

    def Delete_bad_annotation(self, bad_annotation):
        print(f'Deleting {bad_annotation} annotation line')
        bad_image_path = bad_annotation[0]
        bad_image_name = bad_annotation[0].split('/')[-1] # can be used to delete bad image
        bad_xml_path = bad_annotation[0][:-3]+'xml' # can be used to delete bad xml file

        # remove bad annotation line from annotation file
        with open(self.annot_path, "r+") as f:
            d = f.readlines()
            f.seek(0)
            for i in d:
                if bad_image_name not in i:
                    f.write(i)
            f.truncate()
    
    def __next__(self):
        with tf.device('/cpu:0'):
            self.train_input_size = random.choice([self.train_input_sizes])
            self.train_output_sizes = self.train_input_size // self.strides

            batch_image = np.zeros((self.batch_size, self.train_input_size, self.train_input_size, 3), dtype=np.float32)

            batch_label_sbbox = np.zeros((self.batch_size, self.train_output_sizes[0], self.train_output_sizes[0],
                                          self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)
            batch_label_mbbox = np.zeros((self.batch_size, self.train_output_sizes[1], self.train_output_sizes[1],
                                          self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)
            batch_label_lbbox = np.zeros((self.batch_size, self.train_output_sizes[2], self.train_output_sizes[2],
                                          self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)

            batch_sbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)
            batch_mbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)
            batch_lbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)

            exceptions = False
            num = 0
            #annotation more than 1
            #random <0.5

            #ambil gambar pertama
            #ambil gambar kedua
            #implement fungsi mix up
            # 
            def mixup(image1, bboxes1, image2, bboxes2, alpha=MIX_UP_THRESHOLD):
                '''
                    Mixup 2 image
                    
                    image_info_1, image_info_2: Info dict 2 image with keys = {"image", "label", "box", "difficult"}
                    lambd: Mixup ratio
                    
                    Out: mix_image (Temsor), mix_boxes, mix_labels, mix_difficulties
                '''
                l = np.random.beta(alpha,alpha)
                
                mixup_width = max(image1.shape[1], image2.shape[1])
                mix_up_height = max(image1.shape[0], image2.shape[0])
                
                mix_img = np.zeros((mix_up_height, mixup_width,3),dtype=float)
                mix_img[:, :image1.shape[0], :image1.shape[1]] = image1 * l
                mix_img[:, :image2.shape[0], :image2.shape[1]] += image2 * (1. - l)
            
                mix_boxes = np.concatenate((bboxes1,bboxes2), axis= 0)
                
                return mix_img, mix_boxes
                
            # curr_dict = {"image" : annotation[2], "box" : annotation[2]}
            if self.batch_count < self.num_batchs:
                while num < self.batch_size:
                    index = self.batch_count * self.batch_size + num
                    if index >= self.num_samples: index -= self.num_samples
                    annotation = self.annotations[index]
                    image, bboxes = self.parse_annotation(annotation)
                    if MIX_UP:
                        if num > 0:
                            #apply mixup function here with 50% chance
                            if random.random()<0.5:
                                #annotation = [image_path,bboxes,image]
                                select_index_prev = int(random.randint(0,index))
                                select_index_prev = select_index_prev if select_index_prev != index else index-1
                                prev_annotation = self.annotations[select_index_prev]
                                prev_image, prev_bboxes = self.parse_annotation(prev_annotation, only_parse=True)
                                image, bboxes = mixup(image, bboxes, prev_image, prev_bboxes)
                                # image = cv2.cvtColor(image.astype('float32'),cv2.COLOR_BGR2RGB)
                                
                    try:
                        label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.preprocess_true_boxes(bboxes)
                    except IndexError:
                        exceptions = True
                        self.Delete_bad_annotation(annotation)
                        print("IndexError, something wrong with", annotation[0], "removed this line from annotation file")

                    batch_image[num, :, :, :] = image
                    batch_label_sbbox[num, :, :, :, :] = label_sbbox
                    batch_label_mbbox[num, :, :, :, :] = label_mbbox
                    batch_label_lbbox[num, :, :, :, :] = label_lbbox
                    batch_sbboxes[num, :, :] = sbboxes
                    batch_mbboxes[num, :, :] = mbboxes
                    batch_lbboxes[num, :, :] = lbboxes
                    num += 1

                if exceptions: 
                    print('\n')
                    raise Exception("There were problems with dataset, I fixed them, now restart the training process.")
                self.batch_count += 1
                batch_smaller_target = batch_label_sbbox, batch_sbboxes
                batch_medium_target  = batch_label_mbbox, batch_mbboxes
                batch_larger_target  = batch_label_lbbox, batch_lbboxes

                return batch_image, (batch_smaller_target, batch_medium_target, batch_larger_target)
            else:
                self.batch_count = 0
                np.random.shuffle(self.annotations)
                raise StopIteration


    # https://www.ecva.net/papers/eccv_2020/papers_ECCV/papers/123720562.pdf

    ##Augmentor with imgaug
    aug = iaa.SomeOf(2, [
    #top 3 augment technique from paper
    iaa.pillike.Equalize(),
    iaa.Affine(translate_percent={"y":(-1, 1)}),
    iaa.Rotate(),

    # iaa.Affine(scale=(0.5, 1.5)),
    iaa.Sharpen(alpha=(0,1.0)),
    iaa.Posterize(),
    iaa.Solarize(0.5),
    iaa.pillike.Autocontrast(0.5),
    iaa.Affine(translate_percent={"x":(-1, 1),"y":(-1, 1)}),
    iaa.Affine(translate_percent={"x":(-1, 1)}),
    iaa.imgcorruptlike.Contrast(),
    iaa.imgcorruptlike.Brightness(),
    iaa.ShearY()

    ])

    def aug_with_imgaug(self,image, bboxes,aug = aug):
        if random.random() < 0.5:
            bbs = BoundingBoxesOnImage.from_xyxy_array(bboxes[:,:-1], shape= image.shape)
            image, bbs = aug(image=image, bounding_boxes=bbs)
            #disregard bounding boxes which have fallen out of image pane    
            bbs = bbs.remove_out_of_image()

            #clip bounding boxes which are partially outside of image pane
            bbs = bbs.clip_out_of_image()
            bboxes = np.column_stack((bbs.to_xyxy_array(),bboxes[:,-1][:bbs.to_xyxy_array().shape[0],np.newaxis])).astype(int)
            
        return image, bboxes
    
    def parse_annotation(self, annotation, mAP = 'False', only_parse= False):
        
        if TRAIN_LOAD_IMAGES_TO_RAM:
            image_path = annotation[0]
            image = annotation[2]
        else:
            image_path = annotation[0]
            image = cv2.imread(image_path)
            
        bboxes = np.array([list(map(int, box.split(','))) for box in annotation[1]])
        if not only_parse:
          if self.data_aug:
              image, bboxes = self.aug_with_imgaug(np.copy(image), np.copy(bboxes))
              # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if mAP == True: 
            return image, bboxes
        
        image, bboxes = image_preprocess(np.copy(image), [self.input_sizes, self.input_sizes], np.copy(bboxes))
        return image, bboxes

    def preprocess_true_boxes(self, bboxes):
        label = [np.zeros((self.train_output_sizes[i], self.train_output_sizes[i], self.anchor_per_scale,
                           5 + self.num_classes)) for i in range(3)]
        bboxes_xywh = [np.zeros((self.max_bbox_per_scale, 4)) for _ in range(3)]
        bbox_count = np.zeros((3,))

        for bbox in bboxes:
            bbox_coor = bbox[:4]
            bbox_class_ind = bbox[4]

            onehot = np.zeros(self.num_classes, dtype=np.float)
            onehot[bbox_class_ind] = 1.0
            uniform_distribution = np.full(self.num_classes, 1.0 / self.num_classes)
            deta = 0.01
            smooth_onehot = onehot * (1 - deta) + deta * uniform_distribution

            bbox_xywh = np.concatenate([(bbox_coor[2:] + bbox_coor[:2]) * 0.5, bbox_coor[2:] - bbox_coor[:2]], axis=-1)
            bbox_xywh_scaled = 1.0 * bbox_xywh[np.newaxis, :] / self.strides[:, np.newaxis]

            iou = []
            exist_positive = False
            for i in range(3):
                anchors_xywh = np.zeros((self.anchor_per_scale, 4))
                anchors_xywh[:, 0:2] = np.floor(bbox_xywh_scaled[i, 0:2]).astype(np.int32) + 0.5
                anchors_xywh[:, 2:4] = self.anchors[i]

                iou_scale = bbox_iou(bbox_xywh_scaled[i][np.newaxis, :], anchors_xywh)
                iou.append(iou_scale)
                iou_mask = iou_scale > 0.3

                if np.any(iou_mask):
                    xind, yind = np.floor(bbox_xywh_scaled[i, 0:2]).astype(np.int32)

                    label[i][yind, xind, iou_mask, :] = 0
                    label[i][yind, xind, iou_mask, 0:4] = bbox_xywh
                    label[i][yind, xind, iou_mask, 4:5] = 1.0
                    label[i][yind, xind, iou_mask, 5:] = smooth_onehot

                    bbox_ind = int(bbox_count[i] % self.max_bbox_per_scale)
                    bboxes_xywh[i][bbox_ind, :4] = bbox_xywh
                    bbox_count[i] += 1

                    exist_positive = True

            if not exist_positive:
                best_anchor_ind = np.argmax(np.array(iou).reshape(-1), axis=-1)
                best_detect = int(best_anchor_ind / self.anchor_per_scale)
                best_anchor = int(best_anchor_ind % self.anchor_per_scale)
                xind, yind = np.floor(bbox_xywh_scaled[best_detect, 0:2]).astype(np.int32)

                label[best_detect][yind, xind, best_anchor, :] = 0
                label[best_detect][yind, xind, best_anchor, 0:4] = bbox_xywh
                label[best_detect][yind, xind, best_anchor, 4:5] = 1.0
                label[best_detect][yind, xind, best_anchor, 5:] = smooth_onehot

                bbox_ind = int(bbox_count[best_detect] % self.max_bbox_per_scale)
                bboxes_xywh[best_detect][bbox_ind, :4] = bbox_xywh
                bbox_count[best_detect] += 1
        label_sbbox, label_mbbox, label_lbbox = label
        sbboxes, mbboxes, lbboxes = bboxes_xywh
        return label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes

    def __len__(self):
        return self.num_batchs
Exemplo n.º 24
0
            iaa.GaussianBlur(1.5),
            iaa.MotionBlur(k=15, angle=60, direction=1),
            iaa.MotionBlur(k=5, angle=60, direction=-1),
            iaa.Grayscale(0.5),
            iaa.SigmoidContrast(gain=10, cutoff=0.3),
            iaa.LogContrast(0.7),
            iaa.LogContrast(1.3),
            iaa.Sharpen(alpha=0.2, lightness=0.9),
            iaa.Sharpen(alpha=0.2, lightness=1.2),
            iaa.Fliplr(1),
            iaa.Flipud(1),
            iaa.Rotate(15),
            iaa.Rotate(-15),
            iaa.ShearX(-10),
            iaa.ShearX(10),
            iaa.ShearY(-10),
            iaa.ShearY(10),
            iaa.ScaleX(0.7),
            iaa.ScaleX(1.3),
            iaa.ScaleY(0.7),
            iaa.ScaleY(1.3),
            ]

def custom_imshow(imgList, labels):
    
    fig = plt.figure()

    rows = 2
    cols = 5
    
    for i in range(10):
        #These three are from contrast module, the above five from  blur module.
        sometimes2(iaa.HistogramEqualization()),
        sometimes2(iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6))),
        sometimes2(iaa.LinearContrast(alpha=(0.6, 1.4)))
    ])

sometimes3 = lambda aug: iaa.Sometimes(0.93, aug)

seq3 = iaa.SomeOf(
    (1, 2),
    [

        #sometimes3(iaa.Affine(scale=1.5)), # cuts out part of the objects, resulting in negative coordinates..
        sometimes3(iaa.ElasticTransformation(alpha=(20, 30.0), sigma=5.0)),
        sometimes3(iaa.Rot90((1, 3), keep_size=False)
                   ),  # On 90,270 transformation, the width and height change.
        sometimes3(iaa.Rotate((-30, 30))),
        sometimes3(iaa.WithPolarWarping(iaa.AveragePooling((2, 7)))),
        sometimes3(iaa.ShearY((30, 50))),
        sometimes3(iaa.ShearX((30, 70))),
    ])

img_path = "C:\\Users\\gishy\\Dropbox\\My PC (LAPTOP-SQRN8N46)\\Desktop\\sample_images"
txt_path = "C:\\Users\\gishy\\Dropbox\\My PC (LAPTOP-SQRN8N46)\\Desktop\\sample_annotations"
#img_path="C:\\Users\\gishy\\Dropbox\\My PC (LAPTOP-SQRN8N46)\\Desktop\\final-dataset\\main\\Not Augmented\\train_images"
#txt_path="C:\\Users\\gishy\\Dropbox\\My PC (LAPTOP-SQRN8N46)\\Desktop\\final-dataset\\main\\Not Augmented\\train_annotations_yolo"

if __name__ == "__main__":
    conversion(txt_path, img_path, seq3)
def GenerateRandomImgaugAugmentation(
        pNbAugmentations=5,  # number of augmentations
        pEnableResizing=True,  # enable scaling
        pScaleFactor=0.5,  # maximum scale factor
        pEnableCropping=True,  # enable cropping
        pCropFactor=0.25,  # maximum crop out size (minimum new size is 1.0-pCropFactor)
        pEnableFlipping1=True,  # enable x flipping
        pEnableFlipping2=True,  # enable y flipping
        pEnableRotation90=True,  # enable rotation
        pEnableRotation=True,  # enable rotation
        pMaxRotationDegree=15,  # maximum shear degree
        pEnableShearX=True,  # enable x shear
        pEnableShearY=True,  # enable y shear
        pMaxShearDegree=15,  # maximum shear degree
        pEnableDropOut=True,  # enable pixel dropout
        pMaxDropoutPercentage=.1,  # maximum dropout percentage
        pEnableBlur=True,  # enable gaussian blur
        pBlurSigma=.25,  # maximum sigma for gaussian blur
        pEnableSharpness=True,  # enable sharpness
        pSharpnessFactor=.1,  # maximum additional sharpness
        pEnableEmboss=True,  # enable emboss
        pEmbossFactor=.1,  # maximum emboss
        pEnableBrightness=True,  # enable brightness
        pBrightnessFactor=.1,  # maximum +- brightness
        pEnableRandomNoise=True,  # enable random noise
        pMaxRandomNoise=.1,  # maximum random noise strength
        pEnableInvert=False,  # enables color invert
        pEnableContrast=True,  # enable contrast change
        pContrastFactor=.1,  # maximum +- contrast
):

    augmentationMap = []
    augmentationMapOutput = []

    if pEnableResizing:
        if random.Random().randint(0, 1) == 1:
            randomResizeX = 1 - random.Random().random() * pScaleFactor
        else:
            randomResizeX = 1 + random.Random().random() * pScaleFactor
        if random.Random().randint(0, 1) == 1:
            randomResizeY = 1 - random.Random().random() * pScaleFactor
        else:
            randomResizeY = 1 + random.Random().random() * pScaleFactor
        aug = iaa.Resize({"height": randomResizeY, "width": randomResizeX})
        augmentationMap.append(aug)

    if pEnableCropping:
        randomCrop2 = random.Random().random() * pCropFactor
        randomCrop4 = random.Random().random() * pCropFactor
        randomCrop1 = random.Random().random() * pCropFactor
        randomCrop3 = random.Random().random() * pCropFactor
        aug = iaa.Crop(percent=(randomCrop1, randomCrop2, randomCrop3,
                                randomCrop4))
        augmentationMap.append(aug)

    if pEnableFlipping1:
        aug = iaa.Fliplr()
        augmentationMap.append(aug)

    if pEnableFlipping2:
        aug = iaa.Flipud()
        augmentationMap.append(aug)

    if pEnableRotation90:
        randomNumber = random.Random().randint(1, 3)
        aug = iaa.Rot90(randomNumber)
        augmentationMap.append(aug)

    if pEnableRotation:
        if random.Random().randint(0, 1) == 1:
            randomRotation = random.Random().random() * pMaxRotationDegree
        else:
            randomRotation = -random.Random().random() * pMaxRotationDegree
        aug = iaa.Rotate(randomRotation)
        augmentationMap.append(aug)

    if pEnableShearX:
        if random.Random().randint(0, 1) == 1:
            randomShearingX = random.Random().random() * pMaxShearDegree
        else:
            randomShearingX = -random.Random().random() * pMaxShearDegree
        aug = iaa.ShearX(randomShearingX)
        augmentationMap.append(aug)

    if pEnableShearY:
        if random.Random().randint(0, 1) == 1:
            randomShearingY = random.Random().random() * pMaxShearDegree
        else:
            randomShearingY = -random.Random().random() * pMaxShearDegree
        aug = iaa.ShearY(randomShearingY)
        augmentationMap.append(aug)

    if pEnableDropOut:
        randomDropOut = random.Random().random() * pMaxDropoutPercentage
        aug = iaa.Dropout(p=randomDropOut, per_channel=False)
        augmentationMap.append(aug)

    if pEnableBlur:
        randomBlur = random.Random().random() * pBlurSigma
        aug = iaa.GaussianBlur(randomBlur)
        augmentationMap.append(aug)

    if pEnableSharpness:
        randomSharpness = random.Random().random() * pSharpnessFactor
        aug = iaa.Sharpen(randomSharpness)
        augmentationMap.append(aug)

    if pEnableEmboss:
        randomEmboss = random.Random().random() * pEmbossFactor
        aug = iaa.Emboss(randomEmboss)
        augmentationMap.append(aug)

    if pEnableBrightness:
        if random.Random().randint(0, 1) == 1:
            randomBrightness = 1 - random.Random().random() * pBrightnessFactor
        else:
            randomBrightness = 1 + random.Random().random() * pBrightnessFactor
        aug = iaa.Add(randomBrightness)
        augmentationMap.append(aug)

    if pEnableRandomNoise:
        if random.Random().randint(0, 1) == 1:
            randomNoise = 1 - random.Random().random() * pMaxRandomNoise
        else:
            randomNoise = 1 + random.Random().random() * pMaxRandomNoise
        aug = iaa.MultiplyElementwise(randomNoise, per_channel=True)
        augmentationMap.append(aug)

    if pEnableInvert:
        aug = iaa.Invert(1)
        augmentationMap.append(aug)

    if pEnableContrast:
        if random.Random().randint(0, 1) == 1:
            randomContrast = 1 - random.Random().random() * pContrastFactor
        else:
            randomContrast = 1 + random.Random().random() * pContrastFactor
        aug = iaa.contrast.LinearContrast(randomContrast)
        augmentationMap.append(aug)

    widthFactor = 1
    heightFactor = 1

    arr = numpy.arange(0, len(augmentationMap))
    numpy.random.shuffle(arr)

    switchWidthHeight = False
    for i in range(pNbAugmentations):
        augmentationMapOutput.append(augmentationMap[arr[i]])
        if arr[i] == 0:
            widthFactor *= randomResizeX
            heightFactor *= randomResizeY
        if arr[i] == 1:
            widthFactor *= (1.0 - (randomCrop2 + randomCrop4))
            heightFactor *= (1.0 - (randomCrop1 + randomCrop3))
        if arr[i] == 4:
            if randomNumber == 1 or randomNumber == 3:
                switchWidhtHeight = True

    return iaa.Sequential(
        augmentationMapOutput), widthFactor, heightFactor, switchWidthHeight
Exemplo n.º 27
0
seq_blur = iaa.Sequential([
    iaa.Crop(px=(1, 16), keep_size=False),
    iaa.Fliplr(0.5),
    iaa.GaussianBlur(sigma=(0, 3.0))
])

seq_rot = iaa.Sequential([
    iaa.PerspectiveTransform(scale=(0.01, 0.15)),
    iaa.Rot90((1, 3))
])

seq_shear = iaa.Sequential([
    iaa.Crop(px=(1, 16), keep_size=False),
    iaa.ShearX((-20, 20)),
    iaa.ShearY((-20,20))
])

seq_scale = iaa.Sequential([
    iaa.Affine(scale={"x": (0.5, 1.5), "y": (0.5, 1.5)})
])

seq_offset = iaa.Sequential([
    iaa.Affine(translate_px={"x": (-100, 100), "y": (-100, 100)}),
    iaa.Crop(px=(1, 16), keep_size=False)
])



def create_aug_img(folder_path):
    contents = folder_path.split('/')
Exemplo n.º 28
0
            )
            img_batch = []

    os.chdir("..")


aug = iaa.SomeOf(3, [
    iaa.OneOf([
        iaa.Affine(scale=(0.5, 1.5)),
        iaa.ScaleX((0.5, 1.5)),
        iaa.ScaleY((0.5, 1.5)),
    ]),
    iaa.OneOf([
        iaa.Rotate((-180, 180)),
        iaa.ShearX((-60, 60)),
        iaa.ShearY((-60, 60)),
    ]),
    iaa.OneOf([
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
    ]),
    iaa.OneOf([
        iaa.AverageBlur(k=(2, 6)),
        iaa.GaussianBlur(sigma=(0.0, 2.0)),
        iaa.Sharpen(alpha=(0.0, 0.8), lightness=(0.8, 1.2)),
    ]),
    iaa.OneOf([
        iaa.imgcorruptlike.GaussianNoise(severity=random.randint(1, 3)),
        iaa.imgcorruptlike.ImpulseNoise(severity=random.randint(1, 3)),
        iaa.imgcorruptlike.ElasticTransform(severity=random.randint(1, 3)),
    ]),