Exemplo n.º 1
0
def augmentation_img(img1,img2):
    aug = np.random.randint(2,size=5)
    img_a1, img_a2 = np.zeros_like(img1) , np.zeros_like(img2)
    if aug[0]:
        # 旋转
        img_a1 = image.random_rotation(img1,30,0,1,2)
        img_a2 = image.random_rotation(img2,30,0,1,2)
    if aug[1]:
        # 错位
        img_a1 = image.random_shear(img1,30,0,1,2)
        img_a2 = image.random_shear(img2,30,0,1,2)
    if aug[2]:
        # 缩放
        img_a1 = image.random_zoom(img_a1,(0.8,1.2),0,1,2) 
        img_a2 = image.random_zoom(img_a2,(0.8,1.2),0,1,2) 
    if aug[3]:
        # 亮度
        img_a1 = image.random_brightness(img_a1, (0.5,1.5))
    if aug[4]:
        # 噪声
        img_a1 += np.random.uniform(0,32,(np.shape(img_a1)))
        img_a1 = np.clip(img_a1,0,255)
    # 转成 0-255 整数
    img_a1 = np.array(img_a1,'uint8')
    img_a2 = np.array(img_a2,'uint8')
    return img_a1, img_a2
    def apply(self, samples):
        # Apply the transformation and return the transformed version.
        # sample must be a 3D tensor with shape [rows,cols,channels].
        out = np.zeros(samples.shape)

        for i in range(len(samples)):
            out[i] = samples[i]

            if self.shiftX > 0 or self.shiftY > 0:
                out[i] = image.random_shift(out[i], self.shiftX, self.shiftY)

            if self.rot > 0:
                out[i] = image.random_rotation(out[i], self.rot)

            if self.shear > 0:
                out[i] = image.random_shear(out[i], self.shear)

            if self.zoomMin > 0 and self.zoomMax > 0:
                out[i] = image.random_zoom(out[i],
                                           [self.zoomMin, self.zoomMax])

            if self.flipH and np.random.uniform() < 0.5:
                out[i] = image.flip_axis(out[i], 1)

            if self.flipV and np.random.uniform() < 0.5:
                out[i] = image.flip_axis(out[i], 2)

        return out
Exemplo n.º 3
0
def shear_image(img_arr, givenIntensity=5):
    return kim.random_shear(img_arr,
                            intensity=givenIntensity,
                            row_axis=0,
                            col_axis=1,
                            channel_axis=2,
                            fill_mode='nearest')
Exemplo n.º 4
0
 def test_random_transforms(self):
     x = np.random.random((2, 28, 28))
     assert image.random_rotation(x, 45).shape == (2, 28, 28)
     assert image.random_shift(x, 1, 1).shape == (2, 28, 28)
     assert image.random_shear(x, 20).shape == (2, 28, 28)
     assert image.random_zoom(x, (5, 5)).shape == (2, 28, 28)
     assert image.random_channel_shift(x, 20).shape == (2, 28, 28)
Exemplo n.º 5
0
def rnd_shear(img, factor):
    return random_shear(img,
                        intensity=factor,
                        row_axis=0,
                        col_axis=1,
                        channel_axis=2,
                        fill_mode='nearest')
Exemplo n.º 6
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 = 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, 10) == 7:
      img_and_mask = 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:
    # 没任何显示效果,f**k
      img_and_mask = random_shear(img_and_mask, intensity=15, row_axis=0, col_axis=1, channel_axis=2,
                                  fill_mode='nearest', cval=0.)

    if np.random.randint(0, 10) == 7:
      # fill_mode = 'nearest'
    # zoom_range=(0.1, 0.5)  #超近距离
    # zoom_range=(0.5, 5)  # 超远距离
      zoom_range = (0.35, 2)  # 一般般

      img_and_mask = random_zoom(img_and_mask, zoom_range=zoom_range, row_axis=0, col_axis=1, channel_axis=2,
                                 fill_mode='constant', cval=0.)

    if np.random.randint(0, 10) == 7:#按y轴左右翻转
      img_and_mask = flip_axis(img_and_mask, axis=1)


    # if np.random.randint(0, 10) == 7:#按x轴上下翻转,上下倒过来了有点恐怖
    #   img_and_mask = flip_axis(img_and_mask, axis=0)

    #椒盐噪声
    # if np.random.randint(0, 10) == 7:
    # # amount=10 #密密麻麻
    #   amount = 2 #正常些
    #   salt_pepper_noise(img_and_mask, salt=1, amount=amount)


    # 这个elastic_transform导致显示的东西像屎一样,干脆不要了
    # if np.random.randint(0, 5) == 3:
    # img_and_mask = elastic_transform(img_and_mask, alpha=1000, sigma=80, alpha_affine=50)

    #最终的维度处理
    if batch_of_images.ndim == 4:
      batch_of_images[i, ...] = img_and_mask[..., 0:3]
      batch_of_masks[i, ...] = np.expand_dims(img_and_mask[...,3],axis=-1)


    # 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)
