Ejemplo n.º 1
0
    def rotate(images, degree_range=10, n_rotations=1):
        """
            Performs a random rotation of a Numpy image tensor.
            For more details check:
            https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/random_rotation

                :param images: 	        (np_array)  An array of gray scale images of shape
                                                    (num_images, img_width, img_height)
                :param degree_range:    (int)       Rotation range, in degrees.
                :param n_rotations:     (int)       Number of random rotation iterations to perform.
                :return:                (np_array)  Rotated Numpy image tensor.
        """
        if len(images.shape) != 3:
            raise e.DimensionalityError(
                'Invalid shape {} for grayscale images array! Only shapes '
                '(num_images, img_width, img_height) are accepted.'.format(
                    images.shape))

        if n_rotations <= 0:
            raise ValueError(
                'Provide a positive number of rotations. Negative and 0 iterations are not allowed!'
            )

        rotated = keras_image.random_rotation(images, rg=degree_range)
        for i in range(n_rotations - 1):
            rotated = np.append(rotated,
                                keras_image.random_rotation(images,
                                                            rg=degree_range),
                                axis=0)
        return rotated
Ejemplo n.º 2
0
    def _augment_image(self, x):
        """
        Randomply augment image
        """

        assert x.dtype == np.uint8

        # common options
        co = {
            "row_axis": 0,
            "col_axis": 1,
            "channel_axis": 2,
            # can be 'constant', 'nearest', 'reflect', 'wrap'
            "fill_mode": "reflect",
            "cval": 0.0,
        }

        o = self._augmentation_options

        x = random_rotation(x, o["rotation_max_degrees"], **co)
        x = random_shear(x, o["shear_max_degrees"], **co)
        x = random_shift(x, o["shift_max_fraction"]["w"],
                         o["shift_max_fraction"]["h"], **co)
        x = random_zoom(x, o["zoom_range"], **co)
        x = random_brightness(x, o["brightness_range"])

        return x.astype(np.uint8)
Ejemplo n.º 3
0
    def _augment_image(self, x):
        """
        Randomply augment image
        """

        assert x.dtype == np.uint8

        # common options
        co = {
            "row_axis": 0,
            "col_axis": 1,
            "channel_axis": 2,
            # can be 'constant', 'nearest', 'reflect', 'wrap'
            "fill_mode": "nearest",
            "cval": 0.0,
        }

        # default_image_augmenation_options = {
        #     "rotation_max_degrees": 45,
        #     "zoom_range": (0.75, 1.25),
        #     "shift_max_fraction": {"w": 0.25, "h": 0.25},
        #     "shear_max_degrees": 45,
        #     "brightness_range": (0.5, 1.5),
        # }

        o = self.image_augmentation_options

        x = random_rotation(x, o["rotation_max_degrees"], **co)
        x = random_shear(x, o["shear_max_degrees"], **co)
        x = random_shift(x, o["shift_max_fraction"]["w"],
                         o["shift_max_fraction"]["h"], **co)
        x = random_zoom(x, o["zoom_range"], **co)
        x = random_brightness(x, o["brightness_range"])

        return x
Ejemplo n.º 4
0
 def __call__(self, seed_input):
     if tf.is_tensor(seed_input):
         seed_input = seed_input.numpy()
     seed_input = np.array([
         random_rotation(x, self.rg, row_axis=0, col_axis=1, channel_axis=2)
         for x in seed_input
     ])
     return tf.constant(seed_input)
Ejemplo n.º 5
0
 def __call__(self, seed_input):
     seed_input = [np.asarray(x) for x in seed_input]
     seed_input = [
         random_rotation(x, self.rg, row_axis=0, col_axis=1, channel_axis=2)
         for x in seed_input
     ]
     seed_input = [tf.expand_dims(x, axis=0) for x in seed_input]
     return K.concatenate(seed_input, axis=0)
