Ejemplo n.º 1
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)

    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 + ' <---')
Ejemplo n.º 2
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.º 3
0
    def run(self, configuration):
        # Load configuration
        self.cf = configuration.load()
        self.cf.data_format = K.image_data_format()
        if self.cf.train_model:
            # Enable log file
            sys.stdout = Logger(self.cf.log_file)
        print(' ---> Starting experiment: ' + self.cf.experiment_name +
              ' <---')

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

        # Build model
        print('\n > Building model...')
        model = CycleGAN(self.cf)
        model.make()

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

        try:
            if self.cf.train_model:
                # Train the model
                model.train(train_gen, cb)
                # losses saved in train function to JSON
                #loss_value=hist.history['loss']
                #val_loss_value=hist.history['val_loss']
                #np.save(join(cf.savepath,'loss_' + str(cf.experiment_name)+ '.npy'),loss_value)
                #np.save(join(cf.savepath,'val_loss_' + str(cf.experiment_name)+ '.npy'),val_loss_value)

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

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

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

        # Finish
        print(' ---> Finished experiment: ' + self.cf.experiment_name +
              ' <---')