Ejemplo n.º 1
0
def process(cf):
    # Enable log file
    sys.stdout = Logger(cf.log_file)
    print(' ---> Init experiment: ' + cf.exp_name + ' <---')

    # Create the data generators
    train_gen, valid_gen, test_gen = Dataset_Generators().make(cf)

    # Create the optimizer
    print('\n > Creating optimizer...')
    optimizer = Optimizer_Factory().make(cf)

    # Build model
    print('\n > Building model...')
    model = Model_Factory().make(cf, optimizer)

    # Create the callbacks
    print('\n > Creating callbacks...')
    cb = Callbacks_Factory().make(cf, valid_gen)

    if cf.train_model:
        # Train the model
        model.train(train_gen, valid_gen, cb)

    if cf.test_model:
        # Compute test metrics
        model.test(test_gen)

    if cf.pred_model:
        # Compute test metrics
        model.predict(test_gen, tag='pred')

    # Finish
    print(' ---> Finish experiment: ' + cf.exp_name + ' <---')
Ejemplo n.º 2
0
def process(configuration):
    # Load configuration
    cf = configuration.load()

    # Enable log file
    sys.stdout = Logger(cf.log_file)
    print (' ---> Init experiment: ' + cf.exp_name + ' <---')

    # Create the data generators
    if cf.dataset.data_format != 'npz':
        train_gen, valid_gen, test_gen = Dataset_Generators().make(cf)
    else:
        train_gen, valid_gen, test_gen = None, None, None

    # Create the optimizer
    print ('\n > Creating optimizer...')
    optimizer = Optimizer_Factory().make(cf)

    # Build model
    print ('\n > Building model...')
    model = Model_Factory().make(cf, optimizer)

    # Create the callbacks
    print ('\n > Creating callbacks...')
    #cb = Callbacks_Factory().make(cf, valid_gen)
    cb = None

    try:
        if cf.train_model:
            # Train the model
            if cf.dataset.data_format == 'npz':
                model.train2(cf.dataset.load_data_func, cb)
            elif cf.dataset.data_format == 'folders':
                model.train(train_gen, valid_gen, cb)
            else:
                raise ValueError('Unknown data format')

        if cf.test_model:
            # Compute validation metrics
            model.test(valid_gen)
            # Compute test metrics
            model.test(test_gen)

        if cf.pred_model:
            # Compute validation metrics
            model.predict(valid_gen, tag='pred')
            # Compute test metrics
            model.predict(test_gen, tag='pred')

    except KeyboardInterrupt:
        # In case of early stopping, transfer the local files
        do_copy = raw_input('\033[93m KeyboardInterrupt \nDo you want to transfer files to {} ? ([y]/n) \033[0m'
                            .format(cf.final_savepath))
        if do_copy in ['', 'y']:
            # Copy result to shared directory
            configuration.copy_to_shared()
        raise

    # Finish
    print (' ---> Finish experiment: ' + cf.exp_name + ' <---')