Example #1
0
 def shift_images(self):
     for i in range(self.x.shape[0]):
         random_shift(self.x[i],
                      self.max_shift,
                      self.max_shift,
                      row_axis=0,
                      col_axis=1,
                      channel_axis=2)
Example #2
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()
def Shift(image, width_rg=0.1, height_rg=0.1, u=0.5, v=1.0):

    if v < u:
        image = prep.random_shift(image, wrg=width_rg, hrg=height_rg, 
                                  row_axis=0, col_axis=1, channel_axis=2)

    return image
Example #4
0
def load_images(C_or_R, paths, labels, batch_size=32, eval=False):
    batch_n = 0
    while True:
        batch_d = []
        batch_l = []
        for i in range(batch_size):
            if batch_n * batch_size + i > len(paths) - 1:
                batch_n = 0
            path = paths[batch_n * batch_size + i]
            img = image.load_img(path, target_size=(IMAGE_SIZE, IMAGE_SIZE))
            x = image.img_to_array(img) / 255
            if not eval:
                x = image.random_rotation(x, 20)
                x = image.random_shift(x, 0.1, 0.1)
                if np.random.random() < 0.5:
                    x = image.flip_axis(x, 1)
            y = labels[batch_n * batch_size + i]
            batch_d.append(x)
            batch_l.append(y)
        batch_d = np.array(batch_d).reshape(
            (batch_size, IMAGE_SIZE, IMAGE_SIZE, 3))
        if C_or_R == CLASSIFY:
            batch_l = np.array(batch_l).reshape((batch_size, 8))
        else:
            batch_l = np.array(batch_l).reshape((batch_size, 2))
        yield (batch_d, batch_l)
        batch_n += 1
Example #5
0
    def next(self):

        index_array, current_index, current_batch_size = next(
            self.index_generator)

        lungs = np.ndarray([current_batch_size, 1, img_width, img_height])
        masks = np.ndarray([current_batch_size, 1, img_width, img_height])
        for i in range(0, current_batch_size):
            # get image data and file
            ann = self.df.iloc[index_array[i]]
            filename = self.path + ann['seriesuid'] + '.mhd'
            image, origin, spacing = load_itk_image(filename)

            # get voxelCoords
            worldCoord = np.asarray(
                [ann['coordZ'], ann['coordY'], ann['coordX']])
            voxelCoord = worldToVoxelCoord(worldCoord, origin, spacing)

            # segment lungs and make mask
            lungs[i, 0] = segment_lungs(image[voxelCoord[0]])
            masks[i,
                  0] = make_mask((voxelCoord[2], voxelCoord[1]),
                                 ann['diameter_mm'] + 5, img_width, img_height)

            img_stack = np.stack([lungs[i, 0], masks[i, 0]])
            img_stack = random_rotation(img_stack, self.rot_range)
            img_stack = random_shift(img_stack, self.shift_range,
                                     self.shift_range)
            lungs[i, 0] = img_stack[0]
            masks[i, 0] = img_stack[1]

        return lungs, masks
Example #6
0
def augment(im_array):
    #flip image
    if np.random.uniform(0, 1) > 0.5:
        im_array = np.fliplr(im_array)

    #FINAL add noise
    im_array = random_rotation(im_array,
                               rg=360,
                               row_axis=0,
                               col_axis=1,
                               channel_axis=2,
                               fill_mode='nearest')
    im_array = random_shift(im_array,
                            wrg=0.1,
                            hrg=0.3,
                            row_axis=0,
                            col_axis=1,
                            channel_axis=2,
                            fill_mode='nearest')
    im_array = random_zoom(im_array,
                           zoom_range=(1, 1.2),
                           row_axis=0,
                           col_axis=1,
                           channel_axis=2,
                           fill_mode='nearest')
    im_array = random_greyscale(im_array, 0.4)

    return im_array
    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
Example #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)
Example #9
0
def get_generator(f, params):
    while True:
        vals = shuffle(f)
        for imagename, age in zip(vals['Image Index'], vals['Patient Age']):
            filename = join('images_resized', imagename)
            x = cv2.imread(filename)
            x = cv2.resize(x, (params['size'], params['size']))
            x = np.expand_dims(x[:, :, 0], axis=-1)
            x = x.astype(np.float32)
            x -= 126.95534595
            x /= 63.95665607

            if params['flip_horizontal']:
                if np.random.rand() < 0.5:
                    x = image.flip_axis(x, axis=1)

            if params['rotation'] > 0:
                x = image.random_rotation(x, rg=params['rotation'])

            x = image.random_shift(x,
                                   wrg=params['shift_w'],
                                   hrg=params['shift_h'])

            y = age / 100

            yield x, y
