Build a list of default hooks, including timing, evaluation, checkpointing, lr scheduling, precise BN, writing events. Returns: list[HookBase]:
(self)
| 103 | # TODO: release GPU cluster submit scripts based on submitit for multi-node training |
| 104 | |
| 105 | def build_hooks(self): |
| 106 | """ |
| 107 | Build a list of default hooks, including timing, evaluation, |
| 108 | checkpointing, lr scheduling, precise BN, writing events. |
| 109 | |
| 110 | Returns: |
| 111 | list[HookBase]: |
| 112 | """ |
| 113 | cfg = copy.deepcopy(self.cfg) |
| 114 | cfg.DATALOADER.NUM_WORKERS = 0 # save some memory and time for PreciseBN |
| 115 | ret = [ |
| 116 | hooks.IterationTimer(), |
| 117 | hooks.LRScheduler(), |
| 118 | None, |
| 119 | ] |
| 120 | |
| 121 | # Do PreciseBN before checkpointer, because it updates the model and need to |
| 122 | # be saved by checkpointer. |
| 123 | # This is not always the best: if checkpointing has a different frequency, |
| 124 | # some checkpoints may have more precise statistics than others. |
| 125 | if comm.is_main_process(): |
| 126 | ret.append(hooks.PeriodicCheckpointer(self.checkpointer, cfg.SOLVER.CHECKPOINT_PERIOD)) |
| 127 | |
| 128 | def test_and_save_results(): |
| 129 | self._last_eval_results = self.test(self.cfg, self.model) |
| 130 | return self._last_eval_results |
| 131 | |
| 132 | # Do evaluation after checkpointer, because then if it fails, |
| 133 | # we can use the saved checkpoint to debug. |
| 134 | ret.append(hooks.EvalHook(cfg.TEST.EVAL_PERIOD, test_and_save_results)) |
| 135 | |
| 136 | if comm.is_main_process(): |
| 137 | # Here the default print/log frequency of each writer is used. |
| 138 | # run writers in the end, so that evaluation metrics are written |
| 139 | ret.append(hooks.PeriodicWriter(self.build_writers(), period=1)) |
| 140 | return ret |
| 141 | |
| 142 | @classmethod |
| 143 | def build_model(cls, cfg): |