Example #1
0
def configure_transforms(resize_to):
    train_transforms = iaa.Sequential([
        iaa.PadToAspectRatio(1.0, position='center'),
        iaa.Resize({
            "height": resize_to,
            "width": resize_to
        }),
        iaa.Affine(
            scale={
                "x": (0.95, 1.05),
                "y": (0.95, 1.05)
            },
            translate_percent={
                "x": (-0.05, 0.05),
                "y": (-0.05, 0.05)
            },
            rotate=(-10, 10),
            shear=(-5, 5),
        ),
    ])

    valid_transforms = iaa.Sequential([
        iaa.PadToAspectRatio(1.0, position='center'),
        iaa.Resize({
            "height": resize_to,
            "width": resize_to
        }),
    ])
    return train_transforms, valid_transforms
Example #2
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)
Example #3
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 generate_cartoon():
    ia.seed(1)

    image1 = imageio.imread(
        os.path.join(INPUT_IMAGES_DIR, "Pahalgam_Valley.jpg"))
    image2 = imageio.imread(
        os.path.join(INPUT_IMAGES_DIR, "1024px-Salad_platter.jpg"))
    image1 = iaa.Resize({
        "width": 256,
        "height": "keep-aspect-ratio"
    })(image=image1)
    image2 = iaa.Resize({
        "width": 256,
        "height": "keep-aspect-ratio"
    })(image=image2)

    images_aug = [image1]
    images_aug.extend(iaa.Cartoon()(images=[image1] * 3))
    images_aug.append(image2)
    images_aug.extend(iaa.Cartoon()(images=[image2] * 3))

    # if we use a single draw_grid() call here, the function will currently
    # add an ugly black border at the bottom of the first row, because the
    # height of image1 is lower than of image2 and it will ensure that both
    # rows have the same height
    row1 = ia.draw_grid(images_aug[0:4], cols=4, rows=1)
    row2 = ia.draw_grid(images_aug[4:], cols=4, rows=1)
    _save("cartoon.jpg", np.vstack([row1, row2]))
Example #5
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
Example #6
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)),
    ])
    def preprocess(self, ims, augment):
        def normalize(batch):
            return batch.astype(np.float32) / float(255)

        if augment:
            augmentations = iaa.Sequential([
                iaa.Resize(int(1.1 * hp.img_size)),
                iaa.Fliplr(0.5),
                iaa.Sometimes(0.4, iaa.Rotate((-30, 30))),
                iaa.Sometimes(0.4, iaa.Affine(scale=(0.9, 1.2))),
                iaa.Sometimes(0.5,
                              iaa.PerspectiveTransform(scale=(0.01, 0.20))),
                # Crop/resize image to proper dimension
                iaa.CropToFixedSize(hp.img_size, hp.img_size),
                iaa.Resize(hp.img_size),
                iaa.Sometimes(0.3, iaa.SaltAndPepper(0.01)),
                iaa.CLAHE(to_colorspace='HSV')
            ])
        else:
            augmentations = ia.Sequential([iaa.CLAHE(to_colorspace='HSV')])

        augmented = augmentations.augment_images(ims)
        for i in augmented:
            if i.shape != (hp.img_size, hp.img_size, 3):
                print(i.shape)
        augmented = np.stack(augmented, axis=0)
        return normalize(augmented)
Example #8
0
def complex_imgaug(x, org_size, scale_size):
    """input single RGB PIL Image instance"""
    x = np.array(x)
    x = x[np.newaxis, :, :, :]
    aug_seq = iaa.Sequential([
        iaa.Sometimes(
            0.5,
            iaa.OneOf([
                iaa.GaussianBlur((3, 15)),
                iaa.AverageBlur(k=(3, 15)),
                iaa.MedianBlur(k=(3, 15)),
                iaa.MotionBlur((5, 25))
            ])),
        iaa.Resize(scale_size, interpolation=ia.ALL),
        iaa.Sometimes(
            0.2,
            iaa.AdditiveGaussianNoise(loc=0,
                                      scale=(0.0, 0.1 * 255),
                                      per_channel=0.5)),
        iaa.Sometimes(0.7, iaa.JpegCompression(compression=(10, 65))),
        iaa.Resize(org_size),
    ])

    aug_img = aug_seq(images=x)
    return aug_img[0]
Example #9
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]
    def build_augmenter(self):
        augmenter = iaa.Sometimes(
            0.7,
            iaa.Sequential([
                iaa.Sequential([
                    iaa.Resize(
                        {
                            'longer-side': (96, 48),
                            'shorter-side': 'keep-aspect-ratio'
                        },
                        interpolation=ia.ALL),
                    iaa.Resize(
                        {
                            'longer-side': self.config.image_size,
                            'shorter-side': 'keep-aspect-ratio'
                        },
                        interpolation=ia.ALL)
                ]),
                iaa.Affine(
                    scale=(0.8, 1.2),
                    translate_percent={
                        "x": (-0.2, 0.2),
                        "y": (-0.2, 0.2)
                    },
                    rotate=(-15, 15),
                    order=[0, 1],
                    mode=ia.ALL,
                ),
                iaa.OneOf([
                    iaa.Noop(),
                    iaa.Multiply((0.2, 2.0)),
                    iaa.GammaContrast((0.5, 1.7)),
                ]),
                iaa.OneOf([
                    iaa.Noop(),
                    iaa.JpegCompression(compression=(85, 95)),
                    iaa.GaussianBlur(sigma=(0.75, 2.25)),
                    iaa.MotionBlur(k=(10, 15))
                ]),
                iaa.OneOf([
                    iaa.Noop(),
                    iaa.OneOf([
                        iaa.Crop(percent=((0.2, 0.5), 0, 0, 0),
                                 keep_size=False),
                        iaa.Crop(percent=(0, (0.2, 0.5), 0, 0),
                                 keep_size=False),
                        iaa.Crop(percent=(0, 0, (0.2, 0.5), 0),
                                 keep_size=False),
                        iaa.Crop(percent=(0, 0, 0, (0.2, 0.5)),
                                 keep_size=False),
                        iaa.Crop(percent=((0.1, 0.3), 0, (0.1, 0.3), 0),
                                 keep_size=False),
                        iaa.Crop(percent=(0, (0.1, 0.3), 0, (0.1, 0.3)),
                                 keep_size=False)
                    ]),
                    iaa.PerspectiveTransform(0.1)
                ])
            ]))

        return augmenter
Example #11
0
def resize(image, target_shape, boxes=None, keep_aspect_ratio=True):
    """Resize 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)
    keep_aspect_ratio: (default: True)

    Returns
    -------
    image_resized: the image resized
    boxes_resized: the boxes resized (optional: if boxes is not None)
    """

    if keep_aspect_ratio:
        aug = iaa.Sequential([
            iaa.Resize({
                "longer-side": target_shape[0],
                "shorter-side": "keep-aspect-ratio"
            }),
        ])
    else:
        aug = iaa.Sequential([
            iaa.Resize({
                "height": target_shape[0],
                "width": target_shape[1]
            }),
        ])

    return __transform(image, aug, boxes=boxes)
Example #12
0
def get_augs(img_size=320):
    train_aug = iaa.Sequential([
        iaa.Fliplr(0.5),
        # iaa.Affine(
        #     scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},  # scale images to 80-120% of their size, individually per axis
        #     translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},  # translate by -20 to +20 percent (per axis)
        #     rotate=(-45, 45),  # rotate by -45 to +45 degrees
        #     shear=(-16, 16),  # shear by -16 to +16 degrees
        #     order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
        #     cval=(0, 255),  # if mode is constant, use a cval between 0 and 255
        #     mode=ia.ALL),
        iaa.AddToHueAndSaturation((-20, 20)),  # change hue and saturation
        iaa.Crop(percent=(0.05, 0.5)),
        iaa.Resize(size=(img_size, img_size))
        # iaa.PadToFixedSize(img_size, img_size)
    ])

    valid_aug = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.Resize(size=(img_size, img_size))
        # iaa.CropToFixedSize(img_size, img_size),
        # iaa.PadToFixedSize(img_size, img_size)
    ])

    return train_aug, valid_aug