Ejemplo n.º 6
0
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
    with cairo.Context(surface) as context:
        context.set_source_rgb(1, 1, 1)  # White
        context.paint()
        # this font list works in CentOS 7
        if multi_fonts:
            fonts = [
                'Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L',
                'FreeMono'
            ]
            context.select_font_face(
                np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
                np.random.choice(
                    [cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
        else:
            context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL,
                                     cairo.FONT_WEIGHT_BOLD)
        context.set_font_size(25)
        box = context.text_extents(text)
        border_w_h = (4, 4)
        if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h -
                                                         2 * border_w_h[0]):
            raise IOError(
                ('Could not fit string into image.'
                 'Max char count is too large for given image width.'))

        # teach the RNN translational invariance by
        # fitting text box randomly on canvas, with some room to rotate
        max_shift_x = w - box[2] - border_w_h[0]
        max_shift_y = h - box[3] - border_w_h[1]
        top_left_x = np.random.randint(0, int(max_shift_x))
        if ud:
            top_left_y = np.random.randint(0, int(max_shift_y))
        else:
            top_left_y = h // 2
        context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
        context.set_source_rgb(0, 0, 0)
        context.show_text(text)

    buf = surface.get_data()
    a = np.frombuffer(buf, np.uint8)
    a.shape = (h, w, 4)
    a = a[:, :, 0]  # grab single channel
    a = a.astype(np.float32) / 255
    a = np.expand_dims(a, 0)
    if rotate:
        a = image.random_rotation(a, 3 * (w - top_left_x) / w + 1)
    a = speckle(a)

    return a
Ejemplo n.º 7
0
def augmentImages(batch_of_images, batch_of_masks):
    for i in range(len(batch_of_images)):
        img_and_mask = np.concatenate((batch_of_images[i, ...], batch_of_masks[i,...]), axis=2)
        if img_and_mask.ndim == 4: # This assumes single channel data. For multi-channel you'll need
            # change this to put all channel in slices channel
            orig_shape = img_and_mask.shape
            img_and_mask = img_and_mask.reshape((img_and_mask.shape[0:3]))

        if np.random.randint(0,10) == 7:
            img_and_mask = preprocess.random_rotation(img_and_mask, rg=45, row_axis=0, col_axis=1, channel_axis=2,
                                           fill_mode='constant', cval=0.)

        if np.random.randint(0, 5) == 3:
            img_and_mask = elastic_transform(img_and_mask, alpha=1000, sigma=80, alpha_affine=50)

        if np.random.randint(0, 10) == 7:
            img_and_mask = preprocess.random_shift(img_and_mask, wrg=0.2, hrg=0.2, row_axis=0, col_axis=1, channel_axis=2,
                                        fill_mode='constant', cval=0.)

        if np.random.randint(0, 10) == 7:
            img_and_mask = preprocess.random_shear(img_and_mask, intensity=16, row_axis=0, col_axis=1, channel_axis=2,
                         fill_mode='constant', cval=0.)

        if np.random.randint(0, 10) == 7:
            img_and_mask = preprocess.random_zoom(img_and_mask, zoom_range=(0.75, 0.75), row_axis=0, col_axis=1, channel_axis=2,
                         fill_mode='constant', cval=0.)

        if np.random.randint(0, 10) == 7:
            img_and_mask = flip_axis(img_and_mask, axis=1)

        if np.random.randint(0, 10) == 7:
            img_and_mask = flip_axis(img_and_mask, axis=0)

        if np.random.randint(0, 10) == 7:
            salt_pepper_noise(img_and_mask, salt=0.2, amount=0.04)

        if batch_of_images.ndim == 4:
            batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2]
            batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:]
        if batch_of_images.ndim == 5:
            img_and_mask = img_and_mask.reshape(orig_shape)
            batch_of_images[i, ...] = img_and_mask[...,0:img_and_mask.shape[2]//2, :]
            batch_of_masks[i,...] = img_and_mask[...,img_and_mask.shape[2]//2:, :]

        # Ensure the masks did not get any non-binary values.
        batch_of_masks[batch_of_masks > 0.5] = 1
        batch_of_masks[batch_of_masks <= 0.5] = 0

    return(batch_of_images, batch_of_masks)
Ejemplo n.º 8
0
def _augment(image):
    image = random_rotation(image,
                            10,
                            row_axis=0,
                            col_axis=1,
                            channel_axis=2,
                            fill_mode='constant')
    image = random_shift(image,
                         0.1,
                         0.1,
                         row_axis=0,
                         col_axis=1,
                         channel_axis=2,
                         fill_mode='constant')
    image = random_brightness(image, (0.9, 1.1))
    image = random_zoom(image, (0.85, 1.15),
                        row_axis=0,
                        col_axis=1,
                        channel_axis=2,
                        fill_mode='constant')
    image = tf.image.random_flip_left_right(image)
    return image
Ejemplo n.º 9
0
def rot(img):
    return random_rotation(np.array(img),
                           90,
                           row_axis=0,
                           col_axis=1,
                           channel_axis=2)
Ejemplo n.º 10
0
import os
import random
import numpy as np
from PIL import Image, ImageStat
from keras.utils import to_categorical
from tensorflow.keras.utils import Sequence
from tensorflow.keras.preprocessing.image import random_brightness, random_shift, random_rotation, random_zoom

# augmentations
brightness_dec = lambda im: random_brightness(im, (0.2, 1))
brightness_inc = lambda im: random_brightness(im, (1, 3))
shift = lambda im: random_shift(
    im, wrg=0.2, hrg=0.2, row_axis=0, col_axis=1, channel_axis=2)
rotation = lambda im: random_rotation(
    im, 20, row_axis=0, col_axis=1, channel_axis=2)
zoom_in = lambda im: random_zoom(
    im, (0.85, 0.85), row_axis=0, col_axis=1, channel_axis=2)
zoom_out = lambda im: random_zoom(
    im, (1.15, 1.15), row_axis=0, col_axis=1, channel_axis=2)


class ImageDataGenerator(Sequence):
    """Generates data for Keras
    Sequence based data generator. Suitable for building data generator for training and prediction.
    """
    def __init__(self,
                 list_IDs,
                 sample_k,
                 mode,
                 batch_size=32,
                 dim=(70, 70),