<jupyter_start><jupyter_text>Helpers for detectron2 training<jupyter_code>#export
from drone_detector.imports import *
from drone_detector.utils import *

from drone_detector.engines.detectron2.augmentations import *

import detectron2
from detectron2.engine import DefaultTrainer
from detectron2.evaluation import COCOEvaluator, DatasetEvaluators
from detectron2.data import build_detection_train_loader, DatasetMapper, MetadataCatalog
from detectron2.data import detection_utils as utils
from detectron2.utils.visualizer import Visualizer
from detectron2.data import transforms as T

from fastai.vision.data import get_grid
# export

class Trainer(DefaultTrainer):
    """
    Trainer class for training detectron2 models, using default augmentations
    """
    
    def __init__(self, cfg, data_loader):
        super().__init__(cfg)
        self.augs = augs
    
    @classmethod
    def build_evaluator(cls, cfg, dataset_name, output_folder=None):
        return DatasetEvaluators([COCOEvaluator(dataset_name, output_dir=output_folder)])
    
    @classmethod
    def build_train_loader(cls, cfg):
        if not cls.augs:
            augs = [T.ResizeShortestEdge(cfg.INPUT.MIN_SIZE_TRAIN, 
                                     cfg.INPUT.MAX_SIZE_TRAIN, 
                                     cfg.INPUT.MIN_SIZE_TRAIN_SAMPLING)]
        else: augs = cls.augs
        return build_detection_train_loader(cfg, mapper=DatasetMapper(cfg, is_train=True, 
                                                                      augmentations=build_aug_transforms(cfg)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06641604010025062, 'fraction_numerical': 0.006265664160401002, 'mean_word_length': 3.1053984575835476, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '45200', 'license': 'Apache-2.0', 'n_tokens_mistral': 469, 'n_tokens_neox': 466, 'n_words': 97, 'path': 'nbs/41_engines.detectron2.training.ipynb', 'repo_name': 'tvuor/drone_detector', 'size': '1596'}