2019-11-05 15:41:59 +00:00
|
|
|
import os
|
2020-02-27 21:21:14 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from typing import Union
|
2019-11-05 15:41:59 +00:00
|
|
|
|
2019-10-22 01:16:51 +00:00
|
|
|
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
|
2020-02-27 21:21:14 +00:00
|
|
|
from pytorch_lightning.loggers import LightningLoggerBase
|
2019-10-22 01:16:51 +00:00
|
|
|
|
|
|
|
|
2019-12-04 15:57:32 +00:00
|
|
|
class TrainerCallbackConfigMixin(ABC):
|
|
|
|
|
2020-02-27 21:21:14 +00:00
|
|
|
# this is just a summary on variables used in this abstract class,
|
|
|
|
# the proper values/initialisation should be done in child class
|
|
|
|
default_save_path: str
|
|
|
|
logger: Union[LightningLoggerBase, bool]
|
2020-03-03 02:50:38 +00:00
|
|
|
weights_save_path: str
|
|
|
|
ckpt_path: str
|
|
|
|
checkpoint_callback: ModelCheckpoint
|
2020-02-27 21:21:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def slurm_job_id(self) -> int:
|
|
|
|
"""Warning: this is just empty shell for code implemented in other class."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def save_checkpoint(self, *args):
|
|
|
|
"""Warning: this is just empty shell for code implemented in other class."""
|
2019-12-04 15:57:32 +00:00
|
|
|
|
2019-10-22 01:16:51 +00:00
|
|
|
def configure_checkpoint_callback(self):
|
|
|
|
"""
|
|
|
|
Weight path set in this priority:
|
|
|
|
Checkpoint_callback's path (if passed in).
|
|
|
|
User provided weights_saved_path
|
|
|
|
Otherwise use os.getcwd()
|
|
|
|
"""
|
2020-03-03 02:50:38 +00:00
|
|
|
ckpt_path = self.default_save_path
|
2019-10-22 01:16:51 +00:00
|
|
|
if self.checkpoint_callback is True:
|
|
|
|
# init a default one
|
2019-11-05 15:41:59 +00:00
|
|
|
if self.logger is not None:
|
2020-02-01 18:36:50 +00:00
|
|
|
save_dir = (getattr(self.logger, 'save_dir', None) or
|
|
|
|
getattr(self.logger, '_save_dir', None) or
|
|
|
|
self.default_save_path)
|
2019-11-05 15:41:59 +00:00
|
|
|
ckpt_path = os.path.join(
|
2020-02-01 18:36:50 +00:00
|
|
|
save_dir,
|
2019-11-05 15:41:59 +00:00
|
|
|
self.logger.name,
|
|
|
|
f'version_{self.logger.version}',
|
|
|
|
"checkpoints"
|
|
|
|
)
|
2019-10-22 01:16:51 +00:00
|
|
|
else:
|
2019-11-05 15:41:59 +00:00
|
|
|
ckpt_path = os.path.join(self.default_save_path, "checkpoints")
|
2019-10-22 01:16:51 +00:00
|
|
|
|
2020-03-05 04:02:19 +00:00
|
|
|
# when no val step is defined, use 'loss' otherwise 'val_loss'
|
|
|
|
train_step_only = not self.is_overriden('validation_step')
|
|
|
|
monitor_key = 'loss' if train_step_only else 'val_loss'
|
|
|
|
|
2020-03-03 02:50:38 +00:00
|
|
|
self.ckpt_path = ckpt_path
|
2020-03-05 04:02:19 +00:00
|
|
|
os.makedirs(ckpt_path, exist_ok=True)
|
2019-10-22 01:16:51 +00:00
|
|
|
self.checkpoint_callback = ModelCheckpoint(
|
2020-03-05 04:02:19 +00:00
|
|
|
filepath=ckpt_path,
|
|
|
|
monitor=monitor_key
|
2019-10-22 01:16:51 +00:00
|
|
|
)
|
|
|
|
elif self.checkpoint_callback is False:
|
|
|
|
self.checkpoint_callback = None
|
|
|
|
|
2020-03-03 02:50:38 +00:00
|
|
|
self.ckpt_path = ckpt_path
|
|
|
|
|
2019-10-22 01:16:51 +00:00
|
|
|
if self.checkpoint_callback:
|
|
|
|
# set the path for the callbacks
|
|
|
|
self.checkpoint_callback.save_function = self.save_checkpoint
|
|
|
|
|
|
|
|
# if checkpoint callback used, then override the weights path
|
2020-03-03 20:16:57 +00:00
|
|
|
self.weights_save_path = self.checkpoint_callback.dirpath
|
2019-10-22 01:16:51 +00:00
|
|
|
|
|
|
|
# if weights_save_path is still none here, set to current working dir
|
|
|
|
if self.weights_save_path is None:
|
|
|
|
self.weights_save_path = self.default_save_path
|
|
|
|
|
2020-01-26 14:42:57 +00:00
|
|
|
def configure_early_stopping(self, early_stop_callback):
|
2020-03-02 22:12:22 +00:00
|
|
|
if early_stop_callback is True or None:
|
2019-10-22 01:16:51 +00:00
|
|
|
self.early_stop_callback = EarlyStopping(
|
|
|
|
monitor='val_loss',
|
|
|
|
patience=3,
|
2020-01-23 16:12:51 +00:00
|
|
|
strict=True,
|
2019-10-22 01:16:51 +00:00
|
|
|
verbose=True,
|
|
|
|
mode='min'
|
|
|
|
)
|
|
|
|
self.enable_early_stop = True
|
|
|
|
elif not early_stop_callback:
|
|
|
|
self.early_stop_callback = None
|
|
|
|
self.enable_early_stop = False
|
|
|
|
else:
|
|
|
|
self.early_stop_callback = early_stop_callback
|
|
|
|
self.enable_early_stop = True
|