-
Notifications
You must be signed in to change notification settings - Fork 18
/
plot_stacking_ablation.py
266 lines (189 loc) · 9.08 KB
/
plot_stacking_ablation.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import seaborn as sns
import os
import glob
import argparse
import math
def pretty(text):
"""Convert a string into a consistent format for
presentation in a matplotlib pyplot:
this version looks like: One Two Three Four
"""
text = text.replace("_", " ")
text = text.replace("-", " ")
text = text.replace("/", " ")
text = text.strip()
prev_c = None
out_str = []
for c in text:
if prev_c is not None and \
prev_c.islower() and c.isupper():
out_str.append(" ")
prev_c = " "
if prev_c is None or prev_c == " ":
c = c.upper()
out_str.append(c)
prev_c = c
return "".join(out_str)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Few-Shot Baseline")
parser.add_argument("--logdirs", nargs="+", type=str, default=[
"./spurge-baselines", "./pascal-baselines", "./coco-baselines"])
parser.add_argument("--datasets", nargs="+", type=str,
default=["Spurge", "Pascal", "COCO", "Overall"])
parser.add_argument("--method-dirs", nargs="+", type=str,
default=["textual-inversion-0.5", "textual-inversion-1.0-0.75-0.5-0.25"])
parser.add_argument("--baseline-dir", type=str, default="baseline")
parser.add_argument("--method-names", nargs="+", type=str,
default=["Model-Based Data Augmentation (k = 1)",
"Model-Based Data Augmentation (k = 4)"])
parser.add_argument("--name", type=str, default="stacking-results-bar")
parser.add_argument("--rows", type=int, default=1)
parser.add_argument("--num-trials", type=int, default=8)
parser.add_argument("--no-legend", action="store_true")
args = parser.parse_args()
combined_dataframe = []
for logdir, dataset in zip(
args.logdirs, args.datasets):
for bname in os.listdir(logdir):
bpath = os.path.join(logdir, bname)
if not os.path.isdir(bpath):
continue
files = list(glob.glob(os.path.join(bpath, "*.csv")))
if len(files) == 0:
continue
data = pd.concat([pd.read_csv(x, index_col=0)
for x in files], ignore_index=True)
data = data[(data["metric"] == "Accuracy") &
(data[ "split"] == "Validation")]
def select_by_epoch(df):
selected_row = df.loc[df["value"].idxmax()]
return data[(data["epoch"] == selected_row["epoch"]) &
(data[ "examples_per_class"] ==
selected_row["examples_per_class"])]
best = data.groupby(["examples_per_class", "epoch"])
best = best["value"].mean().to_frame('value').reset_index()
best = best.groupby("examples_per_class").apply(
select_by_epoch
)
best["method"] = bname
best["dataset"] = dataset
combined_dataframe.append(best)
matplotlib.rc('font', family='Times New Roman', serif='cm10')
matplotlib.rc('mathtext', fontset='cm')
plt.rcParams['text.usetex'] = False
combined_dataframe = pd.concat(
combined_dataframe, ignore_index=True)
combined_dataframe = pd.concat([combined_dataframe[
combined_dataframe['method'] == n] for n in args.method_dirs + [args.baseline_dir]])
color_palette = sns.color_palette(n_colors=len(args.method_dirs))
legend_rows = int(math.ceil(len(args.method_names) / len(args.datasets)))
columns = int(math.ceil(len(args.datasets) / args.rows))
fig, axs = plt.subplots(
args.rows, columns,
figsize=(6 * columns, 3.5 * args.rows + ((
2.0 if legend_rows == 1 else
2.5 if legend_rows == 2 else 3
) if not args.no_legend else 1.0)))
auc_df = []
baseline_performance = {dataset: 0 for dataset in args.datasets}
for dataset in args.datasets:
for seed in range(args.num_trials):
results = combined_dataframe[
(combined_dataframe["dataset"] == dataset) &
(combined_dataframe["method"] == args.baseline_dir) &
(combined_dataframe["seed"] == seed)
]
cumulative_value = 0.0
invalid = False
for examples_a, examples_b in zip([1, 2, 4, 8], [2, 4, 8, 16]):
value_a = results[results["examples_per_class"] == examples_a]["value"].to_numpy()
value_b = results[results["examples_per_class"] == examples_b]["value"].to_numpy()
if value_a.size > 0 and value_b.size > 0:
cumulative_value += ((value_a + value_b) / 2) * (examples_b - examples_a)
else: invalid = True
if not invalid:
baseline_performance[dataset] += \
cumulative_value[0] / args.num_trials
for dataset in args.datasets:
for method in args.method_dirs:
for seed in range(args.num_trials):
results = combined_dataframe[
(combined_dataframe["dataset"] == dataset) &
(combined_dataframe["method"] == method) &
(combined_dataframe["seed"] == seed)
]
if dataset not in baseline_performance: continue
cumulative_value = -baseline_performance[dataset]
invalid = False
for examples_a, examples_b in zip([1, 2, 4, 8], [2, 4, 8, 16]):
value_a = results[results["examples_per_class"] == examples_a]["value"].to_numpy()
value_b = results[results["examples_per_class"] == examples_b]["value"].to_numpy()
if value_a.size > 0 and value_b.size > 0:
cumulative_value += ((value_a + value_b) / 2) * (examples_b - examples_a)
else: invalid = True
if not invalid:
auc_df.append(
dict(
dataset=dataset,
method=method,
seed=seed,
value=cumulative_value[0],
)
)
combined_dataframe = pd.DataFrame.from_records(auc_df)
for dataset in args.datasets:
df = combined_dataframe.loc[combined_dataframe["dataset"] == dataset]
if df.size == 0: continue
acc_max = df["value"].to_numpy().max()
acc_min = df["value"].to_numpy().min()
combined_dataframe.loc[
combined_dataframe["dataset"] == dataset,
"normalized_value"
] = (df["value"] - acc_min) / (acc_max - acc_min)
for i, dataset in enumerate(args.datasets):
results = combined_dataframe[combined_dataframe[
"dataset"] == dataset] if dataset != "Overall" else combined_dataframe
axis = sns.barplot(
y="normalized_value" if dataset == "Overall" else "value",
x="method", data=results, errorbar=('ci', 68),
linewidth=4, palette=color_palette,
ax=(axs[i // columns, i % columns]
if args.rows > 1 and len(args.datasets) > 1
else axs[i] if len(args.datasets) > 1 else axs))
if i == 0: handles, labels = axis.get_legend_handles_labels()
axis.legend([],[], frameon=False)
axis.set(xlabel=None)
axis.set(ylabel=None)
axis.spines['right'].set_visible(False)
axis.spines['top'].set_visible(False)
axis.xaxis.set_ticks_position('bottom')
axis.yaxis.set_ticks_position('left')
axis.yaxis.set_tick_params(labelsize=16)
axis.xaxis.set_ticklabels([])
acc_max = results["normalized_value" if dataset == "Overall" else "value"].to_numpy().max()
acc_min = results["normalized_value" if dataset == "Overall" else "value"].to_numpy().min()
axis.set_ylim(max(0, acc_min), acc_max)
axis.set_ylabel("Normalized Score" if dataset == "Overall"
else "Gained AUC (Val)", fontsize=24,
fontweight='bold', labelpad=12)
axis.set_title(dataset, fontsize=24, fontweight='bold', pad=12)
axis.grid(color='grey', linestyle='dotted', linewidth=2)
if not args.no_legend:
legend = fig.legend([x for x in args.method_names],
loc="lower center", prop={'size': 24, 'weight': 'bold'},
ncol=min(len(args.method_names), len(args.datasets)))
for i, legend_object in enumerate(legend.legendHandles):
legend_object.set_linewidth(4.0)
legend_object.set_color(color_palette[i])
plt.tight_layout(pad=1.0)
fig.subplots_adjust(hspace=0.3)
if not args.no_legend:
fig.subplots_adjust(bottom=(
0.20 if legend_rows == 1 else
0.30 if legend_rows == 2 else 0.35
) / args.rows + 0.05)
plt.savefig(f"{args.name}.pdf")
plt.savefig(f"{args.name}.png")