def _zoom(img, ground_truth, zoom_rg=(0.1, 0.1), u=0.5, v=1.0): # Current version of zoom re-sizes image. TODO add padding if v < u: img = prep.random_zoom(img, zoom_range=zoom_rg, row_axis=0, col_axis=1, channel_axis=2) ground_truth = prep.random_zoom(ground_truth, zoom_range=zoom_rg, row_axis=0, col_axis=1, channel_axis=2) return img, ground_truth
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
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
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 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
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)
def zoom_image(img_arr, zoomRangeWidth=1.5, zoomRangeHeight=.7): return kim.random_zoom(img_arr, zoom_range=(zoomRangeWidth, zoomRangeHeight), row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
def Zoom(image, zoom_rg=(0.1, 0.1), u=0.5, v=1.0): if v < u: image = prep.random_zoom(image, zoom_range=zoom_rg, row_axis=0, col_axis=1, channel_axis=2) return image
def random_transforms(self, samples, seed=None): if seed is not None: np.random.seed(seed) if len(samples) != 2: x = samples y = None else: x = samples[0] y = samples[1] if self.rotation_range: theta = int( 180 * np.random.uniform(-self.rotation_range, self.rotation_range)) (h, w) = x.shape[:2] (cx, cy) = [w // 2, h // 2] M = cv2.getRotationMatrix2D((cx, cy), -theta, 1.0) x = cv2.warpAffine(x, M, (w, h)) y = cv2.warpAffine(y, M, (w, h)) if self.horizontal_flip: if np.random.random() < 0.5: x = np.fliplr(x) y = np.fliplr(y) if self.vertical_flip: if np.random.random() < 0.5: x = np.flipud(x) y = np.flipud(y) if self.zoom_range[0] == 1 and self.zoom_range[1] == 1: zx, zy = 1, 1 else: zx, zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1], 2) x = image.random_zoom(x, (zx, zy), channel_axis=self.channel_axis) y = np.expand_dims(y, axis=2) y = image.random_zoom(y, (zx, zy), channel_axis=self.channel_axis) return (x, y)
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
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
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 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
def Zoom(image, prob, zoom_rg=(0.1, 0.1)): if random() < prob: image = prep.random_zoom(image, zoom_range=zoom_rg, row_axis=0, col_axis=1, channel_axis=2) return image
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
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
def prepare_image(image, target): # if the image mode is not RGB, convert it if image.mode != "RGB": image = image.convert("RGB") # resize the input image and preprocess it image = image.resize(target) image = img_to_array(image) image = random_zoom(image, (0.5, 0.5), 0, 1, 2) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) # return the processed image return image
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 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
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)
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 get_processed_image(image): if random() > P and AUGMENTS['ho_flip']: image = cv2.flip(image, 1) if random() > P and AUGMENTS['ver_flip']: image = cv2.flip(image, 0) if random() > P: image = random_rotation(image, **AUGMENTS['rotation']) if random() > P: image = random_shift(image, **AUGMENTS['shift']) if random() > P: image = random_zoom(image, **AUGMENTS['zoom']) return image
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')
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 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
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
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 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
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
def load_image(img_path, e_dims=False, image_flip=0.5, image_shift=0.20, image_rotate_degrees=15, image_zoom=0.15, output_BGR=True): """ WARNING this funciton should only manipulation images meant for resnet50 consumption. To make it applicable for other environments remove preprocess_input. Do some image manipulation image input assumed to be in RGB format output format default is GBR unless output_BGR is set to False e_dims = e_dims false will output (x,y,3) sized images e_domes true will output (1,x,y,3) sized images image_flip = probability that image will be flipped rt to left image_shift = percent of image to randomly shift up/down & right/left image_rotate_degrees = rotate image randomly between [-image_rotate_degrees image_rotate_degrees] image_zoom = randomly zoom image [1-image_zoom 1+image_zoom] output_BGR = True -> image output will be in BGR formate RGB otherwise """ img = image.load_img(img_path, target_size=(224, 224)) my_img = image.img_to_array(img) if image_flip is not None: if image_flip > 1 or image_flip < -1: raise ValueError('|image_flip:{0}| > 1'.format(image_flip)) image_flip = abs(image_flip) if random.random() > image_flip: my_img = image.flip_axis(my_img, axis=1) if image_rotate_degrees is not None: image_rotate_degrees = int(image_rotate_degrees) if image_rotate_degrees > 360: image_rotate_degrees = image_rotate_degrees % 360 my_img = image.random_rotation(my_img, image_rotate_degrees, row_index=0, col_index=1, channel_index=2) if image_shift is not None: if image_shift > 1 or image_shift < -1: raise ValueError('|image_shift:{0}| > 1'.format(image_shift)) image_shift = abs(image_shift) my_img = image.random_shift(my_img, image_shift, image_shift, row_index=0, col_index=1, channel_index=2) if image_zoom is not None: if image_zoom > 1 or image_zoom < -1: raise ValueError('|image_zoom:{0}| > 1'.format(image_zoom)) image_zoom = abs(image_zoom) low = 1 - image_zoom high = 1 + image_zoom rng = [low, high] my_img = image.random_zoom(my_img, rng, row_index=0, col_index=1, channel_index=2) if not output_BGR: my_img = bgr2rgb(my_img) my_img = np.expand_dims(my_img, axis=0) my_img = preprocess_input(my_img) if not e_dims: my_img = my_img.squeeze() return my_img