Esempio n. 1
0
def main(p: Parameters, o: Options):
    profiler = Profiler()
    profiler.event("start")
    if o.verbose:
        print(f"Experimenting with parameters: {p}")
    accuracy_results = experiment(p, o)
    profiler.event("end")
    print(profiler.summary(human=True))
    config.save_accuracy(accuracy_results)
    def do_train():
        model, optimizer = p.model.make_model_and_optimizer(
            dataset.input_shape, dataset.num_classes, o.use_cuda)

        def generate_epochs_callbacks():
            epochs_callbacks = []
            for epoch in p.savepoints:

                def callback(epoch=epoch):
                    scores = training.eval_scores(
                        model, dataset, p.transformations,
                        TransformationStrategy.random_sample,
                        o.get_eval_options())
                    if o.verbose_general:
                        print(
                            f"Saving model {model.name} at epoch {epoch}/{p.epochs}."
                        )
                    training.save_model(p, o, model, scores,
                                        experiment.model_path(p, epoch))

                epochs_callbacks.append((epoch, callback))

            return dict(epochs_callbacks)

        epochs_callbacks = generate_epochs_callbacks()

        # TRAINING
        if 0 in p.savepoints:
            scores = training.eval_scores(model, dataset, p.transformations,
                                          TransformationStrategy.random_sample,
                                          o.get_eval_options())
            print(f"Saving model {model.name} at epoch {0} (before training).")
            training.save_model(p, o, model, scores,
                                experiment.model_path(p, 0))
        pr = Profiler()
        pr.event("start")
        scores, history = training.run(p,
                                       o,
                                       model,
                                       optimizer,
                                       dataset,
                                       epochs_callbacks=epochs_callbacks)
        pr.event("end")
        print(pr.summary(human=True))

        training.print_scores(scores)
        return model, history, scores