Exemplo n.º 1
0
    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
        """
        default_logger.info(
            f'Started augmentation with {self.workers} workers')
        default_logger.info(f'Total images to augment: {self.total_images}')
        default_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()
        default_logger.info(f'Augmentation completed')
        augmentation_frame = pd.DataFrame(self.augmentation_data,
                                          columns=self.mapping.columns)
        saving_path = os.path.join('..', 'Output', 'Data',
                                   f'augmented_data_plus_original.csv')
        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)
        default_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)
        default_logger.info(
            f'Saved old + augmented (adjusted) labels to {adjusted_saving_path}'
        )
        return adjusted_combined
Exemplo n.º 2
0
    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
                - adjusted_frame

        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(
                os.path.join('..', 'Data', 'XML Labels'),
                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('adjusted_frame'):
            if check:
                raise ValueError(f'Got more than one configuration')
            labels_frame = pd.read_csv(configuration['adjusted_frame'])
            check += 1
        return labels_frame