Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
fastreid.engine.hooks.EvalHook Class Reference
Inheritance diagram for fastreid.engine.hooks.EvalHook:
fastreid.engine.train_loop.HookBase

Public Member Functions

 __init__ (self, eval_period, eval_function)
 
 after_step (self)
 
 after_train (self)
 
- Public Member Functions inherited from fastreid.engine.train_loop.HookBase
 before_train (self)
 
 before_step (self)
 

Protected Member Functions

 _do_eval (self)
 

Protected Attributes

 _period
 
 _func
 

Detailed Description

Run an evaluation function periodically, and at the end of training.
It is executed every ``eval_period`` iterations and after the last iteration.

Definition at line 292 of file hooks.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.engine.hooks.EvalHook.__init__ ( self,
eval_period,
eval_function )
Args:
    eval_period (int): the period to run `eval_function`.
    eval_function (callable): a function which takes no arguments, and
        returns a nested dict of evaluation metrics.
Note:
    This hook must be enabled in all or none workers.
    If you would like only certain workers to perform evaluation,
    give other workers a no-op function (`eval_function=lambda: None`).

Definition at line 298 of file hooks.py.

298 def __init__(self, eval_period, eval_function):
299 """
300 Args:
301 eval_period (int): the period to run `eval_function`.
302 eval_function (callable): a function which takes no arguments, and
303 returns a nested dict of evaluation metrics.
304 Note:
305 This hook must be enabled in all or none workers.
306 If you would like only certain workers to perform evaluation,
307 give other workers a no-op function (`eval_function=lambda: None`).
308 """
309 self._period = eval_period
310 self._func = eval_function
311

Member Function Documentation

◆ _do_eval()

fastreid.engine.hooks.EvalHook._do_eval ( self)
protected

Definition at line 312 of file hooks.py.

312 def _do_eval(self):
313 results = self._func()
314
315 if results:
316 assert isinstance(
317 results, dict
318 ), "Eval function must return a dict. Got {} instead.".format(results)
319
320 flattened_results = flatten_results_dict(results)
321 for k, v in flattened_results.items():
322 try:
323 v = float(v)
324 except Exception:
325 raise ValueError(
326 "[EvalHook] eval_function should return a nested dict of float. "
327 "Got '{}: {}' instead.".format(k, v)
328 )
329 self.trainer.storage.put_scalars(**flattened_results, smoothing_hint=False)
330
331 # Remove extra memory cache of main process due to evaluation
332 torch.cuda.empty_cache()
333

◆ after_step()

fastreid.engine.hooks.EvalHook.after_step ( self)
Called after each iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 334 of file hooks.py.

334 def after_step(self):
335 next_iter = self.trainer.iter + 1
336 is_final = next_iter == self.trainer.max_iter
337 if is_final or (self._period > 0 and next_iter % self._period == 0):
338 self._do_eval()
339 # Evaluation may take different time among workers.
340 # A barrier make them start the next iteration together.
341 comm.synchronize()
342

◆ after_train()

fastreid.engine.hooks.EvalHook.after_train ( self)
Called after the last iteration.

Reimplemented from fastreid.engine.train_loop.HookBase.

Definition at line 343 of file hooks.py.

343 def after_train(self):
344 # func is likely a closure that holds reference to the trainer
345 # therefore we clean it to avoid circular reference in the end
346 del self._func
347
348

Member Data Documentation

◆ _func

fastreid.engine.hooks.EvalHook._func
protected

Definition at line 310 of file hooks.py.

◆ _period

fastreid.engine.hooks.EvalHook._period
protected

Definition at line 309 of file hooks.py.


The documentation for this class was generated from the following file: