-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathablation_study.py
More file actions
234 lines (197 loc) · 7.87 KB
/
ablation_study.py
File metadata and controls
234 lines (197 loc) · 7.87 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
import math
from collections import defaultdict
from importlib.resources import files
import holoviews as hv
import hvplot.pandas # noqa F401
import mlflow
import pandas as pd
import panel as pn
from tqdm.auto import tqdm
from component_classifier.data_utils import (
LABEL_COLS,
ImageDataset,
df_to_error_plot,
get_metadata_df,
)
from component_classifier.main import loader_from_ds, start_training
try:
hv.notebook_extension("bokeh")
except Exception:
hv.extension("bokeh")
pn.extension()
ABLATION_RUNS_PATH = files("component_classifier") / "runs/ablation_study.json"
SUBSAMPLING_RUNS_PATH = files("component_classifier") / "runs/subsampling_study.json"
def train_dataset_size_ablation():
mlflow.environment_variables.MLFLOW_EXPERIMENT_NAME.set("Subsampling")
meta_df = get_metadata_df()
µs = [0, 7]
subsamplings = [150, 500, 1000] # Start with the smallest so it fails early if too small
result = defaultdict(list)
for subsampling in tqdm(subsamplings):
for µ in µs:
for _ in range(5):
default_train_dev_unlabeled_loader = [
loader_from_ds(
ImageDataset.from_metadata_df(
meta_df, LABEL_COLS, split=split, n_cache=int(1e6), µ=µ, n_train=subsampling
)
)
for split in ["train", "test", "unlabeled"]
]
train_size = len(default_train_dev_unlabeled_loader[0].dataset)
assert math.isclose(subsampling, train_size, abs_tol=len(LABEL_COLS)), train_size # Rounded down
override_params = {"subsampling": subsampling, "µ": µ, "dataset_name": "engines"}
run_id = start_training(*default_train_dev_unlabeled_loader, **override_params)
result[f"{subsampling}_{µ}"].append(run_id)
SUBSAMPLING_RUNS_PATH.write_text(json.dumps(result, indent=4))
def plot_subsampling():
runs = json.load(open(files("component_classifier") / "runs/subsampling_study.json"))
plot_data = []
for run_name, run_ids in runs.items():
subsampling, _, mu = run_name.rpartition("_")
for run_id in run_ids:
run = mlflow.get_run(run_id)
plot_data.append(
{"Error rate": 100 * (1 - run.data.metrics["dev MulticlassAccuracy"])}
| {"supervised labels": int(subsampling), "mu": int(mu)}
)
df = pd.DataFrame(plot_data).replace({0: "µ0", 7: "µ7"}).assign(x="x")
pn.serve(
df_to_error_plot(
df,
x="supervised labels",
y="Error rate",
by="mu",
groupby="x",
by_color_map={"µ0": "blue", "µ7": "red"},
override_params={"width": 300},
)[0].opts(title=""),
title="subsampling",
)
# plot = df.hvplot.box(
# by=["subsampling", "µ"],
# y="Error rate",
# grid=True,
# width=550,
# height=300,
# yformatter="%.0f%%",
# shared_axes=False,
# fontsize={"labels": 18, "xticks": 14, "yticks": 14},
# )
# plot
# pn.serve(plot, title="subsampling_study")
# plots["µ"].opts(xlabel="µ (unlabeled/labeled ratio)")
# plots["λ"].opts(xlabel="λ (unlabeled loss ratio)")
# plots["τ"].opts(xlabel="τ (pseudo-label threshold)")
# plots["n_strong_aug"].opts(xlabel="#Strong augmentations")
# # pn.serve(hv.Layout(plots).cols(2), title='ablation_study')
# pn.serve(pn.Column(*[plots[v] for v in ["µ", "λ", "τ", "n_strong_aug"]]), title="ablation_study")
def plot_ablation():
runs = json.load(open(files("component_classifier") / "runs/ablation_study.json"))
plot_data = []
for run_name, run_ids in runs.items():
param, _, value = run_name.rpartition("_")
for run_id in run_ids:
run = mlflow.get_run(run_id)
plot_data.append(
{"Error rate": 100 * (1 - run.data.metrics["dev MulticlassAccuracy"])}
| {"param": param, "value": float(value)}
)
df = pd.DataFrame(plot_data).rename(columns={"value": "magnitude"}).assign(none="")
pn.serve(
df_to_error_plot(
df,
x="magnitude",
y="Error rate",
by="none",
groupby="param",
by_color_map={"": "blue"},
override_params={"ylim": (7, 29)},
),
title="ablation",
)
# plots = df.hvplot.box(
# by="value",
# y="Error rate",
# groupby="param",
# grid=True,
# width=350,
# height=300,
# yformatter="%.0f%%",
# ylim=(15, 30),
# shared_axes=False,
# fontsize={"labels": 18, "xticks": 14, "yticks": 14},
# )
# plots = df.hvplot.scatter(
# x="value",
# y="Error rate",
# groupby="param",
# grid=True,
# width=350,
# height=300,
# yformatter="%.0f%%",
# ylim=(15, 30),
# shared_axes=False,
# fontsize={"labels": 18, "xticks": 14, "yticks": 14},
# )
# pn.serve(plots["µ"].opts(xlabel="µ (unlabeled/labeled ratio)"))
# plots["λ"].opts(xlabel="λ (unlabeled loss ratio)")
# plots["τ"].opts(xlabel="τ (pseudo-label threshold)")
# plots["n_strong_aug"].opts(xlabel="#Strong augmentations")
# # pn.serve(hv.Layout(plots).cols(2), title='ablation_study')
# pn.serve(pn.Column(*[plots[v] for v in ["µ", "λ", "τ", "n_strong_aug"]]), title="ablation_study")
def perform_ablation_study():
mlflow.environment_variables.MLFLOW_EXPERIMENT_NAME.set("Ablation")
n_train = 500
meta_df = get_metadata_df()
searches = {
"n_strong_aug": [0, 1, 2, 4, 8],
"µ": [0, 1, 5, 10, 20],
"λ": [0.5, 0.75, 1, 1.5, 2],
"τ": [0.5, 0.75, 0.95, 0.99],
}
result = defaultdict(list)
for _ in range(5):
cached_ds = [
ImageDataset.from_metadata_df(meta_df, LABEL_COLS, split=split, n_cache=int(1e6), µ=7, n_train=n_train)
for split in ["train", "test", "unlabeled"]
]
for param, values in searches.items():
for value in tqdm(values, desc=f'Optimising "{param}"', leave=True):
ds = (
[
ImageDataset.from_metadata_df(
meta_df, LABEL_COLS, split=split, n_cache=int(1e6), µ=value, n_train=n_train
)
for split in ["train", "test", "unlabeled"]
]
if param == "µ"
else cached_ds
)
override_params = {param: value, "subsampling": n_train, "dataset_name": "engines"}
run_id = start_training(*map(loader_from_ds, ds), **override_params)
result[f"{param}_{value}"].append(run_id)
ABLATION_RUNS_PATH.write_text(json.dumps(result, indent=4))
def n_aug_dev_set():
runs = json.load(open(files("component_classifier") / "runs/n_aug_study.json"))
plot_data = []
for tag, run_id in runs.items():
run = mlflow.get_run(run_id)
_, _, value = tag.rpartition("_")
plot_data.append({"Error rate": 1 - run.data.metrics["dev MulticlassAccuracy"]} | {"n_aug": value})
df = pd.DataFrame(plot_data)
aug_plot = df.hvplot.scatter(
x="n_aug",
y="Error rate",
grid=True,
width=350,
height=300,
s=80,
fontsize={"labels": 18, "xticks": 14, "yticks": 14},
)
pn.serve(pn.Column(aug_plot), title="n_aug")
if __name__ == "__main__":
# perform_ablation_study()
train_dataset_size_ablation()
# plot_ablation()