Source code for cvpods.checkpoint.checkpoint

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

import copy
import logging
import os
import pickle
from typing import Any, Optional

import numpy as np

import torch
import torch.nn as nn
from torch.nn.parallel import DataParallel, DistributedDataParallel

from cvpods.utils import PathManager
from cvpods.utils.distributed import comm

from .c2_model_loading import align_and_update_state_dicts
from .utils import (
    _strip_prefix_if_present,
    get_missing_parameters_message,
    get_unexpected_parameters_message
)


[docs]class Checkpointer(object): """ A checkpointer that can save/load model as well as extra checkpointable objects. """
[docs] def __init__( self, model: nn.Module, save_dir: str = "", resume: bool = False, *, save_to_disk: bool = True, **checkpointables: object, ): """ Args: 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)`. """ if isinstance(model, (DistributedDataParallel, DataParallel)): model = model.module self.model = model self.checkpointables = copy.copy(checkpointables) self.resume = resume self.logger = logging.getLogger(__name__) self.save_dir = save_dir self.save_to_disk = save_to_disk
[docs] def save(self, name: str, tag_checkpoint: bool = True, **kwargs: dict): """ Dump model and checkpointables to a file. Args: name (str): name of the file. kwargs (dict): extra arbitrary data to save. """ if not self.save_dir or not self.save_to_disk: return data = {} data["model"] = self.model.state_dict() for key, obj in self.checkpointables.items(): data[key] = obj.state_dict() data.update(kwargs) basename = "{}.pth".format(name) save_file = os.path.join(self.save_dir, basename) assert os.path.basename(save_file) == basename, basename self.logger.info("Saving checkpoint to {}".format(save_file)) with PathManager.open(save_file, "wb") as f: torch.save(data, f) if tag_checkpoint: self.tag_last_checkpoint(basename)
[docs] def load(self, path: str): """ Load from the given checkpoint. When path points to network file, this function has to be called on all ranks. Args: 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 :meth:`.save(**extra_data)`. """ if not path: # no checkpoint provided self.logger.info( "No checkpoint found. Initializing model from scratch" ) return {} self.logger.info("Loading checkpoint from {}".format(path)) if not os.path.isfile(path): path = PathManager.get_local_path(path) assert PathManager.isfile(path), "Checkpoint {} not found!".format(path) checkpoint = self._load_file(path) self._load_model(checkpoint) if self.resume: for key, obj in self.checkpointables.items(): if key in checkpoint: self.logger.info("Loading {} from {}".format(key, path)) obj.load_state_dict(checkpoint.pop(key)) # return any further checkpoint data return checkpoint else: return {}
[docs] def has_checkpoint(self): """ Returns: bool: whether a checkpoint exists in the target directory. """ save_file = os.path.join(self.save_dir, "last_checkpoint") return PathManager.exists(save_file)
[docs] def get_checkpoint_file(self): """ Returns: str: The latest checkpoint file in target directory. """ save_file = os.path.join(self.save_dir, "last_checkpoint") try: with PathManager.open(save_file, "r") as f: last_saved = f.read().strip() except IOError: # if file doesn't exist, maybe because it has just been # deleted by a separate process return "" return os.path.join(self.save_dir, last_saved)
[docs] def get_all_checkpoint_files(self): """ Returns: list: All available checkpoint files (.pth files) in target directory. """ all_model_checkpoints = [ os.path.join(self.save_dir, file) for file in PathManager.ls(self.save_dir) if PathManager.isfile(os.path.join(self.save_dir, file)) and file.endswith(".pth") ] return all_model_checkpoints
[docs] def resume_or_load(self, path: str, *, resume: bool = True): """ 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. Args: path (str): path to the checkpoint. resume (bool): if True, resume from the last checkpoint if it exists. Returns: same as :meth:`load`. """ if resume and self.has_checkpoint(): path = self.get_checkpoint_file() return self.load(path)
[docs] def tag_last_checkpoint(self, last_filename_basename: str): """ Tag the last checkpoint. Args: last_filename_basename (str): the basename of the last filename. """ save_file = os.path.join(self.save_dir, "last_checkpoint") with PathManager.open(save_file, "w") as f: f.write(last_filename_basename)
def _load_file(self, f: str): """ Load a checkpoint file. Can be overwritten by subclasses to support different formats. Args: f (str): a locally mounted file path. Returns: dict: with keys "model" and optionally others that are saved by the checkpointer dict["model"] must be a dict which maps strings to torch.Tensor or numpy arrays. """ return torch.load(f, map_location=torch.device("cpu")) def _load_model(self, checkpoint: Any): """ Load weights from a checkpoint. Args: checkpoint (Any): checkpoint contains the weights. """ checkpoint_state_dict = checkpoint.pop("model") self._convert_ndarray_to_tensor(checkpoint_state_dict) # if the state_dict comes from a model that was wrapped in a # DataParallel or DistributedDataParallel during serialization, # remove the "module" prefix before performing the matching. _strip_prefix_if_present(checkpoint_state_dict, "module.") # work around https://github.com/pytorch/pytorch/issues/24139 model_state_dict = self.model.state_dict() for k in list(checkpoint_state_dict.keys()): if k in model_state_dict: shape_model = tuple(model_state_dict[k].shape) shape_checkpoint = tuple(checkpoint_state_dict[k].shape) if shape_model != shape_checkpoint: self.logger.warning( "'{}' has shape {} in the checkpoint but {} in the " "model! Skipped.".format( k, shape_checkpoint, shape_model ) ) checkpoint_state_dict.pop(k) incompatible = self.model.load_state_dict( checkpoint_state_dict, strict=False ) if incompatible.missing_keys: self.logger.info( get_missing_parameters_message(incompatible.missing_keys) ) if incompatible.unexpected_keys: self.logger.info( get_unexpected_parameters_message(incompatible.unexpected_keys) ) def _convert_ndarray_to_tensor(self, state_dict: dict): """ In-place convert all numpy arrays in the state_dict to torch tensor. Args: state_dict (dict): a state-dict to be loaded to the model. """ # model could be an OrderedDict with _metadata attribute # (as returned by Pytorch's state_dict()). We should preserve these # properties. for k in list(state_dict.keys()): if "weight_order" in k: continue v = state_dict[k] if not isinstance(v, np.ndarray) and not isinstance( v, torch.Tensor ): raise ValueError( "Unsupported type found in checkpoint! {}: {}".format( k, type(v) ) ) if not isinstance(v, torch.Tensor): state_dict[k] = torch.from_numpy(v)
[docs]class PeriodicCheckpointer: """ 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. """
[docs] def __init__(self, checkpointer: Any, period: int, max_iter: int = None, max_epoch: Optional[int] = None): """ Args: 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. """ self.checkpointer = checkpointer self.period = int(period) self.max_iter = max_iter self.max_epoch = max_epoch
[docs] def step(self, iteration: int, **kwargs: Any): """ Perform the appropriate action at the given iteration. Args: iteration (int): the current iteration, ranged in [0, max_iter-1]. kwargs (Any): extra data to save, same as in :meth:`Checkpointer.save`. """ iteration = int(iteration) additional_state = {"iteration": iteration} additional_state.update(kwargs) if (iteration + 1) % self.period == 0: if self.max_epoch is not None: epoch_iters = self.max_iter // self.max_epoch curr_epoch = (iteration + 1) // epoch_iters ckpt_name = "model_epoch_{:04d}".format(curr_epoch) else: ckpt_name = "model_iter_{:07d}".format(iteration + 1) self.checkpointer.save(ckpt_name, **additional_state) if iteration >= self.max_iter - 1: self.checkpointer.save("model_final", **additional_state)
[docs] def save(self, name: str, **kwargs: Any): """ Same argument as :meth:`Checkpointer.save`. Use this method to manually save checkpoints outside the schedule. Args: name (str): file name. kwargs (Any): extra data to save, same as in :meth:`Checkpointer.save`. """ self.checkpointer.save(name, **kwargs)
[docs]class DefaultCheckpointer(Checkpointer): """ Same as :class:`Checkpointer`, but is able to handle models in detectron & cvpods model zoo, and apply conversions for legacy models. """
[docs] def __init__(self, model, save_dir="", resume=False, *, save_to_disk=None, **checkpointables): """ Args: 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)`. """ is_main_process = comm.is_main_process() super().__init__( model, save_dir, resume, save_to_disk=is_main_process if save_to_disk is None else save_to_disk, **checkpointables, )
def _load_file(self, filename): """ Args: filename (str): load checkpoint file from local or oss. checkpoint can be of type pkl, pth """ if filename.endswith(".pkl"): with PathManager.open(filename, "rb") as f: data = pickle.load(f, encoding="latin1") if "model" in data and "__author__" in data: # file is in cvpods model zoo format self.logger.info("Reading a file from '{}'".format(data["__author__"])) return data else: # assume file is from Caffe2 / Detectron1 model zoo if "blobs" in data: # Detection models have "blobs", but ImageNet models don't data = data["blobs"] data = {k: v for k, v in data.items() if not k.endswith("_momentum")} return {"model": data, "__author__": "Caffe2", "matching_heuristics": True} elif filename.endswith(".pth"): if filename.startswith("s3://"): with PathManager.open(filename, "rb") as f: loaded = torch.load(f, map_location=torch.device("cpu")) else: loaded = super()._load_file(filename) # load native pth checkpoint if "model" not in loaded: loaded = {"model": loaded} return loaded def _load_model(self, checkpoint): """ Args: checkpoint (dict): model state dict. """ if checkpoint.get("matching_heuristics", False): self._convert_ndarray_to_tensor(checkpoint["model"]) # convert weights by name-matching heuristics model_state_dict = self.model.state_dict() align_and_update_state_dicts( model_state_dict, checkpoint["model"], c2_conversion=checkpoint.get("__author__", None) == "Caffe2", ) checkpoint["model"] = model_state_dict # for non-caffe2 models, use standard ways to load it super()._load_model(checkpoint)