Exemplo n.º 7
0
def augmentation_pipeline(img_arr):

    import keras.preprocessing.image as im

    img_arr = im.random_rotation(img_arr,
                                 18,
                                 row_axis=0,
                                 col_axis=1,
                                 channel_axis=2,
                                 fill_mode='nearest')
    img_arr = im.random_shear(img_arr,
                              intensity=0.4,
                              row_axis=0,
                              col_axis=1,
                              channel_axis=2,
                              fill_mode='nearest')
    img_arr = im.random_zoom(img_arr,
                             zoom_range=(0.9, 2.0),
                             row_axis=0,
                             col_axis=1,
                             channel_axis=2,
                             fill_mode='nearest')
    img_arr = random_greyscale(img_arr, 0.4)

    return img_arr
Exemplo n.º 8
0
 def test_random_transforms(self):
     x = np.random.random((2, 28, 28))
     assert image.random_rotation(x, 45).shape == (2, 28, 28)
     assert image.random_shift(x, 1, 1).shape == (2, 28, 28)
     assert image.random_shear(x, 20).shape == (2, 28, 28)
     assert image.random_zoom(x, (5, 5)).shape == (2, 28, 28)
     assert image.random_channel_shift(x, 20).shape == (2, 28, 28)
Exemplo n.º 9
0
def augment_data(dataset, dataset_labels, augmentation_factor=1, use_random_rotation=True, use_random_shear=True, use_random_shift=True, use_random_zoom=True):
        augmented_image = []
        augmented_image_labels = []

        for num in range (0, dataset.shape[0]):
                # original image:
                augmented_image.append(dataset[num])
                augmented_image_labels.append(dataset_labels[num])

                for i in range(0, augmentation_factor):

                        if use_random_rotation:
                                augmented_image.append(random_rotation(dataset[num], 20, row_axis=0, col_axis=1, channel_axis=2))
                                augmented_image_labels.append(dataset_labels[num])

                        if use_random_shear:
                                augmented_image.append(random_shear(dataset[num], 0.2, row_axis=0, col_axis=1, channel_axis=2))
                                augmented_image_labels.append(dataset_labels[num])

                        if use_random_shift:
                                augmented_image.append(random_shift(dataset[num], 0.2, 0.2, row_axis=0, col_axis=1, channel_axis=2))
                                augmented_image_labels.append(dataset_labels[num])

        s = np.arange(len(augmented_image))#Permutation
        return np.array(augmented_image)[s], np.array(augmented_image_labels)[s]
Exemplo n.º 10
0
def img_augmentation(img, num):
    img_arr_origin = img_to_array(img)
    img_list = []
    for i in range(num):
        img_arr = random_rotation(img_arr_origin,
                                  40,
                                  row_axis=0,
                                  col_axis=1,
                                  channel_axis=2,
                                  fill_mode='nearest')
        img_arr = random_shear(img_arr,
                               intensity=0.4,
                               row_axis=0,
                               col_axis=1,
                               channel_axis=2,
                               fill_mode='nearest')
        img_arr = random_zoom(img_arr,
                              zoom_range=(0.9, 1.2),
                              row_axis=0,
                              col_axis=1,
                              channel_axis=2,
                              fill_mode='nearest')
        img = array_to_img(img_arr)
        img = np.array(img)
        img_list.append(img)
        #img_arr = random_greyscale(img_arr, 0.4)
    return img_list
