Пример #1
0
def data_augmentation(images, labels):
    new_images = []
    new_labels = []
    for idx in range(images.shape[0] * 3):
        shifts = np.random.randint(-10, 10, 2)
        rotation = np.random.randint(-20, 20)
        zoom = 1.15 - np.random.random(2) / 4
        new_im = apply_affine_transform(images[idx // 3],
                                        theta=rotation,
                                        tx=shifts[0],
                                        ty=shifts[1],
                                        zx=zoom[0],
                                        zy=zoom[1],
                                        fill_mode='constant',
                                        cval=0.0)
        new_label = apply_affine_transform(labels[idx // 3],
                                           theta=rotation,
                                           tx=shifts[0],
                                           ty=shifts[1],
                                           zx=zoom[0],
                                           zy=zoom[1],
                                           fill_mode='constant',
                                           cval=0.0)
        new_images.append(new_im)
        new_labels.append(new_label)
    new_images = np.stack(new_images)
    new_labels = np.stack(new_labels)
    images = np.concatenate([images, new_images])
    labels = np.concatenate([labels, new_labels])
    return images, labels
Пример #2
0
def random_transformation(im, gt, **kwargs):
    noise = random.choice(kwargs["noises"])
    alpha = random.choice(kwargs["alphas"])
    beta = random.choice(kwargs["betas"])
    flip = random.choice(kwargs["flips"])
    zoom = random.choice(kwargs["zooms"])
    rot_ang = random.choice(kwargs["rot_angs"])
    shear_ang = random.choice(kwargs["shear_angs"])

    noisy = noisy_version(im, noise)
    adjusted = illumination_adjustment_version(noisy, alpha, beta)

    flipped = flipped_version(adjusted, flip)
    flipped_gt = flipped_version(gt, flip)

    affine_transformed = image.apply_affine_transform(flipped,
                                                      rot_ang,
                                                      shear=shear_ang,
                                                      zx=zoom,
                                                      zy=zoom,
                                                      fill_mode="reflect")
    affine_transformed_gt = image.apply_affine_transform(flipped_gt,
                                                         rot_ang,
                                                         shear=shear_ang,
                                                         zx=zoom,
                                                         zy=zoom,
                                                         fill_mode="reflect")

    return affine_transformed, np.where(affine_transformed_gt > 0.5, 1.0, 0.0)
    def random_shift(self,
                     video,
                     wrg,
                     hrg,
                     prob=0.5,
                     row_axis=0,
                     col_axis=1,
                     channel_axis=2,
                     fill_mode='nearest',
                     cval=0.,
                     interpolation_order=1):
        s = np.random.rand()
        if s > prob:
            return video
        h, w = video.shape[1], video.shape[2]
        tx = np.random.uniform(-hrg, hrg) * h
        ty = np.random.uniform(-wrg, wrg) * w

        for i in range(video.shape[0]):
            x = apply_affine_transform(video[i, :, :, :],
                                       tx=tx,
                                       ty=ty,
                                       channel_axis=channel_axis,
                                       fill_mode=fill_mode,
                                       cval=cval,
                                       order=interpolation_order)
            video[i] = x
        return video
Пример #4
0
def augmentation(im, gt, **kwargs):
    noises = kwargs["noises"]
    alphas = kwargs["alphas"]
    betas = kwargs["betas"]
    flips = kwargs["flips"]
    zooms = kwargs["zooms"]
    rot_angs = kwargs["rot_angs"]
    shear_angs = kwargs["shear_angs"]

    for noise in noises:
        noisy = noisy_version(im, noise)

        for alpha in alphas:
            for beta in betas:
                adjusted = illumination_adjustment_version(noisy, alpha, beta)

                for flip in flips:
                    flipped = flipped_version(adjusted, flip)
                    flipped_gt = flipped_version(gt, flip)

                    for zoom in zooms:
                        for rot_ang in rot_angs:
                            for shear_ang in shear_angs:
                                affine_transformed = image.apply_affine_transform(
                                    flipped,
                                    rot_ang,
                                    shear=shear_ang,
                                    zx=zoom,
                                    zy=zoom,
                                    fill_mode="reflect")
                                affine_transformed_gt = image.apply_affine_transform(
                                    flipped_gt,
                                    rot_ang,
                                    shear=shear_ang,
                                    zx=zoom,
                                    zy=zoom,
                                    fill_mode="reflect")
                                yield [
                                    affine_transformed,
                                    np.where(affine_transformed_gt > 0.5, 1.0,
                                             0.0)
                                ]
    def apply_transform(self, x, transform_parameters):
        """Applies a transformation to an image according to given parameters.
        # Arguments
            x: 3D tensor, single image.
            transform_parameters: Dictionary with string - parameter pairs
                describing the transformation.
                Currently, the following parameters
                from the dictionary are used:
                - `'theta'`: Float. Rotation angle in degrees.
                - `'tx'`: Float. Shift in the x direction.
                - `'ty'`: Float. Shift in the y direction.
                - `'shear'`: Float. Shear angle in degrees.
                - `'zx'`: Float. Zoom in the x direction.
                - `'zy'`: Float. Zoom in the y direction.
                - `'flip_horizontal'`: Boolean. Horizontal flip.
                - `'flip_vertical'`: Boolean. Vertical flip.
                - `'channel_shift_intencity'`: Float. Channel shift intensity.
                - `'brightness'`: Float. Brightness shift intensity.
        # Returns
            A transformed version of the input (same shape).
        """
        # x is a single image, so it doesn't have image number at index 0
        img_row_axis = self.row_axis - 1
        img_col_axis = self.col_axis - 1
        img_channel_axis = self.channel_axis - 1

        x = apply_affine_transform(x, transform_parameters.get('theta', 0),
                                   transform_parameters.get('tx', 0),
                                   transform_parameters.get('ty', 0),
                                   transform_parameters.get('shear', 0),
                                   transform_parameters.get('zx', 1),
                                   transform_parameters.get('zy', 1),
                                   row_axis=img_row_axis,
                                   col_axis=img_col_axis,
                                   channel_axis=img_channel_axis,
                                   fill_mode=self.fill_mode,
                                   cval=self.cval,
                                   order=self.interpolation_order)

        if transform_parameters.get('channel_shift_intensity') is not None:
            x = apply_channel_shift(x,
                                    transform_parameters['channel_shift_intensity'],
                                    img_channel_axis)

        if transform_parameters.get('flip_horizontal', False):
            x = flip_axis(x, img_col_axis)

        if transform_parameters.get('flip_vertical', False):
            x = flip_axis(x, img_row_axis)

        if transform_parameters.get('brightness') is not None:
            x = apply_brightness_shift(x, transform_parameters['brightness'])
        return x
    def __call__(self, x):
        res_x = x
        if self.flip:
            res_x = np.fliplr(res_x)
        if self.tx != 0 or self.ty != 0:
            res_x = apply_affine_transform(res_x,
                                           tx=self.tx,
                                           ty=self.ty,
                                           channel_axis=2,
                                           fill_mode='reflect')
        if self.k_90_rotate != 0:
            res_x = np.rot90(res_x, self.k_90_rotate)

        return res_x
    def __call__(self, x):
        res_x = x
        if self.gauss:
            res_x = cnn2d_depthwise_torch(
                res_x, check_shape_kernel(self.gauss_kernel, res_x))
        if self.log:
            res_x = cnn2d_depthwise_torch(
                res_x, check_shape_kernel(self.log_kernel, res_x))
        if self.flip:
            res_x = np.fliplr(res_x)
        if self.tx != 0 or self.ty != 0:
            res_x = apply_affine_transform(res_x,
                                           tx=self.tx,
                                           ty=self.ty,
                                           channel_axis=2,
                                           fill_mode='reflect')
        if self.k_90_rotate != 0:
            res_x = np.rot90(res_x, self.k_90_rotate)

        return res_x
Пример #8
0
    def __getitem__(self, idx):
        batch_x_files = self.x[idx * self.batch_size:(idx + 1) *
                               self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]

        batch_x = []
        for filename in batch_x_files:
            # TODO: add some more augmentation here!
            if self.augment:
                loading_fn = np.random.choice(
                    [self.image_loading_fn, keep_one_dim_square])
            else:
                loading_fn = self.image_loading_fn
            pil_im = loading_fn(filename)
            im_ar = image.img_to_array(pil_im)
            if self.augment:
                aug_params = self.custom_aug_params()
                im_ar = apply_affine_transform(im_ar, **aug_params)
            batch_x.append(im_ar)
        batch_x_proc = preprocess_input(np.array(batch_x))
        return batch_x_proc, batch_y
 def random_rotation(self,
                     video,
                     rg,
                     prob=0.5,
                     row_axis=0,
                     col_axis=1,
                     channel_axis=2,
                     fill_mode='nearest',
                     cval=0.,
                     interpolation_order=1):
     s = np.random.rand()
     if s > prob:
         return video
     theta = np.random.uniform(-rg, rg)
     for i in range(np.shape(video)[0]):
         x = apply_affine_transform(video[i, :, :, :],
                                    theta=theta,
                                    channel_axis=channel_axis,
                                    fill_mode=fill_mode,
                                    cval=cval,
                                    order=interpolation_order)
         video[i] = x
     return video
    def random_shear(self,
                     video,
                     intensity,
                     prob=0.5,
                     row_axis=0,
                     col_axis=1,
                     channel_axis=2,
                     fill_mode='nearest',
                     cval=0.,
                     interpolation_order=1):
        s = np.random.rand()
        if s > prob:
            return video
        shear = np.random.uniform(-intensity, intensity)

        for i in range(video.shape[0]):
            x = apply_affine_transform(video[i, :, :, :],
                                       shear=shear,
                                       channel_axis=channel_axis,
                                       fill_mode=fill_mode,
                                       cval=cval,
                                       order=interpolation_order)
            video[i] = x
        return video
Пример #11
0


data_folder_path='D:\\workspace\\common\\Data Set\\Category and attribute prediction\\Category and Attribute Prediction Benchmark'
train_set, dev_set, test_set = get_dict_bboxes(data_folder_path)
train_generator = data_generator(data_set=train_set)

for index, (img, label, bbox) in enumerate(train_generator):

    img = image.apply_affine_transform(
        theta=0, # rotation
        tx=0, # Translate X
        ty=0, # Translate Y
        shear=0, # Shear
        zx=1, # Scale X
        zy=1, # Scale Y
        row_axis=0, # axis of row
        col_axis=1, # axis of col
        channel_axis=2, # axis of color channel
        fill_mode='constant', # missing pixel fill mode
        cval=0.,
        order=1
    )
    show_image(image=img, bbox=bbox, label=label)
    print(type(img))
    print(img.shape)
    print(label)
    print(bbox)
    break