Build a list of default hooks, including timing, evaluation, checkpointing, lr scheduling, precise BN, writing events. Returns: list[HookBase]:
(self)
| 335 | self.start_iter = comm.all_gather(self.start_iter)[0] |
| 336 | |
| 337 | def build_hooks(self): |
| 338 | """ |
| 339 | Build a list of default hooks, including timing, evaluation, |
| 340 | checkpointing, lr scheduling, precise BN, writing events. |
| 341 | |
| 342 | Returns: |
| 343 | list[HookBase]: |
| 344 | """ |
| 345 | cfg = self.cfg.clone() |
| 346 | cfg.defrost() |
| 347 | cfg.DATALOADER.NUM_WORKERS = 0 # save some memory and time for PreciseBN |
| 348 | |
| 349 | ret = [ |
| 350 | hooks.IterationTimer(), |
| 351 | hooks.LRScheduler(), |
| 352 | hooks.PreciseBN( |
| 353 | # Run at the same freq as (but before) evaluation. |
| 354 | cfg.TEST.EVAL_PERIOD, |
| 355 | self.model, |
| 356 | # Build a new data loader to not affect training |
| 357 | self.build_train_loader(cfg), |
| 358 | cfg.TEST.PRECISE_BN.NUM_ITER, |
| 359 | ) |
| 360 | if cfg.TEST.PRECISE_BN.ENABLED and get_bn_modules(self.model) |
| 361 | else None, |
| 362 | ] |
| 363 | |
| 364 | # Do PreciseBN before checkpointer, because it updates the model and need to |
| 365 | # be saved by checkpointer. |
| 366 | # This is not always the best: if checkpointing has a different frequency, |
| 367 | # some checkpoints may have more precise statistics than others. |
| 368 | if comm.is_main_process(): |
| 369 | ret.append(hooks.PeriodicCheckpointer(self.checkpointer, cfg.SOLVER.CHECKPOINT_PERIOD)) |
| 370 | |
| 371 | def test_and_save_results(): |
| 372 | self._last_eval_results = self.test(self.cfg, self.model) |
| 373 | return self._last_eval_results |
| 374 | |
| 375 | # Do evaluation after checkpointer, because then if it fails, |
| 376 | # we can use the saved checkpoint to debug. |
| 377 | ret.append(hooks.EvalHook(cfg.TEST.EVAL_PERIOD, test_and_save_results)) |
| 378 | |
| 379 | if comm.is_main_process(): |
| 380 | # run writers in the end, so that evaluation metrics are written |
| 381 | ret.append(hooks.PeriodicWriter(self.build_writers(), period=20)) |
| 382 | return ret |
| 383 | |
| 384 | def build_writers(self): |
| 385 | """ |
no test coverage detected