Exemplo n.º 11
0
def paint_text(text, w, h, rotate=False, move=False, multi_fonts=False, background=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()
        if multi_fonts:
            # Calibri Century Comic Sans  Courier New Futura Georgia
            fonts = ['Century Schoolbook', 'Courier', 'Arial', 'STIX','Tahoma','Times New Roman','Trebuchet MS',
                     'Verdana','Wide Latin','Calibri','Century','Comic Sans','Courier','New Futura','Georgia',
                     'Lucida','Lucida Console','Magneto','Mistral','URW Chancery L', 'FreeMono','DejaVue Sans Mono']
            font_slant = np.random.choice([cairo.FONT_SLANT_NORMAL,cairo.FONT_SLANT_ITALIC,cairo.FONT_SLANT_OBLIQUE])
            font_weight = np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL, cairo.FONT_WEIGHT_NORMAL])
            context.select_font_face(np.random.choice(fonts), font_slant, font_weight)
        else:
            context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        # context.set_font_size(25)
        font_size = randint(12, 42)
        context.set_font_size(font_size)
        box = context.text_extents(text)
        border_w_h = (font_size/2, font_size/2)
        # 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
        min_x = 0 #font_size/4
        min_y = 0# font_size/4
        max_shift_x = w - box[2] - border_w_h[0]
        max_shift_y = h - box[3] - border_w_h[1]

        if max_shift_x <= min_x :
            top_left_x = 10
        else:
            top_left_x = np.random.randint(min_x, int(max_shift_x))

        if move and max_shift_y > min_y + 1:
            top_left_y = np.random.randint(min_y, 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)
        if text:
            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:
        angle = randint(0, 3)
        a = image.random_rotation(a, angle * (w - top_left_x) / w + 1)
        sheer = randint(0, 3)
        a = image.random_shear(a,sheer)
    if background:
        a = speckle(a)

    return a
