forked from brandontrabucco/da-fusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_stratify.py
158 lines (110 loc) · 5.23 KB
/
plot_stratify.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
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import seaborn as sns
import os
import glob
import argparse
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=[
"./pascal-baselines"])
parser.add_argument("--datasets", nargs="+", type=str,
default=["Pascal"])
parser.add_argument("--method-names", nargs="+", type=str,
default=["baseline", "real-guidance-0.5", "textual-inversion-0.5"])
parser.add_argument("--name", type=str, default="stratification")
args = parser.parse_args()
combined_dataframe = []
for dataset, logdir in zip(
args.datasets, args.logdirs):
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"].str.contains("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_names])
combined_dataframe["class_name"] = \
combined_dataframe["metric"].str.replace("Accuracy ", "")
color_palette = sns.color_palette(n_colors=len(args.method_names))
for i, dataset in enumerate(args.datasets):
results = combined_dataframe[combined_dataframe["dataset"] == dataset]
for j, examples_per_class in enumerate(
results["examples_per_class"].unique()):
results2 = results[results["examples_per_class"] == examples_per_class]
fig, axs = plt.subplots(1, 1, figsize=(20, 6))
axis = sns.barplot(x="class_name", y="value", hue="method",
data=results2, errorbar=('ci', 68),
linewidth=4, palette=color_palette,
ax=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_tick_params(labelsize=16, labelrotation=45)
axis.set_ylabel("Accuracy (Val)", fontsize=24,
fontweight='bold', labelpad=12)
axis.set_title(f"Dataset = {dataset} (Examples Per Class = {examples_per_class})",
fontsize=24, fontweight='bold', pad=12)
axis.grid(color='grey', linestyle='dotted', linewidth=2)
legend = fig.legend(handles, [pretty(x) for x in args.method_names],
loc="lower center", ncol=len(args.method_names),
prop={'size': 24, 'weight': 'bold'})
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(bottom=0.35)
plt.savefig(f"{args.name}-{dataset}-{examples_per_class}-barplot.pdf")
plt.savefig(f"{args.name}-{dataset}-{examples_per_class}-barplot.png")