예제 #1
0
def sroie_refine():
    aug_list = []
    stage_0, stage_1, stage_2, stage_3 = 1536, 2048, 768, 512
    # Pad the height to stage_0
    aug_list.append(
        augmenters.PadToFixedSize(width=1, height=stage_0, pad_cval=255))
    # Resize its height to stage_1, note that stage_0 is smaller than stage_1
    # so that the font size could be increased for most cases.
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_1,
            "width": "keep-aspect-ratio"
        }))
    # Crop a stage_2 x stage_2 area
    aug_list.append(augmenters.CropToFixedSize(width=stage_2, height=stage_2))
    # In case the width is not enough, pad it to stage_2 x stage_2
    aug_list.append(
        augmenters.PadToFixedSize(width=stage_2, height=stage_2, pad_cval=255))
    # Resize to stage_3 x stage_3
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_3,
            "width": stage_3
        }))
    # Perform Flip
    aug_list.append(augmenters.Fliplr(0.33, name="horizontal_flip"))
    aug_list.append(augmenters.Flipud(0.33, name="vertical_flip"))
    return aug_list
    def __init__(self):
        self.all_hand = []
        for suffix in ['.jpg', '.png', '.JPG', '.PNG']:
            for f in ['hand', 'egohands_extracted', 'GTEA_extracted']:
                self.all_hand += glob.glob(
                    os.path.join(cfg.path.data_dir, f, '*' + suffix))

        self.all_shape = []
        for suffix in ['.jpg', '.png', '.JPG', '.PNG']:
            for f in ['ShapePick']:
                self.all_shape += glob.glob(
                    os.path.join(cfg.path.data_dir, f, '*' + suffix))

        self.hand_augmentor = iaa.Sequential([
            iaa.PadToFixedSize(cfg.imsize, cfg.imsize),
            iaa.Fliplr(0.5),
            iaa.Affine(rotate=(-180, 180),
                       scale=(0.4, 0.6),
                       translate_percent={
                           'x': (-0.5, 0.5),
                           'y': (-0.5, 0.5)
                       })
        ])

        self.shape_augmentor = iaa.Sequential([
            iaa.PadToFixedSize(cfg.imsize, cfg.imsize),
            iaa.Fliplr(0.5),
            iaa.Affine(rotate=(-180, 180),
                       scale=(0.6, 0.8),
                       translate_percent={
                           'x': (-0.5, 0.5),
                           'y': (-0.5, 0.5)
                       })
        ])
예제 #3
0
def aug_sroie(args, bg_color=255):
    aug_list = []
    stage_0, stage_1, stage_2, stage_3 = 1536, 2048, 768, 512
    # Pad the height to stage_0
    aug_list.append(
        augmenters.PadToFixedSize(width=1, height=stage_0, pad_cval=bg_color))
    # Resize its height to stage_1, note that stage_0 is smaller than stage_1
    # so that the font size could be increased for most cases.
    aug_list.append(
        augmenters.Resize(size={
            "height": stage_1,
            "width": "keep-aspect-ratio"
        }))
    # increase the aspect ratio
    aug_list.append(
        augmenters.Sometimes(
            args.augment_zoom_probability,
            augmenters.Affine(scale=(args.augment_zoom_lower_bound,
                                     args.augment_zoom_higher_bound))))
    # Crop a stage_2 x stage_2 area
    aug_list.append(augmenters.CropToFixedSize(width=stage_2, height=stage_2))
    # In case the width is not enough, pad it to stage_2 x stage_2
    aug_list.append(
        augmenters.PadToFixedSize(width=stage_2,
                                  height=stage_2,
                                  pad_cval=bg_color))
    # Resize to stage_3 x stage_3
    #aug_list.append(augmenters.Resize(size={"height": stage_3, "width": stage_3}))
    # Perform Flip
    aug_list.append(augmenters.Fliplr(0.33, name="horizontal_flip"))
    aug_list.append(augmenters.Flipud(0.33, name="vertical_flip"))
    return aug_list
