Ejemplo n.º 1
0
    def __init__(self, metadata, session=None):
        super(LogicModel, self).__init__(metadata)

        # freiburg
        perf_fn = str(Path(__file__).parents[4] / "perf_matrix.csv")
        feat_fn = str(Path(__file__).parents[4] / "meta_features.csv")
        af = AFCsvFacade(perf_fn=perf_fn, feat_fn=feat_fn)

        config = {
            'StandardScaler': False,
            'fgroup_all': True,
            'imputer_strategy': 'mean',
            'pca': False,
            'selector': 'PairwiseClassifier',
            'classifier': 'RandomForest',
            'rf:bootstrap': False,
            'rf:criterion': 'gini',
            'rf:max_depth': 132,
            'rf:max_features': 'log2',
            'rf:min_samples_leaf': 3,
            'rf:min_samples_split': 3,
            'rf:n_estimators': 68
        }

        LOGGER.info('--------- Fitting AF model ----------')

        # fit AF using a loaded configuration on all data!
        af.fit(config=config)

        LOGGER.info('--------- Model.metadata ----------')
        LOGGER.info('path: %s', self.metadata.get_dataset_name())
        LOGGER.info('shape:  %s', self.metadata.get_tensor_size(0))
        LOGGER.info('size: %s', self.metadata.size())
        LOGGER.info('num_class:  %s', self.metadata.get_output_size())

        test_metadata_filename = self.metadata.get_dataset_name().replace(
            'train', 'test') + '/metadata.textproto'
        self.num_test = [
            int(line.split(':')[1])
            for line in open(test_metadata_filename, 'r').readlines()[:3]
            if 'sample_count' in line
        ][0]
        LOGGER.info('num_test:  %d', self.num_test)

        self.timers = {
            'train': skeleton.utils.Timer(),
            'test': skeleton.utils.Timer()
        }
        self.info = {
            'dataset': {
                'path': self.metadata.get_dataset_name(),
                'shape': self.metadata.get_tensor_size(0),
                'size': self.metadata.size(),
                'num_class': self.metadata.get_output_size()
            },
            'loop': {
                'epoch': 0,
                'test': 0,
                'best_score': 0.0
            },
            'condition': {
                'first': {
                    'train': True,
                    'valid': True,
                    'test': True
                }
            },
            'terminate': False
        }

        # TODO: adaptive logic for hyper parameter

        # 支持导入配置文件,可修改config_path
        # config_path = 'AutoDL_sample_code_submission/configs/configs_deepwis/config_with_chucky_freiburg_params_and_dataloading.yaml'
        # default_path = 'AutoDL_sample_code_submission/configs/config_deepblue.yaml'
        # print('Path: ', config_path)
        # get feature vector containing num_channels, num_classes, num_train, resolution_0
        train_metadata_filename = self.metadata.get_dataset_name(
        ) + '/metadata.textproto'
        num_train = \
        [int(line.split(':')[1]) for line in open(train_metadata_filename, 'r').readlines()[:3] if
            'sample_count' in line][0]
        LOGGER.info('num_test:  %d', num_train)

        _, resolution_0, _, num_channels = self.metadata.get_tensor_shape()
        num_classes = self.info['dataset']['num_class']
        feature = [num_channels, num_classes, num_train, resolution_0]

        pred_config_name = af.predict(
            np.array(feature))["pseudo_instance"][0][0]

        # pred_config_name = AFCsvFacade.load_and_predict(vec=np.array(feature), load_fn=model_fn)

        LOGGER.info("AF suggesting to use config: {}".format(pred_config_name))

        config_path = Path(__file__).parents[
            4] / "configs" / "effnet_optimized_per_dataset_new_cs_new_data_03_14" / pred_config_name
        config_path = config_path.with_suffix(".yaml")

        try:
            with config_path.open() as in_stream:
                model_config = yaml.safe_load(in_stream)
                LOGGER.info("Using config: {}".format(config_path))
        except:
            with config_path.with_name("default.yaml").open() as in_stream:
                model_config = yaml.safe_load(in_stream)
                LOGGER.info("Using default config.")

        self.hyper_params = model_config["autocv"]
        skip_valid_after_test = min(
            10, max(3, int(self.info['dataset']['size'] // 1000)))
        self.hyper_params["conditions"][
            "skip_valid_after_test"] = skip_valid_after_test

        self.checkpoints = []
        LOGGER.info('[init] build')

        self.build()
        LOGGER.info('[init] session')

        self.dataloaders = {'train': None, 'valid': None, 'test': None}
        self.is_skip_valid = True
        self.changed_model = False
        self.save_res = False
        self.preds = []
        self.results = []
        # self.last_best_score=0
        # self.is_fuse=False
        LOGGER.info('[init] done')
Ejemplo n.º 2
0
from autofolio.facade.af_csv_facade import AFCsvFacade

__author__ = "Marius Lindauer"
__license__ = "BSD"
__version__ = "2.1.0"

perf_fn = "/home/lindauer/git/AutoDLComp19/experiments/new_config_space_after_March_4/eval_new_cs_old_data_approx_600/perf_matrix.csv"
feat_fn = "/home/lindauer/git/AutoDLComp19/src/meta_features/non-nn/old_data/meta_features.csv"

# will be created (or overwritten) by AutoFolio
old_model_fn = "af_model.pkl"
new_model_fn = "af_model_final.pkl"
config_fn = "config.pkl"

af = AFCsvFacade(perf_fn=perf_fn, feat_fn=feat_fn)

#with open(old_model_fn, "br") as fp:
#_, _, _, _, config = pickle.load(fp)

#with open(config_fn, "bw") as fp:
#    pickle.dump(config, fp)

with open(config_fn, "br") as fp:
    config = pickle.load(fp)

print(config)

# evaluate configuration using a 10-fold cross validation
score = af.cross_validation(config=config)
Ejemplo n.º 3
0
import numpy as np

from autofolio.facade.af_csv_facade import AFCsvFacade

__author__ = "Marius Lindauer"
__license__ = "BSD"
__version__ = "2.1.0"

perf_fn = "perf_matrix.csv"
feat_fn = "meta_features.csv"


# will be created (or overwritten) by AutoFolio
model_fn = "af_model.pkl"

af = AFCsvFacade(perf_fn=perf_fn, feat_fn=feat_fn)

# fit AutoFolio; will use default hyperparameters of AutoFolio
af.fit()

# tune AutoFolio's hyperparameter configuration for 4 seconds
config = af.tune(wallclock_limit=360)

# evaluate configuration using a 10-fold cross validation
score = af.cross_validation(config=config)

# re-fit AutoFolio using the (hopefully) better configuration
# and save model to disk
af.fit(config=config, save_fn=model_fn)

# load AutoFolio model and
Ejemplo n.º 4
0
import numpy as np

from autofolio.facade.af_csv_facade import AFCsvFacade

__author__ = "Marius Lindauer"
__license__ = "BSD"
__version__ = "2.1.0"

# will be created (or overwritten) by AutoFolio
model_fn = "af_model_final.pkl"

# load AutoFolio model and
# get predictions for new meta-feature vector
pred = AFCsvFacade.load_and_predict(vec=np.array([3.,102., 6149., -1.,]), load_fn=model_fn)

print(pred)


import numpy as np

from autofolio.facade.af_csv_facade import AFCsvFacade

__author__ = "Marius Lindauer"
__license__ = "BSD"
__version__ = "2.1.0"

perf_fn = "perf.csv"
feat_fn = "feats.csv"

# will be created (or overwritten) by AutoFolio
model_fn = "af_model.pkl"

af = AFCsvFacade(perf_fn=perf_fn, feat_fn=feat_fn)

# load AutoFolio model and
# get predictions for new meta-feature vector
pred = AFCsvFacade.load_and_predict(vec=np.array([1.]), load_fn=model_fn)

print(pred)


Ejemplo n.º 6
0
import pickle
import numpy as np

from autofolio.facade.af_csv_facade import AFCsvFacade

__author__ = "Marius Lindauer"
__license__ = "BSD"
__version__ = "2.1.0"

perf_fn = "perf_matrix.csv"
feat_fn = "meta_features.csv"

af = AFCsvFacade(perf_fn=perf_fn, feat_fn=feat_fn)

config = {
    'StandardScaler': False,
    'fgroup_all': True,
    'imputer_strategy': 'mean',
    'pca': False,
    'selector': 'PairwiseClassifier',
    'classifier': 'RandomForest',
    'rf:bootstrap': False,
    'rf:criterion': 'gini',
    'rf:max_depth': 132,
    'rf:max_features': 'log2',
    'rf:min_samples_leaf': 3,
    'rf:min_samples_split': 3,
    'rf:n_estimators': 68
}

# fit AF using a loaded configuration on all data!