def run(self): # Getting lists of images for train and test. # Train , Test # [ [file1, file2, ...] , [file1, file2, ...] ] sets_with_labels = self.get_sets() # Gets augmentations if available, empty lists otherwise. # Train , Test # [ [file1, file2, ...] , [file1, file2, ...] ] augmentations = self.get_augmentations(len(sets_with_labels)) # If the augmentation reference file already exists, don't run this again. if augmentations is None: augmentations = [[] for _ in range(len(sets_with_labels))] # Creates augmentation patches from the original images, it stores the information # into pickle files. self.augment(sets_with_labels, augmentations) # Randomize patches for each of the train/test sets. for set_ in augmentations: random.shuffle(set_) # Saves augmentations into a pickle file. utils.store_data(augmentations, self.augmentations_file_path) print('"augmentations" file finished') # Creates Hdf5 database and save patches images, saving the patches if off by default. self.save_images(augmentations, sets_with_labels, save=self.save_img)
def augment(self, sets, augmentations): # For train and test lists. for set_, augmentation_set in zip(sets, augmentations): ''' Set: [('231___2_114_13_6.jpg', 13.4630136986) , ...] [('Image name.jpg', Survival years), ...] ''' current_img_idx = 0 for filename, labels in set_: self.patches_per_file[filename] = 0 n = len(augmentation_set) for config in self.sample_patches(filename): config = (current_img_idx, ) + config augmentation_set.append(config) self.patches_per_file[filename] += 1 print('Patches per image: %s %s ' % (filename, self.patches_per_file[filename])) # If no new patches are created, remove image file from list and replace sets pickle. # Possibilities? # Useless image: most of the picture is white. if n == len(augmentation_set): del set_[current_img_idx] utils.store_data(sets, self.sets_file_path) print('"sets" updated (%s discarded)' % filename) # Save augmentations in each iteration. utils.store_data(augmentations, self.augmentations_file_path) print('checkpoint saved') current_img_idx += 1
def augment(self, sets, augmentations): # For train and test lists. for set_, augmentation_set in zip(sets, augmentations): ''' Set: [('231___2_114_13_6.jpg', 13.4630136986) , ...] [('Image name.jpg', Survival years), ...] ''' current_img_idx = 0 for filename, labels in set_: self.patches_per_file[filename] = 0 n = len(augmentation_set) for config in self.sample_patches(filename): config = (current_img_idx,) + config augmentation_set.append(config) self.patches_per_file[filename] += 1 print('Patches per image: %s %s ' % (filename, self.patches_per_file[filename])) # If no new patches are created, remove image file from list and replace sets pickle. # Possibilities? # Useless image: most of the picture is white. if n == len(augmentation_set): del set_[current_img_idx] utils.store_data(sets, self.sets_file_path) print('"sets" updated (%s discarded)' % filename) # Save augmentations in each iteration. utils.store_data(augmentations, self.augmentations_file_path) print('checkpoint saved') current_img_idx += 1
def get_sets(self): try: sets_with_labels = utils.load_data(self.sets_file_path) print('"sets" file loaded') except FileNotFoundError: sets = self.split_data_into_sets([0.8, 0.2]) sets_with_labels = self.append_labels(sets) utils.store_data(sets_with_labels, self.sets_file_path) print('"sets" file created') return sets_with_labels