def get_transform(image):
    seq = []

    if image.shape[0] >= image.shape[1]:

        seq.append(iaa.Resize({"height": 480, "width": "keep-aspect-ratio"}))

        if image.shape[1] / image.shape[1] * 480. > 640.:

            seq.append(
                iaa.Resize({
                    "width": 640,
                    "height": "keep-aspect-ratio"
                }))

    if image.shape[0] < image.shape[1]:

        seq.append(iaa.Resize({"width": 640, "height": "keep-aspect-ratio"}))

        if image.shape[0] / image.shape[1] * 640. > 480.:

            seq.append(
                iaa.Resize({
                    "height": 480,
                    "width": "keep-aspect-ratio"
                }))
    seq.append(iaa.CenterPadToFixedSize(
        height=480,
        width=640,
    ))
    return iaa.Sequential(seq)
Example #14
0
def get_training_augmentation(**kwargs):
    seq = iaa.Sequential([
        iaa.Resize({
            'height': kwargs['crop_sz'],
            'width': kwargs['crop_sz']
        }),
        iaa.flip.Fliplr(p=0.5),
        iaa.OneOf(
            [iaa.GaussianBlur(sigma=(0.0, 1.0)),
             iaa.MotionBlur(k=(3, 5))]),
        iaa.OneOf([
            iaa.GammaContrast((0.8, 1.0)),
            iaa.LinearContrast((0.75, 1.5)),
            iaa.LogContrast((0.8, 1.0))
        ]),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        iaa.Crop(px=(0, 2 * (kwargs['crop_sz'] - kwargs['inp_sz']))),
        iaa.Resize({
            'height': kwargs['inp_sz'],
            'width': kwargs['inp_sz']
        })
    ])
    return seq
Example #15
0
    def __init__(self,
                 image_folder,
                 annotation_file,
                 input_size=(512, 512),
                 target_domain_glob=None,
                 num_classes=80,
                 num_keypoints=0,
                 rotated_boxes=False,
                 mean=(0.40789654, 0.44719302, 0.47026115),
                 std=(0.28863828, 0.27408164, 0.27809835),
                 augmentation=None,
                 augment_target_domain=False,
                 max_detections=150,
                 down_ratio=4):
        self.image_folder = Path(image_folder)
        self.coco = COCO(annotation_file)
        self.images = self.coco.getImgIds()
        self.use_rotated_boxes = rotated_boxes
        self.max_detections = max_detections
        self.down_ratio = down_ratio
        self.input_size = input_size
        self.mean = np.array(mean, dtype=np.float32).reshape(1, 1, 3)
        self.std = np.array(std, dtype=np.float32).reshape(1, 1, 3)
        self.augmentation = augmentation
        self.num_classes = num_classes
        self.num_keypoints = num_keypoints
        self.string_id_mapping = {}
        self.augment_target_domain = augment_target_domain
        self.cat_mapping = {
            v: i
            for i, v in enumerate(range(1, num_classes + 1))
        }
        self.classes = {
            y: self.coco.cats[x] if x in self.coco.cats else ''
            for x, y in self.cat_mapping.items()
        }
        assert len(input_size) == 2

        if isinstance(target_domain_glob, str):
            self.target_domain_files = glob(target_domain_glob)
        elif isinstance(target_domain_glob, (list, ListConfig)):
            self.target_domain_files = []
            for pattern in target_domain_glob:
                self.target_domain_files.extend(glob(pattern))
        else:
            self.target_domain_files = []

        if self.augmentation:
            augmentation_methods = instantiate_augmenters(augmentation)
            self.augmentation = iaa.Sequential(augmentation_methods)

        self.resize = iaa.Resize((self.input_size[0], self.input_size[1]))
        self.resize_out = iaa.Resize((self.input_size[0] // down_ratio,
                                      self.input_size[1] // down_ratio))

        log.info(
            f"found {len(self.target_domain_files)} samples for target domain")
        super().__init__()
def get_down():
    return ia.Sequential([
        # random downsample between 4x to 8x and get back
        ia.Resize((1.0, 1.0), interpolation="cubic"),
        ia.Resize({
            "height": 512,
            "width": 512
        }, interpolation="cubic"),
    ])
Example #17
0
def get_down():
    return ia.Sequential([
        # random downsample between 4x to 8x and get back
        ia.Resize((0.125, 0.25)),
        ia.Resize({
            "height": 512,
            "width": 512
        }),
    ])
Example #18
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)
Example #19
0
def prismatize(images, random_state, parents, hooks):
    ret_images = list()
    for image in images:
        val    = random_state.uniform(0.0001, 0.9999)
        aug1   = iaa.Resize({"width": val})
        image1 = aug1(images=image.reshape(1,*image.shape)).reshape(image.shape[0],-1,1)
        aug2   = iaa.Resize({"width": 1 - val})
        image2 = aug2(images=image.reshape(1,*image.shape)).reshape(image.shape[0],-1,1)
        ret_images.append(np.concatenate((image1, image2), axis=1).reshape(image.shape[0],-1,1))
    return np.stack(ret_images, axis=0).reshape(len(ret_images), *ret_images[0].shape)
Example #20
0
    def __call__(self, *args, **kwargs) -> typing.Tuple[np.ndarray, typing.List[Polygon]]:

        if self.is_training:
            resize = iaa.Resize(size=dict(longer_side=self.long_sizes,
                                          width='keep-aspect-ratio'))
            rotate = iaa.Rotate(rotate=self.angles, fit_output=True)
            resize_height = iaa.Resize(size=dict(height=self.height_ratios,
                                                 width='keep'))
            crop = iaa.CropToFixedSize(width=self.cropped_size[0], height=self.cropped_size[1])
            fix_resize = iaa.Resize(size=self.output_size)


            # blur = iaa.GaussianBlur()
            # blur = iaa.Sometimes(p=self.blur_prob,
            #                      then_list=blur)

            brightness = iaa.MultiplyBrightness((0.5, 1.5))
            brightness = iaa.Sometimes(self.color_jitter_prob, then_list=brightness)

            saturation = iaa.MultiplySaturation((0.5, 1.5))
            saturation = iaa.Sometimes(self.color_jitter_prob, then_list=saturation)

            contrast = iaa.LinearContrast(0.5)
            contrast = iaa.Sometimes(self.color_jitter_prob, then_list=contrast)

            hue = iaa.MultiplyHue()
            hue = iaa.Sometimes(self.color_jitter_prob, then_list=hue)

            augs = [resize,
                    rotate,
                    resize_height,
                    crop,
                    fix_resize,
                    brightness,
                    saturation,
                    contrast,
                    hue]
            ia = iaa.Sequential(augs)
        else:
            fix_resize = iaa.Resize(size=self.output_size)
            ia = iaa.Sequential([fix_resize])

        image = args[0]
        polygons = args[1]

        polygon_list = []
        for i in range(polygons.shape[0]):
            polygon_list.append(Polygon(polygons[i].tolist()))

        polygons_on_image = PolygonsOnImage(polygon_list, shape=image.shape)

        image_aug, polygons_aug = ia(image=image, polygons=polygons_on_image)

        return image_aug, polygons_aug.polygons
def augmentation_pipeline(level):
    if level == 'resize_only':
        list_augmentations = [iaa.Resize(512)]

    elif level == 'light':
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.1,
                shear=(2.5, 2.5),
                rotate=(-5, 5),
            ),
        ]

    elif level == 'heavy':  #no rotation included
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.15,
                shear=(4.0, 4.0),
            ),
            iaa.Fliplr(0.2),  # horizontally flip 20% of the images
            iaa.Sometimes(
                0.1,
                iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                        size_percent=(0.1, 0.2))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))),
        ]

    elif level == 'heavy_with_rotations':
        list_augmentations = [
            iaa.Resize(512),
            iaa.Affine(
                scale=1.15,
                shear=(4.0, 4.0),
                rotate=(-6, 6),
            ),
            iaa.Fliplr(0.2),  # horizontally flip 20% of the images
            iaa.Sometimes(
                0.1,
                iaa.CoarseSaltAndPepper(p=(0.01, 0.01),
                                        size_percent=(0.1, 0.2))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))),
        ]

    return list_augmentations
 def __init__(self, width=1020, height=1020):
     self.images = np.zeros((0, 3, width, height), dtype=np.uint8)
     self.sizes = np.zeros((0, 2), dtype=int)
     self.width = width
     self.height = height
     self.by_height_resizer = iaa.Resize({
         'width': 'keep-aspect-ratio',
         'height': self.height
     })
     self.by_width_resizer = iaa.Resize({
         'width': self.width,
         'height': 'keep-aspect-ratio'
     })
     self.mutex = Lock()
