-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRcurve.py
More file actions
121 lines (82 loc) · 4.15 KB
/
PRcurve.py
File metadata and controls
121 lines (82 loc) · 4.15 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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from sklearn.metrics import auc
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
matplotlib.use('Agg')
plt.rcParams.update({
"text.usetex": False,
"mathtext.fontset": "dejavusans",
"font.family": "sans-serif",
"font.sans-serif": ["Arial", "DejaVu Sans", "Liberation Sans", "Bitstream Vera Sans", "sans-serif"],
"font.size": 14,
"axes.labelsize": 18,
"axes.titlesize": 18,
"legend.fontsize": 13,
"xtick.labelsize": 14,
"ytick.labelsize": 14
})
mnist_precision = np.load("./PR_curve/mnist_hidden_precision.npy")
mnist_recall = np.load("./PR_curve/mnist_hidden_recall.npy")
mnist_thresholds = np.load("./PR_curve/mnist_hidden_thresholds.npy")
fashion_precision = np.load("./PR_curve/fashion_noise_precision.npy")
fashion_recall = np.load("./PR_curve/fashion_noise_recall.npy")
fashion_thresholds = np.load("./PR_curve/fashion_noise_thresholds.npy")
cifar10_precision = np.load("./PR_curve/cifar10_content_precision.npy")
cifar10_recall = np.load("./PR_curve/cifar10_content_recall.npy")
cifar10_thresholds = np.load("./PR_curve/cifar10_content_thresholds.npy")
colors = {
"mnist": "#005AB5",
"fashion": "#DC3220",
"cifar10": "#117733"
}
mnist_ap = auc(mnist_recall, mnist_precision)
fashion_ap = auc(fashion_recall, fashion_precision)
cifar10_ap = auc(cifar10_recall, cifar10_precision)
plt.figure(figsize=(5, 4), dpi=300)
plt.plot(mnist_recall, mnist_precision, label=f"MNIST-Hidden (AP={mnist_ap:.3f})", color=colors["mnist"], linestyle="-", linewidth=3)
plt.plot(fashion_recall, fashion_precision, label=f"FASHION-Torjan (AP={fashion_ap:.3f})", color=colors["fashion"], linestyle="--", linewidth=3)
plt.plot(cifar10_recall, cifar10_precision, label=f"CIFAR10-BadNet (AP={cifar10_ap:.3f})", color=colors["cifar10"], linestyle="-.", linewidth=3)
ax = plt.gca()
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
def find_threshold_index(thresholds, target=0.1):
return (np.abs(thresholds - target)).argmin()
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.grid(True, linestyle="--", alpha=0.6)
ax.hlines(y=0.5, xmin=0.0, xmax=1.0, color='black', linestyle=':', linewidth=2.5, alpha=0.8)
plt.legend(loc="best", frameon=True, fancybox=True, shadow=False, borderpad=0.6, framealpha=0.6)
plt.xlim([0.0, 1.05])
plt.ylim([0.4, 1.05])
plt.tight_layout()
axins = inset_axes(ax, width="40%", height="30%", loc='center left',
bbox_to_anchor=(0.3, 0.1, 1, 1),
bbox_transform=ax.transAxes)
axins.plot(mnist_recall, mnist_precision, label=f"MNIST-Hidden (AP={mnist_ap:.3f})", color=colors["mnist"], linestyle="-", linewidth=3)
axins.plot(fashion_recall, fashion_precision, label=f"FASHION-Torjan (AP={fashion_ap:.3f})", color=colors["fashion"], linestyle="--", linewidth=3)
axins.plot(cifar10_recall, cifar10_precision, label=f"CIFAR10-BadNet (AP={cifar10_ap:.3f})", color=colors["cifar10"], linestyle="-.", linewidth=3)
axins.set_xlim([0.95, 1.01])
axins.set_ylim([0.9, 1.03])
from matplotlib.text import Text
class LatexText(Text):
def __init__(self, *args, **kwargs):
kwargs['usetex'] = True
super().__init__(*args, **kwargs)
for dataset, precision, recall, thresholds, color in zip(
["MNIST", "Fashion-MNIST", "CIFAR-10"],
[mnist_precision, fashion_precision, cifar10_precision],
[mnist_recall, fashion_recall, cifar10_recall],
[mnist_thresholds, fashion_thresholds, cifar10_thresholds],
[colors["mnist"], colors["fashion"], colors["cifar10"]]
):
idx = find_threshold_index(thresholds, 0.1)
axins.scatter(recall[idx], precision[idx], color=color, s=100, edgecolors="black", alpha=0.8, zorder=3)
if dataset == "MNIST":
latex_text = LatexText(recall[idx], precision[idx] + 0.02,
r"$\mathbf{\tau=0.1}$", fontsize=12, color="black",
ha="center", weight="bold")
axins.add_artist(latex_text)
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec='k', lw=1)
plt.savefig("./PR_curve/PR_curve_with_threshold.pdf", bbox_inches="tight")