detectron2.checkpoint

class detectron2.checkpoint.Checkpointer(model: Module, save_dir: str = '', *, save_to_disk: bool = True, **checkpointables: Any)[源代码]

基类:object

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

__init__(model: Module, save_dir: str = '', *, save_to_disk: bool = True, **checkpointables: Any) None[源代码]
参数:
  • 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).

add_checkpointable(key: str, checkpointable: Any) None[源代码]

Add checkpointable object for this checkpointer to track.

参数:
  • key (str) – the key used to save the object

  • checkpointable – any object with state_dict() and load_state_dict() method

save(name: str, **kwargs: Any) None[源代码]

Dump model and checkpointables to a file.

参数:
  • name (str) – name of the file.

  • kwargs (dict) – extra arbitrary data to save.

load(path: str, checkpointables: Optional[List[str]] = None) Dict[str, Any][源代码]

Load from the given checkpoint.

参数:
  • path (str) – path or url to the checkpoint. If empty, will not load anything.

  • checkpointables (list) – List of checkpointable names to load. If not specified (None), will load all the possible checkpointables.

返回:

dict – extra data loaded from the checkpoint that has not been processed. For example, those saved with save(**extra_data)().

has_checkpoint() bool[源代码]
返回:

bool – whether a checkpoint exists in the target directory.

get_checkpoint_file() str[源代码]
返回:

str – The latest checkpoint file in target directory.

get_all_checkpoint_files() List[str][源代码]
返回:

list

All available checkpoint files (.pth files) in target

directory.

resume_or_load(path: str, *, resume: bool = True) Dict[str, Any][源代码]

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.

参数:
  • path (str) – path to the checkpoint.

  • resume (bool) – if True, resume from the last checkpoint if it exists and load the model together with all the checkpointables. Otherwise only load the model without loading any checkpointables.

返回:

same as load().

tag_last_checkpoint(last_filename_basename: str) None[源代码]

Tag the last checkpoint.

参数:

last_filename_basename (str) – the basename of the last filename.

class detectron2.checkpoint.PeriodicCheckpointer(checkpointer: Checkpointer, period: int, max_iter: Optional[int] = None, max_to_keep: Optional[int] = None, file_prefix: str = 'model')[源代码]

基类: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.

checkpointer

the underlying checkpointer object

Type:

Checkpointer

__init__(checkpointer: Checkpointer, period: int, max_iter: Optional[int] = None, max_to_keep: Optional[int] = None, file_prefix: str = 'model') None[源代码]
参数:
  • checkpointer – 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 “{file_prefix}_final” will be saved.

  • max_to_keep (int) – maximum number of most current checkpoints to keep, previous checkpoints will be deleted

  • file_prefix (str) – the prefix of checkpoint’s filename

step(iteration: int, **kwargs: Any) None[源代码]

Perform the appropriate action at the given iteration.

参数:
  • 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) None[源代码]

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

参数:
class detectron2.checkpoint.DetectionCheckpointer(model, save_dir='', *, save_to_disk=None, **checkpointables)[源代码]

基类:Checkpointer

Same as Checkpointer, but is able to: 1. handle models in detectron & detectron2 model zoo, and apply conversions for legacy models. 2. correctly load checkpoints that are only available on the master worker

load(path, *args, **kwargs)[源代码]