Exemplo n.º 12
0
 def augment_image(self, img_arr):
     img_arr = random_rotation(img_arr, 18, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
     img_arr = random_shear(img_arr, intensity=0.4, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
     img_arr = random_zoom(img_arr, zoom_range=(0.9, 2.0), row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
     if img_arr.shape[-1] == 3:
         img_arr = self.random_greyscale(img_arr, 0.4)
             
     return img_arr
Exemplo n.º 13
0
def show_preprocessed_example(X_train):
    x = X_train[5]
    row_axis, col_axis, channel_axis = 0, 1, 2

    # random rotation
    x_r = io.random_rotation(x,
                             rg=45,
                             row_axis=row_axis,
                             col_axis=col_axis,
                             channel_axis=channel_axis)

    # random horizontal shift
    x_ws = io.random_shift(x,
                           wrg=.2,
                           hrg=0,
                           row_axis=row_axis,
                           col_axis=col_axis,
                           channel_axis=channel_axis)

    # random vertical shift
    x_hs = io.random_shift(x,
                           wrg=0,
                           hrg=.2,
                           row_axis=row_axis,
                           col_axis=col_axis,
                           channel_axis=channel_axis)

    # random shear
    x_s = io.random_shear(x,
                          intensity=.2,
                          row_axis=row_axis,
                          col_axis=col_axis,
                          channel_axis=channel_axis)

    # random zoom
    x_z = io.random_zoom(x,
                         zoom_range=(.8, 1.2),
                         row_axis=row_axis,
                         col_axis=col_axis,
                         channel_axis=channel_axis)

    images = [x, x_r, x_ws, x_hs, x_s, x_z]
    titles = [
        "Original", "Rotate", "Horizontal shift", "Vertical shift", "Shear",
        "Zoom"
    ]

    plt.figure(figsize=(8, 4.5))
    for i, image in enumerate(images):
        plt.subplot(2, 3, i + 1)
        plt.xticks(())
        plt.yticks(())
        plt.title(titles[i])
        image = inputs.global_contrast_normalization(image)
        image = inputs.minmax_normalization(image)
        plt.imshow(image)

    plt.show()
Exemplo n.º 14
0
def transform(x, seed=0):
    np.random.seed(seed)
    img = image_processing.random_rotation(x, 0.2)
    img = image_processing.random_shear(img, 30)
    img = image_processing.random_zoom(img, (0.5, 1.1))
    if np.random.rand() >= 0.5:
        img = np.fliplr(img)

    return img
 def augment_data(self, x):
     
     x = random_shift(x, self.width_shift_range, self.height_shift_range, 
                      row_axis=0, col_axis = 1, channel_axis = 2)
     x = random_rotation(x, self.max_rotation, 
                         row_axis = 0, col_axis = 1, channel_axis = 2)
     x = random_shear(x, self.shear, row_axis = 0, col_axis = 1, channel_axis = 2)
     x = random_zoom(x, self.zoom_range, row_axis = 0, col_axis = 1, channel_axis = 2)
     
     return x
Exemplo n.º 16
0
 def test_img_transforms(self):
     x = np.random.random((3, 200, 200))
     _ = preprocessing_image.random_rotation(x, 20)
     _ = preprocessing_image.random_shift(x, 0.2, 0.2)
     _ = preprocessing_image.random_shear(x, 2.)
     _ = preprocessing_image.random_zoom(x, (0.5, 0.5))
     _ = preprocessing_image.apply_channel_shift(x, 2, 2)
     _ = preprocessing_image.apply_affine_transform(x, 2)
     with self.assertRaises(ValueError):
         preprocessing_image.random_zoom(x, (0, 0, 0))
     _ = preprocessing_image.random_channel_shift(x, 2.)
Exemplo n.º 17
0
def data_augmentor(x,mode,row_axis=1,col_axis=0,channel_axis=-1):
    #temp = [0,0,0,0,0,0]
    #for i in range(len(mode)):
	#temp[6-i-1] = int(str(mode[-i-1]))	
	
    if int(mode[0]):
        x = flip_axis(x, 0)
        #print x.shape
 	
    if int(mode[1]):
        x = flip_axis(x,1)
        #print x.shape

    if int(mode[2]):
        x = random_rotation(x,360, row_axis, col_axis, channel_axis,fill_mode='reflect')
        x = np.swapaxes(x,0,1)
        x = np.swapaxes(x,1,2)
        '''M = cv2.getRotationMatrix2D((x.shape[1],x.shape[0]),np.random.randint(360),1)   #last argument is scale in rotation
        x = cv2.warpAffine(x,M,(x.shape[1],x.shape[0]), borderMode=cv2.BORDER_REFLECT)'''
        #print x.shape
		#del M

    if int(mode[3]):
        x = random_shift(x, 0.1,0.1, row_axis, col_axis, channel_axis,fill_mode='reflect')
        x = np.swapaxes(x,0,1)
        x = np.swapaxes(x,1,2)
        '''M = np.float32([[1,0,np.random.randint(x.shape[0])],[0,1,np.random.randint(x.shape[1])]])
        x = cv2.warpAffine(x,M,(x.shape[1],x.shape[0]), borderMode = cv2.BORDER_REFLECT)'''
        #print x.shape
        #del M

    if int(mode[4]):
        x = random_shear(x, 1, row_axis, col_axis, channel_axis,fill_mode='reflect')
        x = np.swapaxes(x,0,1)
        x = np.swapaxes(x,1,2)
        '''pts1 = np.float32([[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])],[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])],[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])]])
        pts2 = np.float32([[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])],[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])],[np.random.randint(x.shape[0]),np.random.randint(x.shape[1])]])
        M = cv2.getAffineTransform(pts1,pts2)
        x = cv2.warpAffine(x,M,(x.shape[1],x.shape[0]),borderMode = cv2.BORDER_REFLECT)'''
        #print x.shape
        #del M
		#del pts1
		#del pts2

    if int(mode[5]):
        x = random_zoom(x, (1.1,1.1), row_axis, col_axis, channel_axis,fill_mode='reflect')
        x = np.swapaxes(x,0,1)
        x = np.swapaxes(x,1,2)
        #print x.shape
        

    return x
Exemplo n.º 18
0
    def test_random_transforms(self):
        x = np.random.random((2, 28, 28))
        assert image.random_rotation(x, 45).shape == (2, 28, 28)
        assert image.random_shift(x, 1, 1).shape == (2, 28, 28)
        assert image.random_shear(x, 20).shape == (2, 28, 28)
        assert image.random_zoom(x, (5, 5)).shape == (2, 28, 28)
        assert image.random_channel_shift(x, 20).shape == (2, 28, 28)

        # Test get_random_transform with predefined seed
        seed = 1
        generator = image.ImageDataGenerator(
            rotation_range=90.,
            width_shift_range=0.1,
            height_shift_range=0.1,
            shear_range=0.5,
            zoom_range=0.2,
            channel_shift_range=0.1,
            brightness_range=(1, 5),
            horizontal_flip=True,
            vertical_flip=True)
        transform_dict = generator.get_random_transform(x.shape, seed)
        transform_dict2 = generator.get_random_transform(x.shape, seed * 2)
        assert transform_dict['theta'] != 0
        assert transform_dict['theta'] != transform_dict2['theta']
        assert transform_dict['tx'] != 0
        assert transform_dict['tx'] != transform_dict2['tx']
        assert transform_dict['ty'] != 0
        assert transform_dict['ty'] != transform_dict2['ty']
        assert transform_dict['shear'] != 0
        assert transform_dict['shear'] != transform_dict2['shear']
        assert transform_dict['zx'] != 0
        assert transform_dict['zx'] != transform_dict2['zx']
        assert transform_dict['zy'] != 0
        assert transform_dict['zy'] != transform_dict2['zy']
        assert transform_dict['channel_shift_intensity'] != 0
        assert (transform_dict['channel_shift_intensity'] !=
                transform_dict2['channel_shift_intensity'])
        assert transform_dict['brightness'] != 0
        assert transform_dict['brightness'] != transform_dict2['brightness']

        # Test get_random_transform without any randomness
        generator = image.ImageDataGenerator()
        transform_dict = generator.get_random_transform(x.shape, seed)
        assert transform_dict['theta'] == 0
        assert transform_dict['tx'] == 0
        assert transform_dict['ty'] == 0
        assert transform_dict['shear'] == 0
        assert transform_dict['zx'] == 1
        assert transform_dict['zy'] == 1
        assert transform_dict['channel_shift_intensity'] is None
        assert transform_dict['brightness'] is None
Exemplo n.º 19
0
    def test_random_transforms(self):
        x = np.random.random((2, 28, 28))
        assert image.random_rotation(x, 45).shape == (2, 28, 28)
        assert image.random_shift(x, 1, 1).shape == (2, 28, 28)
        assert image.random_shear(x, 20).shape == (2, 28, 28)
        assert image.random_zoom(x, (5, 5)).shape == (2, 28, 28)
        assert image.random_channel_shift(x, 20).shape == (2, 28, 28)

        # Test get_random_transform with predefined seed
        seed = 1
        generator = image.ImageDataGenerator(
            rotation_range=90.,
            width_shift_range=0.1,
            height_shift_range=0.1,
            shear_range=0.5,
            zoom_range=0.2,
            channel_shift_range=0.1,
            brightness_range=(1, 5),
            horizontal_flip=True,
            vertical_flip=True)
        transform_dict = generator.get_random_transform(x.shape, seed)
        transform_dict2 = generator.get_random_transform(x.shape, seed * 2)
        assert transform_dict['theta'] != 0
        assert transform_dict['theta'] != transform_dict2['theta']
        assert transform_dict['tx'] != 0
        assert transform_dict['tx'] != transform_dict2['tx']
        assert transform_dict['ty'] != 0
        assert transform_dict['ty'] != transform_dict2['ty']
        assert transform_dict['shear'] != 0
        assert transform_dict['shear'] != transform_dict2['shear']
        assert transform_dict['zx'] != 0
        assert transform_dict['zx'] != transform_dict2['zx']
        assert transform_dict['zy'] != 0
        assert transform_dict['zy'] != transform_dict2['zy']
        assert transform_dict['channel_shift_intensity'] != 0
        assert (transform_dict['channel_shift_intensity'] !=
                transform_dict2['channel_shift_intensity'])
        assert transform_dict['brightness'] != 0
        assert transform_dict['brightness'] != transform_dict2['brightness']

        # Test get_random_transform without any randomness
        generator = image.ImageDataGenerator()
        transform_dict = generator.get_random_transform(x.shape, seed)
        assert transform_dict['theta'] == 0
        assert transform_dict['tx'] == 0
        assert transform_dict['ty'] == 0
        assert transform_dict['shear'] == 0
        assert transform_dict['zx'] == 1
        assert transform_dict['zy'] == 1
        assert transform_dict['channel_shift_intensity'] is None
        assert transform_dict['brightness'] is None
Exemplo n.º 20
0
def generator(samples, batch_size=32):
    """
	This function defination generates training features and lables for given samples and batch size
	samples - list of lists that stores images, steering angles, speed etc.,

	parse through the list and read images (center, left and right camera) and steering angles with (0, 0.2, -0.2) corrections

	images are augmented using following methods
	- image flip
	- addition of noise
	- random shear
	"""
    num_samples = len(samples)
    while 1:  # Loop forever so the generator never terminates
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset + batch_size]

            images = []
            angles = []
            for batch_sample in batch_samples:

                for i, correction in zip(range(3), corrections):

                    # read image and steering angle + correction (depending upon center, left or right camera image)
                    name = './data/IMG/' + batch_sample[i].split('\\')[-1]
                    image = mpimg.imread(name)
                    angle = float(batch_sample[3]) + correction

                    # original image
                    images.append(image)
                    angles.append(angle)

                    # augmentation
                    # flip image
                    images.append(np.fliplr(image))
                    angles.append(angle * -1.0)

                    # add noise
                    images.append(image - 0.35)
                    angles.append(angle)

                    # random shear
                    images.append(random_shear(image, np.random.randint(10)))
                    angles.append(angle)

            # trim image to only see section with road
            X_train = np.array(images)
            y_train = np.array(angles)

            yield shuffle(X_train, y_train)
Exemplo n.º 21
0
def random_transform(x,
                     seed=42,
                     rotation_range=30,
                     width_shift_range=.1,
                     height_shift_range=.1,
                     shear_range=0.2,
                     zoom_range=(.9, 1.1),
                     row_axis=0,
                     col_axis=1,
                     channel_axis=2):
    '''
    https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py
    :param x: 3D input image 
    :param seed: 
    :param rotation_range: Rotation range, in degrees.
    :param width_shift_range: Width shift range, as a float fraction of the width.
    :param height_shift_range: Height shift range, as a float fraction of the height.
    :param shear_range: Transformation intensity
    :param zoom_range: Tuple of floats; zoom range for width and height.
    :return: transformed image that has the same shape
    '''
    random.seed(seed)

    x = io.random_rotation(x,
                           rotation_range,
                           row_axis=row_axis,
                           col_axis=col_axis,
                           channel_axis=channel_axis)

    x = io.random_shift(x,
                        width_shift_range,
                        height_shift_range,
                        row_axis=row_axis,
                        col_axis=col_axis,
                        channel_axis=channel_axis)

    x = io.random_shear(x,
                        shear_range,
                        row_axis=row_axis,
                        col_axis=col_axis,
                        channel_axis=channel_axis)

    x = io.random_zoom(x,
                       zoom_range,
                       row_axis=row_axis,
                       col_axis=col_axis,
                       channel_axis=channel_axis)

    return x
    def original_process(original_img):
        # 随机旋转
        process_image = image.random_rotation(original_img,
                                              180,
                                              row_axis=0,
                                              col_axis=1,
                                              channel_axis=2,
                                              fill_mode='constant',
                                              cval=0)

        # 随机投影变换
        h, w = IMAGE_SIZE[0], IMAGE_SIZE[1]

        ratio = np.random.normal(0.125, 0.075)
        if ratio > 0.2:
            ratio = 0.2
        elif ratio < 0.05:
            ratio = 0.05
        pts1 = np.float32([[0, 0], [w, 0], [0, h], [w, h]])
        pts2 = np.float32([[0, 0], [w, ratio * h], [0, h],
                           [w, (1 - ratio) * h]])

        M = cv2.getPerspectiveTransform(pts1, pts2)
        process_image = cv2.warpPerspective(process_image, M, (w, h))

        # 随机平移
        process_image = image.random_shift(process_image,
                                           0.3,
                                           0.3,
                                           row_axis=0,
                                           col_axis=1,
                                           channel_axis=2,
                                           fill_mode='constant',
                                           cval=0)

        # 随机拉伸
        ratio = np.random.normal(35, 10)
        if ratio > 45:
            ratio = 45
        elif ratio < 25:
            ratio = 25
        process_image = image.random_shear(process_image,
                                           ratio,
                                           row_axis=0,
                                           col_axis=1,
                                           channel_axis=2)

        return process_image
Exemplo n.º 23
0
def data_augmentation(imgs, c):
    # imgs - list of 2D images

    def sequence_change(imgs, change_faktor):
        # change_faktor: float from 0 to 0.4
        l = random.randint(0, int(len(imgs) * change_faktor))
        r = random.randint(0, int(len(imgs) * change_faktor)) + 1
        return imgs[l:-r]

    imgs = sequence_change(imgs, c.sequence_change)
    imgs = np.stack(imgs)
    imgs = resample_imgs(imgs, c.n_frames)
    imgs = image.random_zoom(imgs, c.zoom_range)
    imgs = image.random_rotation(imgs, c.random_rotation)
    imgs = image.random_shear(imgs, c.random_shear)
    return imgs
Exemplo n.º 24
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 = 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)
    #这个elastic_transform导致显示的东西像屎一样,干脆不要了
    if np.random.randint(0, 10) == 7:
      img_and_mask = 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 = 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 = 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:3]
      batch_of_masks[i, ...] = np.expand_dims(img_and_mask[...,3],axis=-1)


    # 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)
    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
        X = []
        Y = []
        for file_name, y in zip(batch_x, batch_y):
            img = create_image(file_name, self.shape)

            if img is None:
                continue

            img = self.horizontal_flip(img)
            img = random_rotation(img, 0.20)
            img = random_shift(img, 0.10, 0.10)
            img = random_shear(img, 0.10)
            X.append(img)
            Y.append(y)
        X = np.array(X).astype('float32') / 255.0
        return np.array(X), np.array(Y)