def load_image(paths: np.ndarray, size: int, input_size,
               is_augmentation: bool):
    images = [cv2.imread('{}'.format(img_path)) for img_path in paths]
    images = [
        cv2.resize(image, (size, size), interpolation=cv2.INTER_CUBIC)
        for image in images
    ]
    if input_size[3] == 1:
        images = [cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) for image in images]
        images = np.expand_dims(images, -1)
        return np.array(images, dtype='uint8')

    else:
        # data augmentation
        if is_augmentation:
            images = [
                image_augmentation.img_to_array(image) for image in images
            ]
            images = [
                image_augmentation.random_rotation(image, rg=10)
                for image in images
            ]
            images = [
                image_augmentation.random_shift(image, wrg=0.1, hrg=0.1)
                for image in images
            ]
            images = [
                image_augmentation.random_zoom(image, zoom_range=[0.1, 0.3])
                for image in images
            ]
            images = [
                image_augmentation.flip_axis(image, axis=0) for image in images
            ]
        return np.array(images, dtype='uint8')
Example #11
0
def get_generator(f, params, data_augmentation=True):
    while True:
        vals = shuffle(f)
        for imagename, age in zip(vals['Image Index'], vals['Patient Age']):
            filename = join('images_resized', imagename)
            x = cv2.imread(filename)
            x = cv2.resize(x, (settings.IMAGE_SIZE, settings.IMAGE_SIZE))
            x = np.expand_dims(x[:, :, 0], axis=-1)
            x = x.astype(np.float32)
            x -= 126.95534595
            x /= 63.95665607
            x -= np.mean(x)
            x /= np.std(x)

            if data_augmentation:
                if params['flip_horizontal']:
                    if np.random.rand() < 0.5:
                        x = image.flip_axis(x, axis=1)

                rotation = params['rotation']
                if rotation > 0:
                    x = image.random_rotation(x, rg=rotation)

                shift_w = params['shift_w']
                shift_h = params['shift_h']
                if shift_h > 0 or shift_w > 0:
                    x = image.random_shift(x, wrg=shift_w, hrg=shift_h)

            y = age / 100

            # cv2.imshow('', x[:, :, 0])
            # cv2.waitKey()

            yield x, y
Example #12
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)
Example #13
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]
Example #14
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)
Example #15
0
def shift_image(img_arr, widthRange=0.1, heightRange=0.3):
    return kim.random_shift(img_arr,
                            wrg=widthRange,
                            hrg=heightRange,
                            row_axis=0,
                            col_axis=1,
                            channel_axis=2,
                            fill_mode='nearest')
Example #16
0
def distorted_image(image):
    image1 = random_rotation(
        image, 15, row_axis=0, col_axis=1, channel_axis=2)
    image2 = random_zoom(
        image1, (0.8, 1.2), row_axis=0, col_axis=1, channel_axis=2)
    image3 = random_shift(
        image2, 0.05, 0.05, row_axis=0, col_axis=1, channel_axis=2)
    return image3
Example #17
0
def rnd_shift(img, width_shift, horizontal_shift):
    return random_shift(img,
                        wrg=width_shift,
                        hrg=horizontal_shift,
                        row_axis=0,
                        col_axis=1,
                        channel_axis=2,
                        fill_mode='nearest')
def process_image(name):

    image = mpimg.imread(name)
    # random shift image
    image = random_shift(image, 0, 0.2, 0, 1, 2)
    # random birghtness adjustment
    image = randomize_brightness(image)

    return image
 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
Example #20
0
def generate_enhanced_dataset(root, destination, images_count, break_range=10, blur=True, crop=0, clahe=True):
    print('Generating enhanced dataset')
    do_random_shift = True
    rnd_lines = True

    data = load_data(root, break_range)

    # load images
    for idx in range(0, len(data)):
        dd = data[idx]
        img = img_to_array(load_image_from_file(os.path.join(dd[3],dd[4]), clahe))
        data[idx,5] = img

    try:
        os.mkdir(destination)
    except:
        pass

    cnt = 0
    # Generate enhanced images
    while cnt < images_count:
        for idx in range(0, len(data)):
            img = data[idx, 5]

            if do_random_shift:
                img = random_shift(img, 0.1, 0.0, row_axis=0, col_axis=1, channel_axis=2) 
            if rnd_lines:
                if random.randint(0,3) == 3:
                    lines_img = np.zeros((120,160), dtype='float32')
                    rnd1 = random.randint(0, 120)
                    rnd2 = random.randint(0, 120)
                    cv2.line(lines_img,(0,rnd1+40),(160,rnd2+40),(256,256,256),2, cv2.LINE_AA)
                    cv2.line(lines_img,(0,rnd1),(160,rnd2),(256,256,256),2, cv2.LINE_AA)
                    cv2.line(lines_img,(0,rnd1-40),(160,rnd2-40),(256,256,256),2, cv2.LINE_AA)

                    img = np.array(cv2.addWeighted(img,0.5,lines_img,0.10,0, dtype=1), dtype='uint8')
            # last transformation
            if blur:
                img = np.array(cv2.bilateralFilter(img,9,75,75), dtype='uint8').reshape(120,160,1)
            # step3
            if crop:
                img[crop:120] = 0

            # save image
            img_filename = '{:08d}_cam-image_array_.jpg'.format(cnt)
            save_img(os.path.join(destination, img_filename), img)
            # save data
            ff = open(os.path.join(destination, 'record_{:08d}.json'.format(cnt)), "w")
            json.dump({ "user/mode": "user", "cam/image_array": img_filename, "user/throttle": data[idx, 1], "user/angle": data[idx, 2] }, ff)
            ff.close()

            cnt = cnt + 1
            if cnt > images_count:
                break
    print('Generate enhanced dataset done')
Example #21
0
def Shift(image, prob, width_rg=0.1, height_rg=0.1):

    if random() < prob:
        image = prep.random_shift(image,
                                  wrg=width_rg,
                                  hrg=height_rg,
                                  row_axis=0,
                                  col_axis=1,
                                  channel_axis=2)

    return image
Example #22
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.)
Example #23
0
def random_enhance(img_arr, lab_arr):
    sample_num = img_arr.shape[0]
    for i in range(sample_num):
        img = img_arr[i, :, :, 0]
        label = lab_arr[i, :, :, 0]
        merge = np.array((img, label))
        merge = random_rotation(merge, 20)
        merge = random_shift(merge, 0.2, 0.2)
        img_arr[i, :, :, :] = np.expand_dims(merge[0, :, :], 2)
        lab_arr[i, :, :, :] = np.expand_dims(merge[1, :, :], 2)
    return img_arr, lab_arr
def load_training():
    labels_dict = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,
                   'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,
                   'Z':25,'space':26,'del':27,'nothing':28}
    data = []
    targets = []
    size = 64, 64
    print("reading in training data...")
    for folder in os.listdir("set1/asl_alphabet_train/"):
        print("First set; reading in training data for: " + folder)
        count = 0
        for image in os.listdir("set1/asl_alphabet_train/" + folder):
            count += 1
            img = cv2.imread("set1/asl_alphabet_train/" + folder + "/" + image)
            img = cv2.resize(img, size)
            #randomization goes here
            img = random_shift(img, .2, .2, fill_mode = "reflect", row_axis = 0, col_axis = 1, channel_axis = 2)
            img = random_zoom(img, zoom_range=[.85, .85], row_axis = 0, col_axis = 1, channel_axis = 2)
            img = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
            img = convertBW(img)
            data.append(img)
            targets.append(labels_dict[folder])
        print("First set; there were " + str(count) + "images in folder " + folder)
    for folder in os.listdir("set2/ASLDataSet/"):
        print("Second set; reading in training data for: " + folder)
        count = 0
        for image in os.listdir("set2/ASLDataSet/" + folder):
            count += 1
            img = cv2.imread("set2/ASLDataSet/" + folder + "/" + image)
            img = cv2.resize(img, size)
            #randomization goes here
            img = random_shift(img, .2, .2, fill_mode = "reflect", row_axis = 0, col_axis = 1, channel_axis = 2)
            img = random_zoom(img, zoom_range=[.85, .85], row_axis = 0, col_axis = 1, channel_axis = 2)
            img = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
            img = convertBW(img)
            data.append(img)
            targets.append(labels_dict[folder])
        print("Second set; there were " + str(count) + "images in folder " + folder)
    data = np.array(data)
    return data, targets
Example #25
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
    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
