detectron2.checkpoint package

class detectron2.checkpoint.Checkpointer(model: torch.nn.modules.module.Module, save_dir: str = '', *, save_to_disk: bool = True, **checkpointables)[source]

Bases: object

A checkpointer that can save/load model as well as extra checkpointable objects.

__init__(model: torch.nn.modules.module.Module, save_dir: str = '', *, save_to_disk: bool = True, **checkpointables)[source]
Parameters:
  • model (nn.Module) – model.
  • save_dir (str) – a directory to save and find checkpoints.
  • save_to_disk (bool) – if True, save checkpoint to disk, otherwise disable saving for this checkpointer.
  • checkpointables (object) – any checkpointable objects, i.e., objects that have the state_dict() and load_state_dict() method. For example, it can be used like Checkpointer(model, “dir”, optimizer=optimizer).
save(name: str, **kwargs)[source]

Dump model and checkpointables to a file.

Parameters:
  • name (str) – name of the file.
  • kwargs (dict) – extra arbitrary data to save.
load(path: str)[source]

Load from the given checkpoint. When path points to network file, this function has to be called on all ranks.

Parameters:path (str) – path or url to the checkpoint. If empty, will not load anything.
Returns:dict – extra data loaded from the checkpoint that has not been processed. For example, those saved with save(**extra_data)().
has_checkpoint()[source]
Returns:bool – whether a checkpoint exists in the target directory.
get_checkpoint_file()[source]
Returns:str – The latest checkpoint file in target directory.
get_all_checkpoint_files()[source]
Returns:list
All available checkpoint files (.pth files) in target
directory.
resume_or_load(path: str, *, resume: bool = True)[source]

If resume is True, this method attempts to resume from the last checkpoint, if exists. Otherwise, load checkpoint from the given path. This is useful when restarting an interrupted training job.

Parameters:
  • path (str) – path to the checkpoint.
  • resume (bool) – if True, resume from the last checkpoint if it exists.
Returns:

same as load().

tag_last_checkpoint(last_filename_basename: str)[source]

Tag the last checkpoint.

Parameters:last_filename_basename (str) – the basename of the last filename.
class detectron2.checkpoint.PeriodicCheckpointer(checkpointer: Any, period: int, max_iter: int = None)[source]

Bases: object

Save checkpoints periodically. When .step(iteration) is called, it will execute checkpointer.save on the given checkpointer, if iteration is a multiple of period or if max_iter is reached.

__init__(checkpointer: Any, period: int, max_iter: int = None)[source]
Parameters:
  • checkpointer (Any) – the checkpointer object used to save
  • checkpoints.
  • period (int) – the period to save checkpoint.
  • max_iter (int) – maximum number of iterations. When it is reached, a checkpoint named “model_final” will be saved.
step(iteration: int, **kwargs)[source]

Perform the appropriate action at the given iteration.

Parameters:
  • iteration (int) – the current iteration, ranged in [0, max_iter-1].
  • kwargs (Any) – extra data to save, same as in Checkpointer.save().
save(name: str, **kwargs)[source]

Same argument as Checkpointer.save(). Use this method to manually save checkpoints outside the schedule.

Parameters:
class detectron2.checkpoint.DetectionCheckpointer(model, save_dir='', *, save_to_disk=None, **checkpointables)[source]

Bases: fvcore.common.checkpoint.Checkpointer

Same as Checkpointer, but is able to handle models in detectron & detectron2 model zoo, and apply conversions for legacy models.