def augm(array):
    flag = int(random.randint(0, 6) / 7)
    if flag == 0:
        a = array
    if flag == 1:
        a = image.random_shift(array, -0.2, 0.2)
    if flag == 2:
        a = image.random_shear(array, 80)
    if flag == 3:
        a = image.random_zoom(array, (0.7, 1))
    if flag == 4:
        a = img_pca(array)
    if flag == 5:
        a = image.random_brightness(array, (0.05, 0.8))
    if flag == 6:
        a = image.random_rotation(array, 180)
    # if flag == 7:
    #     a = image.random_channel_shift(array, 0.05)
    return a
def augmentation_pipeline(img_arr):
    img_arr = random_rotation(img_arr,
                              18,
                              row_axis=0,
                              col_axis=1,
                              channel_axis=2,
                              fill_mode='nearest')
    img_arr = random_shear(img_arr,
                           intensity=0.4,
                           row_axis=0,
                           col_axis=1,
                           channel_axis=2,
                           fill_mode='nearest')
    img_arr = random_zoom(img_arr,
                          zoom_range=(0.9, 2.0),
                          row_axis=0,
                          col_axis=1,
                          channel_axis=2,
                          fill_mode='nearest')
    return img_arr
Exemplo n.º 28
0
def argumentation(imgs):
    print(np.shape(imgs))
    r_imgs = [
        random_rotation(imgs,
                        30,
                        row_axis=1,
                        col_axis=2,
                        channel_axis=3,
                        fill_mode='nearest') * 255 for _ in range(5)
    ]
    s_imgs = [
        random_shear(imgs,
                     intensity=0.4,
                     row_axis=1,
                     col_axis=2,
                     channel_axis=3,
                     fill_mode='nearest') * 255 for _ in range(5)
    ]
    sh_imgs = [
        random_shift(imgs,
                     wrg=0.1,
                     hrg=0.3,
                     row_axis=1,
                     col_axis=2,
                     channel_axis=3,
                     fill_mode='nearest') * 255 for _ in range(5)
    ]
    z_imgs = [
        random_zoom(imgs,
                    zoom_range=(1.5, 0.7),
                    row_axis=1,
                    col_axis=2,
                    channel_axis=3,
                    fill_mode='nearest') * 255 for _ in range(5)
    ]
    imgs = np.append(imgs, r_imgs)
    imgs = np.append(imgs, s_imgs)
    imgs = np.append(imgs, sh_imgs)
    imgs = np.append(imgs, z_imgs)
    return imgs
