def __init__(self): freetype.init() cur_file_path = os.path.dirname(__file__) font_dir = os.path.join(cur_file_path, data_cfg.font_dir) self.font_list = os.listdir(font_dir) self.font_list = [os.path.join(font_dir, font_name) for font_name in self.font_list] self.standard_font_path = os.path.join(cur_file_path, data_cfg.standard_font_path) color_filepath = os.path.join(cur_file_path, data_cfg.color_filepath) self.colorsRGB, self.colorsLAB = colorize.get_color_matrix(color_filepath) text_filepath = os.path.join(cur_file_path, data_cfg.text_filepath) self.text_list = open(text_filepath, 'r').readlines() self.text_list = [text.strip() for text in self.text_list] bg_filepath = os.path.join(cur_file_path, data_cfg.bg_filepath) self.bg_list = open(bg_filepath, 'r').readlines() self.bg_list = [img_path.strip() for img_path in self.bg_list] self.surf_augmentor = Augmentor.DataPipeline(None) self.surf_augmentor.random_distortion(probability = data_cfg.elastic_rate, grid_width = data_cfg.elastic_grid_size, grid_height = data_cfg.elastic_grid_size, magnitude = data_cfg.elastic_magnitude) self.bg_augmentor = Augmentor.DataPipeline(None) self.bg_augmentor.random_brightness(probability = data_cfg.brightness_rate, min_factor = data_cfg.brightness_min, max_factor = data_cfg.brightness_max) self.bg_augmentor.random_color(probability = data_cfg.color_rate, min_factor = data_cfg.color_min, max_factor = data_cfg.color_max) self.bg_augmentor.random_contrast(probability = data_cfg.contrast_rate, min_factor = data_cfg.contrast_min, max_factor = data_cfg.contrast_max)
def augmentate_batch(self, imgs_in, imgs_gt): """Generate ordered augmented batch of images, using Augmentor""" # Non-Linear transformations. imgs = [[imgs_in[i], imgs_gt[i]] for i in range(len(imgs_in))] p = Augmentor.DataPipeline(imgs) p.random_distortion(0.5, 6, 6, 4) # Linear transformations. # p.rotate(0.75, 15, 15) p.shear(0.75, 10.0, 10.0) p.zoom(0.75, 1.0, 1.2) p.skew(0.75, 0.75) imgs = self.__apply_augmentation__(p) imgs_in = [p[0] for p in imgs] imgs_gt = [p[1] for p in imgs] # Noise transformations. p = Augmentor.DataPipeline([[img] for img in imgs_in]) gaussian_noise = GaussianNoiseAugmentor(0.25, 0, 10) p.add_operation(gaussian_noise) salt_pepper_noise = SaltPepperNoiseAugmentor(0.25, 0.005) p.add_operation(salt_pepper_noise) # Brightness transformation. p.random_brightness(0.75, 0.5, 1.5) p.random_contrast(0.75, 0.5, 1.5) # Colors invertion. invert = InvertPartAugmentor(0.25) p.add_operation(invert) p.invert(0.5) imgs_in = self.__apply_augmentation__(p) imgs_in = [p[0] for p in imgs_in] return imgs_in, imgs_gt
def __data_generation__(self, id_name): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization img_path = os.path.join(self.imgs_dir, id_name) # polyp segmentation/images/id_name.jpg mask_path = os.path.join(self.masks_dir, id_name) # polyp segmenatation/masks/id_name.jpg img = io.imread(img_path) mask = cv2.imread(mask_path) p = Augmentor.DataPipeline([[img, mask]]) p.resize(probability=1.0, width=self.img_size, height=self.img_size) p.rotate_without_crop(probability=0.3, max_left_rotation=10, max_right_rotation=10) #p.random_distortion(probability=0.3, grid_height=10, grid_width=10, magnitude=1) p.shear(probability=0.3, max_shear_left=1, max_shear_right=1) #p.skew_tilt(probability=0.3, magnitude=0.1) p.flip_random(probability=0.3) sample_p = p.sample(1) sample_p = np.array(sample_p).squeeze() p_img = sample_p[0] p_mask = sample_p[1] augmented_mask = (p_mask // 255) * 255 # denoising q = Augmentor.DataPipeline([[p_img]]) q.random_contrast(probability=0.3, min_factor=0.2, max_factor=1.0) # low to High q.random_brightness(probability=0.3, min_factor=0.2, max_factor=1.0) # dark to bright sample_q = q.sample(1) sample_q = np.array(sample_q).squeeze() image = sample_q mask = augmented_mask[::, ::, 0] kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) """ # reading the image from dataset ## Reading Image image = io.imread(img_path) # reading image to image vaiable image = resize(image, (self.img_size, self.img_size), anti_aliasing=True) # resizing input image to 128 * 128 mask = io.imread(mask_path, as_gray=True) # mask image of same size with all zeros mask = resize(mask, (self.img_size, self.img_size), anti_aliasing=True) # resizing mask to fit the 128 * 128 image mask = np.expand_dims(mask, axis=-1) """ # image normalization image = image / 255.0 mask = mask / 255.0 return image, mask
def get_training_generator(train_dir, batch_size): """Training data generator with augmentation on the fly.""" collated_images = list( zip( glob.glob(os.path.join(train_dir, 'images', '*.bmp')), glob.glob(os.path.join(train_dir, 'masks', '*.bmp')), )) data = [[imread(y) for y in x] for x in collated_images] p = Augmentor.DataPipeline(data) p.set_seed(0) p.rotate_random_90(0.5) p.zoom_random(0.5, percentage_area=0.8) p.crop_by_size(1, config.PATCH_SIZE, config.PATCH_SIZE, centre=False) p.flip_left_right(0.5) p.flip_top_bottom(0.5) def datagen(): while True: augmented = p.sample(batch_size) x_batch = np.array([pair[0] for pair in augmented]) / 255. y_batch = np.expand_dims(np.array([pair[1] for pair in augmented]), -1) yield x_batch, y_batch return datagen()
def augment_Images(imgs_in, imgs_gt): imgs = [[imgs_in[i], imgs_gt[i]] for i in range(len(imgs_in))] p = Augmentor.DataPipeline(imgs) p.random_distortion(0.5, 6, 6, 4) # Linear transformations. p.rotate(0.75, 15, 15) p.shear(0.75, 10.0, 10.0) p.zoom(0.75, 1.0, 1.2) p.skew(0.75, 0.75) batch = [] for i in range(0, len(p.augmentor_images)): images_to_return = [ Image.fromarray(x) for x in p.augmentor_images[i] ] for operation in p.operations: r = round(random.uniform(0, 1), 1) if r <= operation.probability: images_to_return = operation.perform_operation( images_to_return) images_to_return = [np.asarray(x) for x in images_to_return] batch.append(images_to_return) imgs = batch imgs_in = [p[0] for p in imgs] imgs_gt = [p[1] for p in imgs] return imgs_in, imgs_gt
def __getitem__(self, index): # Creating numpy arrays with images. start = index * self.batch_size stop = start + self.batch_size if stop >= self.indexes.shape[0]: stop = self.indexes.shape[0] images_pairs = [] for i in range(start, stop): image_path = os.path.join(self.images_dir, self.image_name_lst[self.indexes[i]]) image_gt_path = os.path.join(self.images_gt_dir, self.image_name_lst[self.indexes[i]]) image = np.asarray(Image.open(image_path).convert('L'), dtype=np.uint8) image_gt = np.asarray(Image.open(image_gt_path).convert('L'), dtype=np.uint8) images_pairs.append([image, image_gt]) p = Augmentor.DataPipeline(images_pairs) p.rotate(probability=1, max_left_rotation=25, max_right_rotation=25) p.flip_left_right(probability=0.5) p.zoom_random(probability=0.5, percentage_area=0.8) p.flip_top_bottom(probability=0.5) p.crop_by_size(probability=1, width=self.image_dim, height=self.image_dim, centre=False) p.resize(probability=1, width=self.image_dim, height=self.image_dim) augmented_images = p.sample(self.batch_size) images = np.asarray([np.expand_dims(normalize_image(augmented_image[0]), axis=2) for augmented_image in augmented_images]) images_gt = np.asarray([np.expand_dims(normalize_image(augmented_image[1], True), axis=2) for augmented_image in augmented_images]) return images, images_gt
def __data_generation__(self, id_name): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization img_path = os.path.join( self.imgs_dir, id_name) # polyp segmentation/images/id_name.jpg mask_path = os.path.join( self.masks_dir, id_name) # polyp segmenatation/masks/id_name.jpg img = io.imread(img_path) mask = cv2.imread(mask_path) p = Augmentor.DataPipeline([[img, mask]]) p.resize(probability=1.0, width=self.img_size, height=self.img_size) p.rotate_without_crop(probability=0.3, max_left_rotation=10, max_right_rotation=10) p.shear(probability=0.3, max_shear_left=1, max_shear_right=1) p.flip_random(probability=0.3) sample_p = p.sample(1) sample_p = np.array(sample_p).squeeze() p_img = sample_p[0] p_mask = sample_p[1] augmented_mask = (p_mask // 255) * 255 # denoising q = Augmentor.DataPipeline([[p_img]]) q.random_contrast(probability=0.3, min_factor=0.2, max_factor=1.0) # low to High q.random_brightness(probability=0.3, min_factor=0.2, max_factor=1.0) # dark to bright sample_q = q.sample(1) sample_q = np.array(sample_q).squeeze() image = sample_q mask = augmented_mask[::, ::, 0] kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # image normalization image = image / 255.0 mask = mask / 255.0 return image, mask
def augment_pair(full_img_path, full_mask_path, dest_folder_path, b=30, FISH_EYE=True, PERSPECTIVE=True): """ augmenting tool using the Augmentor library. Supports augumeting two images at the same time. """ img1 = cv2.imread(full_img_path) img2 = cv2.imread(full_mask_path) image_name = os.path.basename(full_img_path) mask_name = os.path.basename(full_mask_path) # Initialize pipeline p = Augmentor.DataPipeline([[img1, img2]]) # Apply augmentations p.rotate(1, max_left_rotation=5, max_right_rotation=5) p.shear(1, max_shear_left=5, max_shear_right=5) p.zoom_random(1, percentage_area=0.9) if FISH_EYE: p.random_distortion(probability=1, grid_width=5, grid_height=5, magnitude=5) p.gaussian_distortion(probability=1, grid_width=5, grid_height=5, magnitude=5, corner='bell', method='in') if PERSPECTIVE: p.shear(1, 10, 10) p.skew_tilt(1, magnitude=0.2) images_aug = p.sample(b) if dest_folder_path not in os.listdir(): os.mkdir(dest_folder_path) os.mkdir(os.path.join(dest_folder_path, 'imgs')) os.mkdir(os.path.join(dest_folder_path, 'masks')) for i in range(len(images_aug)): mask_gray = cv2.cvtColor(images_aug[i][1], cv2.COLOR_BGR2GRAY) # Assumes .jpg extension name for masks cv2.imwrite( os.path.join(dest_folder_path, 'imgs', image_name[:-3] + str(i) + ".jpg"), images_aug[i][0]) cv2.imwrite( os.path.join( dest_folder_path, 'masks', mask_name[:-3] + str(i) + ".jpg", ), mask_gray)
def augment_images(lib_dir, num_sample): rotate = True flip_lr = True flip_tb = True zoom = True print('\nCreating augmented images using:') pics_dir = lib_dir + '/pics' masks_dir = lib_dir + '/masks' ground_truth_images = natsorted(glob.glob(pics_dir + '/*.jpg')) segmentation_mask_images = natsorted(glob.glob(masks_dir + '/*.png')) print('\nOriginal tile-mask pairs:') for i in range(0, len(ground_truth_images)): print("%s: Ground: %s | Mask: %s" % (i + 1, os.path.basename(ground_truth_images[i]), os.path.basename(segmentation_mask_images[i]))) collated_images_and_masks = list( zip(ground_truth_images, segmentation_mask_images)) images = [[np.asarray(Image.open(y)) for y in x] for x in collated_images_and_masks] p = Augmentor.DataPipeline(images) if rotate: p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) if flip_lr: p.flip_left_right(probability=0.5) if zoom: p.zoom_random(probability=0.5, percentage_area=0.8) if flip_tb: p.flip_top_bottom(probability=0.5) ndx = len(ground_truth_images) augmented_images = p.sample(num_sample) print('\nAdding %s augmented samples to library...' % num_sample) for i in range(num_sample): # print('Adding augmented image: %s / %s'%(i+1, num_sample)) Image.fromarray(augmented_images[i][0].astype( np.uint8)).save(pics_dir + '/%s.jpg' % str(i + ndx)) Image.fromarray(augmented_images[i][1].astype( np.uint8)).save(masks_dir + '/%s.png' % str(i + ndx)) size_p = len(glob.glob(pics_dir + '/*.jpg')) size_m = len(glob.glob(masks_dir + '/*.png')) print('\nTraining library size:\nTiles: %s\nMasks: %s' % (size_p, size_m)) return
def test_sample_with_no_masks(): # NOTE: # --- # Temporarily disable this test as it will fail currently. # The DataPipeline class currently does not handle images # that do not have associated masks. When this functionality # has been added, this test will be reinstated. # --- # This is to test if the user passes data that does not contain # any masks, in other words a list of images rather than the # data structure you have in other examples in this file. width = 80 height = 80 tmpdir = tempfile.mkdtemp() tmps = [] num_of_images = 10 for i in range(num_of_images): tmps.append(tempfile.NamedTemporaryFile(dir=tmpdir, suffix='.JPEG')) bytestream = io.BytesIO() im = Image.new('RGB', (width, height)) im.save(bytestream, 'JPEG') tmps[i].file.write(bytestream.getvalue()) tmps[i].flush() # Make our data structures # Labels y = [0 if random.random() <= 0.5 else 1 for x in range(0, num_of_images)] # Image data images = [np.asarray(x) for x in tmps] p = Augmentor.DataPipeline(images) assert len(p.augmentor_images) == len(glob.glob(os.path.join(tmpdir, "*.JPEG"))) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) sample_size = 100 augmented_images = p.sample(sample_size) assert len(augmented_images) == sample_size # Close all temporary files which will also delete them automatically for i in range(len(tmps)): tmps[i].close() # Finally remove the directory (and everything in it) as mkdtemp does # not delete itself after closing automatically shutil.rmtree(tmpdir)
def train_aug(images, probability=0.5): p = Augmentor.DataPipeline([images]) p.rotate90(probability=probability) p.rotate270(probability=probability) p.flip_left_right(probability=probability) p.flip_top_bottom(probability=probability) p.rotate(1, max_left_rotation=5, max_right_rotation=5) p.zoom_random(probability=probability, percentage_area=0.7) p.random_distortion(probability=1, grid_width=4, grid_height=4, magnitude=8) return p.sample(1)[0]
def random_slice(arrays): assert arrays[0].shape == arrays[1].shape n_h = np.random.randint(arrays[0].shape[0]) slices = [arr[n_h] for arr in arrays] #Augmentation p = Augmentor.DataPipeline([slices]) p.rotate(1, max_left_rotation=5, max_right_rotation=5) p.flip_top_bottom(0.5) p.zoom_random(0.8, percentage_area=0.8) augmented_images = p.sample(1) return augmented_images
def test_sample_with_no_masks(): # NOTE: # --- # Temporarily disable this test as it will fail currently. # The DataPipeline class currently does not handle images # that do not have associated masks. When this functionality # has been added, this test will be reinstated. # --- # This is to test if the user passes data that does not contain # any masks, in other words a list of images rather than the # data structure you have in other examples in this file. width = 80 height = 80 tmpdir = tempfile.mkdtemp() num_of_images = 10 tmpdir = tempfile.mkdtemp() tmps = create_multiple_images(tmpdir, num_of_images, (width, height)) # Make our data structures # Labels y = [0 if random.random() <= 0.5 else 1 for x in range(0, num_of_images)] # Image data images = [np.asarray(x) for x in tmps] p = Augmentor.DataPipeline(images) assert len(p.augmentor_images) == len( glob.glob(os.path.join(tmpdir, "*.JPEG"))) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) sample_size = 100 augmented_images = p.sample(sample_size) assert len(augmented_images) == sample_size # Finally remove the directory (and everything in it) as mkdtemp does # not delete itself after closing automatically shutil.rmtree(tmpdir)
def make_data_gen_pipeline(images, augmentor_prob): # initialize augmentor: p = Augmentor.DataPipeline(images) # define augmentation: p.skew_tilt(probability=augmentor_prob, magnitude=0.2) p.random_distortion(probability=augmentor_prob, grid_width=16, grid_height=16, magnitude=8) p.rotate(probability=augmentor_prob, max_left_rotation=5, max_right_rotation=5) p.flip_top_bottom(probability=augmentor_prob) p.flip_left_right(probability=augmentor_prob) p.zoom(probability=augmentor_prob, min_factor=0.9, max_factor=1.1) # define generator with the right batch size: g = p.generator(batch_size=batch_size) data_gen = do_data_gen(g=g) return data_gen
def data_augment(self, images, edges, imgh, imgw): images = Image.fromarray(np.uint8(np.asarray(images)), mode="RGB") mask = Image.fromarray(np.uint8(np.ones_like(edges))) edges = Image.fromarray(np.uint8(edges)) if np.random.binomial(1, 0.8) > 0: brightness_factor = np.random.uniform(0.8, 1.2) images = F.adjust_brightness(images, brightness_factor) if np.random.binomial(1, 0.8) > 0: contrast_factor = np.random.uniform(0.5, 2) images = F.adjust_contrast(images, contrast_factor) if np.random.binomial(1, 0.8) > 0: hue_factor = np.random.uniform(-0.2, 0.2) images = F.adjust_hue(images, hue_factor) if np.random.binomial(1, 0.8) > 0: saturation_factor = np.random.uniform(0.8, 1.2) images = F.adjust_saturation(images, saturation_factor) if np.random.binomial(1, 0.8) > 0: angle = random.randint(-2, 2) translate = (random.randint(-int(imgw * 0.1), int(imgw * 0.1)), random.randint(-int(imgh * 0.1), int(imgh * 0.1))) scale = np.random.uniform(0.9, 1.1) if scale < 1: # scale:[0.9,1.1] scale = scale / 2 + 0.5 shear = random.randint(-2, 2) images = F.affine(images, angle, translate, scale, shear, resample=Image.BICUBIC) edges = F.affine(edges, angle, translate, scale, shear, resample=Image.BICUBIC) mask = F.affine(mask, angle, translate, scale, shear, resample=Image.BICUBIC) if np.random.binomial(1, 0.5) > 0: images = F.hflip(images) edges = F.hflip(edges) mask = F.hflip(mask) if np.random.binomial(1, 0.5) > 0: images = F.vflip(images) edges = F.vflip(edges) mask = F.vflip(mask) images = np.asarray(images) edges = np.asarray(edges) mask = np.asarray(mask) # https://github.com/mdbloice/Augmentor images = [[images, edges, mask]] p = Augmentor.DataPipeline(images) p.random_distortion(1, 10, 10, 10) g = p.generator(batch_size=1) augmented_images = next(g) images = augmented_images[0][0] edges = augmented_images[0][1] mask = augmented_images[0][2] edges = edges.copy() edges[edges < 128] = 0 edges[edges >= 128] = 1 edges = (morphology.skeletonize(edges)).astype(np.uint8) edges[edges == 1] = 255 edges = self.resize(edges, imgh, imgw) mask = self.resize(mask, imgh, imgw) # 1.not thin edge truth edges[edges > 0] = 255 # 2.thin edge truth # edges[edges>0]=1 # edges = (morphology.skeletonize(edges)).astype(np.uint8) # edges[edges==1] = 255 edges = (255 - edges) * mask return images, edges, mask * 255
def create_augmented_dataset(train_fns, test_fns, df_masks, filepath='../siim-train-test-augmented/', no_pneumothorax_examples=2000, pneumothorax_augmented_examples=2000): ''' Function to create an augmented dataset: INPUT: train_fns, test_fns, df_masks - loaded original dataset filepath - path to save files of the augmented dataset no_pneumothorax_examples - number of healthy images to stay in the dataset pneumothorax_augmented_examples - number of augmented pneumothorax images to add to the dataset ''' # image height and width im_height = 1024 im_width = 1024 # image channels im_chan = 1 filepath_img = filepath + '/img/' filepath_mask = filepath + '/mask/' # loop over the training set for idx, fname in tqdm(enumerate(train_fns)): try: # If it is a picture without pneumothrax then we only take it # to the training dataset with a certain ptobability if '-1' in df_masks.loc[fname.split('/')[-1][:-4], ' EncodedPixels']: # Get sample from uniform distribution and decide to copy the healthy sample if random.uniform( 0, 1) < (no_pneumothorax_examples / len(train_fns)): np_image = np.zeros((im_height, im_width, im_chan), dtype=np.uint8) data = pydicom.read_file(fname) np_image = np.expand_dims(data.pixel_array, axis=2) image = Image.fromarray( np_image.reshape(im_height, im_width), 'L') image = normalize_image(image) image.save(filepath_img + fname.split('/')[-1][:-4] + '.png') np_mask = np.zeros((im_height, im_width, 1), dtype=np.bool) mask = Image.fromarray( np_mask.reshape(im_height, im_width).astype(np.uint8), 'L') mask.save(filepath_mask + fname.split('/')[-1][:-4] + '_mask.png') else: # If there is pneumothrax on the image then we copy it to the output directory np_image = np.zeros((im_height, im_width, im_chan), dtype=np.uint8) data = pydicom.read_file(fname) np_image = np.expand_dims(data.pixel_array, axis=2) image = Image.fromarray(np_image.reshape(im_height, im_width), 'L') image = normalize_image(image) image.save(filepath_img + fname.split('/')[-1][:-4] + '.png') # Copy the mask to the output directory np_mask = np.zeros((im_height, im_width, 1), dtype=np.bool) if type(df_masks.loc[fname.split('/')[-1][:-4], ' EncodedPixels']) == str: np_mask = np.expand_dims(rle2mask( df_masks.loc[fname.split('/')[-1][:-4], ' EncodedPixels'], im_height, im_width), axis=2) else: np_mask = np.zeros((im_height, im_width, 1)) for x in df_masks.loc[fname.split('/')[-1][:-4], ' EncodedPixels']: np_mask = np_mask + np.expand_dims( rle2mask(x, im_height, im_width), axis=2) np_mask = np.transpose(np_mask) mask = Image.fromarray( np_mask.reshape(im_height, im_width).astype(np.uint8), 'L') mask.save(filepath_mask + fname.split('/')[-1][:-4] + '_mask.png') # Get sample from uniform distribution and decide to include augmented example to the train set if random.uniform(0, 1) < (pneumothorax_augmented_examples / len(train_fns)): # augment image and masks p = Augmentor.DataPipeline( [[np.array(image), np.array(mask)]]) p.rotate(0.7, max_left_rotation=3, max_right_rotation=3) p.zoom_random(probability=0.3, percentage_area=0.95) images_aug = p.sample(1) image = Image.fromarray( images_aug[0][0].reshape(im_height, im_width, 3).astype(np.uint8), 'L') mask = Image.fromarray( images_aug[0][1].reshape(im_height, im_width).astype(np.uint8), 'L') # apply random horizontal flip flip = random.uniform(0, 1) if (flip > 0.5): image = TF.hflip(image) mask = TF.hflip(mask) # Save augmented image as png and mask as png image.save(filepath_img + new_image_id + '.png') mask.save(filepath_mask + new_image_id + '_mask.png') except: pass
def __getitem__(self, idx): image_name = self.image_name_list[idx] #glob(searching criteria) : find files that meet searching criteria, output type : list. #glob(탐색 기준식) : 탐색 기준식을 만족하는 파일을 찾아, 그 항목들을 리스트로 반환. mask_file = glob(self.masks_dir + image_name + '.*') # (in the case of 'image name == mask name') img_file = glob(self.imgs_dir + image_name + '.*') #(in the case of 'image name == mask name') assert len(mask_file) == 1, \ f'Either no mask or multiple masks found for the ID {image_name}: {mask_file}' assert len(img_file) == 1, \ f'Either no image or multiple images found for the ID {image_name}: {img_file}' # print("img name : {}".format(img_file[0])) img = io.imread(img_file[0]) mask_for_notf = io.imread(mask_file[0]) mask = cv2.imread( mask_file[0] ) # for augmenting, gray image: cv2.imread=(width, height, 3) , io.imread=(width, height) assert img.shape == mask.shape, \ f'Image and mask {image_name} should be the same size, but are {img.size} and {mask.size}' if self.is_transform == False: img = self.preprocess(img, self.resizing) mask = self.preprocess(mask_for_notf, self.resizing) data = { 'image': torch.from_numpy(img), 'mask': torch.from_numpy(mask) } else: if np.random.uniform(size=1)[0] >= 0.7: sigma = np.random.uniform(0.1, 1, size=1)[0] img = (gaussian(img, sigma=sigma, multichannel=True) * 255).astype(np.uint8) p = Augmentor.DataPipeline([[img, mask]]) p.resize(probability=1.0, width=256, height=256) p.rotate_without_crop(probability=0.3, max_left_rotation=25, max_right_rotation=25) p.shear(probability=0.3, max_shear_left=0.5, max_shear_right=0.5) p.flip_random(probability=0.6) # p.skew_tilt(probability=0.3, magnitude=0.1) # p.random_distortion(probability=0.3, grid_height=10, grid_width=10, magnitude=1) # p.zoom(probability=1.0, min_factor=1.1, max_factor=1.3) sample_p = p.sample(1) sample_p = np.array(sample_p).squeeze() p_img = sample_p[0] p_mask = sample_p[1] augmented_mask = (p_mask // 255) * 255 #np.where(p_mask<=0, p_mask, 255) q = Augmentor.DataPipeline([[p_img]]) q.random_contrast(probability=0.3, min_factor=0.2, max_factor=1.0) # low to High q.random_brightness(probability=0.3, min_factor=0.2, max_factor=1.0) # dark to bright sample_q = q.sample(1) sample_q = np.array(sample_q).squeeze() augmented_img = sample_q augmented_mask = augmented_mask[::, ::, 0] kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) augmented_mask = cv2.morphologyEx(augmented_mask, cv2.MORPH_CLOSE, kernel) result_img = self.preprocess_wo_resize(augmented_img) result_mask = self.preprocess_wo_resize(augmented_mask) data = { 'image': torch.from_numpy(result_img), 'mask': torch.from_numpy(result_mask) } return data
def use_augmetor_for_data(dataset_bs_paths, args): """ Act * Params * dataset_bs_paths * args Return * """ try: path_to_be_loaded = dataset_bs_paths paths_of_imgs = list(dataset_bs_paths[0]) # print("paths_of_imgs",paths_of_imgs) # [('/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/d84b9aa8-bbb6-11e8-b2ba-ac1f6b6435d0_blue.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/d84b9aa8-bbb6-11e8-b2ba-ac1f6b6435d0_green.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/d84b9aa8-bbb6-11e8-b2ba-ac1f6b6435d0_red.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/d84b9aa8-bbb6-11e8-b2ba-ac1f6b6435d0_yellow.png'), # ('/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/6d3a7a02-bb9f-11e8-b2b9-ac1f6b6435d0_blue.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/6d3a7a02-bb9f-11e8-b2b9-ac1f6b6435d0_green.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/6d3a7a02-bb9f-11e8-b2b9-ac1f6b6435d0_red.png', # '/mnt/1T-5e7/mycodehtml/bio_health/Kaggle/human-protein-atlas-image-classification/Data/train/6d3a7a02-bb9f-11e8-b2b9-ac1f6b6435d0_yellow.png')] # ================================================================================ labels_of_imgs = dataset_bs_paths[1] # print("labels_of_imgs",labels_of_imgs) # ['0' '5 0'] # ================================================================================ labels_of_imgs_li = [] for one_lbl_set in labels_of_imgs: one_list = one_lbl_set.split(" ") # print("one_list",one_list) # ['0'] one_list = list(map(int, one_list)) # print("one_list",one_list) # [0] labels_of_imgs_li.append(one_list) # print("labels_of_imgs_li",labels_of_imgs_li) # [[0], [5, 0]] # ================================================================================ # @ Load images dataset_bs_paths = [] for one_protein in paths_of_imgs: loaded_b_img = np.array( Image.open(one_protein[0].replace("\n", ""))) loaded_g_img = np.array( Image.open(one_protein[1].replace("\n", ""))) loaded_r_img = np.array( Image.open(one_protein[2].replace("\n", ""))) loaded_y_img = np.array( Image.open(one_protein[3].replace("\n", ""))) dataset_bs_paths.append( [loaded_b_img, loaded_g_img, loaded_r_img, loaded_y_img]) # print("dataset_bs_paths",np.array(dataset_bs_paths).shape) # (2, 4, 512, 512) # ================================================================================ labels_of_imgs = labels_of_imgs_li # ================================================================================ # region augmentation on group aug_pipeline = Augmentor.DataPipeline(dataset_bs_paths, labels_of_imgs) # ================================================================================ # @ crop_by_size # aug_pipeline.crop_by_size(probability=1.0,width=48,height=48,centre=True) # ================================================================================ # @ rotate aug_pipeline.rotate(probability=0.5, max_left_rotation=6, max_right_rotation=7) # ================================================================================ # @ flip_random aug_pipeline.flip_random(probability=0.5) # ================================================================================ # @ resize aug_pipeline.resize(probability=1.0, width=224, height=224, resample_filter="BILINEAR") # ================================================================================ # @ sample sampled_trn_and_rgt_imgs_li, label_values = aug_pipeline.sample( int(args.batch_size)) # print("sampled_trn_and_rgt_imgs_li",len(sampled_trn_and_rgt_imgs_li)) # 2 # print("label_values",label_values) # label_values [[18, 0], [22, 0, 21]] # ================================================================================ sampled_trn_imgs = np.array(sampled_trn_and_rgt_imgs_li) / 255.0 # print("sampled_trn_imgs",sampled_trn_imgs.shape) # (2, 4, 224, 224) return sampled_trn_imgs, label_values except: print(traceback.format_exc()) print("Error when loading images") print("path_to_be_loaded", path_to_be_loaded)
# TODO: Edit according to class number collated_images_and_masks = list(zip(overall_list[0], overall_list[1], overall_list[2], overall_list[3], overall_list[4], overall_list[5])) images = [[np.asarray(Image.open(y)) for y in x] for x in collated_images_and_masks] print("image and masks loaded!") if debug: print(collated_images_and_masks) # import background bg_list = [] for bg in glob.glob(background_file): bg_list.append(bg) print("background list load: " + str(len(bg_list))) # define augmentor p = Augmentor.DataPipeline(images) p.zoom(1, 0.3, 1.0) p.zoom_random(1, .9) p.skew(1, .8) p.flip_random(1) p.random_distortion(.3, 10, 10, 7) p.random_color(1, .3, 1.2) p.random_contrast(1, .5, 1.5) p.random_brightness(1, 0.5, 1.5) p.shear(.5, 15, 15) # p.random_erasing(.75, 0.25) p.rotate_random_90(1) p.rotate(1, max_left_rotation=15, max_right_rotation=15) # begin generation for i in range(begin//batch, count//batch):
def __getitem__(self, idx): ''' Function to get items from dataset by idx. INPUT: idx - id of the image in the dataset ''' # image height and width im_height = 1024 im_width = 1024 # image channels im_chan = 3 # get train image and mask np_image = np.zeros((im_height, im_width, im_chan), dtype=np.uint8) np_mask = np.zeros((im_height, im_width, im_chan), dtype=np.bool) # if validation then return filename instead of mask if self.mode == 'validation': # read dcm file with image dataset = pydicom.read_file(self.fns[idx]) np_image = np.expand_dims(dataset.pixel_array, axis=2) image = Image.fromarray(np_image.reshape(im_height, im_width), 'L') image = image.convert('RGB') image = self.transforms_image(image) return [image, self.fns[idx].split('/')[-1][:-4]] # read dcm file with image dataset = pydicom.read_file(self.files_list[idx]) np_image = np.expand_dims(dataset.pixel_array, axis=2) # load mask try: # no pneumothorax if '-1' in self.labels_frame.loc[ self.files_list[idx].split('/')[-1][:-4], ' EncodedPixels']: np_mask = np.zeros((im_height, im_width, 1), dtype=np.bool) else: # there is pneumothorax if type(self.labels_frame.loc[ self.files_list[idx].split('/')[-1][:-4], ' EncodedPixels']) == str: np_mask = np.expand_dims(rle2mask( self.labels_frame.loc[ self.files_list[idx].split('/')[-1][:-4], ' EncodedPixels'], im_height, im_width), axis=2) else: np_mask = np.zeros((1024, 1024, 1)) for x in self.labels_frame.loc[ self.files_list[idx].split('/')[-1][:-4], ' EncodedPixels']: np_mask = np_mask + np.expand_dims( rle2mask(x, 1024, 1024), axis=2) except KeyError: # couldn't find mask in dataframe np_mask = np.zeros( (im_height, im_width, 1), dtype=np.bool) # Assume missing masks are empty masks. # convert to PIL image = Image.fromarray(np_image.reshape(im_height, im_width), 'L') image = image.convert('RGB') np_mask = np.transpose(np_mask) mask = Image.fromarray( np_mask.reshape(im_height, im_width).astype(np.uint8), 'L') # apply optional transforms (random rotation for the training set) if self.transform: # apply random rotation p = Augmentor.DataPipeline([[np.array(image), np.array(mask)]]) p.rotate(0.7, max_left_rotation=3, max_right_rotation=3) p.zoom_random(probability=0.3, percentage_area=0.95) images_aug = p.sample(1) image = Image.fromarray( images_aug[0][0].reshape(im_height, im_width, 3).astype(np.uint8), 'RGB') mask = Image.fromarray( images_aug[0][1].reshape(im_height, im_width).astype(np.uint8), 'L') # apply random horizontal flip flip = random.uniform(0, 1) if (flip > 0.5): image = TF.hflip(image) mask = TF.hflip(mask) # apply required transforms normalization, reshape and convert to tensor image = TF.resize(image, self.size) mask = TF.resize(mask, self.size) # convert image to tensors and normalize image = TF.to_tensor(image) image = TF.normalize(image, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ts = time.time() TF.to_pil_image(image).save('image' + str(ts) + '.png') # convert to tensor and clip mask mask = torch.from_numpy(np.array(mask, dtype=np.int64)) mask = np.clip(mask, 0, 1) return image, mask
def test_sample_with_masks(): width = 80 height = 80 # Original images tmpdir = tempfile.mkdtemp() tmps = [] num_of_images = 10 for i in range(num_of_images): tmps.append(tempfile.NamedTemporaryFile(dir=tmpdir, prefix=str(i), suffix='.JPEG')) bytestream = io.BytesIO() im = Image.new('RGB', (width, height)) im.save(bytestream, 'JPEG') tmps[i].file.write(bytestream.getvalue()) tmps[i].flush() # Mask images mask_tmpdir = tempfile.mkdtemp() mask_tmps = [] for i in range(num_of_images): mask_tmps.append(tempfile.NamedTemporaryFile(dir=mask_tmpdir, prefix=str(i), suffix='.JPEG')) bytestream = io.BytesIO() im = Image.new('RGB', (width, height)) im.save(bytestream, 'JPEG') mask_tmps[i].file.write(bytestream.getvalue()) mask_tmps[i].flush() original_image_list = glob.glob(os.path.join(tmpdir, "*.JPEG")) mask_image_list = glob.glob(os.path.join(mask_tmpdir, "*.JPEG")) assert len(original_image_list) == len(mask_image_list) assert len(original_image_list) == num_of_images assert len(mask_image_list) == num_of_images collated_paths = list(zip(original_image_list, mask_image_list)) # list() required as Python 3 returns an iterator assert len(collated_paths) == num_of_images # Generate our labels and image data structure # y = [0 if random.random() <= 0.5 else 1 for x in range(0, num_of_images)] # Random list of 0s and 1s image_class = 0 if random.random() <= 0.5 else 1 y = [image_class] * num_of_images # List of either all 0s or all 1s assert len(y) == num_of_images images = [[np.asarray(Image.open(im)) for im in im_list] for im_list in collated_paths] assert len(images) == num_of_images p = Augmentor.DataPipeline(images, y) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) sample_size = 10 augmented_images, augmented_labels = p.sample(sample_size) assert len(augmented_images) == sample_size assert len(augmented_labels) == sample_size print(augmented_labels) for i in range(0, len(augmented_labels)): assert augmented_labels[i] == image_class for im_list in augmented_images: for im in im_list: pil_image_from_array = Image.fromarray(im) assert pil_image_from_array is not None # Now without labels p = Augmentor.DataPipeline(images) p.zoom_random(probability=1, percentage_area=0.5) augmented_images_no_labels = p.sample(sample_size) assert len(augmented_images_no_labels) == sample_size for im_list_no_labels in augmented_images_no_labels: for im in im_list_no_labels: pil_image_from_array_no_lbl = Image.fromarray(im) assert pil_image_from_array_no_lbl is not None # Close all temporary files which will also delete them automatically for i in range(len(tmps)): tmps[i].close() for i in range(len(tmps)): mask_tmps[i].close() # Finally remove the directory (and everything in it) as mkdtemp does # not delete itself after closing automatically shutil.rmtree(tmpdir) shutil.rmtree(mask_tmpdir)
def __load__(self, load_image_index): ## Path image_path = self.image_paths[load_image_index] target_path = self.target_paths[load_image_index] ## Reading Image image = cv2.imread(image_path) image = cv2.resize(image, (self.o_w, self.o_h)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ## Reading Mask target = cv2.imread(target_path) target = cv2.resize(target, (self.o_w, self.o_h)) target = cv2.cvtColor(target, cv2.COLOR_BGR2RGB) ## Image and mask augmentation with random perspective scalling, translation, and rotation height, width = self.o_h, self.o_w ## Set ranges frame = self.s_p theta = self.r_d hor = self.t_p ver = self.t_p ## Generate rectangles for dropout rectangles = {} for i in range(0, self.d_n): x = random.randint(0, width - self.d_s) y = random.randint(0, width - self.d_s) rectangles['point%s' % (i * 2)] = (x, y) rectangles['point%s' % (i * 2 + 1)] = (x + random.randint(0, self.d_s), y + random.randint(0, self.d_s)) ## Generate random variables theta = random.uniform(-theta, theta) x1 = random.uniform(-frame, frame) y1 = random.uniform(-frame, frame) x2 = random.uniform(width - frame, width + frame) y2 = random.uniform(-frame, frame) x3 = random.uniform(-frame, frame) y3 = random.uniform(height - frame, height + frame) x4 = random.uniform(width - frame, width + frame) y4 = random.uniform(height - frame, height + frame) hor = random.uniform(-hor, hor) ver = random.uniform(-ver, ver) h_flip = random.randint(0, 1) v_flip = random.randint(0, 1) blur_rad = random.randint(0, self.b_p) ## Transform image # Ripple if self.in_rp == 1 and self.tar_rp == 0: image = ripple(image, self.rp_prob) # Rotation if self.in_r == 1: M = cv2.getRotationMatrix2D( ((width - 1) / 2.0, (height - 1) / 2.0), theta, 1) image = cv2.warpAffine(image, M, (width, height)) else: pass # Scaling if self.in_s == 1: pts1 = np.float32([[x1, y1], [x2, y2], [x3, y3], [x4, y4]]) pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) M = cv2.getPerspectiveTransform(pts1, pts2) image = cv2.warpPerspective(image, M, (width, height)) else: pass # Translation if self.in_t == 1: M = np.float32([[1, 0, hor], [0, 1, ver]]) image = cv2.warpAffine(image, M, (width, height)) # Flip if self.in_f == 1: if h_flip == 1: image = cv2.flip(image, 0) else: pass if v_flip == 1: image = cv2.flip(image, 1) else: pass else: pass color = (0, 0, 0) thickness = -1 # Rectangular dropout if self.in_d == 1: for n in range(0, self.d_n): pt1 = rectangles['point%s' % (n * 2)] pt2 = rectangles['point%s' % (n * 2 + 1)] image = cv2.rectangle(image, pt1, pt2, color, thickness) # Elastic transform if self.in_e == 1 and self.tar_e == 0: images = [[image]] p = Augmentor.DataPipeline(images) p.random_distortion(probability=1, grid_width=int(self.o_w / 7), grid_height=int(self.o_h / 7), magnitude=self.e_p) a_images = p.sample(1) image = a_images[0][0] else: pass # Gaussian blur if self.in_b == 1 and blur_rad > 0: image = cv2.blur(image, (blur_rad, blur_rad)) else: pass # Gaussian noise if self.in_n == 1 and self.tar_n == 0: image = gaussian_noise(image, random.uniform(0, self.n_p)) else: pass # Contrast if self.in_c == 1 and self.tar_c == 0: factor = random.uniform(1, self.c_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor image = keras.preprocessing.image.array_to_img(image) image = PIL.ImageEnhance.Contrast(image).enhance(enh) image = np.array(image) # Brightness if self.in_br == 1 and self.tar_br == 0: factor = random.uniform(1, self.b_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor image = keras.preprocessing.image.array_to_img(image) image = PIL.ImageEnhance.Brightness(image).enhance(enh) image = np.array(image) ## Transform mask # Ripple if self.in_rp == 0 and self.tar_rp == 1: target = ripple(target, self.rp_prob) # Rotation if self.tar_r == 1: M = cv2.getRotationMatrix2D( ((width - 1) / 2.0, (height - 1) / 2.0), theta, 1) target = cv2.warpAffine(target, M, (width, height)) else: pass # Scaling if self.tar_s == 1: pts1 = np.float32([[x1, y1], [x2, y2], [x3, y3], [x4, y4]]) pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]]) M = cv2.getPerspectiveTransform(pts1, pts2) target = cv2.warpPerspective(target, M, (width, height)) else: pass # Translation if self.tar_t == 1: M = np.float32([[1, 0, hor], [0, 1, ver]]) target = cv2.warpAffine(target, M, (width, height)) # Flip if self.tar_f == 1: if h_flip == 1: target = cv2.flip(target, 0) else: pass if v_flip == 1: target = cv2.flip(target, 1) else: pass else: pass # Rectangular dropout if self.tar_d == 1: for n in range(0, self.d_n): pt1 = rectangles['point%s' % (n * 2)] pt2 = rectangles['point%s' % (n * 2 + 1)] target = cv2.rectangle(target, pt1, pt2, color, thickness) # Elastic transform if self.in_e == 0 and self.tar_e == 1: target = [[target]] p = Augmentor.DataPipeline(target) p.random_distortion(probability=1, grid_width=int(self.o_w / 7), grid_height=int(self.o_h / 7), magnitude=self.e_p) a_target = p.sample(1) target = a_target[0][0] else: pass # Gaussian blur if self.tar_b == 1 and blur_rad > 0: target = cv2.blur(target, (blur_rad, blur_rad)) else: pass # Gaussian noise if self.in_n == 0 and self.tar_n == 1: target = gaussian_noise(target, random.uniform(0, self.n_p)) else: pass # Contrast if self.in_c == 0 and self.tar_c == 1: factor = random.uniform(1, self.c_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor target = keras.preprocessing.image.array_to_img(target) target = PIL.ImageEnhance.Contrast(target).enhance(enh) target = np.array(target) # Brightness if self.in_br == 0 and self.tar_br == 1: factor = random.uniform(1, self.b_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor target = keras.preprocessing.image.array_to_img(target) target = PIL.ImageEnhance.Brightness(target).enhance(enh) target = np.array(target) ### Apply to both # Ripple if self.in_rp == 1 and self.tar_rp == 1: image, target = ripple_both(image, target, self.rp_prob) # Elastic transform if self.in_e == 1 and self.tar_e == 1: images = [[image, target]] p = Augmentor.DataPipeline(images) p.random_distortion(probability=1, grid_width=int(self.o_w / 7), grid_height=int(self.o_h / 7), magnitude=self.e_p) a_images = p.sample(1) image = a_images[0][0] target = a_images[0][1] else: pass # Gaussian noise if self.in_n == 1 and self.tar_n == 1: image, target = gaussian_noise_both(image, target, random.uniform(0, self.n_p)) # Contrast if self.in_c == 1 and self.tar_c == 1: factor = random.uniform(1, self.c_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor image = keras.preprocessing.image.array_to_img(image) target = keras.preprocessing.image.array_to_img(target) image = PIL.ImageEnhance.Contrast(image).enhance(enh) target = PIL.ImageEnhance.Contrast(target).enhance(enh) image = np.array(image) target = np.array(target) # Brightness if self.in_br == 1 and self.tar_br == 1: factor = random.uniform(1, self.b_u) increase_decrease = random.randint(0, 1) if increase_decrease == 0: enh = 1 / factor**3 else: enh = factor image = keras.preprocessing.image.array_to_img(image) target = keras.preprocessing.image.array_to_img(target) image = PIL.ImageEnhance.Brightness(image).enhance(enh) target = PIL.ImageEnhance.Brightness(target).enhance(enh) image = np.array(image) target = np.array(target) #target = cv2.resize(target, (self.t_w, self.t_h)) #target = cv2.cvtColor(target, cv2.COLOR_BGR2RGB) ## Normalizaing image = image / 255.0 target = target / 255.0 return image, target
image_folder = 'image' mask_folder = 'label' images_list = glob.glob( os.path.join(train_path, image_folder, '*' + '.jpg')) labels_list = [] for image_path in images_list: labels_list.append( os.path.join(train_path, mask_folder, os.path.basename(image_path).split('.')[0] + '.png')) collated_images_and_masks = list(zip(images_list, labels_list)) images = [[np.asarray(Image.open(y)) for y in x] for x in collated_images_and_masks] y = len(images_list) * [1] # TODO: hardcoding from class 1 p = Augmentor.DataPipeline(images, y) # p.set_seed(seed) # p.rotate(1, max_left_rotation=5, max_right_rotation=5) # p.flip_top_bottom(0.5) # p.zoom_random(1, percentage_area=0.5) ### Train if ds_mode == 'train': p.skew( 0.25, magnitude=0.2 ) # To skew or tilt an image either left, right, forwards, or backwards and 8 cornors p.rotate(0.25, max_left_rotation=15, max_right_rotation=15 ) # You could combine this with a resize operation, p.crop_random(0.25, percentage_area=0.7) p.zoom_random(0.25, percentage_area=0.8) augmented_images_masks, ys = p.sample(200)
def init_augmentor(images: List[List[np.array]], augment_params: Dict[str, str]) -> Augmentor.DataPipeline: """ Initializes the augmentor pipeline for a given list of images (as numpy arrays) and the specified augmentation parameters # Arguments images: List[List[np.array]]. a list of images to apply augmentations to (as numpy arrays). For each image additional mask images like bounding boxes can be specified which will recieve the same augmentations as the image itself augment_params: Dict[str, str]. the augmentations to be performed. Allowed keys are: 'color': 'none', 'small', 'medium', 'large' 'distortion': 'none', 'small', 'medium', 'large' 'noise': 'none', 'small', 'medium', 'large' 'rotate': 'none', 'standard', 'standard_crop' 'scale': 'none', 'x_small', 'x_medium', 'x_large', 'both_small', 'both_medium', 'both_large' 'shear': 'none', 'x_small', 'x_medium', 'x_large', 'y_small', 'y_medium', 'y_large', 'both_small', 'both_medium', 'both_large' # Returns DataPipeline. the augmentation pipeline used to generate augmented images """ params = {**defaultParams, **augment_params} p = Augmentor.DataPipeline(images) # color, brightness, contrast if params['color'] == 'small': p.random_color(0.3, min_factor=0.95, max_factor=1.05) p.random_brightness(0.3, min_factor=0.95, max_factor=1.05) p.random_contrast(0.3, min_factor=0.95, max_factor=1.05) elif params['color'] == 'medium': p.random_color(0.4, min_factor=0.9, max_factor=1.1) p.random_brightness(0.4, min_factor=0.9, max_factor=1.1) p.random_contrast(0.4, min_factor=0.9, max_factor=1.1) elif params['color'] == 'large': p.random_color(0.4, min_factor=0.85, max_factor=1.15) p.random_brightness(0.4, min_factor=0.9, max_factor=1.1) p.random_contrast(0.4, min_factor=0.85, max_factor=1.15) # distortion if params['distortion'] == 'small': p.random_distortion(0.4, grid_width=4, grid_height=4, magnitude=1) elif params['distortion'] == 'medium': p.random_distortion(0.4, grid_width=10, grid_height=5, magnitude=2) elif params['distortion'] == 'large': p.random_distortion(0.4, grid_width=15, grid_height=5, magnitude=2) # noise, salt&pepper if params['noise'] == 'small': p.add_operation(RandomNoise(0.25, 0.002)) p.add_operation(RandomSaltAndPepper(0.2, 0.001, 0.001)) elif params['noise'] == 'medium': p.add_operation(RandomNoise(0.25, 0.006)) p.add_operation(RandomSaltAndPepper(0.2, 0.003, 0.003)) elif params['noise'] == 'large': p.add_operation(RandomNoise(0.25, 0.01)) p.add_operation(RandomSaltAndPepper(0.2, 0.005, 0.005)) # rotate if params['rotate'] == 'standard': p.rotate_without_crop(0.4, max_left_rotation=2, max_right_rotation=2, expand=True) elif params['rotate'] == 'standard_crop': p.rotate_without_crop(0.4, max_left_rotation=2, max_right_rotation=2, expand=False) # scale if params['scale'] == 'x_small': p.add_operation(Scale(0.3, min_scale_x=0.92, max_scale_x=1.02, min_scale_y=0.99, max_scale_y=1.01)) elif params['scale'] == 'x_medium': p.add_operation(Scale(0.3, min_scale_x=0.85, max_scale_x=1.1, min_scale_y=0.95, max_scale_y=1.05)) elif params['scale'] == 'x_large': p.add_operation(Scale(0.3, min_scale_x=0.8, max_scale_x=1.2, min_scale_y=0.95, max_scale_y=1.05)) elif params['scale'] == 'both_small': p.add_operation(Scale(0.3, min_scale_x=0.95, max_scale_x=1.05, min_scale_y=0.95, max_scale_y=1.05)) elif params['scale'] == 'both_medium': p.add_operation(Scale(0.3, min_scale_x=0.9, max_scale_x=1.1, min_scale_y=0.9, max_scale_y=1.1)) elif params['scale'] == 'both_large': p.add_operation(Scale(0.3, min_scale_x=0.8, max_scale_x=1.2, min_scale_y=0.8, max_scale_y=1.2)) # shear, skew if params['shear'] == 'x_small': p.skew_left_right(0.3, magnitude=0.005) p.add_operation(ShearFloatPrecision(0.4, max_shear_left=1.0, max_shear_right=0.05)) elif params['shear'] == 'x_medium': p.skew_left_right(0.3, magnitude=0.01) # final p.add_operation(ShearFloatPrecision(0.4, max_shear_left=2.0, max_shear_right=0.25)) elif params['shear'] == 'x_large': p.skew_left_right(0.3, magnitude=0.015) p.add_operation(ShearFloatPrecision(0.4, max_shear_left=3.0, max_shear_right=0.5)) elif params['shear'] == 'y_small': p.add_operation(ShearFloatPrecision(0.4, max_shear_left=0.05, max_shear_right=1.0)) # measure, border elif params['shear'] == 'y_medium': p.add_operation(ShearFloatPrecision(0.4, max_shear_left=0.25, max_shear_right=2.0)) elif params['shear'] == 'y_large': p.add_operation(ShearFloatPrecision(0.4, max_shear_left=0.5, max_shear_right=3.0)) elif params['shear'] == 'both_small': p.skew_left_right(0.3, magnitude=0.005) p.add_operation(ShearFloatPrecision(0.4, max_shear_left=1.0, max_shear_right=1.0)) # part elif params['shear'] == 'both_medium': p.skew_left_right(0.3, magnitude=0.01) p.add_operation(ShearFloatPrecision(0.4, max_shear_left=2.0, max_shear_right=2.0, keep_size=True)) elif params['shear'] == 'both_large': p.skew_left_right(0.3, magnitude=0.015) p.add_operation(ShearFloatPrecision(0.4, max_shear_left=3.0, max_shear_right=3.0, keep_size=True)) return p
def test_sample_with_masks(): width = 80 height = 80 # Original images num_of_images = 10 tmpdir = tempfile.mkdtemp() create_multiple_images(tmpdir, num_of_images, (width, height)) # Mask images mask_tmpdir = tempfile.mkdtemp() create_multiple_images(mask_tmpdir, num_of_images, (width, height)) original_image_list = glob.glob(os.path.join(tmpdir, "*.JPEG")) mask_image_list = glob.glob(os.path.join(mask_tmpdir, "*.JPEG")) assert len(original_image_list) == len(mask_image_list) assert len(original_image_list) == num_of_images assert len(mask_image_list) == num_of_images collated_paths = list(zip( original_image_list, mask_image_list)) # list() required as Python 3 returns an iterator assert len(collated_paths) == num_of_images # Generate our labels and image data structure # y = [0 if random.random() <= 0.5 else 1 for x in range(0, num_of_images)] # Random list of 0s and 1s image_class = 0 if random.random() <= 0.5 else 1 y = [image_class] * num_of_images # List of either all 0s or all 1s assert len(y) == num_of_images images = [[np.asarray(Image.open(im)) for im in im_list] for im_list in collated_paths] assert len(images) == num_of_images p = Augmentor.DataPipeline(images, y) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) sample_size = 10 augmented_images, augmented_labels = p.sample(sample_size) assert len(augmented_images) == sample_size assert len(augmented_labels) == sample_size print(augmented_labels) for i in range(0, len(augmented_labels)): assert augmented_labels[i] == image_class for im_list in augmented_images: for im in im_list: pil_image_from_array = Image.fromarray(im) assert pil_image_from_array is not None # Now without labels p = Augmentor.DataPipeline(images) p.zoom_random(probability=1, percentage_area=0.5) augmented_images_no_labels = p.sample(sample_size) assert len(augmented_images_no_labels) == sample_size for im_list_no_labels in augmented_images_no_labels: for im in im_list_no_labels: pil_image_from_array_no_lbl = Image.fromarray(im) assert pil_image_from_array_no_lbl is not None # Finally remove the directory (and everything in it) as mkdtemp does # not delete itself after closing automatically shutil.rmtree(tmpdir) shutil.rmtree(mask_tmpdir)
z = list(zip(x)) print(len((z))) def unzip(z): x_p = [a[0] for a in z] return x_p import random random.seed(1997) import Augmentor p = Augmentor.DataPipeline(z, y) p.rotate(probability=0.2, max_left_rotation=5, max_right_rotation=5) p.flip_left_right(probability=0.2) p.zoom_random(probability=0.1, percentage_area=0.8) p.flip_top_bottom(probability=0.3) p.gaussian_distortion(probability=0.05, grid_width=4, grid_height=4, magnitude=3, corner='bell', method='in', mex=0.5, mey=0.5, sdx=0.05, sdy=0.05)
# p.ground_truth("path_to_ground_truth") # Add operators to the pipeline 'p' p.rotate( probability=0.5, max_left_rotation=15, max_right_rotation=15) # probability = chance of applying this operator # Sample a specified number of pictures from augmented data, output will be stored in a separate directory p.sample(4, multi_threaded=False) # supports multi-threading ## Multiple Mask Image Augmentation image1 = Image.open("data/grid.png") image2 = Image.open("data/8.png") images = [[np.asarray(image1)], [np.asarray(image2)]] # images must be in List of Lists [[]] format p = Augmentor.DataPipeline(images, labels=None) p.rotate(probability=0.5, max_left_rotation=15, max_right_rotation=15) augmented_images = p.sample(1) plt.imshow(augmented_images[0][0]) plt.show() ## Supported operators # Rotations p.rotate(probability=1, max_left_rotation=0, max_right_rotation=0) p.rotate90(probability=1) p.rotate180(probability=1) p.rotate270(probability=1) p.rotate_random_90(probability=1) p.rotate_without_crop(probability=1, max_left_rotation=0,
def trainGenerator2(batch_size, train_path, image_folder, mask_folder, aug_dict, image_color_mode="grayscale", mask_color_mode="grayscale", image_save_prefix="image", mask_save_prefix="mask", flag_multi_class=False, num_class=2, save_to_dir=None, target_size=(256, 256), seed=1, class_mode=None, shuffle=False): # images_dir = os.path.join(train_path, image_folder) # labels_dir = os.path.join(train_path, mask_folder) # logging.debug('images_dir {}'.format(images_dir)) # logging.debug('labels_dir {}'.format(labels_dir)) images_list = glob.glob( os.path.join(train_path, image_folder, '*' + '.jpg')) # labels = glob.glob(os.path.join(images_dir, 'label', '*' + '.png')) # images = [] # labels = [] # for image_path in images_list: # image = load_img(image_path) # target_size= # PIL # x = img_to_array(image) # images.append(x) # label_path = os.path.join(train_path, mask_folder, os.path.basename(image_path).split('.')[0] + '.png') # label = load_img(label_path) # target_size= # y = img_to_array(label) # labels.append(y) # labels.append([os.path.join(train_path, mask_folder, os.path.basename(image_path).split('.')[0] + '.png')]) labels_list = [] for image_path in images_list: labels_list.append( os.path.join(train_path, mask_folder, os.path.basename(image_path).split('.')[0] + '.png')) images = [np.asarray(Image.open(x)) for x in images_list] labels = [np.asarray(Image.open(x)) for x in labels_list] # images = np.array(images) # labels = np.array(labels) p = Augmentor.DataPipeline(images, labels) # p.rotate(1, max_left_rotation=5, max_right_rotation=5) # p.flip_top_bottom(0.5) p.zoom_random(1, percentage_area=0.5) # TODO: Batch size should be always 1 img, mask = p.sample(1) # g = p.keras_generator(batch_size=1) img = np.array(img) mask = np.array(mask) img = np.squeeze(img) mask = np.squeeze(mask) img, mask = adjustData(img, mask, flag_multi_class, num_class) hash_str = str(random.getrandbits(16)) img_rgb = Image.fromarray((img * 255).astype(np.uint8)) img_rgb.save(os.path.join('output', 'aug', 'img-' + hash_str + '.jpg')) img_gs = Image.fromarray((mask * 255).astype(np.uint8)) img_gs.save(os.path.join('output', 'aug', 'mask-' + hash_str + '.jpg')) # yield (img, mask) ### images, labels = next(g) # for img, mask in g: # img, mask = adjustData(img,mask,flag_multi_class,num_class) # hash_srt = str(random.getrandbits(16)) # img_rgb = Image.fromarray((img * 255).astype(numpy.uint8)) # img_rbg.save(os.path.join('output', 'aug', 'aug_image-' + hash_str + '.jpg')) # yield (img, mask) return None