-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
138 lines (104 loc) · 4.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Main active testing loop."""
import os
import logging
import hydra
import warnings
import numpy as np
import torch
from activetesting.experiment import Experiment
from activetesting.utils import maps
from activetesting.hoover import Hoover
from activetesting.models import make_efficient
from omegaconf import OmegaConf
@hydra.main(config_path='conf', config_name='config')
def main(cfg):
"""Run main experiment loop.
Repeat active testing across multiple data splits and acquisition
functions for all risk estimators.
"""
rng = cfg.experiment.random_seed
if rng == -1:
rng = np.random.randint(0, 1000)
if rng is not False:
np.random.seed(rng)
torch.torch.manual_seed(rng)
logging.info(f'Setting random seed to {rng}.')
hoover = Hoover(cfg.hoover)
logging.info(f'Logging to {os.getcwd()}.')
model = None
# Right now this averages over both train and testing!
for run in range(cfg.experiment.n_runs):
if run % cfg.experiment.log_every == 0 or cfg.experiment.debug:
logging.info(f'Run {run} in {os.getcwd()}.')
if cuda := torch.cuda.is_available():
logging.info(f'Still using cuda: {cuda}.')
else:
os.system('touch cuda_failure.txt')
dataset = maps.dataset[cfg.dataset.name](cfg.dataset, cfg.model)
# Train model on training data.
if (not cfg.model.get('keep_constant', False)) or (model is None):
# default case
model = maps.model[cfg.model.name](cfg.model)
model.fit(*dataset.train_data)
# Always predict on test data again
# TODO: need to fix this for efficient prediction
if cfg.model.get('efficient', False):
logging.info('Eficient prediction on test set.')
model = make_efficient(model, dataset)
# if cfg.experiment.debug:
# Report train error
# logging.info('Model train error:')
# model.performance(
# *dataset.train_data, dataset.cfg.task_type)
# if not check_valid(model, dataset):
# continue
if run < cfg.experiment.save_data_until:
hoover.add_data(run, dataset.export())
for acq_dict in cfg.acquisition_functions:
# Slightly unclean, but could not figure out how to make
# this work with Hydra otherwise
acquisition = list(acq_dict.keys())[0]
acq_cfg_name = list(acq_dict.values())[0]
if cfg.experiment.debug:
logging.info(f'\t Acquisition: {acquisition}')
if (n := acq_cfg_name) is not None:
acq_config = cfg['acquisition_configs'][n]
else:
acq_config = None
experiment = Experiment(
run, cfg, dataset, model, acquisition, acq_config)
i = 0
while not experiment.finished:
i += 1
if cfg.experiment.debug:
logging.info(
f'\t Acquisition: {acquisition} – \t Step {i}.')
experiment.step(i)
# Add config to name for logging.
if (n := acq_cfg_name) is not None:
acquisition = f'{acquisition}_{n}'
# Extract results from acquisition experiment
hoover.add_results(run, acquisition, experiment.export_data())
# Reset selected test_indices.
dataset.restart()
if run % cfg.experiment.get('save_every', 1e19) == 0:
logging.info('Intermediate save.')
hoover.save()
logging.info('Completed all runs.')
hoover.save()
def check_valid(model, dataset):
"""For classification with small number of points and unstratified."""
if hasattr(model.model, 'n_classes_'):
if (nc := model.model.n_classes_) != dataset.cfg.n_classes:
warnings.warn(
f'Not all classes present in train data. '
f'Skipping run.')
return False
return True
if __name__ == '__main__':
import os
os.environ['HYDRA_FULL_ERROR'] = '1'
def get_base_dir():
return os.getenv('BASE_DIR', default='.')
OmegaConf.register_resolver('BASE_DIR', get_base_dir)
main()