예제 #4
0
    def __call__(self, img, anno):
        labels = anno[0].numpy()
        gts = anno[1].numpy()
        np_img = np.asarray(img)

        # get BoundingBoxesOnImage
        bbs = []
        for gt in gts:
            bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3]))

        bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape)
        # draw_img = bbs_on_img.draw_on_image(np_img, size=2)

        height = np_img.shape[0]
        width = np_img.shape[1]
        if height >= width:
            length = height
        else:
            length = width
        # position is must, because the bbs and images are augmented separately
        self.seq = iaa.Sequential([
            iaa.PadToFixedSize(width=length, height=length, position=self.position)
        ])

        # apply augment
        image_aug = self.seq.augment_image(np_img)
        bbs_aug = self.seq.augment_bounding_boxes(bbs_on_img).bounding_boxes

        gts = []
        for bb in bbs_aug:
            gts.append([bb.x1, bb.y1, bb.x2, bb.y2])

        gts = torch.from_numpy(np.array(gts))
        return image_aug, (anno[0], gts)
def overlay(args):
    '''
    Build a dataset augmenting logos images found under a base_dir
    to be overlayed on a random bg image
    '''
    logos = list(
        get_labeled_images(
            pathlib.Path(args.base_dir).resolve(), args.label_filter))
    bg_paths = get_file_paths(args.background_dir)

    out_size = args.out_size

    # convert the image to a set size
    base = [
        iaa.Resize({
            "shorter-side": 256,
            "longer-side": "keep-aspect-ratio"
        })
    ]

    augmenters = build_augmenters(args, augmenters=base)
    augmenters.append(iaa.PadToFixedSize(width=out_size[0],
                                         height=out_size[1]))

    overlay_aug = iaa.Sequential(augmenters)

    data = overlay_images_backgrounds(overlay_aug, logos, bg_paths, out_size,
                                      args.count)

    process_command(args, logos, data)
 def augment_3(self, image, depth, lidar, crop_size, degree):
     # rsz = iaa.Resize({"height": resize_size[0], "width": resize_size[1]})
     seq = iaa.Sequential(
         [
             iaa.PadToFixedSize(height=crop_size[0],
                                width=crop_size[1]),  # 保证可crop
             iaa.CropToFixedSize(height=crop_size[0],
                                 width=crop_size[1]),  # random crop
             iaa.Fliplr(0.5),
             # iaa.Flipud(0.5),
             iaa.Rotate((-degree, degree)),
             iaa.GammaContrast((0.9, 1.1)),
             iaa.Multiply((0.9, 1.1)),
         ],
         random_order=True)
     depth, lidar = np.expand_dims(depth, 2), np.expand_dims(lidar, 2)
     tmp = np.concatenate((depth, lidar), axis=2)
     tmp = (tmp * 1000).astype(np.int32)  # 米单位*1000保留精度
     tmp = SegmentationMapsOnImage(tmp, shape=tmp.shape)
     # image, tmp = rsz(image=image, segmentation_maps=tmp)
     image, tmp = seq(image=image, segmentation_maps=tmp)
     tmp = tmp.arr
     tmp = tmp.astype(np.float32) / 1000  # 再转回米
     depth, lidar = tmp[:, :, 0], tmp[:, :, 1]
     return image, depth, lidar
