Example #1
0
 def _augment(self, x, y):
     for idx, (xx, yy) in enumerate(zip(x, y)):
         if self.hflip and np.random.rand() > 0.5:
             x[idx, ...] = kp.flip_axis(xx, axis=1)
             y[idx, ...] = kp.flip_axis(yy, axis=1)
         if self.vflip and np.random.rand() > 0.5:
             x[idx, ...] = kp.flip_axis(xx, axis=0)
             y[idx, ...] = kp.flip_axis(yy, axis=0)
         if self.brg is not None:
             # random_brightness per channel
             for chn in range(xx.shape[-1]):
                 u = np.random.uniform(*self.brg)
                 x[idx, ..., chn:chn + 1] = kp.apply_brightness_shift(
                     xx[..., chn:chn + 1], u)
         if self.rg > 0 or self.wrg > 0 or self.hrg > 0 or self.zrg is not None:
             params = {
                 'theta': 0,
                 'tx': 0,
                 'ty': 0,
                 'shear': 0,
                 'zx': 1,
                 'zy': 1
             }
             if self.rg > 0:
                 params['theta'] = np.random.uniform(-self.rg, self.rg)
             if self.wrg > 0 or self.hrg > 0:
                 h, w = xx.shape[0], xx.shape[1]
                 params['tx'] = np.random.uniform(-self.wrg, self.wrg) * w
                 params['ty'] = np.random.uniform(-self.hrg, self.hrg) * h
             if self.zrg is not None:
                 params['zx'] = np.random.uniform(*self.zrg, 1)[0]
                 params['zy'] = params['zx']
             x[idx, ...] = kp.apply_affine_transform(xx, **params)
             y[idx, ...] = kp.apply_affine_transform(yy, **params)
     return x, y
Example #2
0
def random_global_brightness_shift(seq, brightness_range):
    """
    perturb sequence wide brightness -- the brightness of the entire sequence
    """
    brightness_lo, brightness_hi = brightness_range
    u = np.random.uniform(brightness_lo, brightness_hi)

    return [apply_brightness_shift(x, u) for x in seq]
    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 random_brightness(image, label, brightness_range):
    if np.ndim(label) == 2:
        label = np.expand_dims(label, axis=-1)
    assert np.ndim(label) == 3

    if brightness_range is not None:
        if isinstance(brightness_range, (tuple, list)) and len(brightness_range) == 2:
            brightness = np.random.uniform(brightness_range[0], brightness_range[1])
        else:
            raise ValueError('`brightness_range` should be '
                             'a tuple or list of two floats. '
                             'Received: %s' % (brightness_range,))
        image = keras_image.apply_brightness_shift(image, brightness)
    return image, label
Example #5
0
def random_brightness(image, label, brightness_range):
    if np.ndim(label) == 2:
        label = np.expand_dims(label, axis=-1)
    assert np.ndim(label) == 3

    if np.ndim(image) == 2:
        #need color image to triple grayscale
        image = np.stack((image, ) * 3, axis=2).astype('uint8')

    if brightness_range is not None:
        if isinstance(brightness_range,
                      (tuple, list)) and len(brightness_range) == 2:
            brightness = np.random.uniform(brightness_range[0],
                                           brightness_range[1])
        else:
            raise ValueError('`brightness_range` should be '
                             'a tuple or list of two floats. '
                             'Received: %s' % (brightness_range, ))
        image = keras_image.apply_brightness_shift(Image.fromarray(image),
                                                   brightness)

    image = image[:, :, 0]
    return image, label
Example #6
0
 def _apply_brightness(self,image,brightness):
     return np.stack([IM.apply_brightness_shift(np.expand_dims(image[i],axis=2),brightness) for i in range(image.shape[0])],axis=0)
Example #7
0
 def keras(self, img):
     return keras.apply_brightness_shift(img,
                                         brightness=1.5).astype(np.uint8)
 def keras(self, img):
     return keras.apply_brightness_shift(img, brightness=1.5).astype(np.uint8)