cvpods.checkpoint package

class cvpods.checkpoint.Checkpointer(model: torch.nn.modules.module.Module, save_dir: str = '', resume: bool = False, *, save_to_disk: bool = True, **checkpointables: object)[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 = '', resume: bool = False, *, save_to_disk: bool = True, **checkpointables: object)[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, tag_checkpoint: bool = True, **kwargs: dict)[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 cvpods.checkpoint.DefaultCheckpointer(model, save_dir='', resume=False, *, save_to_disk=None, **checkpointables)[source]

Bases: cvpods.checkpoint.checkpoint.Checkpointer

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

__init__(model, save_dir='', resume=False, *, save_to_disk=None, **checkpointables)[source]
Parameters
  • model (nn.Module) – model.

  • save_dir (str) – a directory to save and find checkpoints.

  • resume (bool) – indicate whether to resume from latest checkpoint or start from scratch.

  • 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).

class cvpods.checkpoint.PeriodicCheckpointer(checkpointer: Any, period: int, max_iter: int = None, max_epoch: Optional[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, max_epoch: Optional[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: Any)[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: Any)[source]

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

Parameters

cvpods.checkpoint.checkpoint module

class cvpods.checkpoint.checkpoint.Checkpointer(model: torch.nn.modules.module.Module, save_dir: str = '', resume: bool = False, *, save_to_disk: bool = True, **checkpointables: object)[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 = '', resume: bool = False, *, save_to_disk: bool = True, **checkpointables: object)[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, tag_checkpoint: bool = True, **kwargs: dict)[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 cvpods.checkpoint.checkpoint.PeriodicCheckpointer(checkpointer: Any, period: int, max_iter: int = None, max_epoch: Optional[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, max_epoch: Optional[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: Any)[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: Any)[source]

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

Parameters
class cvpods.checkpoint.checkpoint.DefaultCheckpointer(model, save_dir='', resume=False, *, save_to_disk=None, **checkpointables)[source]

Bases: cvpods.checkpoint.checkpoint.Checkpointer

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

__init__(model, save_dir='', resume=False, *, save_to_disk=None, **checkpointables)[source]
Parameters
  • model (nn.Module) – model.

  • save_dir (str) – a directory to save and find checkpoints.

  • resume (bool) – indicate whether to resume from latest checkpoint or start from scratch.

  • 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).