def get_dataloader(data_dir='',
                   batch_size=64,
                   train_set=True,
                   num_workers=1,
                   augment=True):

    if augment:
        seq = iaa.Sequential([
            iaa.Resize((64, 64)),
            iaa.Affine(rotate=(-5, 5), translate_px=(-10, 10), shear=(-5, 5))
        ],
                             random_order=False)
    else:
        seq = iaa.Resize((64, 64))

    dataloader = torch.utils.data.DataLoader(datasets.MNIST(
        data_dir,
        train=train_set,
        download=True,
        transform=transforms.Compose([
            np.array,
            seq.augment_images,
            transforms.ToTensor(),
        ])),
                                             num_workers=num_workers,
                                             batch_size=batch_size,
                                             shuffle=True)

    return dataloader


# class CustomDataset(Dataset):
#     def __init__(self, n_images, n_classes, transform=None):
#         self.images = np.random.randint(0, 255,
#                                         (n_images, 224, 224, 3),
#                                         dtype=np.uint8)
#         self.targets = np.random.randn(n_images, n_classes)
#         self.transform = transform
#
#     def __getitem__(self, item):
#         image = self.images[item]
#         target = self.targets[item]
#
#         if self.transform:
#             image = self.transform(image)
#
#         return image, target
#
#     def __len__(self):
#         return len(self.images)
Example #24
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 train_augmentors(self):
     shape_augs = [
         iaa.Resize((512, 512), interpolation='nearest'),
         # iaa.CropToFixedSize(width=800, height=800),
     ]
     #
     sometimes = lambda aug: iaa.Sometimes(0.2, aug)
     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),
         ]),
         sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
         # move pixels locally around (with random strengths)
         sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))
                   ),  # sometimes move parts of the image around
         sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))),
         iaa.Sequential([
             iaa.Add((-26, 26)),
             iaa.AddToHueAndSaturation((-20, 20)),
             iaa.LinearContrast((0.75, 1.25), per_channel=1.0),
         ],
                        random_order=True),
         sometimes([
             iaa.CropAndPad(percent=(-0.05, 0.1),
                            pad_mode="reflect",
                            pad_cval=(0, 255)),
         ]),
     ]
     return shape_augs, input_augs
