Python tensorflow.keras.callbacks.ModelCheckpoint() Examples
The following are 17
code examples of tensorflow.keras.callbacks.ModelCheckpoint().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.keras.callbacks
, or try the search function
.
Example #1
Source File: train.py From bcnn with MIT License | 6 votes |
def train(weights_path, epochs, batch_size, initial_epoch, kl_start_epoch, kl_alpha_increase_per_epoch): """Trains a model.""" print ('loading data...') # Loads or creates training data. input_shape, train, valid, train_targets, valid_targets = get_train_data() print ('getting model...') # Loads or creates model. model, checkpoint_path, kl_alpha = get_model(input_shape, scale_factor=len(train)/batch_size, weights_path=weights_path) # Sets callbacks. checkpointer = ModelCheckpoint(checkpoint_path, verbose=1, save_weights_only=True, save_best_only=True) scheduler = LearningRateScheduler(schedule) annealer = Callback() if kl_alpha is None else AnnealingCallback(kl_alpha, kl_start_epoch, kl_alpha_increase_per_epoch) print ('fitting model...') # Trains model. model.fit(train, train_targets, batch_size, epochs, initial_epoch=initial_epoch, callbacks=[checkpointer, scheduler, annealer], validation_data=(valid, valid_targets))
Example #2
Source File: deep_classifier.py From nlp-journey with Apache License 2.0 | 6 votes |
def train(self, batch_size=512, epochs=20): model = self.build_model() # early_stop配合checkpoint使用,可以得到val_loss最小的模型 early_stop = EarlyStopping(patience=3, verbose=1) checkpoint = ModelCheckpoint(os.path.join(self.model_path, 'weights.{epoch:03d}-{val_loss:.3f}.h5'), verbose=1, monitor='val_loss', save_best_only=True) history = model.fit(self.x_train, self.y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint, early_stop], validation_data=(self.x_test, self.y_test)) plot(history) return model
Example #3
Source File: train.py From object-localization with MIT License | 6 votes |
def main(): model = create_model(trainable=TRAINABLE) model.summary() if TRAINABLE: model.load_weights(WEIGHTS) train_datagen = DataGenerator(TRAIN_CSV) validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV)) optimizer = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss=loss, optimizer=optimizer, metrics=[]) checkpoint = ModelCheckpoint("model-{val_dice:.2f}.h5", monitor="val_dice", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_dice", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_dice", factor=0.2, patience=5, min_lr=1e-6, verbose=1, mode="max") model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTI_PROCESSING, shuffle=True, verbose=1)
Example #4
Source File: train.py From object-localization with MIT License | 6 votes |
def main(): model = create_model() train_datagen = DataGenerator(TRAIN_CSV) validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV)) optimizer = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss={"coords" : log_mse, "classes" : focal_loss()}, loss_weights={"coords" : 1, "classes" : 1}, optimizer=optimizer, metrics=[]) checkpoint = ModelCheckpoint("model-{val_iou:.2f}.h5", monitor="val_iou", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_iou", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_iou", factor=0.2, patience=10, min_lr=1e-7, verbose=1, mode="max") model.summary() model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTI_PROCESSING, shuffle=True, verbose=1)
Example #5
Source File: callbacks.py From DeepPoseKit with Apache License 2.0 | 6 votes |
def __init__( self, filepath, monitor="val_loss", verbose=0, save_best_only=True, mode="auto", save_freq="epoch", **kwargs ): super(ModelCheckpoint, self).__init__( filepath=filepath, monitor=monitor, verbose=verbose, save_best_only=save_best_only, mode=mode, save_freq=save_freq, **kwargs )
Example #6
Source File: train.py From object-localization with MIT License | 6 votes |
def main(): model = create_model() model.summary() train_datagen = DataGenerator(TRAIN_CSV) validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV)) model.compile(loss="mean_squared_error", optimizer="adam", metrics=[]) checkpoint = ModelCheckpoint("model-{val_iou:.2f}.h5", monitor="val_iou", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_iou", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_iou", factor=0.2, patience=10, min_lr=1e-7, verbose=1, mode="max") model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTI_PROCESSING, shuffle=True, verbose=1)
Example #7
Source File: train.py From bootcamp with Apache License 2.0 | 6 votes |
def fit_model(dsm: DeepSpeakerModel, working_dir: str, max_length: int = NUM_FRAMES, batch_size=BATCH_SIZE): batcher = LazyTripletBatcher(working_dir, max_length, dsm) # build small test set. test_batches = [] for _ in tqdm(range(200), desc='Build test set'): test_batches.append(batcher.get_batch_test(batch_size)) def test_generator(): while True: for bb in test_batches: yield bb def train_generator(): while True: yield batcher.get_random_batch(batch_size, is_test=False) checkpoint_name = dsm.m.name + '_checkpoint' checkpoint_filename = os.path.join(CHECKPOINTS_TRIPLET_DIR, checkpoint_name + '_{epoch}.h5') checkpoint = ModelCheckpoint(monitor='val_loss', filepath=checkpoint_filename, save_best_only=True) dsm.m.fit(x=train_generator(), y=None, steps_per_epoch=2000, shuffle=False, epochs=1000, validation_data=test_generator(), validation_steps=len(test_batches), callbacks=[checkpoint])
Example #8
Source File: siamese_similarity.py From nlp-journey with Apache License 2.0 | 5 votes |
def train(self, weights_only=True, call_back=False): model = self._build_model() if call_back: early_stopping = EarlyStopping(monitor='val_loss', patience=30) stamp = 'lstm_%d' % self.n_hidden checkpoint_dir = os.path.join( self.model_path, 'checkpoints/' + str(int(time.time())) + '/') if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) bst_model_path = checkpoint_dir + stamp + '.h5' if weights_only: model_checkpoint = ModelCheckpoint( bst_model_path, save_best_only=True, save_weights_only=True) else: model_checkpoint = ModelCheckpoint( bst_model_path, save_best_only=True) tensor_board = TensorBoard( log_dir=checkpoint_dir + "logs/{}".format(time.time())) callbacks = [early_stopping, model_checkpoint, tensor_board] else: callbacks = None model_trained = model.fit([self.x_train['left'], self.x_train['right']], self.y_train, batch_size=self.batch_size, epochs=self.epochs, validation_data=([self.x_val['left'], self.x_val['right']], self.y_val), verbose=1, callbacks=callbacks) if weights_only and not call_back: model.save_weights(os.path.join(self.model_path, 'weights_only.h5')) elif not weights_only and not call_back: model.save(os.path.join(self.model_path, 'model.h5')) self._save_config() plot(model_trained) return model
Example #9
Source File: cross_validation.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def run_fold(fold, model, epochs=20, iterations=None, evaluation_path="evaluation", draw_figures=True, callbacks=[], save_models=True): # Load sampling fold from disk fold_path = os.path.join(evaluation_path, "fold_" + str(fold), "sample_list.csv") training, validation = load_csv2fold(fold_path) # Reset Neural Network model weights model.reset_weights() # Initialize evaluation subdirectory for current fold subdir = os.path.join(evaluation_path, "fold_" + str(fold)) # Save model for each fold cb_model = ModelCheckpoint(os.path.join(subdir, "model.hdf5"), monitor="val_loss", verbose=1, save_best_only=True, mode="min") if save_models == True : cb_list = callbacks + [cb_model] else : cb_list = callbacks # Run training & validation history = model.evaluate(training, validation, epochs=epochs, iterations=iterations, callbacks=cb_list) # Backup current history dictionary backup_history(history.history, subdir) # Draw plots for the training & validation if draw_figures: plot_validation(history.history, model.metrics, subdir) #-----------------------------------------------------# # CSV Management # #-----------------------------------------------------# # Subfunction for writing a fold sampling to disk
Example #10
Source File: 训练.py From Semantic-segmentation-of-remote-sensing-images with Apache License 2.0 | 5 votes |
def train(args): model = SegNet() modelcheck = ModelCheckpoint(args['model'],monitor='val_acc',save_best_only=True,mode='max') callable = [modelcheck,tf.keras.callbacks.TensorBoard(log_dir='.')] train_set,val_set = get_train_val() train_numb = len(train_set) valid_numb = len(val_set) print ("the number of train data is",train_numb) print ("the number of val data is",valid_numb) H = model.fit(x=generateData(BS,train_set),steps_per_epoch=(train_numb//BS),epochs=EPOCHS,verbose=2, validation_data=generateValidData(BS,val_set),validation_steps=(valid_numb//BS),callbacks=callable) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() N = EPOCHS plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, N), H.history["acc"], label="train_acc") plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy on SegNet Satellite Seg") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend(loc="lower left") plt.savefig(args["plot"]) #获取参数
Example #11
Source File: main_IQA.py From CNNArt with Apache License 2.0 | 5 votes |
def get_callbacks(model_file, logging_file=None, early_stopping_patience=None, initial_learning_rate=0.01, lr_change_mode=None, verbosity=1): callbacks = list() # save the model callbacks.append(ModelCheckpoint(model_file, monitor='val_loss', save_best_only=True, mode='auto')) # records the basic metrics callbacks.append(CSVLogger(logging_file, append=True)) return callbacks
Example #12
Source File: train.py From bootcamp with Apache License 2.0 | 5 votes |
def fit_model_softmax(dsm: DeepSpeakerModel, kx_train, ky_train, kx_test, ky_test, batch_size=BATCH_SIZE, max_epochs=1000, initial_epoch=0): checkpoint_name = dsm.m.name + '_checkpoint' checkpoint_filename = os.path.join(CHECKPOINTS_SOFTMAX_DIR, checkpoint_name + '_{epoch}.h5') checkpoint = ModelCheckpoint(monitor='val_accuracy', filepath=checkpoint_filename, save_best_only=True) # if the accuracy does not increase by 0.1% over 20 epochs, we stop the training. early_stopping = EarlyStopping(monitor='val_accuracy', min_delta=0.001, patience=20, verbose=1, mode='max') # if the accuracy does not increase over 10 epochs, we reduce the learning rate by half. reduce_lr = ReduceLROnPlateau(monitor='val_accuracy', factor=0.5, patience=10, min_lr=0.0001, verbose=1) max_len_train = len(kx_train) - len(kx_train) % batch_size kx_train = kx_train[0:max_len_train] ky_train = ky_train[0:max_len_train] max_len_test = len(kx_test) - len(kx_test) % batch_size kx_test = kx_test[0:max_len_test] ky_test = ky_test[0:max_len_test] dsm.m.fit(x=kx_train, y=ky_train, batch_size=batch_size, epochs=initial_epoch + max_epochs, initial_epoch=initial_epoch, verbose=1, shuffle=True, validation_data=(kx_test, ky_test), callbacks=[early_stopping, reduce_lr, checkpoint])
Example #13
Source File: train.py From object-localization with MIT License | 5 votes |
def main(): model = create_model(trainable=TRAINABLE) model.summary() if TRAINABLE: model.load_weights(WEIGHTS) train_datagen = DataGenerator(TRAIN_CSV) val_generator = DataGenerator(VALIDATION_CSV, rnd_rescale=False, rnd_multiply=False, rnd_crop=False, rnd_flip=False, debug=False) validation_datagen = Validation(generator=val_generator) learning_rate = LEARNING_RATE if TRAINABLE: learning_rate /= 10 optimizer = SGD(lr=learning_rate, decay=LR_DECAY, momentum=0.9, nesterov=False) model.compile(loss=detection_loss(), optimizer=optimizer, metrics=[]) checkpoint = ModelCheckpoint("model-{val_iou:.2f}.h5", monitor="val_iou", verbose=1, save_best_only=True, save_weights_only=True, mode="max") stop = EarlyStopping(monitor="val_iou", patience=PATIENCE, mode="max") reduce_lr = ReduceLROnPlateau(monitor="val_iou", factor=0.6, patience=5, min_lr=1e-6, verbose=1, mode="max") model.fit_generator(generator=train_datagen, epochs=EPOCHS, callbacks=[validation_datagen, checkpoint, reduce_lr, stop], workers=THREADS, use_multiprocessing=MULTITHREADING, shuffle=True, verbose=1)
Example #14
Source File: train.py From keras-mobile-detectnet with MIT License | 4 votes |
def main(batch_size: int = 24, epochs: int = 384, train_path: str = 'train', val_path: str = 'val', weights=None, workers: int = 8): # We use an extra input during training to discount bounding box loss when a class is not present in an image. discount_input = Input(shape=(7, 7), name='discount') keras_model = MobileDetectNetModel.complete_model(extra_inputs=[discount_input]) keras_model.summary() if weights is not None: keras_model.load_weights(weights, by_name=True) train_seq = MobileDetectNetSequence(train_path, stage="train", batch_size=batch_size) val_seq = MobileDetectNetSequence(val_path, stage="val", batch_size=batch_size) callbacks = [] def region_loss(classes): def loss_fn(y_true, y_pred): # Don't penalize bounding box errors when there is no object present return 10 * (classes * K.abs(y_pred[:, :, :, 0] - y_true[:, :, :, 0]) + classes * K.abs(y_pred[:, :, :, 1] - y_true[:, :, :, 1]) + classes * K.abs(y_pred[:, :, :, 2] - y_true[:, :, :, 2]) + classes * K.abs(y_pred[:, :, :, 3] - y_true[:, :, :, 3])) return loss_fn keras_model.compile(optimizer=Nadam(lr=0.001), loss=['mean_absolute_error', region_loss(discount_input), 'binary_crossentropy']) filepath = "weights-{epoch:02d}-{val_loss:.4f}-multi-gpu.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min') callbacks.append(checkpoint) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=0.00001, verbose=1) callbacks.append(reduce_lr) try: os.mkdir('logs') except FileExistsError: pass tensorboard = TensorBoard(log_dir='logs/%s' % time.strftime("%Y-%m-%d_%H-%M-%S")) callbacks.append(tensorboard) keras_model.fit_generator(train_seq, validation_data=val_seq, epochs=epochs, steps_per_epoch=np.ceil(len(train_seq) / batch_size), validation_steps=np.ceil(len(val_seq) / batch_size), callbacks=callbacks, use_multiprocessing=True, workers=workers, shuffle=True)
Example #15
Source File: kashgari_entity_extractor.py From rasa_nlu_gq with Apache License 2.0 | 4 votes |
def train(self, training_data, cfg, **kwargs): labeling_model = eval("labeling." + self.labeling_model) epochs = self.component_config.get('epochs') batch_size = self.component_config.get('batch_size') validation_split = self.component_config.get('validation_split') patience = self.component_config.get('patience') factor = self.component_config.get('factor') verbose = self.component_config.get('verbose') filtered_entity_examples = self.filter_trainable_entities(training_data.training_examples) X, Y = self._create_dataset(filtered_entity_examples) train_x, validate_x, train_y, validate_y = train_test_split( X, Y, test_size=validation_split, random_state=100) self.model = labeling_model(self.bert_embedding) checkpoint = ModelCheckpoint( 'entity_weights.h5', monitor='val_loss', save_best_only=True, save_weights_only=False, verbose=verbose) early_stopping = EarlyStopping( monitor='val_loss', patience=patience) reduce_lr = ReduceLROnPlateau( monitor='val_loss', factor=factor, patience=patience, verbose=verbose) self.model.fit( train_x, train_y, validate_x, validate_y, epochs=epochs, batch_size=batch_size, callbacks=[checkpoint, early_stopping, reduce_lr] )
Example #16
Source File: cross_validation.py From MIScnn with GNU General Public License v3.0 | 4 votes |
def cross_validation(sample_list, model, k_fold=3, epochs=20, iterations=None, evaluation_path="evaluation", draw_figures=False, run_detailed_evaluation=False, callbacks=[], save_models=True, return_output=False): # Initialize result cache if return_output : validation_results = [] # Randomly permute the sample list samples_permuted = np.random.permutation(sample_list) # Split sample list into folds folds = np.array_split(samples_permuted, k_fold) fold_indices = list(range(len(folds))) # Start cross-validation for i in fold_indices: # Reset Neural Network model weights model.reset_weights() # Subset training and validation data set training = np.concatenate([folds[x] for x in fold_indices if x!=i], axis=0) validation = folds[i] # Initialize evaluation subdirectory for current fold subdir = create_directories(evaluation_path, "fold_" + str(i)) # Save model for each fold cb_model = ModelCheckpoint(os.path.join(subdir, "model.hdf5"), monitor="val_loss", verbose=1, save_best_only=True, mode="min") if save_models == True : cb_list = callbacks + [cb_model] else : cb_list = callbacks # Run training & validation history = model.evaluate(training, validation, epochs=epochs, iterations=iterations, callbacks=cb_list) # Backup current history dictionary if return_output : validation_results.append(history.history) else : backup_history(history.history, subdir) # Draw plots for the training & validation if draw_figures: plot_validation(history.history, model.metrics, subdir) # Make a detailed validation of the current cv-fold if run_detailed_evaluation: detailed_validation(validation, model, subdir) # Return the validation results if return_output : return validation_results #-----------------------------------------------------# # Splitted k-fold Cross-Validation # #-----------------------------------------------------#
Example #17
Source File: kashgari_intent_classifier.py From rasa_nlu_gq with Apache License 2.0 | 4 votes |
def train(self, training_data, cfg, **kwargs): classifier_model = eval("clf." + self.classifier_model) epochs = self.component_config.get('epochs') batch_size = self.component_config.get('batch_size') validation_split = self.component_config.get('validation_split') patience = self.component_config.get('patience') factor = self.component_config.get('factor') verbose = self.component_config.get('verbose') X, Y = [], [] for msg in training_data.intent_examples: X.append(self.tokenizer.tokenize(msg.text)) Y.append(msg.get('intent')) train_x, validate_x, train_y, validate_y = train_test_split( X, Y, test_size=validation_split, random_state=100) self.bert_embedding.processor.add_bos_eos = False self.model = classifier_model(self.bert_embedding) checkpoint = ModelCheckpoint( 'intent_weights.h5', monitor='val_loss', save_best_only=True, save_weights_only=False, verbose=verbose) early_stopping = EarlyStopping( monitor='val_loss', patience=patience) reduce_lr = ReduceLROnPlateau( monitor='val_loss', factor=factor, patience=patience, verbose=verbose) self.model.fit( train_x, train_y, validate_x, validate_y, epochs=epochs, batch_size=batch_size, callbacks=[checkpoint, early_stopping, reduce_lr] )