callbacks.py 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
'''
Code here is mostly based on the code from the torchsample and Keras
'''
import numpy as np
import os
import time
import shutil
import datetime
import warnings
import torch

12
from torch import nn
13
from tqdm import tqdm
14
from copy import deepcopy
15
from .wdtypes import *
16

17 18 19 20

def _get_current_time():
    return datetime.datetime.now().strftime("%B %d, %Y - %I:%M%p")

21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class CallbackContainer(object):
    """
    Container holding a list of callbacks.
    """
    def __init__(self, callbacks:Optional[List]=None, queue_length:int=10):
        instantiated_callbacks = []
        if callbacks is not None:
            for callback in callbacks:
                if isinstance(callback, type): instantiated_callbacks.append(callback())
                else: instantiated_callbacks.append(callback)
        self.callbacks = [c for c in instantiated_callbacks]
        self.queue_length = queue_length

    def set_params(self, params):
        for callback in self.callbacks:
            callback.set_params(params)

39
    def set_model(self, model:nn.Module):
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        self.model = model
        for callback in self.callbacks:
            callback.set_model(model)

    def on_epoch_begin(self, epoch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        for callback in self.callbacks:
            callback.on_epoch_begin(epoch, logs)

    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        for callback in self.callbacks:
            callback.on_epoch_end(epoch, logs)

    def on_batch_begin(self, batch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        for callback in self.callbacks:
            callback.on_batch_begin(batch, logs)

    def on_batch_end(self, batch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        for callback in self.callbacks:
            callback.on_batch_end(batch, logs)

    def on_train_begin(self, logs:Optional[Dict]=None):
        logs = logs or {}
        logs['start_time'] = _get_current_time()
        for callback in self.callbacks:
            callback.on_train_begin(logs)

    def on_train_end(self, logs:Optional[Dict]=None):
        logs = logs or {}
72 73 74
        # logs['final_loss'] = self.model.history.epoch_losses[-1],
        # logs['best_loss'] = min(self.model.history.epoch_losses),
        # logs['stop_time'] = _get_current_time()
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        for callback in self.callbacks:
            callback.on_train_end(logs)


class Callback(object):
    """
    Abstract base class used to build new callbacks.
    """

    def __init__(self):
        pass

    def set_params(self, params):
        self.params = params

90
    def set_model(self, model:nn.Module):
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        self.model = model

    def on_epoch_begin(self, epoch:int, logs:Optional[Dict]=None):
        pass

    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
        pass

    def on_batch_begin(self, batch:int, logs:Optional[Dict]=None):
        pass

    def on_batch_end(self, batch:int, logs:Optional[Dict]=None):
        pass

    def on_train_begin(self, logs:Optional[Dict]=None):
        pass

    def on_train_end(self, logs:Optional[Dict]=None):
        pass


class History(Callback):
    """
    Callback that records events into a `History` object.
    """

    def on_train_begin(self, logs:Optional[Dict]=None):
        self.epoch = []
J
jrzaurin 已提交
119
        self._history = {}
120

121 122 123 124 125 126
    def on_epoch_begin(self, epoch:int, logs:Optional[Dict]=None):
        # avoid mutation during epoch run
        logs = deepcopy(logs) or {}
        for k, v in logs.items():
            self._history.setdefault(k, []).append(v)

127 128 129 130
    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        self.epoch.append(epoch)
        for k, v in logs.items():
J
jrzaurin 已提交
131
            self._history.setdefault(k, []).append(v)
132 133


134
class LRHistory(Callback):
135 136 137
    def __init__(self, n_epochs):
        super(LRHistory, self).__init__()
        self.n_epochs = n_epochs
138 139 140 141 142 143

    def on_epoch_begin(self, epoch:int, logs:Optional[Dict]=None):
        if epoch==0 and self.model.lr_scheduler:
            self.model.lr_history = {}
            if self.model.lr_scheduler.__class__.__name__ == 'MultipleLRScheduler':
                for model_name, opt in self.model.optimizer._optimizers.items():
144 145 146 147 148
                    if model_name in self.model.lr_scheduler._schedulers:
                        for group_idx, group in enumerate(opt.param_groups):
                            self.model.lr_history.setdefault(
                                ("_").join(['lr', model_name, str(group_idx)]),[]
                                ).append(group['lr'])
149
            elif not self.model.cyclic:
150 151 152
                for group_idx, group in enumerate(self.model.optimizer.param_groups):
                    self.model.lr_history.setdefault(
                        ("_").join(['lr', str(group_idx)]),[]).append(group['lr'])
153 154 155 156 157

    def on_batch_end(self, batch:int, logs:Optional[Dict]=None):
        if self.model.lr_scheduler:
            if self.model.lr_scheduler.__class__.__name__ == 'MultipleLRScheduler':
                for model_name, opt in self.model.optimizer._optimizers.items():
158 159 160 161 162 163
                    if model_name in self.model.lr_scheduler._schedulers:
                        if 'cycl' in self.model.lr_scheduler._schedulers[model_name].__class__.__name__.lower():
                            for group_idx, group in enumerate(opt.param_groups):
                                self.model.lr_history.setdefault(
                                    ("_").join(['lr', model_name, str(group_idx)]),[]
                                    ).append(group['lr'])
164
            elif self.model.cyclic:
165 166 167
                for group_idx, group in enumerate(self.model.optimizer.param_groups):
                    self.model.lr_history.setdefault(
                        ("_").join(['lr', str(group_idx)]),[]).append(group['lr'])
168 169

    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
170
        if epoch != (self.n_epochs-1) and self.model.lr_scheduler:
171 172
            if self.model.lr_scheduler.__class__.__name__ == 'MultipleLRScheduler':
                for model_name, opt in self.model.optimizer._optimizers.items():
173 174 175 176 177 178
                    if model_name in self.model.lr_scheduler._schedulers:
                        if 'cycl' not in self.model.lr_scheduler._schedulers[model_name].__class__.__name__.lower():
                            for group_idx, group in enumerate(opt.param_groups):
                                self.model.lr_history.setdefault(
                                    ("_").join(['lr', model_name, str(group_idx)]),
                                    []).append(group['lr'])
179
            elif not self.model.cyclic:
180 181 182
                for group_idx, group in enumerate(self.model.optimizer.param_groups):
                    self.model.lr_history.setdefault(
                        ("_").join(['lr', str(group_idx)]),[]).append(group['lr'])
183 184


185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
class ModelCheckpoint(Callback):
    """
    Save the model after every epoch.
    """

    def __init__(self, filepath:str, monitor:str='val_loss', verbose:int=0,
                 save_best_only:bool=False, mode:str='auto', period:int=1,
                 max_save:int=-1):
        super(ModelCheckpoint, self).__init__()
        self.monitor = monitor
        self.verbose = verbose
        self.filepath = filepath
        self.save_best_only = save_best_only
        self.period = period
        self.epochs_since_last_save = 0
        self.max_save = max_save

202
        root_dir = ('/').join(filepath.split("/")[:-1])
203 204 205
        if not os.path.exists(root_dir):
            os.makedirs(root_dir)

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        if self.max_save > 0:
            self.old_files = []

        if mode not in ['auto', 'min', 'max']:
            warnings.warn('ModelCheckpoint mode %s is unknown, '
                          'fallback to auto mode.' % (mode),
                          RuntimeWarning)
            mode = 'auto'
        if mode == 'min':
            self.monitor_op = np.less
            self.best = np.Inf
        elif mode == 'max':
            self.monitor_op = np.greater
            self.best = -np.Inf
        else:
            if 'acc' in self.monitor or self.monitor.startswith('fmeasure'):
                self.monitor_op = np.greater
                self.best = -np.Inf
            else:
                self.monitor_op = np.less
                self.best = np.Inf

    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
        logs = logs or {}
        self.epochs_since_last_save += 1
        if self.epochs_since_last_save >= self.period:
            self.epochs_since_last_save = 0
233
            filepath = '{}_{}'.format(self.filepath, epoch+1)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            if self.save_best_only:
                current = logs.get(self.monitor)
                if current is None:
                    warnings.warn('Can save best model only with %s available, '
                                  'skipping.' % (self.monitor), RuntimeWarning)
                else:
                    if self.monitor_op(current, self.best):
                        if self.verbose > 0:
                            print('\nEpoch %05d: %s improved from %0.5f to %0.5f,'
                                  ' saving model to %s'
                                  % (epoch + 1, self.monitor, self.best,
                                     current, filepath))
                        self.best = current
                        torch.save(self.model.state_dict(), filepath)
                        if self.max_save > 0:
                            if len(self.old_files) == self.max_save:
                                try:
                                    os.remove(self.old_files[0])
                                except:
                                    pass
                                self.old_files = self.old_files[1:]
255
                            self.old_files.append(filepath)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                    else:
                        if self.verbose > 0:
                            print('\nEpoch %05d: %s did not improve from %0.5f' %
                                  (epoch + 1, self.monitor, self.best))
            else:
                if self.verbose > 0:
                    print('\nEpoch %05d: saving model to %s' % (epoch + 1, filepath))
                torch.save(self.model.state_dict(), filepath)
                if self.max_save > 0:
                    if len(self.old_files) == self.max_save:
                        try:
                            os.remove(self.old_files[0])
                        except:
                            pass
                        self.old_files = self.old_files[1:]
271
                    self.old_files.append(filepath)
272 273 274 275 276 277 278


class EarlyStopping(Callback):
    """
    Stop training when a monitored quantity has stopped improving.
    """

279
    def __init__(self, monitor:str='val_loss', min_delta:int=0, patience:int=10,
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        verbose:int=0,mode:str='auto', baseline:Optional[float]=None,
        restore_best_weights:bool=False):

        super(EarlyStopping,self).__init__()

        self.monitor = monitor
        self.baseline = baseline
        self.patience = patience
        self.verbose = verbose
        self.min_delta = min_delta
        self.wait = 0
        self.stopped_epoch = 0
        self.restore_best_weights = restore_best_weights
        self.state_dict = None

        if mode not in ['auto', 'min', 'max']:
            warnings.warn('EarlyStopping mode %s is unknown, '
                          'fallback to auto mode.' % mode,
                          RuntimeWarning)
            mode = 'auto'

        if mode == 'min':
            self.monitor_op = np.less
        elif mode == 'max':
            self.monitor_op = np.greater
        else:
            if 'acc' in self.monitor:
                self.monitor_op = np.greater
            else:
                self.monitor_op = np.less

        if self.monitor_op == np.greater:
            self.min_delta *= 1
        else:
            self.min_delta *= -1

    def on_train_begin(self, logs:Optional[Dict]=None):
        # Allow instances to be re-used
        self.wait = 0
        self.stopped_epoch = 0
        if self.baseline is not None:
            self.best = self.baseline
        else:
            self.best = np.Inf if self.monitor_op == np.less else -np.Inf

    def on_epoch_end(self, epoch:int, logs:Optional[Dict]=None):
        current = self.get_monitor_value(logs)
        if current is None:
            return

        if self.monitor_op(current - self.min_delta, self.best):
            self.best = current
            self.wait = 0
            if self.restore_best_weights:
                self.state_dict = self.model.state_dict()
        else:
            self.wait += 1
            if self.wait >= self.patience:
                self.stopped_epoch = epoch
                self.model.early_stop = True
                if self.restore_best_weights:
                    if self.verbose > 0:
                        print('Restoring model weights from the end of '
                              'the best epoch')
                    self.model.load_state_dict(self.state_dict)

    def on_train_end(self, logs:Optional[Dict]=None):
        if self.stopped_epoch > 0 and self.verbose > 0:
            print('Epoch %05d: early stopping' % (self.stopped_epoch + 1))

    def get_monitor_value(self, logs):
        monitor_value = logs.get(self.monitor)
        if monitor_value is None:
            warnings.warn('Early stopping conditioned on metric `%s` '
                'which is not available. Available metrics are: %s' %
                (self.monitor, ','.join(list(logs.keys()))), RuntimeWarning
            )
J
jrzaurin 已提交
357
        return monitor_value