コード例 #1
0
def create_callbacks(model, training_model, prediction_model, validation_generator, args):
    callbacks = []

    # save the model
    if args.snapshots:
        # ensure directory created first; otherwise h5py will error after epoch.
        makedirs(args.snapshot_path)
        checkpoint = keras.callbacks.ModelCheckpoint(
            os.path.join(
                args.snapshot_path,
                '{backbone}_{dataset_type}_{{epoch:02d}}.h5'.format(backbone=args.backbone, dataset_type=args.dataset_type)
            ),
            verbose=1
        )
        checkpoint = RedirectModel(checkpoint, model)
        callbacks.append(checkpoint)

    tensorboard_callback = None

    if args.tensorboard_dir:
        tensorboard_callback = keras.callbacks.TensorBoard(
            log_dir                = args.tensorboard_dir,
            histogram_freq         = 0,
            batch_size             = args.batch_size,
            write_graph            = False,
            write_grads            = False,
            write_images           = False,
            embeddings_freq        = 0,
            embeddings_layer_names = None,
            embeddings_metadata    = None
        )
        callbacks.append(tensorboard_callback)

    if args.evaluation and validation_generator:
        if args.dataset_type == 'coco':
            from keras_retinanet.callbacks.coco import CocoEval

            # use prediction model for evaluation
            evaluation = CocoEval(validation_generator, tensorboard=tensorboard_callback)
        else:
            evaluation = Evaluate(validation_generator, tensorboard=tensorboard_callback)
        evaluation = RedirectModel(evaluation, prediction_model)
        callbacks.append(evaluation)

    callbacks.append(keras.callbacks.ReduceLROnPlateau(
        monitor  = 'loss',
        factor   = 0.1,
        patience = 2,
        verbose  = 1,
        mode     = 'auto',
        epsilon  = 0.0001,
        cooldown = 0,
        min_lr   = 0
    ))

    return callbacks
コード例 #2
0
ファイル: train.py プロジェクト: kunnareekr/DeepForest
def create_callbacks(model, training_model, prediction_model,
                     validation_generator, args, experiment,
                     DeepForest_config):
    """ Creates the callbacks to use during training.

    Args
        model: The base model.
        training_model: The model that is used for training.
        prediction_model: The model that should be used for validation.
        validation_generator: The generator for creating validation data.
        args: parseargs args object.

    Returns:
        A list of callbacks used for training.
    """
    callbacks = []

    tensorboard_callback = None

    if args.tensorboard_dir:
        tensorboard_callback = keras.callbacks.TensorBoard(
            log_dir=args.tensorboard_dir,
            histogram_freq=0,
            batch_size=args.batch_size,
            write_graph=True,
            write_grads=False,
            write_images=False,
            embeddings_freq=0,
            embeddings_layer_names=None,
            embeddings_metadata=None)
        callbacks.append(tensorboard_callback)

    if args.evaluation and validation_generator:

        evaluation = Evaluate(validation_generator,
                              tensorboard=tensorboard_callback,
                              experiment=experiment,
                              save_path=args.save_path,
                              score_threshold=args.score_threshold,
                              DeepForest_config=DeepForest_config)

        evaluation = RedirectModel(evaluation, prediction_model)
        callbacks.append(evaluation)

    # save the model
    if args.snapshots:
        # ensure directory created first; otherwise h5py will error after epoch.
        makedirs(args.snapshot_path)
        checkpoint = keras.callbacks.ModelCheckpoint(os.path.join(
            args.snapshot_path,
            '{backbone}_{{epoch:02d}}.h5'.format(backbone=args.backbone)),
                                                     verbose=1,
                                                     save_best_only=True,
                                                     monitor="mAP",
                                                     mode='max')
        checkpoint = RedirectModel(checkpoint, model)
        callbacks.append(checkpoint)

    callbacks.append(
        keras.callbacks.ReduceLROnPlateau(monitor='loss',
                                          factor=0.1,
                                          patience=2,
                                          verbose=1,
                                          mode='auto',
                                          epsilon=0.0001,
                                          cooldown=0,
                                          min_lr=0))

    #Neon Callbacks
    site = DeepForest_config["evaluation_site"]

    #if site=="OSBS":
    #jaccard=jaccardCallback(validation_generator,DeepForest_config=DeepForest_config,save_path=args.save_path,experiment=experiment)
    #jaccard = RedirectModel(jaccard, prediction_model)
    #callbacks.append(jaccard)

    recall = recallCallback(site=site,
                            generator=validation_generator,
                            save_path=args.save_path,
                            DeepForest_config=DeepForest_config,
                            score_threshold=args.score_threshold,
                            experiment=experiment)

    recall = RedirectModel(recall, prediction_model)

    callbacks.append(recall)

    #Neon mean IoU precision
    #create the NEON mAP generator
    NEON_generator = create_NEON_generator(args, site, DeepForest_config)

    neon_evaluation = NEONmAP(NEON_generator,
                              experiment=experiment,
                              save_path=args.save_path,
                              score_threshold=args.score_threshold,
                              DeepForest_config=DeepForest_config)

    neon_evaluation = RedirectModel(neon_evaluation, prediction_model)
    callbacks.append(neon_evaluation)

    return callbacks