Exemplo n.º 29
0
def random_transform(x, seed=42, rotation_range=30,
                     width_shift_range=.1, height_shift_range=.1,
                     shear_range=0.2, zoom_range = (.9, 1.1),
                     row_axis=0, col_axis=1, channel_axis=2):
    '''
    https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py
    :param x: 3D input image 
    :param seed: 
    :param rotation_range: Rotation range, in degrees.
    :param width_shift_range: Width shift range, as a float fraction of the width.
    :param height_shift_range: Height shift range, as a float fraction of the height.
    :param shear_range: Transformation intensity
    :param zoom_range: Tuple of floats; zoom range for width and height.
    :return: transformed image that has the same shape
    '''
    random.seed(seed)

    x = io.random_rotation(x, rotation_range, 
                           row_axis=row_axis, 
                           col_axis=col_axis, 
                           channel_axis=channel_axis)

    x = io.random_shift(x, width_shift_range, 
                        height_shift_range, 
                        row_axis=row_axis, 
                        col_axis=col_axis, 
                        channel_axis=channel_axis)

    x = io.random_shear(x, shear_range,
                       row_axis=row_axis, 
                        col_axis=col_axis, 
                        channel_axis=channel_axis)

    x = io.random_zoom(x, zoom_range, 
                       row_axis=row_axis, 
                       col_axis=col_axis, 
                       channel_axis=channel_axis)
    
    return x
