-
Notifications
You must be signed in to change notification settings - Fork 9
/
make_newt_plots.py
361 lines (276 loc) · 12.5 KB
/
make_newt_plots.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import argparse
from contextlib import redirect_stdout
import os
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import configs
import plot_utils
# Set matplotlib font size
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIGGER_SIZE = 18
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
def create_newt_plots(newt_results_dir, output_dir):
newt_results_df = pd.DataFrame(None, columns=['model_name', 'name', 'acc'])
for model_spec in configs.model_specs:
model_name = model_spec['name']
results_fp = os.path.join(newt_results_dir, model_name + ".pkl")
if not os.path.exists(results_fp):
print("WARNING: did not find results for model %s at %s" % (model_name, results_fp))
continue
results_df = pd.read_pickle(results_fp)
newt_results_df = pd.concat([newt_results_df, results_df], axis=0, ignore_index=True)
# models are the rows
newt_model_results_df = newt_results_df.pivot(index='model_name', columns='name', values='acc')
newt_model_results_df = newt_model_results_df.loc[[model_spec['name'] for model_spec in configs.model_specs]]
# datasets are the rows
newt_dataset_results_df = newt_results_df.pivot(index='name', columns='model_name', values='acc')
newt_dataset_results_df = newt_dataset_results_df[[model_spec['name'] for model_spec in configs.model_specs]]
# Load in the cluster info
# GVH: this needs to be factored out
newt_clusters_df = pd.read_csv("newt_task_clusters.csv")
# The order in which we want to render the task clusters
task_order=[
{
"name" : "Appearance\nAge",
"cluster" : "appearance",
"sub_cluster" : "age"
},
{
"name" : "Appearance\nAttribute",
"cluster" : "appearance",
"sub_cluster" : "attribute"
},
{
"name" : "Appearance\nHealth",
"cluster" : "appearance",
"sub_cluster" : "health"
},
{
"name" : "Appearance\nSpecies",
"cluster" : "appearance",
"sub_cluster" : "species"
},
{
"name" : "Behavior",
"cluster" : "behavior",
"sub_cluster" : None
},
{
"name" : "Context",
"cluster" : "context",
"sub_cluster" : None
},
{
"name" : "Counting",
"cluster" : "counting",
"sub_cluster" : None
},
{
"name" : "Gestalt",
"cluster" : "gestalt",
"sub_cluster" : None
}
]
# Go through and add task counts
for task_info in task_order:
# Get the dataset names that belong to this cluster (and subcluster)
if task_info['sub_cluster'] is not None:
cluster_dataset_names = newt_clusters_df[(newt_clusters_df['cluster'] == task_info['cluster']) & (newt_clusters_df['sub_cluster'] == task_info['sub_cluster'])]['name']
else:
cluster_dataset_names = newt_clusters_df[newt_clusters_df['cluster'] == task_info['cluster']]['name']
# Get the model results on these datasets
cluster_dataset_results = newt_dataset_results_df[newt_dataset_results_df.index.isin(cluster_dataset_names)]
num_datasets = cluster_dataset_results.shape[0]
task_info['num_datasets'] = num_datasets
#####################
# Stem plot for NeWT tasks
# Organize the method results by average task cluster performance
method_results = []
for model_spec in configs.model_specs:
task_scores = []
for task_info in task_order:
# Get the dataset names that belong to this cluster (and subcluster)
if task_info['sub_cluster'] is not None:
cluster_dataset_names = newt_clusters_df[(newt_clusters_df['cluster'] == task_info['cluster']) & (newt_clusters_df['sub_cluster'] == task_info['sub_cluster'])]['name']
else:
cluster_dataset_names = newt_clusters_df[newt_clusters_df['cluster'] == task_info['cluster']]['name']
# Get the model results on these datasets
cluster_dataset_results = newt_dataset_results_df[newt_dataset_results_df.index.isin(cluster_dataset_names)]
# Get the mean result for this model on these datasets
method_cluster_results = cluster_dataset_results[model_spec['name']].mean()
assert not pd.isna(method_cluster_results)
task_scores.append(method_cluster_results)
task_scores = np.array(task_scores)
r = {
'name' : model_spec['name'],
'scores' : task_scores,
'color' : model_spec['color'],
'display_name' : model_spec['display_name'],
}
if model_spec['name'] == 'random':
r['line_style'] = ':'
elif model_spec['train_objective'] == configs.SUPERVISED:
r['line_style'] = '-'
else:
r['line_style'] = '--'
if model_spec['name'] == 'random':
r['marker_format'] = '>'
elif model_spec['training_dataset'] == configs.IMAGENET:
r['marker_format'] = '^'
elif model_spec['train_objective'] == configs.SUPERVISED:
r['marker_format'] = 'o'
elif model_spec['train_objective'] == configs.SIMCLR or model_spec['train_objective'] == configs.SIMCLR_V2:
r['marker_format'] = '*'
elif model_spec['train_objective'] == configs.MOCO_V2:
r['marker_format'] = '*'
elif model_spec['train_objective'] == configs.SWAV:
r['marker_format'] = '*'
else:
raise ValueError("Unknown train objective: %s" % model_spec['train_objective'])
method_results.append(r)
method_results_df = pd.DataFrame(method_results).set_index('name', drop=True)
result_names=[
'imagenet_simclr',
'imagenet_simclr_x4',
'imagenet_simclr_v2',
'imagenet_swav',
'imagenet_moco_v2',
'inat2021_supervised',
'inat2021_mini_supervised',
'inat2018_supervised',
'inat2021_simclr',
'inat2021_mini_simclr',
'inat2021_mini_simclr_x4',
'inat2021_mini_simclr_v2',
'inat2021_mini_swav',
'inat2021_mini_moco_v2'
]
baseline_scores = method_results_df.loc['imagenet_supervised']['scores']
exp_results = []
for method_name in result_names:
r = method_results_df.loc[method_name].copy()
r['scores'] = r['scores'] - baseline_scores
exp_results.append(r)
result_df = pd.DataFrame(exp_results)
task_labels = ["%s\n%d" % (task['name'], task['num_datasets']) for task in task_order]
plot_utils.task_stem_plot(
result_df,
task_labels,
task_space=4,
task_offset=5,
title='Change in Mean Accuracy from Imagenet Supervised Features',
xlabel='',
ylabel='$\Delta$ ACC',
figsize=(15, 5),
rotate_x_tick_labels=False,
task_baseline_scores=baseline_scores,
task_baseline_scores_x_offset=-.5,
task_baseline_scores_y_pos=-.17
)
output_fp = os.path.join(output_dir, "newt_stem_plot.pdf")
plt.savefig(output_fp, bbox_inches='tight', dpi=400)
##############
# Latex Table of results
# [Source Dataset, Train Loss, {Dataasets}, Mean ACC]
# Get the results of each method for each NeWT cluster:
# GVH: this needs to be refactored (didn't we do this above...)
model_cluster_results = []
for model_name, model_results in newt_model_results_df.iterrows():
r = {'model_name' : model_name}
for task_info in task_order:
# Get the dataset names that belong to this cluster (and subcluster)
if task_info['sub_cluster'] is not None:
cluster_dataset_names = newt_clusters_df[(newt_clusters_df['cluster'] == task_info['cluster']) & (newt_clusters_df['sub_cluster'] == task_info['sub_cluster'])]['name']
else:
cluster_dataset_names = newt_clusters_df[newt_clusters_df['cluster'] == task_info['cluster']]['name']
scores = []
for dataset_name in cluster_dataset_names:
scores.append(model_results[dataset_name])
r[task_info['name'].replace('\n', ' ')] = np.mean(scores)
r['mean_acc'] = np.mean(model_results.values)
model_cluster_results.append(r)
model_cluster_results_df = pd.DataFrame(model_cluster_results).set_index('model_name', drop=True)
tasks = [task['name'].replace('\n', ' ') for task in task_order]
#result_names = [model_spec['name'] for model_spec in configs.model_specs]
result_names=[
'imagenet_supervised',
'imagenet_simclr',
'imagenet_simclr_x4',
'imagenet_simclr_v2',
'imagenet_swav',
'imagenet_moco_v2',
'inat2021_supervised',
'inat2021_supervised_from_scratch',
'inat2021_mini_supervised',
'inat2021_mini_supervised_from_scratch',
'inat2018_supervised',
'inat2021_simclr',
'inat2021_mini_simclr',
'inat2021_mini_simclr_x4',
'inat2021_mini_simclr_v2',
'inat2021_mini_swav',
'inat2021_mini_moco_v2'
]
num_cols = len(tasks)
num_rows = len(result_names)
table_fp = os.path.join(output_dir, "newt_latex_table.txt")
with open(table_fp, "w") as f:
with redirect_stdout(f):
print("\\begin{table*}[t]")
print("\\small")
print("\\centering")
print("\\begin{tabular}{|l | l | %s |}" % (" ".join(["c"] * (num_cols + 1))))
print("\\hline")
header = ["Source Dataset", "Train Loss"] + tasks + ["Mean ACC"]
print(" & \t".join(header) + "\\\\")
print("\hline\hline")
for model_name in result_names:
model_spec = next(model_spec for model_spec in configs.model_specs if model_spec['name'] == model_name)
model_scores = model_cluster_results_df.loc[model_spec['name']]
ys = []
ry = []
for i, label in enumerate(tasks):
v = model_scores[label]
ys.append(
"%0.3f" % v
)
ry.append(v)
td = model_spec['training_dataset'] if model_spec['training_dataset'] is not None else ""
to = model_spec['train_objective']
if model_spec['backbone'] == configs.RESNET50_X4:
to += " x4"
if model_spec['pretrained_weights'] is not None:
to += " (from %s)" % model_spec['pretrained_weights']
row = [td, to] + ys + ["%0.3f" % model_scores['mean_acc']] # We want the mean across all tasks, not the clusters
print(" & \t".join(row) + " \\\\")
print("\\hline")
print("\\end{tabular}")
print("\\caption{}")
print("\\label{table:}")
print("\\end{table*}")
def parse_args():
parser = argparse.ArgumentParser(description='Create the stem plot figure and latex table of results for the NeWT tasks.')
parser.add_argument('--result_dir', dest='result_dir',
help='Path to the directory containing the NeWT results.', type=str,
required=True)
parser.add_argument('--output_dir', dest='output_dir',
help='Path to the directory to save figures and tables.', type=str,
required=True)
parsed_args = parser.parse_args()
return parsed_args
if __name__ == '__main__':
args = parse_args()
if not os.path.exists(args.result_dir):
raise ValueError("FG results directory %s does not exist" % fg_result_dir)
if not os.path.exists(args.output_dir):
print("Creating %s to store plots and tables" % args.output_dir)
os.makedirs(args.output_dir)
create_newt_plots(args.result_dir, args.output_dir)