Example #26
0
 def _augment(self, images):
     '''
     We need the augmentation to keep the temporal information of the clip,
     so all augmentations in one clip without random
     '''
     # Get the resize function in the augmentation
     if self.params.resize.use:
         resize = self.transforms.find_augmenters_by_name('resize')[0]
     elif self.params.CropToFixedSize.use:
         resize = iaa.Resize(
             {
                 "height": self.params.CropToFixedSize.height,
                 "width": self.params.CropToFixedSize.width
             },
             name='resize')
     else:
         raise Exception('YOU MUST HAVE THE SAME SIZE OF IMAGES')
     # Make the some data not augment
     oneof_iaa = iaa.OneOf([self.transforms, resize])
     oneof_iaa_deterministic = oneof_iaa.to_deterministic()
     temp = [
         oneof_iaa_deterministic(image=images[i])
         for i in range(len(images))
     ]
     temp = np.array(temp)
     # import ipdb; ipdb.set_trace()
     return temp
Example #27
0
    def __call__(self, img, anno):
        gts = anno[1].numpy()
        np_img = np.asarray(img)
        scale = self.h / np_img.shape[0]

        # 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)

        self.seq = iaa.Sequential(
            [iaa.Resize({
                "height": self.h,
                "width": self.w
            })])

        # 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))
        anno[2]["scale"] = scale
        return image_aug, (anno[0], gts, anno[2])
def datagen(dataset_path, batch_size, x_mean):  # 函数式generator
    while 1:
        imgs = np.zeros((batch_size, 957, 957, 3), dtype=np.uint8)
        labels = np.zeros((batch_size, ))
        # 从整个数据集的所有类别中,随机选取batch_size张图片,预处理后yield
        for img_idx in range(batch_size):
            label = np.random.randint(0, 50)  # 随机选取一个类别编号,并作为文件夹索引的一部分
            labels[img_idx] = label
            if label < 10:
                img_path = dataset_path + '0' + str(label)
            else:
                img_path = dataset_path + str(label)
            img_path = img_path + '/' + random.sample(
                os.listdir(img_path), 1)[0]  # random.sample从指定目录下随机选择文件
            img = Image.open(img_path)
            imgs[img_idx] = img
        ia.seed(np.random.randint(0, 100))
        data_preprocessor1 = iaa.Sequential([iaa.Affine(rotate=(-179, 180))])
        data_preprocessor2 = iaa.Sequential([
            iaa.Resize({
                "height": 456,
                "width": 456
            }),
            iaa.Sometimes(0.25, iaa.ContrastNormalization(
                (0.8, 1.2))),  # 25%的图像对比度随机变为0.8或1.2倍
            iaa.Sometimes(0.25, iaa.Multiply(
                (0.8, 1.2)))  # 25%的图片像素值乘以0.8-1.2中间的数值,用以增加图片明亮度或改变颜色
        ])
        imgs_aug0 = data_preprocessor1.augment_images(imgs)
        imgs_aug1 = imgs_aug0[:, 140:-140, 140:-140, :]
        imgs_aug2 = data_preprocessor2.augment_images(imgs_aug1)
        plt.imshow(np.squeeze(imgs_aug2, 0))
        plt.show()
        yield imgs_aug2.astype(np.float32) - x_mean, np_utils.to_categorical(
            labels, num_classes=50)
Example #29
0
    def __init__(self, data_config, transform=False, mode="train", visualize=False):

        # Set the mode (train/val)
        self.mode = mode

        self.visualize = visualize

        # Read in necessary configs
        file_data_path = data_config[mode]
        self.image_directory = data_config["image_directory"]
        self.annotation_directory = data_config["annotation_directory"]
        self.classes = tuple(data_config['classes'])

        self.file_list = self.create_file_list(file_data_path)

        self.transform = transform

        self.transformations = iaa.Noop()

        self.height = data_config["figure_size"]
        self.width = data_config["figure_size"]

        self.resize_transformation = iaa.Resize({
            "height": self.height,
            "width": self.width
        })

        self.data_encoder = DataEncoder(data_config)
        self.default_boxes = self.data_encoder.default_boxes
Example #30
0
 def _get_default_augment(self):
     augment_seq = iaa.Sequential([
         iaa.Fliplr(0.5),
         iaa.Affine(rotate=(-10, 10)),
         iaa.Resize((0.5, 3.0))
     ])
     return augment_seq