Ejemplo n.º 1
0
    def _create_matrices(self, shapes, random_state):
        """Create the transformation matrix

        # Arguments
            shapes [list of tuples]: list of image shapes
            random_state [numpy Random state]: some random state

        # Returns
            [list of np array]: list of transformation matrices
            [list of ints]: list of heights
            [list of ints]: list of widths
        """
        matrices = []
        max_heights = []
        max_widths = []
        nb_images = len(shapes)
        seeds = ia.copy_random_state(random_state).randint(
            0, 10**6, (nb_images, ))

        for _idx in range(nb_images):
            height, width = shapes[_idx][:2]

            pts1 = np.float32([[0, 0], [0, height - 1], [width - 1, 0],
                               [width - 1, height - 1]])

            transition = self.jitter.draw_samples(
                (4, 2), random_state=ia.new_random_state(seeds[_idx]))
            transition[:, 0] = transition[:, 0] * np.min([height, width])
            transition[:, 1] = transition[:, 1] * np.min([height, width])
            transition = transition.astype(np.int32)
            transition[:,
                       0] = transition[:, 0] + np.abs(np.min(transition[:, 0]))
            transition[:,
                       1] = transition[:, 1] + np.abs(np.min(transition[:, 1]))

            pts2 = np.float32(
                [[transition[0, 0], transition[0, 1]],
                 [transition[1, 0], height - 1 + transition[1, 1]],
                 [width - 1 + transition[2, 0], transition[2, 1]],
                 [width - 1 + transition[3, 0],
                  height - 1 + transition[3, 1]]])

            height = np.max(pts2[:, 1])
            width = np.max(pts2[:, 0])

            matrices.append(cv2.getPerspectiveTransform(pts1, pts2))
            max_heights.append(height)
            max_widths.append(width)

        return matrices, max_heights, max_widths
Ejemplo n.º 2
0
def augment_object_data(object_data,
                        random_state=None,
                        fit_output=True,
                        aug_color=True,
                        aug_geo=True,
                        augmentations=None,
                        random_order=False,
                        scale=(0.5, 1.0)):
    try:
        iaa.Affine(fit_output=True)
    except TypeError:
        warnings.warn(
            'Your imgaug does not support fit_output kwarg for'
            'imgaug.augmenters.Affine. Please install via'
            '\n\n\tpip install -e git+https://github.com/wkentaro/imgaug@affine_resize\n\n'  # NOQA
            'to enable it.')
        fit_output = False

    if random_state is None:
        random_state = np.random.RandomState()
    if augmentations is None:
        st = lambda x: iaa.Sometimes(0.3, x)  # NOQA
        kwargs_affine = dict(
            order=1,  # order=0 for mask
            cval=0,
            scale=scale,
            translate_px=(-16, 16),
            rotate=(-180, 180),
            shear=(-16, 16),
            mode='constant',
        )
        if fit_output:
            kwargs_affine['fit_output'] = fit_output
        augmentations = [
            st(
                iaa.WithChannels([0, 1], iaa.Multiply([1, 1.5])
                                 ) if aug_color else iaa.Noop()),
            st(
                iaa.WithColorspace('HSV',
                                   children=iaa.
                                   WithChannels([1, 2], iaa.Multiply([0.5, 2]))
                                   ) if aug_color else iaa.Noop()),
            st(
                iaa.GaussianBlur(
                    sigma=[0.0, 1.0]) if aug_color else iaa.Noop()),
            iaa.Sometimes(
                0.8,
                iaa.Affine(**kwargs_affine) if aug_geo else iaa.Noop()),
        ]
    aug = iaa.Sequential(
        augmentations,
        random_order=random_order,
        random_state=ia.copy_random_state(random_state),
    )

    def activator_imgs(images, augmenter, parents, default):
        if isinstance(augmenter, iaa.Affine):
            augmenter.order = Deterministic(1)
            augmenter.cval = Deterministic(0)
        return True

    def activator_masks(images, augmenter, parents, default):
        white_lists = (iaa.Affine, iaa.PerspectiveTransform, iaa.Sequential,
                       iaa.Sometimes)
        if not isinstance(augmenter, white_lists):
            return False
        if isinstance(augmenter, iaa.Affine):
            augmenter.order = Deterministic(0)
            augmenter.cval = Deterministic(0)
        return True

    def activator_lbls(images, augmenter, parents, default):
        white_lists = (iaa.Affine, iaa.PerspectiveTransform, iaa.Sequential,
                       iaa.Sometimes)
        if not isinstance(augmenter, white_lists):
            return False
        if isinstance(augmenter, iaa.Affine):
            augmenter.order = Deterministic(0)
            augmenter.cval = Deterministic(-1)
        return True

    for objd in object_data:
        aug = aug.to_deterministic()
        objd['img'] = aug.augment_image(
            objd['img'], hooks=ia.HooksImages(activator=activator_imgs))
        if 'mask' in objd:
            objd['mask'] = aug.augment_image(
                objd['mask'], hooks=ia.HooksImages(activator=activator_masks))
        if 'lbl' in objd:
            objd['lbl'] = aug.augment_image(
                objd['lbl'], hooks=ia.HooksImages(activator=activator_lbls))
        if 'lbl_suc' in objd:
            objd['lbl_suc'] = aug.augment_image(
                objd['lbl_suc'],
                hooks=ia.HooksImages(activator=activator_lbls))
        if 'masks' in objd:
            masks = []
            for mask in objd['masks']:
                mask = aug.augment_image(
                    mask,
                    hooks=ia.HooksImages(activator=activator_masks),
                )
                masks.append(mask)
            masks = np.asarray(masks)
            objd['masks'] = masks
            del masks
        if 'lbls' in objd:
            lbls = []
            for lbl in objd['lbls']:
                lbl = aug.augment_image(
                    lbl,
                    hooks=ia.HooksImages(activator=activator_lbls),
                )
                lbls.append(lbl)
            lbls = np.asarray(lbls)
            objd['lbls'] = lbls
            del lbls
        yield objd