예제 #7
0
def chapter_augmenters_padtofixedsize():
    fn_start = "size/padtofixedsize"
    aug_cls = iaa.PadToFixedSize

    aug = aug_cls(width=100, height=100)
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = aug_cls(width=100, height=100, position="center")
    run_and_save_augseq(fn_start + "_center.jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 1)],
                        cols=4,
                        rows=1)

    aug = aug_cls(width=100, height=100, pad_mode=ia.ALL)
    run_and_save_augseq(fn_start + "_pad_mode.jpg",
                        aug, [ia.quokka(size=(80, 80)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.Sequential([
        iaa.PadToFixedSize(width=100, height=100),
        iaa.CropToFixedSize(width=100, height=100)
    ])
    run_and_save_augseq(fn_start + "_with_croptofixedsize.jpg",
                        aug, [ia.quokka(size=(80, 120)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)
예제 #8
0
    def __getitem__(self, idx):
        img, seg = self.get_data(idx)
        img = img[..., ::-1]
        h, w, c = img.shape

        if self.rect:
            scale = min(self.img_size[0] / w, self.img_size[1] / h)
            resize = iaa.Sequential([
                iaa.Resize({
                    'width': int(w * scale),
                    'height': int(h * scale)
                }),
                iaa.PadToFixedSize(*self.img_size,
                                  pad_cval=[123.675, 116.28, 103.53],
                                  position='center')
            ])
        else:
            resize = iaa.Resize({
                'width': self.img_size[0],
                'height': self.img_size[1]
            })

        img = resize.augment_image(img)
        seg = resize.augment_segmentation_maps(seg)
        # augment
        if self.augments is not None:
            augments = self.augments.to_deterministic()
            img = augments.augment_image(img)
            seg = augments.augment_segmentation_maps(seg)

        img = img.transpose(2, 0, 1)
        img = np.ascontiguousarray(img)
        seg = seg.get_arr()
        return torch.ByteTensor(img), torch.ByteTensor(seg)
예제 #9
0
def test_sequential(
    sample_image, sample_keypoints, width, height,
):
    # Guarantee that images smaller than crop size are handled fine
    very_small_image = sample_image[:50, :50]
    aug = iaa.Sequential(
        [
            iaa.PadToFixedSize(width, height),
            augmentation.KeypointAwareCropToFixedSize(width, height),
        ]
    )
    images_aug, keypoints_aug = aug(
        images=[very_small_image], keypoints=[sample_keypoints],
    )
    assert len(images_aug) == len(keypoints_aug) == 1
    assert all(im.shape[:2] == (height, width) for im in images_aug)
    # Ensure at least a keypoint is visible in each crop
    assert all(len(kpts) for kpts in keypoints_aug)

    # Test passing in a batch of frames
    n_samples = 8
    images_aug, keypoints_aug = aug(
        images=[very_small_image] * n_samples, keypoints=[sample_keypoints] * n_samples,
    )
    assert len(images_aug) == len(keypoints_aug) == n_samples
예제 #10
0
    def get_item(self, idx):
        img = cv2.imread(self.data[idx][0])
        img = img[:, :, ::-1]
        h, w, c = img.shape

        if self.rect:
            scale = min(self.img_size[0] / w, self.img_size[1] / h)
            resize = iaa.Sequential([
                iaa.Resize({
                    'width': int(w * scale),
                    'height': int(h * scale)
                }),
                iaa.PadToFixedSize(*self.img_size, position='center')
            ])
        else:
            resize = iaa.Resize({
                'width': self.img_size[0],
                'height': self.img_size[1]
            })
        img = resize.augment_image(img)
        # augment
        if self.augments is not None:
            augments = self.augments.to_deterministic()
            img = augments.augment_image(img)

        img = img.transpose(2, 0, 1)
        img = np.ascontiguousarray(img)

        return torch.ByteTensor(img), self.data[idx][1]
예제 #11
0
def inference(model, img, img_size=(64, 64), rect=False):
    img = img[:, :, ::-1]
    h, w, c = img.shape

    if rect:
        scale = min(img_size[0] / w, img_size[1] / h)
        resize = ia.Sequential([
            ia.Resize({
                'width': int(w * scale),
                'height': int(h * scale)
            }),
            ia.PadToFixedSize(*img_size,
                              position='center')
        ])
    else:
        resize = ia.Resize({'width': img_size[0], 'height': img_size[1]})
    img = resize.augment_image(img)
    img = img.transpose(2, 0, 1)
    img = np.ascontiguousarray(img)

    imgs = torch.FloatTensor([img]).to(device)
    imgs -= torch.FloatTensor([123.675, 116.28,
                               103.53]).reshape(1, 3, 1, 1).to(imgs.device)
    imgs /= torch.FloatTensor([58.395, 57.12,
                               57.375]).reshape(1, 3, 1, 1).to(imgs.device)
    preds = model(imgs)[0].softmax(0).cpu().numpy()
    return preds
예제 #12
0
def YOLO():
    """
    Data augmentation model for YOLOv3 training
    """
    return iaa.Sequential([
        iaa.KeepSizeByResize(
            iaa.Affine(
                scale=iap.Normal(1, 0.125),
                translate_percent=0.1,
                cval=128,
            )),
        iaa.Fliplr(0.5),
        iaa.Resize({
            "height": iap.Normal(1, 0.1),
            "width": iap.Normal(1, 0.1)
        }),
        iaa.Resize({
            "longer-side": 416,
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(416, 416, pad_cval=128),
        iaa.MultiplyHueAndSaturation(mul_hue=iap.Uniform(0, 2),
                                     mul_saturation=iap.Uniform(1 / 1.5, 1.5)),
        iaa.AssertShape((None, 416, 416, 3)),
    ])
예제 #13
0
    def __init__(self, size):

        self.seq = iaa.Sequential(
            [
                iaa.Resize((0.5, 2.0)),  # random scale
                iaa.PadToFixedSize(width=size[0], height=size[1]),
                iaa.CropToFixedSize(width=size[0], height=size[1]),
            ],
            random_order=False)
예제 #14
0
    def __getitem__(self, idx):
        img, kps = self.get_data(idx)
        img = img[..., ::-1]
        h, w, c = img.shape

        if self.rect:
            scale = min(self.img_size[0] / w, self.img_size[1] / h)
            resize = ia.Sequential([
                ia.Resize({
                    'width': int(w * scale),
                    'height': int(h * scale)
                }),
                ia.PadToFixedSize(*self.img_size,
                                  pad_cval=[123.675, 116.28, 103.53],
                                  position='center')
            ])
        else:
            resize = ia.Resize({
                'width': self.img_size[0],
                'height': self.img_size[1]
            })

        img = resize.augment_image(img)
        kps = resize.augment_polygons(kps)
        # augment
        if self.augments is not None:
            augments = self.augments.to_deterministic()
            img = augments.augment_image(img)
            kps = augments.augment_polygons(kps)
        heats = [np.zeros(img.shape[:2])] * len(self.classes)
        for kp in kps.polygons:
            c = kp.label
            point = kp.exterior.astype(np.int32)
            x = np.arange(img.shape[1], dtype=np.float)
            y = np.arange(img.shape[0], dtype=np.float)
            xx, yy = np.meshgrid(x, y)

            # evaluate kernels at grid points
            xxyy = np.c_[xx.ravel(), yy.ravel()]
            sigma = 10  # 65.9  # math.sqrt(- math.pow(100, 2) / math.log(0.1))
            xxyy -= point
            x_term = xxyy[:, 0]**2
            y_term = xxyy[:, 1]**2
            exp_value = -(x_term + y_term) / 2 / pow(sigma, 2)
            zz = np.exp(exp_value)
            heat = zz.reshape(img.shape[:2])
            heats[c] = heat
            # cv2.imshow('c', (heat * 255).astype(np.uint8))
            # cv2.waitKey(0)

        heats = np.stack(heats, 0)

        img = img.transpose(2, 0, 1)
        img = np.ascontiguousarray(img)

        return torch.ByteTensor(img), torch.FloatTensor(heats)
예제 #15
0
 def resize_with_pad(images, image_size=224):
     return iaa.Sequential([
         iaa.Resize({
             "longer-side": image_size,
             "shorter-side": "keep-aspect-ratio"
         }),
         iaa.PadToFixedSize(width=image_size,
                            height=image_size,
                            pad_mode=["constant", "edge", "median"])
     ])(images=images)
예제 #16
0
def grid(images, rows, cols, border=1, border_color=255):
    nb_images = len(images)
    cell_height = max([image.shape[0] for image in images])
    cell_width = max([image.shape[1] for image in images])
    channels = set([image.shape[2] for image in images])
    assert len(channels) == 1
    nb_channels = list(channels)[0]
    if rows is None and cols is None:
        rows = cols = int(math.ceil(math.sqrt(nb_images)))
    elif rows is not None:
        cols = int(math.ceil(nb_images / rows))
    elif cols is not None:
        rows = int(math.ceil(nb_images / cols))
    assert rows * cols >= nb_images

    cell_height = cell_height + 2 * border
    cell_width = cell_width + 2 * border

    width = cell_width * cols
    height = cell_height * rows
    grid = np.zeros((height, width, nb_channels), dtype=np.uint8)
    cell_idx = 0
    for row_idx in range(rows):
        for col_idx in range(cols):
            if cell_idx < nb_images:
                image = images[cell_idx]
                border_top = border_right = border_bottom = border_left = border
                #if row_idx > 1:
                #border_top = 0
                #if col_idx > 1:
                #border_left = 0
                #image = np.pad(image, ((border_top, border_bottom), (border_left, border_right), (0, 0)), mode="constant", constant_values=border_color)
                image = iaa.pad(image,
                                top=border_top,
                                right=border_right,
                                bottom=border_bottom,
                                left=border_left,
                                mode="constant",
                                cval=border_color)

                image = iaa.PadToFixedSize(height=cell_height,
                                           width=cell_width,
                                           position="center")(image=image)

                cell_y1 = cell_height * row_idx
                cell_y2 = cell_y1 + image.shape[0]
                cell_x1 = cell_width * col_idx
                cell_x2 = cell_x1 + image.shape[1]
                grid[cell_y1:cell_y2, cell_x1:cell_x2, :] = image
            cell_idx += 1

    #grid = np.pad(grid, ((border, 0), (border, 0), (0, 0)), mode="constant", constant_values=border_color)

    return grid
    def __init__(self, src_dir, is_train=True, target_dim=(256, 256)):
        self.target_dim = target_dim
        self.object_info = dict()
        self.class_weights = []
        self.image_filenames = []
        self.annotation_filenames = []
        self.is_train = is_train
        if is_train:
            images_dir = src_dir + "images/training/"
            annotations_dir = src_dir + "annotations/training/"
            if target_dim is None:  # used in case of class weight computation
                self.transform = transforms.Compose([ToTensor()])
            else:
                self.size_aug = iaa.Sequential([
                    iaa.PadToFixedSize(target_dim[0], target_dim[1]),
                    iaa.CropToFixedSize(target_dim[0], target_dim[1])
                ])
                self.property_aug = iaa.Sequential([
                    iaa.Multiply((0.8, 1.2), per_channel=0.2),
                    iaa.AdditiveGaussianNoise(loc=0,
                                              scale=(0.0, 0.01 * 255),
                                              per_channel=0.3),
                    iaa.AddToHueAndSaturation((-20, 20))
                ])
        else:
            images_dir = src_dir + "images/validation/"
            annotations_dir = src_dir + "annotations/validation/"
            self.size_aug = iaa.Sequential([
                iaa.PadToFixedSize(target_dim[0], target_dim[1]),
                iaa.CropToFixedSize(target_dim[0], target_dim[1])
            ])
        files = os.listdir(images_dir)
        for file in files:
            filename = file.split(".")[0]
            self.image_filenames.append(images_dir + filename + ".jpg")
            self.annotation_filenames.append(annotations_dir + filename +
                                             ".png")

        print(len(self.image_filenames))
예제 #18
0
    def __init__(self):
        self.all_celeba = []
        for suffix in ['.jpg', '.png', '.JPG', '.PNG']:
            self.all_celeba += glob.glob(os.path.join(CELEBA_DIR,
                                                      '*' + suffix))

        self.all_hand = []
        for suffix in ['.jpg', '.png', '.JPG', '.PNG']:
            for f in ['hand', 'egohands_extracted', 'GTEA_extracted']:
                self.all_hand += glob.glob(
                    os.path.join(OCCLUSION_DIR, f, '*' + suffix))

        self.all_shape = []
        for suffix in ['.jpg', '.png', '.JPG', '.PNG']:
            for f in ['ShapePick']:
                self.all_shape += glob.glob(
                    os.path.join(OCCLUSION_DIR, f, '*' + suffix))

        self.hand_augmentor = iaa.Sequential([
            iaa.PadToFixedSize(OCCLUSION_WIDTH, OCCLUSION_HEIGHT),
            iaa.Fliplr(0.5),
            iaa.Affine(rotate=(-180, 180),
                       scale=(0.4, 0.6),
                       translate_percent={
                           'x': (-0.5, 0.5),
                           'y': (-0.45, 0.45)
                       })
        ])

        self.shape_augmentor = iaa.Sequential([
            iaa.PadToFixedSize(OCCLUSION_WIDTH, OCCLUSION_HEIGHT),
            iaa.Fliplr(0.5),
            iaa.Affine(rotate=(-180, 180),
                       scale=(0.6, 0.8),
                       translate_percent={
                           'x': (-0.45, 0.45),
                           'y': (-0.45, 0.45)
                       })
        ])
예제 #19
0
def augment_image(image, bbs, image_size=640):
    bbs = [BoundingBox(*bb) for bb in bbs]
    bbs = BoundingBoxesOnImage(bbs, shape=image.shape)
    resize = iaa.Sequential([
        iaa.Resize({
            "longer-side": image_size,
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(width=image_size, height=image_size)
    ])

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontal flips
            # iaa.Crop(percent=(0, 0.05)), # random crops
            # Gaussian blur with random sigma between 0 and 0.2.
            iaa.GaussianBlur(sigma=(0, 0.2)),
            # Strengthen or weaken the contrast in each image.
            iaa.LinearContrast((0.8, 1.2)),
            # 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 30% 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.3),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            iaa.Affine(
                scale={
                    "x": (1, 1.1),
                    "y": (1, 1.1)
                },
                #             translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
                rotate=[90, -90, 180, 88, -92, 178, 182],
                shear={
                    "x": (-5, 5),
                    "y": (-5, 5)
                },
                cval=(0, 255)),
        ],
        random_order=True)  # apply augmenters in order

    image, bbs = resize(image=image, bounding_boxes=bbs)
    image, bbs = seq(image=image, bounding_boxes=bbs)
    bbs = bbs.remove_out_of_image().clip_out_of_image()
    return image, bbs
예제 #20
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
예제 #21
0
 def train_augmentors(self):
     shape_augs = [
         iaa.PadToFixedSize(self.data_size[0],
                            self.data_size[1],
                            pad_cval=255,
                            position='center',
                            deterministic=True),
         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)
             },
             rotate=(-179, 179),  # rotate by -179 to +179 degrees
             shear=(-5, 5),  # shear by -5 to +5 degrees
             order=[0],  # use nearest neighbour
             backend='cv2'  # opencv for fast processing
         ),
         iaa.Fliplr(0.5),  # horizontally flip 50% of all images
         iaa.Flipud(0.5),  # vertically flip 20% of all images
         iaa.CropToFixedSize(self.input_size[0],
                             self.input_size[1],
                             position='center',
                             deterministic=True),
         # iaa.CropToFixedSize(1440, 1440)
     ]
     #
     input_augs = [
         iaa.OneOf([
             iaa.GaussianBlur((0, 3.0)),  # gaussian blur with random sigma
             iaa.MedianBlur(k=(3, 5)),  # median with random kernel sizes
             iaa.AdditiveGaussianNoise(loc=0,
                                       scale=(0.0, 0.05 * 255),
                                       per_channel=0.5),
         ]),
         iaa.Sequential([
             iaa.Add((-26, 26)),
             iaa.AddToHueAndSaturation((-20, 20)),
             iaa.LinearContrast((0.75, 1.25), per_channel=1.0),
         ],
                        random_order=True),
     ]
     return shape_augs, input_augs
예제 #22
0
def aug_aocr(args, bg_color=255):
    aug_list = []
    aug_list.append(augmenters.Affine(
        scale={"x": (0.8, 1.0), "y": (0.8, 1.0)}, rotate=(-3, 3), cval=bg_color, fit_output=True),
    )
    # aug_list.append(augmenters.AllChannelsCLAHE(tile_grid_size_px =(4, 12), per_channel=False))
    # aug_list.append(augmenters.AddToHueAndSaturation(value=(-10, 10), per_channel=False))
    aug_list.append(augmenters.Resize(
        size={"height": args.resize_height, "width": "keep-aspect-ratio"})
    )
    aug_list.append(augmenters.PadToFixedSize(
        height=args.resize_height, width=args.max_img_size, pad_cval=bg_color)
    )
    aug_list.append(augmenters.CropToFixedSize(
        height=args.resize_height, width=args.max_img_size, position="left-top")
    )
    return aug_list
예제 #23
0
 def infer_augmentors(self):
     shape_augs = [
         # iaa.Scale(
         #     {"height": 1280,
         #      "width" : 1280},
         #     interpolation='nearest',
         #     ),
         iaa.PadToFixedSize(self.data_size[0],
                            self.data_size[1],
                            pad_cval=255,
                            position='center',
                            deterministic=True),
         iaa.CropToFixedSize(self.input_size[0],
                             self.input_size[1],
                             position='center',
                             deterministic=True),
     ]
     return shape_augs
예제 #24
0
    def __init__(self, data, config, is_train=True):
        'Initialization'
        self.data = data
        self.config = config
        self.batch_size = config.BATCH_SIZE
        self.n_classes = config.NUM_CLASS
        self.h = config.INPUT_SHAPE[0]
        self.w = config.INPUT_SHAPE[1]
        self.is_train = is_train
        self.unique_label = np.unique(data["label"])
        self.seq = iaa.Sequential([
            iaa.Fliplr(0.5),
            iaa.Affine(rotate=(-10, 10)),
            iaa.Multiply((0.8, 1.2)),
            iaa.PadToFixedSize(width=self.w + 50, height=self.h + 50),
            iaa.CropToFixedSize(self.h, self.w, 'normal')
        ])

        self.on_epoch_end()
예제 #25
0
def aug_sroie_dynamic_2():
    """Bigger and reasonable augmentation"""
    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))
    # (top, right, bottom, left)
    aug_list.append(
        augmenters.Crop(percent=((0.25, 0.3), (0.0, 0.1), (0.25, 0.23), (0.0,
                                                                         0.1)),
                        keep_size=False))
    aug_list.append(
        augmenters.Resize(size={
            "height": 512,
            "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
예제 #26
0
 def __aug_sequence(self, input_size, target_size):
     iw, ih = input_size
     i_ratio = iw / ih
     tw, th = target_size
     t_ratio = tw / th
     if i_ratio > t_ratio:
         # Input image wider than target, resize width to target width
         resize_aug = iaa.Resize({
             "width": tw,
             "height": "keep-aspect-ratio"
         })
     else:
         # Input image higher than target, resize height to target height
         resize_aug = iaa.Resize({
             "width": "keep-aspect-ratio",
             "height": th
         })
     pad_aug = iaa.PadToFixedSize(width=tw, height=th, position="center")
     seq = iaa.Sequential([resize_aug, pad_aug])
     return seq
예제 #27
0
    def __call__(self, img):
        np_img = np.asarray(img)

        height = np_img.shape[0]
        width = np_img.shape[1]
        if height >= width:
            length = height
        else:
            length = width

        # position is must, because the bbs and images are augmented separately
        self.seq = iaa.Sequential([
            iaa.PadToFixedSize(width=length,
                               height=length,
                               position=self.position)
        ])

        # apply augment
        image_aug = self.seq.augment_image(np_img)

        return image_aug
예제 #28
0
def pad_batch_to_fixed_size(batch_images, target_shape, batch_boxes=None):
    """Resize and pad images and boxes to the target shape
    Arguments
    --------
    batch_images: an array of images with shape (H,W,C)
    target_shape: a shape of type (H,W,C)
    batch_boxes: an array of array with format (xmin, ymin, xmax, ymax)

    Returns
    ------
    images_aug: a list of augmented images
    boxes_aug: a list of augmented boxes (optional: if boxes is not None)
    """
    aug = iaa.Sequential([
        iaa.Resize({
            "longer-side": target_shape[0],
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(height=target_shape[0],
                           width=target_shape[1],
                           position=(1, 1))
    ])
    images_aug, boxes_aug = __transform_batch(batch_images, aug, batch_boxes)
    return images_aug, boxes_aug
예제 #29
0
def pad_to_fixed_size(image, target_shape, boxes=None):
    """Resize and pad images and boxes to the target shape
    Arguments
    --------
    image: an image with shape (H,W,C)
    target_shape: a shape of type (H,W,C)
    boxes: an array of format (xmin, ymin, xmax, ymax)

    Returns
    -------
    image_pad: the image padded
    boxes_pad: the boxes padded (optional: if boxes is not None)

    """
    augmenters = iaa.Sequential([
        iaa.Resize({
            "longer-side": target_shape[0],
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(height=target_shape[0],
                           width=target_shape[1],
                           position=(1, 1))
    ])
    return __transform(image, augmenters, boxes)
예제 #30
0
 def __init__(self,
              images,
              labels,
              batch_size=16,
              image_shape=(256, 512, 1),
              do_shuffle_at_epoch_end=True,
              length=None,
              do_augment=True):
     self.number_batches_augmentation = 4
     self.labels = labels  # array of labels
     self.images = images  # array of image paths
     self.input_shape = image_shape  # image dimensions
     self.label_shape = (image_shape[0], image_shape[1], 1)
     self.length = length
     self.batch_size = batch_size  # batch size
     self.shuffle = do_shuffle_at_epoch_end  # shuffle bool
     self.augment = do_augment  # augment data bool
     self.augmenting_pipeline = iaa.Sequential([
         iaa.PadToFixedSize(pad_mode="symmetric", width=1024, height=0),
         iaa.Affine(shear=(-20, 20)),
         iaa.CropToFixedSize(width=512, height=256, position="center"),
         iaa.Affine(scale={
             "x": (1, 1.5),
             "y": (1, 1.5)
         },
                    translate_percent={"x": (-0.4, 0.4)},
                    mode="symmetric",
                    cval=[0, 0, 0, 0]),
         iaa.PerspectiveTransform(scale=(0, 0.10)),
         iaa.CropToFixedSize(width=512, height=256, position="center-top"),
         # iaa.PiecewiseAffine(scale=(0.001, 0.01), nb_rows=4, nb_cols=8),
         iaa.Lambda(func_images=self.add_background,
                    func_segmentation_maps=self.convert_segmentations),
     ])
     self.indexes = np.arange(len(self.images))
     self.on_epoch_end()