Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Checkpoint and ModelCheckpoint consistency #1606

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions ignite/handlers/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def score_function(engine):

def __init__(
self,
to_save: Optional[Mapping],
to_save: Mapping,
save_handler: Union[Callable, BaseSaveHandler],
filename_prefix: str = "",
score_function: Optional[Callable] = None,
Expand All @@ -268,23 +268,19 @@ def __init__(
greater_or_equal: bool = False,
) -> None:

if to_save is not None: # for compatibility with ModelCheckpoint
if not isinstance(to_save, collections.Mapping):
raise TypeError(f"Argument `to_save` should be a dictionary, but given {type(to_save)}")
if not isinstance(to_save, collections.Mapping):
raise TypeError(f"Argument `to_save` should be a dictionary, but given {type(to_save)}")

if len(to_save) < 1:
raise ValueError("No objects to checkpoint.")

self._check_objects(to_save, "state_dict")
self._check_objects(to_save, "state_dict")

if include_self:
if not isinstance(to_save, collections.MutableMapping):
raise TypeError(
f"If `include_self` is True, then `to_save` must be mutable, but given {type(to_save)}."
)
if include_self:
if not isinstance(to_save, collections.MutableMapping):
raise TypeError(
f"If `include_self` is True, then `to_save` must be mutable, but given {type(to_save)}."
)

if "checkpointer" in to_save:
raise ValueError(f"Cannot have key 'checkpointer' if `include_self` is True: {to_save}")
if "checkpointer" in to_save:
raise ValueError(f"Cannot have key 'checkpointer' if `include_self` is True: {to_save}")

if not (callable(save_handler) or isinstance(save_handler, BaseSaveHandler)):
raise TypeError("Argument `save_handler` should be callable or inherit from BaseSaveHandler")
Expand Down Expand Up @@ -746,7 +742,7 @@ def __init__(
disk_saver = DiskSaver(dirname, atomic=atomic, create_dir=create_dir, require_empty=require_empty, **kwargs)

super(ModelCheckpoint, self).__init__(
to_save=None,
to_save={},
save_handler=disk_saver,
filename_prefix=filename_prefix,
score_function=score_function,
Expand Down
3 changes: 0 additions & 3 deletions tests/ignite/handlers/test_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ def test_checkpoint_wrong_input():
with pytest.raises(TypeError, match=r"Argument `to_save` should be a dictionary"):
Checkpoint([12], lambda x: x, "prefix")

with pytest.raises(ValueError, match=r"No objects to checkpoint."):
Checkpoint({}, lambda x: x, "prefix")

model = DummyModel()
to_save = {"model": model}

Expand Down