5 from keras
import backend
as K
9 def __init__(self, filepath, base_model, monitor='val_loss', verbose=0,
10 save_best_only=
False, save_weights_only=
False,
11 mode=
'auto', period=1):
12 super(MultiGPUCheckpointCallback, self).
__init__()
22 if mode
not in [
'auto',
'min',
'max']:
23 warnings.warn(
'ModelCheckpoint mode %s is unknown, ' 24 'fallback to auto mode.' % (mode),
35 if 'acc' in self.
monitor or self.monitor.startswith(
'fmeasure'):
48 filepath = self.filepath.format(epoch=epoch + 1, **logs)
50 current = logs.get(self.
monitor)
52 warnings.warn(
'Can save best model only with %s available, ' 53 'skipping.' % (self.
monitor), RuntimeWarning)
57 print(
'Epoch %05d: %s improved from %0.5f to %0.5f,' 63 self.base_model.save_weights(filepath, overwrite=
True)
65 self.base_model.save(filepath, overwrite=
True)
68 print(
'Epoch %05d: %s did not improve' %
72 print(
'Epoch %05d: saving model to %s' % (epoch + 1, filepath))
74 self.base_model.save_weights(filepath, overwrite=
True)
76 self.base_model.save(filepath, overwrite=
True)
79 """ Detach model trained on GPUs from its encapsulation 81 :param m: obj, keras model 83 :return: obj, keras model 87 if l.name ==
'resnext':
94 """ Save detached from multi-GPU encapsulation model 95 (very small) modification from https://github.com/fchollet/keras/blob/master/keras/callbacks.py#L331 97 `filepath` can contain named formatting options, 98 which will be filled the value of `epoch` and 99 keys in `logs` (passed in `on_epoch_end`). 101 For example: if `filepath` is `weights.{epoch:02d}-{val_loss:.2f}.hdf5`, 102 then the model checkpoints will be saved with the epoch number and 103 the validation loss in the filename. 106 filepath: string, path to save the model file. 107 monitor: quantity to monitor. 108 verbose: verbosity mode, 0 or 1. 109 save_best_only: if `save_best_only=True`, 110 the latest best model according to 111 the quantity monitored will not be overwritten. 112 mode: one of {auto, min, max}. 113 If `save_best_only=True`, the decision 114 to overwrite the current save file is made 115 based on either the maximization or the 116 minimization of the monitored quantity. For `val_acc`, 117 this should be `max`, for `val_loss` this should 118 be `min`, etc. In `auto` mode, the direction is 119 automatically inferred from the name of the monitored quantity. 120 save_weights_only: if True, then only the model's weights will be 121 saved (`model.save_weights(filepath)`), else the full model 122 is saved (`model.save(filepath)`). 123 period: Interval (number of epochs) between checkpoints. 126 def __init__(self, filepath, monitor='val_loss', verbose=0,
127 save_best_only=
False, save_weights_only=
False,
128 mode=
'auto', period=1):
129 super(ModelCheckpointDetached, self).
__init__()
138 if mode
not in [
'auto',
'min',
'max']:
139 warnings.warn(
'ModelCheckpoint mode %s is unknown, ' 140 'fallback to auto mode.' % mode, RuntimeWarning)
150 if 'acc' in self.
monitor or self.monitor.startswith(
'fmeasure'):
162 filepath = self.filepath.format(epoch=epoch, **logs)
164 current = logs.get(self.
monitor)
166 warnings.warn(
'Can save best model only with %s available, ' 167 'skipping.' % self.
monitor, RuntimeWarning)
171 print(
'Epoch %05d: %s improved from %0.5f to %0.5f,' 172 ' saving model to %s' 177 detachmodel(self.model).save_weights(filepath, overwrite=
True)
182 print(
'Epoch %05d: %s did not improve' %
186 print(
'Epoch %05d: saving model to %s' % (epoch, filepath))
188 detachmodel(self.model).save_weights(filepath, overwrite=
True)
194 current_lr = K.get_value(self.model.optimizer.lr)
195 print "Learning rate:", current_lr
196 new_lr = current_lr * 0.95
197 K.set_value(self.model.optimizer.lr, new_lr)
198 new_lr = K.get_value(self.model.optimizer.lr)
199 print "New learning rate:", new_lr
204 current_lr = K.get_value(self.model.optimizer.lr)
205 print "Learning rate:", current_lr
206 new_lr = current_lr * 0.94
207 K.set_value(self.model.optimizer.lr, new_lr)
208 new_lr = K.get_value(self.model.optimizer.lr)
209 print "New learning rate:", new_lr
215 current_lr = K.get_value(self.model.optimizer.lr)
216 print "Learning rate:", current_lr
217 new_lr = current_lr * 0.94
218 K.set_value(self.model.optimizer.lr, new_lr)
219 new_lr = K.get_value(self.model.optimizer.lr)
220 print "New learning rate:", new_lr
225 def __init__(self, validation_generator, validation_steps):
228 self.
fil =
'/scratch/cvn/branch/log/resnet34' 234 with
open(self.
fil,
'ar+')
as fil:
235 if os.stat(self.
fil).st_size == 0:
236 self.losses.append([
'iter',
'acc',
'loss',
'val_acc',
'val_loss'])
244 self.losses.append([self.
iteration, logs.get(
'acc'), logs.get(
'loss'), val_acc, val_loss])
248 with
open(self.
fil,
'a')
as fil:
249 for iteration, acc, loss, val_acc, val_loss
in self.
losses:
250 fil.write(
str(iteration) +
' ' +
str(acc) +
' ' +
str(loss) +
' ' +
str(val_acc) +
' ' +
str(val_loss) +
'\n')
257 def on_epoch_begin(self, epoch, logs={}): 258 current_lr = K.get_value(self.model.optimizer.lr) 259 print "Learning rate:", current_lr 261 K.set_value(self.model.optimizer.lr, new_lr) 262 new_lr = K.get_value(self.model.optimizer.lr) 263 print "New learning rate:", new_lr 266 def on_train_begin(self, logs={}): 267 current_lr = K.get_value(self.model.optimizer.lr) 268 print "Learning rate:", current_lr 270 K.set_value(self.model.optimizer.lr, new_lr) 271 new_lr = K.get_value(self.model.optimizer.lr) 272 print "New learning rate:", new_lr 275 def on_train_end(self, logs={}): 278 def on_epoch_begin(self, epoch, logs={}): 281 def on_epoch_end(self, epoch, logs={}): 282 self.losses.append(logs.get('loss')) 283 y_pred = self.model.predict(self.validation_data[0]) 284 self.aucs.append(roc_auc_score(self.validation_data[1], y_pred)) 287 def on_batch_begin(self, batch, logs={}): 290 def on_batch_end(self, batch, logs={}):
def on_epoch_end(self, epoch, logs=None)
int open(const char *, int)
Opens a file descriptor.
def on_batch_end(self, batch, logs={})
def on_epoch_end(self, epoch, logs=None)
def __init__(self, filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
def on_epoch_end(self, epoch, logs={})
def on_train_begin(self, logs={})
def on_epoch_end(self, epoch, logs={})
void split(std::string const &s, char c, OutIter dest)
def on_epoch_end(self, epoch, logs={})
def __init__(self, filepath, base_model, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
def __init__(self, validation_generator, validation_steps)
def on_train_begin(self, logs={})