Exemplo n.º 30
0
def image_augment(arr):
    """
    :param arr: array of a image, 3D. [array]
    :return:
    """
    # 旋转 微调
    x = image.random_rotation(arr,
                              rg=5,
                              row_axis=0,
                              col_axis=1,
                              channel_axis=2,
                              fill_mode='nearest')
    # 平移 上下平移 左右平移 微调
    x = image.random_shift(x,
                           wrg=0.1,
                           hrg=0.1,
                           row_axis=0,
                           col_axis=1,
                           channel_axis=2,
                           fill_mode='nearest')
    # 随机空间剪切
    x = image.random_shear(x,
                           intensity=10,
                           row_axis=0,
                           col_axis=1,
                           channel_axis=2,
                           fill_mode='nearest')
    # 随机放缩
    x = image.random_zoom(x,
                          zoom_range=(0.75, 1.25),
                          row_axis=0,
                          col_axis=1,
                          channel_axis=2,
                          fill_mode='nearest')
    # 随机亮度调整
    x = image.random_brightness(x, brightness_range=(0.75, 1.25))

    return x
Exemplo n.º 31
0
def generator(samples, batch_size=32):
    num_samples = len(samples)

    while 1:
        shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset + batch_size]

            images = []
            angles = []

            for batch_sample in batch_samples:

                for i, correction in zip(range(3), angle_correction):

                    name = './data/udacity/IMG/' + batch_sample[i].split(
                        '/')[-1]
                    image = mpimg.imread(name)
                    angle = float(batch_sample[3]) + correction

                    images.append(image)
                    angles.append(angle)

                    #data augumentation:

                    #flip image
                    images.append(np.fliplr(image))
                    angles.append(-angle)

                    # data agumentation: random shear
                    images.append(random_shear(image, np.random.randint(32)))
                    angles.append(angle)

            # convert to np array
            X_train = np.array(images)
            y_train = np.array(angles)

            yield shuffle(X_train, y_train)
