Arulkumar03/Wheat_HEAD_Detection_Counting_ComputerVision_Model
0
1# Training2 3From the previous tutorials, you may now have a custom model and a data loader.4To run training, users typically have a preference in one of the following two styles:5 6### Custom Training Loop7 8With a model and a data loader ready, everything else needed to write a training loop can9be found in PyTorch, and you are free to write the training loop yourself.10This style allows researchers to manage the entire training logic more clearly and have full control.11One such example is provided in [tools/plain_train_net.py](../../tools/plain_train_net.py).12 13Any customization on the training logic is then easily controlled by the user.14 15### Trainer Abstraction16 17We also provide a standardized "trainer" abstraction with a18hook system that helps simplify the standard training behavior.19It includes the following two instantiations:20 21* [SimpleTrainer](../modules/engine.html#detectron2.engine.SimpleTrainer)22 provides a minimal training loop for single-cost single-optimizer single-data-source training, with nothing else.23 Other tasks (checkpointing, logging, etc) can be implemented using24 [the hook system](../modules/engine.html#detectron2.engine.HookBase).25* [DefaultTrainer](../modules/engine.html#detectron2.engine.defaults.DefaultTrainer) is a `SimpleTrainer` initialized from a26 yacs config, used by27 [tools/train_net.py](../../tools/train_net.py) and many scripts.28 It includes more standard default behaviors that one might want to opt in,29 including default configurations for optimizer, learning rate schedule,30 logging, evaluation, checkpointing etc.31 32To customize a `DefaultTrainer`:33 341. For simple customizations (e.g. change optimizer, evaluator, LR scheduler, data loader, etc.), overwrite [its methods](../modules/engine.html#detectron2.engine.defaults.DefaultTrainer) in a subclass, just like [tools/train_net.py](../../tools/train_net.py).352. For extra tasks during training, check the36 [hook system](../modules/engine.html#detectron2.engine.HookBase) to see if it's supported.37 38 As an example, to print hello during training:39 ```python40 class HelloHook(HookBase):41 def after_step(self):42 if self.trainer.iter % 100 == 0:43 print(f"Hello at iteration {self.trainer.iter}!")44 ```453. Using a trainer+hook system means there will always be some non-standard behaviors that cannot be supported, especially in research.46 For this reason, we intentionally keep the trainer & hook system minimal, rather than powerful.47 If anything cannot be achieved by such a system, it's easier to start from [tools/plain_train_net.py](../../tools/plain_train_net.py) to implement custom training logic manually.48 49### Logging of Metrics50 51During training, detectron2 models and trainer put metrics to a centralized [EventStorage](../modules/utils.html#detectron2.utils.events.EventStorage).52You can use the following code to access it and log metrics to it:53```python54from detectron2.utils.events import get_event_storage55 56# inside the model:57if self.training:58 value = # compute the value from inputs59 storage = get_event_storage()60 storage.put_scalar("some_accuracy", value)61```62 63Refer to its documentation for more details.64 65Metrics are then written to various destinations with [EventWriter](../modules/utils.html#module-detectron2.utils.events).66DefaultTrainer enables a few `EventWriter` with default configurations.67See above for how to customize them.68 