-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_gluon_classification.py
149 lines (124 loc) · 4.2 KB
/
run_gluon_classification.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
139
140
141
142
143
144
145
146
147
148
149
import os
import time
import click
import pandas as pd
from autogluon.tabular import TabularPredictor
from tqdm.auto import tqdm
from utils import (
dataset_option,
fix_probabilities,
load_data,
models_dir_option,
repeats_option,
results_dir_option,
score,
size_option,
split_indices,
)
@click.command()
@dataset_option
@repeats_option
@results_dir_option
@models_dir_option
@size_option
@click.option("--verbose", is_flag=True)
@click.option("--force", is_flag=True)
def main(
dataset,
repeats,
results_dir,
models_dir,
test_sizes,
verbose,
force,
):
verbosity = 2 if verbose else 0
dataset_name = dataset.split("/")[-1]
results_path = results_dir.format(dataset_name=dataset_name)
os.makedirs(results_path, exist_ok=True)
model_path = os.path.join(models_dir.format(dataset_name=dataset_name), "gluon")
os.makedirs(model_path, exist_ok=True)
train_data, test_data, meta = load_data(dataset)
# x_columns = train_data.columns.drop(meta["label"])
y_column = meta["label"]
results = []
bar = tqdm(
total=len(test_sizes) * repeats, desc=f"Training AutoGluon ({dataset_name})"
)
for info, train, test in split_indices(dataset, repeats, test_sizes):
train_df = train_data.loc[train]
train_y = train_data.loc[train, y_column]
test_df = test_data.loc[test]
test_y = test_data.loc[test, y_column]
n = info["trainset_fraction"]
i = info["i"]
medi_path = os.path.join(model_path, f"medium_{int(n*100):d}_{i}")
best_path = os.path.join(model_path, f"best_{int(n*100):d}_{i}")
if os.path.exists(medi_path) and not force:
try:
medi = TabularPredictor.load(medi_path)
if medi._learner._time_fit_total is None:
medi = None
except Exception:
medi = None
else:
medi = None
if medi is None:
medi = TabularPredictor(
label=y_column, path=medi_path, verbosity=verbosity
).fit(train_df, num_gpus=1, presets="medium_quality")
if os.path.exists(best_path) and not force:
try:
best = TabularPredictor.load(best_path)
if best._learner._time_fit_total is None:
best = None
except Exception:
best = None
else:
best = None
if best is None:
best = TabularPredictor(
label=y_column, path=best_path, verbosity=verbosity
).fit(train_df, num_gpus=1, presets="best_quality")
t0 = time.perf_counter()
medi_pred = medi.predict(test_df)
medi_test_time = time.perf_counter() - t0
medi_proba = medi.predict_proba(test_df).values
medi_proba = fix_probabilities(test_y.unique(), train_y.unique(), medi_proba)
if medi_proba.shape[1] == 2:
medi_proba = medi_proba[:, 1]
t0 = time.perf_counter()
best_pred = best.predict(test_df)
best_test_time = time.perf_counter() - t0
best_proba = best.predict_proba(test_df).values
best_proba = fix_probabilities(test_y.unique(), train_y.unique(), best_proba)
if best_proba.shape[1] == 2:
best_proba = best_proba[:, 1]
medi_score = score(test_y, medi_pred, medi_proba)
medi_score.update(
{
**info,
"model": "gluon_medium",
"dataset": dataset_name,
"train_time": medi._learner._time_fit_total,
"test_time": medi_test_time,
}
)
best_score = score(test_y, best_pred, best_proba)
best_score.update(
{
**info,
"model": "gluon_best",
"dataset": dataset_name,
"train_time": best._learner._time_fit_total,
"test_time": best_test_time,
}
)
results.append(medi_score)
results.append(best_score)
bar.update(1)
bar.close()
results = pd.DataFrame(results)
results.to_parquet(os.path.join(results_path, "gluon.parquet"))
if __name__ == "__main__":
main()