Exemplo n.º 32
0
def image_augmentation(img):
    '''
    Arguments:
        img = list of np.arrays of images
    Returns:
        list of arrays after transformations
    '''
    rotationSize = 15
    zoomRangeWidth = .8
    zoomRangeHeight = 1.1
    intensity = 15    
    widthRange = .1
    heightRange = .1

    img = [kim.random_rotation(item, rotationSize, row_axis=0, col_axis=1,
                channel_axis=2, fill_mode='nearest') for item in img]
    img = [kim.random_shift(item, wrg=widthRange, hrg=heightRange,row_axis=0,
                col_axis=1, channel_axis=2, fill_mode='nearest') for item in img]
    img = [kim.random_zoom(item, zoom_range=(zoomRangeWidth,zoomRangeHeight),
                row_axis=0,col_axis=1, channel_axis=2, fill_mode='nearest') for item in img]
    img = [kim.random_shear(item, intensity=intensity,
                row_axis=0,col_axis=1, channel_axis=2, fill_mode='nearest') for item in img]
    
    return img
Exemplo n.º 33
0
def generator(samples, batch_size=32):
    num_samples = len(samples)

    while 1:
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset + batch_size]

            images = []
            angles = []

            for batch_sample in batch_samples:

                for i, correction in zip(range(3), angle_offsets):

                    name = './data/all/IMG/' + batch_sample[i].split('/')[-1]
                    #print (name)
                    image = mpimg.imread(name)
                    angle = float(batch_sample[3]) + correction

                    #  append original image
                    images.append(image)
                    angles.append(angle)

                    # data agumentation: flip image (invert angle)
                    images.append(np.fliplr(image))
                    angles.append(angle * -1.0)

                    # data agumentation: random shear
                    images.append(random_shear(image, np.random.randint(32)))
                    angles.append(angle)

            # convert to np array
            X_train = np.array(images)
            y_train = np.array(angles)
            # Shuffle the data
            yield shuffle(X_train, y_train)