Example #27
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
Example #28
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
def generator_train(
        samples,
        batch_size=32,
        wrg=0.01,  # shift range of width for random_shift 
        hrg=0.0,  # shift range of height for random_shift 
        rg=5,  # Angle range for random_rotation
        ang_n_l=0.01  # Angle noise level
):

    num_samples = len(samples)
    while 1:  # Loop forever so the generator never terminates
        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:
                name = batch_sample[0]
                temp_img = cv2.imread(name)
                temp_img = cv2.cvtColor(temp_img, cv2.COLOR_BGR2RGB)

                #RGB color suffle
                planes = cv2.split(temp_img)
                planes = shuffle(planes)
                temp_img = cv2.merge(planes)

                temp_img = image.random_shift(temp_img,
                                              wrg=wrg,
                                              hrg=hrg,
                                              row_axis=0,
                                              col_axis=1,
                                              channel_axis=2)
                temp_img = image.random_rotation(temp_img,
                                                 rg=rg,
                                                 row_axis=0,
                                                 col_axis=1,
                                                 channel_axis=2)
                images.append(temp_img)

                temp_angle = float(
                    batch_sample[1]) + (np.random.random() - 0.5) * ang_n_l
                angles.append(temp_angle)

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

            yield X_train, y_train
Example #31
0
def process_image(img_path, steering_angle, augment, shape=(100, 100)):

    image = load_img(img_path, target_size=shape)

    if augment and random.random() < 0.5:
        image = random_shadow(image)

    image = img_to_array(image)

    if augment:
        image = random_shift(image, 0, 0.2, 0, 1, 2)  # only vertical
        if random.random() < 0.5:
            image = flip_image(image)
            steering_angle = -steering_angle

    return image, steering_angle
Example #32
0
def get_image_batches(batch_size):

    while True:
        random.shuffle(image_paths)
        for batch_i in range(0, len(image_paths), batch_size):
            x = []
            y = []
            for image_path in image_paths[batch_i:batch_i + batch_size]:
                image = image_arrays[image_path]  #load_image(image_path)

                # augment data
                # rotate up to 2 degrees
                image = preprocess.random_rotation(image,
                                                   2,
                                                   row_axis=0,
                                                   col_axis=1,
                                                   channel_axis=2)
                # randomly shift up to n %
                shift_percent = 0.2
                image = preprocess.random_shift(image,
                                                shift_percent,
                                                shift_percent,
                                                row_axis=0,
                                                col_axis=1,
                                                channel_axis=2)
                # randomly zoom in up to n %
                zoom_percent = 0.8
                image = preprocess.random_zoom(image,
                                               (zoom_percent, zoom_percent),
                                               row_axis=0,
                                               col_axis=1,
                                               channel_axis=2)
                #adjust brightness
                #image = preprocess.random_brightness(image, (0.8, 1.2))
                image_tmp = imgenhancer_Brightness = ImageEnhance.Brightness(
                    preprocess.array_to_img(image))
                u = np.random.uniform(0.8, 1.2)
                image_tmp = imgenhancer_Brightness.enhance(u)
                image = preprocess.img_to_array(image_tmp)
                # randomly flip horizontally
                if np.random.random() > 0.5:
                    image = preprocess.flip_axis(image, 1)

                x.append(image)
                y.append(get_one_hot(image_path))

            yield np.array(x), np.array(y)
Example #33
0
def pre_process_img(img_path, enable_zca = False,
                    enable_yuv = False,
                    enable_hsv = False,
                    training = False,
                    normalize = True,
                    target_size = (128, 128)):
    x = image.load_img(img_path, target_size = target_size)

    x = crop_image(x)

    #if training:
    #    if np.random.random() < 0.5:
    #        x = apply_modify(x, Brightness, .8, 1.2)
    #    if np.random.random() < 0.5:
    #        x = apply_modify(x, Sharpness, 1., 2.)

    x = image.img_to_array(x)

    if training:
        if np.random.random() < 0.5:
            x = flip_horizontal(x)
        elif np.random.random() < 0.5:
            x = image.random_shift(x, 0.2, 0., row_axis = 0, col_axis = 1, channel_axis = 2)
        elif np.random.random() < 0.5:
            x = image.random_rotation(x, 30., row_axis = 0, col_axis = 1, channel_axis = 2)
        elif np.random.random() < 0.5:
            x = image.random_zoom(x, [0.7, 1.3], row_axis = 0, col_axis = 1, channel_axis = 2)


    if enable_yuv:
        x = cv2.cvtColor(x, cv2.COLOR_RGB2YUV)
    elif enable_hsv:
        x = cv2.cvtColor(x, cv2.COLOR_RGB2HSV)

    if enable_zca:
        x = zca_whitening(x)


    if normalize:
        #
        # Normalize the image
        x = (x / 255. - 0.5).astype('float32')

    return x
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