def augment_photos_folder(self, batch_size=64, new_size=None): """ Augment photos in data/photos/ Args: batch_size: Size of each augmentation batch. new_size: tuple, new image size. Returns: None """ LOGGER.info(f'Started augmentation with {self.workers} workers') LOGGER.info(f'Total images to augment: {self.total_images}') LOGGER.info(f'Session assigned id: {self.session_id}') with ThreadPoolExecutor(max_workers=self.workers) as executor: while self.image_paths_copy: current_batch, current_paths = self.load_batch( new_size, batch_size) future_augmentations = { executor.submit(self.augment_image, image, path): path for image, path in zip(current_batch, current_paths) } for future_augmented in as_completed(future_augmentations): future_augmented.result() LOGGER.info(f'Augmentation completed') augmentation_frame = pd.DataFrame(self.augmentation_data, columns=self.mapping.columns) saving_path = get_abs_path('output', 'data', f'augmented_data_plus_original.csv', create_parents=True) combined = pd.concat([self.mapping, augmentation_frame]) for item in ['bx', 'by', 'bw', 'bh']: combined = combined.drop(combined[combined[item] > 1].index) combined.to_csv(saving_path, index=False) LOGGER.info(f'Saved old + augmented labels to {saving_path}') adjusted_combined = adjust_non_voc_csv(saving_path, self.image_folder, self.image_width, self.image_height) adjusted_saving_path = saving_path.replace('augmented', 'adjusted_aug') adjusted_combined.to_csv(adjusted_saving_path, index=False) LOGGER.info( f'Saved old + augmented (adjusted) labels to {adjusted_saving_path}' ) return adjusted_combined
def get_adjusted_labels(self, configuration): """ Adjust labels according to given configuration. Args: configuration: A dictionary containing any of the following keys: - relative_labels - xml_labels_folder - voc_conf (required if xml_labels_folder) - coordinate_labels Returns: pandas DataFrame with adjusted labels. """ labels_frame = None check = 0 if configuration.get('relative_labels'): labels_frame = adjust_non_voc_csv( configuration['relative_labels'], self.image_folder, self.image_width, self.image_height, ) check += 1 if xml_folder := configuration.get('xml_labels_folder'): if check: raise ValueError(f'Got more than one configuration') voc_conf = configuration.get('voc_conf') assert voc_conf, f'Missing VOC configuration json file.' labels_frame = parse_voc_folder( xml_folder, get_abs_path(voc_conf, verify=True), ) labels_frame.to_csv( get_abs_path('output', 'data', 'parsed_from_xml.csv', create_parents=True), index=False, ) check += 1
def get_adjusted_labels(self, configuration): """ Adjust labels according to given configuration. Args: configuration: A dictionary containing any of the following keys: - relative_labels - from_xml - coordinate_labels Returns: pandas DataFrame with adjusted labels. """ labels_frame = None check = 0 if configuration.get('relative_labels'): labels_frame = adjust_non_voc_csv( configuration['relative_labels'], self.image_folder, self.image_width, self.image_height, ) check += 1 if configuration.get('from_xml'): if check: raise ValueError(f'Got more than one configuration') labels_frame = parse_voc_folder( self.xml_folder, os.path.join('config', 'voc_conf.json'), ) labels_frame.to_csv( os.path.join('output', 'data', 'parsed_from_xml.csv'), index=False, ) check += 1 if configuration.get('coordinate_labels'): if check: raise ValueError(f'Got more than one configuration') labels_frame = pd.read_csv(configuration['coordinate_labels']) check += 1 return labels_frame