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 ModelCheckpoint not being picklable. #1632

Merged
merged 1 commit into from
Apr 27, 2020
Merged
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
17 changes: 11 additions & 6 deletions pytorch_lightning/callbacks/model_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', ve

torch_inf = torch.tensor(np.Inf)
mode_dict = {
'min': (torch.lt, torch_inf, 'min'),
'max': (torch.gt, -torch_inf, 'max'),
'auto': (torch.gt, -torch_inf, 'max') if 'acc' in self.monitor or self.monitor.startswith('fmeasure')
else (torch.lt, torch_inf, 'min'),
'min': (torch_inf, 'min'),
'max': (-torch_inf, 'max'),
'auto': (-torch_inf, 'max') if 'acc' in self.monitor or self.monitor.startswith('fmeasure')
else (torch_inf, 'min'),
}

if mode not in mode_dict:
rank_zero_warn(f'ModelCheckpoint mode {mode} is unknown, '
f'fallback to auto mode.', RuntimeWarning)
mode = 'auto'

self.monitor_op, self.kth_value, self.mode = mode_dict[mode]
self.kth_value, self.mode = mode_dict[mode]

def _del_model(self, filepath):
if os.path.isfile(filepath):
Expand All @@ -151,7 +151,12 @@ def check_monitor_top_k(self, current):
if not isinstance(current, torch.Tensor):
current = torch.tensor(current)

return self.monitor_op(current, self.best_k_models[self.kth_best_model])
monitor_op = {
"min": torch.lt,
"max": torch.gt,
}[self.mode]

return monitor_op(current, self.best_k_models[self.kth_best_model])

def format_checkpoint_name(self, epoch, metrics, ver=None):
"""Generate a filename according to the defined template.
Expand Down