コード例 #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
コード例 #2
0
def augmentation_img(img1,img2):
    '''
    图像增强(旋转/翻转/缩放/亮度调整/噪声)
    '''
    aug = np.random.randint(2,size=5)
    img_a1, img_a2 = np.copy(img1) , np.copy(img2)
    
    if aug[0]:# 旋转
        angle  = np.random.choice((1,2,-1,-2))
        img_a1 = np.rot90(img_a1,angle)
        img_a2 = np.rot90(img_a2,angle)
    if aug[1]:# 对称翻转 
        x_or_y = np.random.choice((0,1))
        img_a1 = np.flip(img_a1,x_or_y)
        img_a2 = np.flip(img_a2,x_or_y)
    if aug[2]:# 缩放
        zoom = np.random.uniform(0.8,1.2)
        img_a1 = image.random_zoom(img_a1,(zoom,zoom),0,1,2) 
        img_a2 = image.random_zoom(img_a2,(zoom,zoom),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,8,(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
コード例 #3
0
ファイル: model.py プロジェクト: sjjdd/fxgan
def tran_one(img, d=15):
    degree = np.random.randint(-d, d)
    img = iprocess.random_brightness(img, (0.5, 1.5))
    if np.random.rand() >= 0.5:
       img = np.fliplr(img)
 
    return iprocess.apply_affine_transform(img, degree)
コード例 #4
0
 def __getitem__(self, index):
     data = self.data[index * self.batch_size: (index + 1) * self.batch_size]
     words = []
     images = []
     nums = []
     for i in range(self.batch_size):
         img_path = data[i][0][0]
         word = data[i][1][0]
         word = self.__word_process(word)
         img = get_img("{}/{}".format(self.path, img_path), self.width, self.height)
         img = random_brightness(img[:, :, np.newaxis], [0.1, 1.5])
         images.append(img[:, :, 0])
         words.append(word)
         nums.append(len(word))
     words = pad_sequences(words, maxlen=self.max_string_length, truncating="post", padding="post")
     label_input = np.array(nums).reshape([-1, 1])
     images = np.array(images)
     return {"img": images[:, :, :, np.newaxis], "label_input": label_input,
             "y_input": words}, np.zeros([self.batch_size, 1])
コード例 #5
0
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
コード例 #6
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
コード例 #7
0
    def random_transform(self, x, seed=None):
        """Randomly augments a single image tensor.
        Rotation and image flips are applied to both satellite and roadmap.
        Channel shifts and brightness changes are applied to satellite only.
        # Arguments
            x: 3D tensor, single image.
            seed: Random seed.
        # Returns
            A randomly 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

        if seed is not None:
            np.random.seed(seed)

        # Use composition of homographies
        # to generate final transform that needs to be applied
        if self.rotation_range:
            theta = np.deg2rad(
                np.random.uniform(-self.rotation_range, self.rotation_range))
        else:
            theta = 0

        transform_matrix = None
        if theta != 0:
            rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                        [np.sin(theta),
                                         np.cos(theta), 0], [0, 0, 1]])
            transform_matrix = rotation_matrix

        if transform_matrix is not None:
            h, w = x.shape[img_row_axis], x.shape[img_col_axis]
            transform_matrix = transform_matrix_offset_center(
                transform_matrix, h, w)
            x = apply_transform(x,
                                transform_matrix,
                                img_channel_axis,
                                fill_mode=self.fill_mode,
                                cval=self.cval)

        if self.horizontal_flip:
            if np.random.random() < 0.5:
                x = flip_axis(x, img_col_axis)

        if self.vertical_flip:
            if np.random.random() < 0.5:
                x = flip_axis(x, img_row_axis)

        x = np.moveaxis(x, img_channel_axis, -1)
        x_satellite = x[:, :, 0:3]
        x_roadmap = x[:, :, 3:6]

        if self.channel_shift_range != 0:
            x_satellite = random_channel_shift(x_satellite,
                                               self.channel_shift_range,
                                               img_channel_axis)

        if self.brightness_range is not None:
            x_satellite = random_brightness(x_satellite, self.brightness_range)

        x = np.concatenate([x_satellite, x_roadmap], axis=-1)
        x = np.moveaxis(x, -1, img_channel_axis)

        return x