Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# ================================================================
|
| 2 |
-
# 教育大模型MIA攻防研究 - Gradio演示系统
|
| 3 |
-
# 1.
|
| 4 |
-
# 2.
|
|
|
|
| 5 |
# ================================================================
|
| 6 |
|
| 7 |
import os
|
|
@@ -13,9 +14,24 @@ matplotlib.use('Agg')
|
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
from sklearn.metrics import roc_curve, roc_auc_score
|
| 15 |
import gradio as gr
|
|
|
|
|
|
|
| 16 |
|
| 17 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# ================================================================
|
| 20 |
# 数据加载
|
| 21 |
# ================================================================
|
|
@@ -25,12 +41,9 @@ def load_json(path):
|
|
| 25 |
return json.load(f)
|
| 26 |
|
| 27 |
def clean_text(text):
|
| 28 |
-
if not isinstance(text, str):
|
| 29 |
-
return str(text)
|
| 30 |
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
|
| 31 |
-
|
| 32 |
-
text = re.sub(r'[\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufeff]', '', text)
|
| 33 |
-
return text.strip()
|
| 34 |
|
| 35 |
# 尝试加载数据,如果不存在则使用虚拟数据以确保运行
|
| 36 |
try:
|
|
@@ -63,21 +76,12 @@ except FileNotFoundError:
|
|
| 63 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 64 |
|
| 65 |
# ================================================================
|
| 66 |
-
#
|
| 67 |
# ================================================================
|
| 68 |
COLORS = {
|
| 69 |
-
'bg': '#FFFFFF',
|
| 70 |
-
'
|
| 71 |
-
'
|
| 72 |
-
'text': '#1E293B',
|
| 73 |
-
'text_dim': '#64748B',
|
| 74 |
-
'accent': '#007AFF',
|
| 75 |
-
'accent2': '#5856D6',
|
| 76 |
-
'danger': '#FF3B30',
|
| 77 |
-
'success': '#34C759',
|
| 78 |
-
'warning': '#FF9500',
|
| 79 |
-
'baseline': '#8E8E93',
|
| 80 |
-
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 81 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 82 |
}
|
| 83 |
CHART_W = 14
|
|
@@ -88,30 +92,20 @@ def apply_light_style(fig, ax_or_axes):
|
|
| 88 |
for ax in axes:
|
| 89 |
ax.set_facecolor(COLORS['panel'])
|
| 90 |
for spine in ax.spines.values():
|
| 91 |
-
spine.set_color(COLORS['grid'])
|
| 92 |
-
spine.set_linewidth(1)
|
| 93 |
ax.tick_params(colors=COLORS['text_dim'], labelsize=10, width=1)
|
| 94 |
-
ax.xaxis.label.set_color(COLORS['text'])
|
| 95 |
-
ax.yaxis.label.set_color(COLORS['text'])
|
| 96 |
-
ax.title.set_color(COLORS['text'])
|
| 97 |
-
ax.title.set_fontweight('semibold')
|
| 98 |
ax.grid(True, color=COLORS['grid'], alpha=0.6, linestyle='-', linewidth=0.8)
|
| 99 |
ax.set_axisbelow(True)
|
| 100 |
|
| 101 |
-
# ================================================================
|
| 102 |
-
# 维持 v8.0 的完美 Unicode ε 和 σ
|
| 103 |
-
# ================================================================
|
| 104 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 105 |
-
LS_LABELS_PLOT = ["Baseline", "LS(
|
| 106 |
LS_LABELS_MD = ["基线(Baseline)", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 107 |
|
| 108 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 109 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 110 |
-
OP_LABELS_PLOT = [f"OP(
|
| 111 |
OP_LABELS_MD = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 112 |
|
| 113 |
-
ALL_KEYS = LS_KEYS + OP_KEYS
|
| 114 |
-
|
| 115 |
def gm(key, metric, default=0):
|
| 116 |
if key in mia_results: return mia_results[key].get(metric, default)
|
| 117 |
if key in perturb_results: return perturb_results[key].get(metric, default)
|
|
@@ -127,58 +121,13 @@ bl_acc = gu("baseline")
|
|
| 127 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 128 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 129 |
|
| 130 |
-
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题', 'concept': '概念问答', 'error_correction': '错题订正'}
|
| 131 |
-
|
| 132 |
-
np.random.seed(777)
|
| 133 |
-
EVAL_POOL = []
|
| 134 |
-
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
| 135 |
-
for _i in range(300):
|
| 136 |
-
_t = _types[_i]
|
| 137 |
-
if _t == 'calculation':
|
| 138 |
-
_a, _b = int(np.random.randint(10,500)), int(np.random.randint(10,500))
|
| 139 |
-
_op = ['+','-','x'][_i%3]
|
| 140 |
-
if _op=='+': _q,_ans=f"{_a} + {_b} = ?",str(_a+_b)
|
| 141 |
-
elif _op=='-': _q,_ans=f"{_a} - {_b} = ?",str(_a-_b)
|
| 142 |
-
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 143 |
-
elif _t == 'word_problem':
|
| 144 |
-
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 145 |
-
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)), (f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
| 146 |
-
_q,_ans = _tpls[_i%len(_tpls)]
|
| 147 |
-
elif _t == 'concept':
|
| 148 |
-
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
| 149 |
-
_cn,_df = _cs[_i%len(_cs)]; _q,_ans = f"What is {_cn}?",_df
|
| 150 |
-
else:
|
| 151 |
-
_a,_b = int(np.random.randint(10,99)), int(np.random.randint(10,99))
|
| 152 |
-
_w = _a+_b+int(np.random.choice([-1,1,-10,10]))
|
| 153 |
-
_q,_ans = f"Student got {_a}+{_b}={_w}, correct?",str(_a+_b)
|
| 154 |
-
item = {'question':_q,'answer':_ans,'type_cn':TYPE_CN[_t]}
|
| 155 |
-
for key in LS_KEYS:
|
| 156 |
-
acc = gu(key)/100; item[key] = bool(np.random.random()<acc)
|
| 157 |
-
EVAL_POOL.append(item)
|
| 158 |
-
|
| 159 |
# ================================================================
|
| 160 |
-
# 图表绘制
|
| 161 |
# ================================================================
|
| 162 |
-
def
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
ax.
|
| 166 |
-
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 167 |
-
ax.text(m_mean - 0.002, 1.02, f'Member Mean\n{m_mean:.4f}', ha='right', va='bottom', fontsize=9, color=COLORS['accent'], transform=ax.get_xaxis_transform())
|
| 168 |
-
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 169 |
-
ax.text(nm_mean + 0.002, 1.02, f'Non-Member Mean\n{nm_mean:.4f}', ha='left', va='bottom', fontsize=9, color=COLORS['danger'], transform=ax.get_xaxis_transform())
|
| 170 |
-
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 171 |
-
ax.text(thr, 1.25, f'Threshold\n{thr:.4f}', ha='center', va='bottom', fontsize=10, fontweight='bold', color=COLORS['text_dim'], transform=ax.get_xaxis_transform())
|
| 172 |
-
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 173 |
-
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 174 |
-
ax.text(loss_val, 0.75, f'Current Loss\n{loss_val:.4f}', ha='center', fontsize=11, fontweight='bold', color=mc, transform=ax.get_xaxis_transform())
|
| 175 |
-
ax.text((xlo+thr)/2, 0.25, 'MEMBER', ha='center', fontsize=12, color=COLORS['accent'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
| 176 |
-
ax.text((thr+xhi)/2, 0.25, 'NON-MEMBER', ha='center', fontsize=12, color=COLORS['danger'], alpha=0.6, fontweight='bold', transform=ax.get_xaxis_transform())
|
| 177 |
-
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 178 |
-
for s in ax.spines.values(): s.set_visible(False)
|
| 179 |
-
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid']); ax.tick_params(colors=COLORS['text_dim'], width=1)
|
| 180 |
-
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium'); plt.tight_layout(pad=0.5)
|
| 181 |
-
return fig
|
| 182 |
|
| 183 |
def fig_auc_bar():
|
| 184 |
names, vals, clrs = [], [], []
|
|
@@ -190,44 +139,59 @@ def fig_auc_bar():
|
|
| 190 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax)
|
| 191 |
bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 192 |
for b,v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+0.01, f'{v:.4f}', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 193 |
-
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', lw=1.5, alpha=0.6,
|
| 194 |
-
ax.axhline(bl_auc, color=COLORS['danger'], ls=':', lw=1.5, alpha=0.8,
|
| 195 |
-
|
|
|
|
| 196 |
ax.set_ylim(0.45, max(vals)+0.05); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=11)
|
| 197 |
-
|
| 198 |
-
return fig
|
| 199 |
-
|
| 200 |
-
def fig_radar():
|
| 201 |
-
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 202 |
-
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1', 'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 203 |
-
N = len(ms); ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 204 |
-
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7), subplot_kw=dict(polar=True)); fig.patch.set_facecolor('white')
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
plt.tight_layout()
|
| 221 |
return fig
|
| 222 |
|
| 223 |
-
#
|
| 224 |
def fig_d3_dist_compare():
|
| 225 |
configs = [
|
| 226 |
-
("
|
| 227 |
-
("
|
| 228 |
-
("
|
| 229 |
]
|
| 230 |
-
fig, axes = plt.subplots(1, 3, figsize=(
|
| 231 |
apply_light_style(fig, axes)
|
| 232 |
|
| 233 |
for idx, (title, key, color, sigma) in enumerate(configs):
|
|
@@ -241,260 +205,97 @@ def fig_d3_dist_compare():
|
|
| 241 |
nm_losses = nm_losses + rn.normal(0, sigma, len(nm_losses))
|
| 242 |
all_v = np.concatenate([m_losses, nm_losses])
|
| 243 |
bins = np.linspace(all_v.min(), all_v.max(), 35)
|
| 244 |
-
ax.hist(m_losses, bins=bins, alpha=0.55, color=COLORS['accent'], label='Member', density=True, edgecolor='white')
|
| 245 |
-
ax.hist(nm_losses, bins=bins, alpha=0.55, color=COLORS['danger'], label='Non-Member', density=True, edgecolor='white')
|
| 246 |
m_mean = np.mean(m_losses); nm_mean = np.mean(nm_losses)
|
| 247 |
gap = nm_mean - m_mean
|
| 248 |
-
ax.axvline(m_mean, color=COLORS['accent'], ls='--', lw=
|
| 249 |
-
ax.axvline(nm_mean, color=COLORS['danger'], ls='--', lw=
|
| 250 |
-
ax.annotate(f'Gap={gap:.4f}', xy=((m_mean+nm_mean)/2, ax.get_ylim()[1]*0.85 if ax.get_ylim()[1]>0 else 5),
|
| 251 |
-
fontsize=
|
| 252 |
-
bbox=dict(boxstyle='round,pad=0.
|
| 253 |
-
|
| 254 |
-
ax.set_xlabel('Loss',
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
plt.tight_layout(); return fig
|
| 259 |
|
| 260 |
-
def
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
ax.set_title(f'{l}\nAUC={a:.4f}', fontsize=11, fontweight='semibold'); ax.set_xlabel('Loss', fontsize=10); ax.set_ylabel('Density', fontsize=10)
|
| 269 |
-
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 270 |
-
plt.tight_layout(); return fig
|
| 271 |
|
| 272 |
-
def
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
rng_m = np.random.RandomState(42); rng_nm = np.random.RandomState(137)
|
| 278 |
-
mp = ml + rng_m.normal(0, s, len(ml)); np_ = nl + rng_nm.normal(0, s, len(nl)); v = np.concatenate([mp, np_])
|
| 279 |
-
bins = np.linspace(v.min(), v.max(), 28)
|
| 280 |
-
ax.hist(mp, bins=bins, alpha=0.6, color=COLORS['accent'], label='Mem+noise', density=True, edgecolor='white')
|
| 281 |
-
ax.hist(np_, bins=bins, alpha=0.6, color=COLORS['danger'], label='Non+noise', density=True, edgecolor='white')
|
| 282 |
-
pa = gm(f'perturbation_{s}', 'auc')
|
| 283 |
-
ax.set_title(f'OP(σ={s})\nAUC={pa:.4f}', fontsize=11, fontweight='semibold'); ax.set_xlabel('Loss', fontsize=10)
|
| 284 |
-
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 285 |
-
plt.tight_layout(); return fig
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
ax = axes[0]; ls_colors = [COLORS['danger'], COLORS['ls_colors'][0], COLORS['ls_colors'][1], COLORS['ls_colors'][2], COLORS['ls_colors'][3]]
|
| 290 |
-
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 291 |
-
if k not in full_losses: continue
|
| 292 |
-
m = np.array(full_losses[k]['member_losses']); nm = np.array(full_losses[k]['non_member_losses'])
|
| 293 |
-
y_true = np.concatenate([np.ones(len(m)), np.zeros(len(nm))]); y_scores = np.concatenate([-m, -nm])
|
| 294 |
-
fpr, tpr, _ = roc_curve(y_true, y_scores); auc_val = roc_auc_score(y_true, y_scores)
|
| 295 |
-
ax.plot(fpr, tpr, color=ls_colors[i], lw=2.5, label=f'{l} (AUC={auc_val:.4f})')
|
| 296 |
-
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5, label='Random'); ax.set_xlabel('False Positive Rate', fontsize=12, fontweight='medium'); ax.set_ylabel('True Positive Rate', fontsize=12, fontweight='medium'); ax.set_title('[D1] ROC Curves: Label Smoothing', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 297 |
-
ax = axes[1]
|
| 298 |
-
if 'baseline' in full_losses:
|
| 299 |
-
ml_base = np.array(full_losses['baseline']['member_losses']); nl_base = np.array(full_losses['baseline']['non_member_losses']); y_true = np.concatenate([np.ones(len(ml_base)), np.zeros(len(nl_base))]); y_scores = np.concatenate([-ml_base, -nl_base])
|
| 300 |
-
fpr, tpr, _ = roc_curve(y_true, y_scores); ax.plot(fpr, tpr, color=COLORS['danger'], lw=2.5, label=f'Baseline (AUC={bl_auc:.4f})')
|
| 301 |
-
for i, s in enumerate(OP_SIGMAS):
|
| 302 |
-
rng_m = np.random.RandomState(42); rng_nm = np.random.RandomState(137); mp = ml_base + rng_m.normal(0, s, len(ml_base)); np_ = nl_base + rng_nm.normal(0, s, len(nl_base)); y_scores_p = np.concatenate([-mp, -np_]); fpr_p, tpr_p, _ = roc_curve(y_true, y_scores_p); auc_p = roc_auc_score(y_true, y_scores_p)
|
| 303 |
-
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP(σ={s}) (AUC={auc_p:.4f})')
|
| 304 |
-
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5, label='Random'); ax.set_xlabel('False Positive Rate', fontsize=12, fontweight='medium'); ax.set_ylabel('True Positive Rate', fontsize=12, fontweight='medium'); ax.set_title('[D1] ROC Curves: Output Perturbation', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], loc='lower right'); plt.tight_layout()
|
| 305 |
-
return fig
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
return fig
|
| 318 |
|
|
|
|
| 319 |
def fig_acc_bar():
|
| 320 |
names, vals, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 321 |
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 322 |
if k in utility_results: names.append(l); vals.append(utility_results[k]['accuracy']*100); clrs.append(ls_c[i])
|
| 323 |
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 324 |
if k in perturb_results: names.append(l); vals.append(bl_acc); clrs.append(COLORS['op_colors'][i])
|
| 325 |
-
fig, ax = plt.subplots(figsize=(
|
| 326 |
-
for b, v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+1, f'{v:.1f}%', ha='center', fontsize=
|
| 327 |
-
|
|
|
|
| 328 |
return fig
|
| 329 |
|
| 330 |
def fig_tradeoff():
|
| 331 |
-
fig, ax = plt.subplots(figsize=(
|
| 332 |
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 333 |
-
if k in mia_results and k in utility_results: ax.scatter(utility_results[k]['accuracy']*100, mia_results[k]['auc'], label=l, marker=markers_ls[i], color=ls_c[i], s=
|
| 334 |
op_markers = ['^', 'D', 'v', 'P', 'X', 'h']
|
| 335 |
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 336 |
-
if k in perturb_results: ax.scatter(bl_acc, perturb_results[k]['auc'], label=l, marker=op_markers[i], color=COLORS['op_colors'][i], s=
|
| 337 |
-
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', alpha=0.6
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
def fig_auc_trend():
|
| 341 |
-
fig, axes = plt.subplots(1, 2, figsize=(16, 6.5)); apply_light_style(fig, axes); ax = axes[0]; eps_vals = [0.0, 0.02, 0.05, 0.1, 0.2]; auc_vals = [gm(k, 'auc') for k in LS_KEYS]; acc_vals = [gu(k) for k in LS_KEYS]
|
| 342 |
-
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (Risk)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility %', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 343 |
-
ax.fill_between(eps_vals, auc_vals, 0.5, alpha=0.08, color=COLORS['danger'])
|
| 344 |
-
ax.set_xlabel('Label Smoothing ε', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC (Risk)', fontsize=12, fontweight='medium', color=COLORS['danger']); ax2.set_ylabel('Utility (%)', fontsize=12, fontweight='medium', color=COLORS['accent']); ax.set_title('[D5] LS: Risk DOWN + Utility UP = Win-Win', fontsize=14, fontweight='bold', pad=15, color=COLORS['accent2']); ax.tick_params(axis='y', labelcolor=COLORS['danger']); ax2.tick_params(axis='y', labelcolor=COLORS['accent']); ax2.spines['right'].set_color(COLORS['accent']); ax2.spines['left'].set_color(COLORS['danger']); lines = line1 + line2; labels = [l.get_label() for l in lines]; ax.legend(lines, labels, fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 345 |
-
|
| 346 |
-
ax = axes[1]; sig_vals = OP_SIGMAS; auc_op = [gm(k, 'auc') for k in OP_KEYS]; ax.plot(sig_vals, auc_op, 'o-', color=COLORS['success'], lw=3, ms=9, zorder=5, label='MIA AUC'); ax.axhline(bl_auc, color=COLORS['danger'], ls='--', lw=2, alpha=0.6, label=f'Baseline ({bl_auc:.4f})'); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5, label='Random (0.5)'); ax.fill_between(sig_vals, auc_op, bl_auc, alpha=0.2, color=COLORS['success'], label='AUC Reduction')
|
| 347 |
-
|
| 348 |
-
ax2r = ax.twinx(); ax2r.axhline(bl_acc, color=COLORS['success'], ls='-', lw=2.5, alpha=0.8); ax2r.set_ylabel(f'Utility = {bl_acc:.1f}% (unchanged)', fontsize=12, fontweight='medium', color=COLORS['success']); ax2r.set_ylim(0,100); ax2r.tick_params(axis='y', labelcolor=COLORS['success']); ax2r.spines['right'].set_color(COLORS['success'])
|
| 349 |
-
|
| 350 |
-
ax.set_xlabel('Perturbation σ', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC', fontsize=12, fontweight='medium'); ax.set_title('[D5] OP: Risk DOWN + Utility UNCHANGED', fontsize=14, fontweight='bold', pad=15, color=COLORS['success']); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text']); plt.tight_layout()
|
| 351 |
-
return fig
|
| 352 |
-
|
| 353 |
-
def fig_loss_gap_waterfall():
|
| 354 |
-
fig, ax = plt.subplots(figsize=(14, 6.5)); apply_light_style(fig, ax); names, gaps, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 355 |
-
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)): names.append(l); gaps.append(gm(k, 'loss_gap')); clrs.append(ls_c[i])
|
| 356 |
-
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)): names.append(l); gaps.append(gm(k, 'loss_gap')); clrs.append(COLORS['op_colors'][i])
|
| 357 |
-
bars = ax.bar(range(len(names)), gaps, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 358 |
-
for b, v in zip(bars, gaps): ax.text(b.get_x()+b.get_width()/2, v+0.0005, f'{v:.4f}', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 359 |
-
ax.set_ylabel('Loss Gap', fontsize=12, fontweight='medium'); ax.set_title('[D3] Loss Gap: Root Cause of MIA (Smaller = Better Defense)', fontsize=14, fontweight='bold', pad=20); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=11); ax.annotate('Smaller gap = Better Privacy', xy=(8, gaps[0]*0.4), fontsize=11, color=COLORS['success'], fontstyle='italic', ha='center', backgroundcolor=COLORS['bg'], bbox=dict(boxstyle='round,pad=0.4', facecolor=COLORS['panel'], edgecolor=COLORS['success'], alpha=0.8)); plt.tight_layout()
|
| 360 |
return fig
|
| 361 |
|
| 362 |
-
# ================================================================
|
| 363 |
-
# 回调函数
|
| 364 |
-
# ================================================================
|
| 365 |
-
def cb_sample(src):
|
| 366 |
-
pool = member_data if "训练集" in src else non_member_data
|
| 367 |
-
s = pool[np.random.randint(len(pool))]
|
| 368 |
-
m = s['metadata']
|
| 369 |
-
md = f"""
|
| 370 |
-
<table style="width:100%; border-collapse: collapse; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 371 |
-
<tr style="background-color: #F9F9F9;">
|
| 372 |
-
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">字段</th>
|
| 373 |
-
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">值</th>
|
| 374 |
-
</tr>
|
| 375 |
-
<tr><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">姓名</td><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{clean_text(str(m.get('name','')))}</td></tr>
|
| 376 |
-
<tr><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">学号</td><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{clean_text(str(m.get('student_id','')))}</td></tr>
|
| 377 |
-
<tr><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">班级</td><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{clean_text(str(m.get('class','')))}</td></tr>
|
| 378 |
-
<tr><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">成绩</td><td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{clean_text(str(m.get('score','')))} 分</td></tr>
|
| 379 |
-
<tr><td style="padding: 10px; color: #1D1D1F;">类型</td><td style="padding: 10px; color: #1D1D1F;">{TYPE_CN.get(s.get('task_type',''), '')}</td></tr>
|
| 380 |
-
</table>
|
| 381 |
-
"""
|
| 382 |
-
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 383 |
-
|
| 384 |
-
ATK_CHOICES = (
|
| 385 |
-
["基线模型 (Baseline)"] +
|
| 386 |
-
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
| 387 |
-
[f"输出扰动 (σ={s})" for s in OP_SIGMAS]
|
| 388 |
-
)
|
| 389 |
-
ATK_MAP = {"基线模型 (Baseline)": "baseline"}
|
| 390 |
-
for e in [0.02, 0.05, 0.1, 0.2]: ATK_MAP[f"标签平滑 (ε={e})"] = f"smooth_eps_{e}"
|
| 391 |
-
for s in OP_SIGMAS: ATK_MAP[f"输出扰动 (σ={s})"] = f"perturbation_{s}"
|
| 392 |
-
|
| 393 |
-
def cb_attack(idx, src, target):
|
| 394 |
-
is_mem = "训练集" in src
|
| 395 |
-
pool = member_data if is_mem else non_member_data
|
| 396 |
-
idx = min(int(idx), len(pool)-1)
|
| 397 |
-
sample = pool[idx]
|
| 398 |
-
key = ATK_MAP.get(target, "baseline")
|
| 399 |
-
is_op = key.startswith("perturbation_")
|
| 400 |
-
|
| 401 |
-
if is_op:
|
| 402 |
-
sigma = float(key.split("_")[1])
|
| 403 |
-
fr = full_losses.get('baseline', {})
|
| 404 |
-
lk = 'member_losses' if is_mem else 'non_member_losses'
|
| 405 |
-
ll = fr.get(lk, [])
|
| 406 |
-
base_loss = ll[idx] if idx < len(ll) else float(np.random.normal(bl_m_mean if is_mem else bl_nm_mean, 0.02))
|
| 407 |
-
np.random.seed(idx*1000 + int(sigma*10000))
|
| 408 |
-
loss = base_loss + np.random.normal(0, sigma)
|
| 409 |
-
|
| 410 |
-
mm = gm(key, "member_loss_mean", 0.19)
|
| 411 |
-
nm_m = gm(key, "non_member_loss_mean", 0.20)
|
| 412 |
-
ms = gm(key, "member_loss_std", np.sqrt(0.03**2 + sigma**2))
|
| 413 |
-
ns = gm(key, "non_member_loss_std", np.sqrt(0.03**2 + sigma**2))
|
| 414 |
-
auc_v = gm(key, "auc")
|
| 415 |
-
lbl = f"OP(σ={sigma})"
|
| 416 |
-
else:
|
| 417 |
-
info = mia_results.get(key, mia_results.get('baseline', {}))
|
| 418 |
-
fr = full_losses.get(key, full_losses.get('baseline', {}))
|
| 419 |
-
lk = 'member_losses' if is_mem else 'non_member_losses'
|
| 420 |
-
ll = fr.get(lk, [])
|
| 421 |
-
loss = ll[idx] if idx < len(ll) else float(np.random.normal(info.get('member_loss_mean',0.19), 0.02))
|
| 422 |
-
mm = info.get('member_loss_mean', 0.19); nm_m = info.get('non_member_loss_mean', 0.20)
|
| 423 |
-
ms = info.get('member_loss_std', 0.03); ns = info.get('non_member_loss_std', 0.03)
|
| 424 |
-
auc_v = info.get('auc', 0)
|
| 425 |
-
lbl = "Baseline" if key == "baseline" else f"LS(ε={key.replace('smooth_eps_','')})"
|
| 426 |
-
|
| 427 |
-
thr = (mm + nm_m) / 2
|
| 428 |
-
pred = loss < thr
|
| 429 |
-
correct = pred == is_mem
|
| 430 |
-
gauge = fig_gauge(loss, mm, nm_m, thr, ms, ns)
|
| 431 |
-
|
| 432 |
-
pl = "🔴 训练成员" if pred else "🟢 非训练成员"
|
| 433 |
-
al = "🔴 训练成员" if is_mem else "🟢 非训练成员"
|
| 434 |
-
|
| 435 |
-
if correct and pred and is_mem:
|
| 436 |
-
v = f"<div style='background-color: #FFEBEE; border-left: 4px solid {COLORS['danger']}; padding: 12px; border-radius: 8px; color: {COLORS['danger']}; box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-top: 0px;'>⚠️ <b>攻击成功:隐私泄露</b><br><span style='font-size: 0.9em; color: #B71C1C;'>模型对该样本过于熟悉(Loss < 阈值),攻击者成功判定为训练数据。</span></div>"
|
| 437 |
-
elif correct:
|
| 438 |
-
v = f"<div style='background-color: #E8F5E9; border-left: 4px solid {COLORS['success']}; padding: 12px; border-radius: 8px; color: {COLORS['success']}; box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-top: 0px;'>✅ <b>判定正确</b><br><span style='font-size: 0.9em; color: #1B5E20;'>攻击者判定与真实身份一致。</span></div>"
|
| 439 |
-
else:
|
| 440 |
-
v = f"<div style='background-color: #E3F2FD; border-left: 4px solid {COLORS['accent']}; padding: 12px; border-radius: 8px; color: {COLORS['accent']}; box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-top: 0px;'>🛡️ <b>防御成功</b><br><span style='font-size: 0.9em; color: #0D47A1;'>攻击者判定错误,防御起到了保护作用。</span></div>"
|
| 441 |
-
|
| 442 |
-
table_html = f"""
|
| 443 |
-
<table style="width:100%; border-collapse: collapse; margin-top: 10px; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 444 |
-
<thead style="background-color: #F9F9F9;">
|
| 445 |
-
<tr>
|
| 446 |
-
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">项目</th>
|
| 447 |
-
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">攻击者判定</th>
|
| 448 |
-
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">真实身份</th>
|
| 449 |
-
</tr>
|
| 450 |
-
</thead>
|
| 451 |
-
<tbody>
|
| 452 |
-
<tr>
|
| 453 |
-
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">身份</td>
|
| 454 |
-
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{pl}</td>
|
| 455 |
-
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{al}</td>
|
| 456 |
-
</tr>
|
| 457 |
-
<tr>
|
| 458 |
-
<td style="padding: 10px; color: #1D1D1F;">Loss / 阈值</td>
|
| 459 |
-
<td style="padding: 10px; color: #1D1D1F;">Loss: {loss:.4f}</td>
|
| 460 |
-
<td style="padding: 10px; color: #1D1D1F;">阈值: {thr:.4f}</td>
|
| 461 |
-
</tr>
|
| 462 |
-
</tbody>
|
| 463 |
-
</table>
|
| 464 |
-
"""
|
| 465 |
-
|
| 466 |
-
res = v + f"<div style='font-weight: 600; margin: 12px 0 8px 0;'>🎯 攻击目标: {lbl} <span style='margin-left: 20px; color: #86868B;'>📊 AUC: {auc_v:.4f}</span></div>" + table_html
|
| 467 |
-
qtxt = f"**样本 #{idx}**\n\n" + clean_text(sample.get('question',''))[:500]
|
| 468 |
-
return qtxt, gauge, res
|
| 469 |
-
|
| 470 |
-
EVAL_CHOICES = (
|
| 471 |
-
["基线模型"] +
|
| 472 |
-
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
| 473 |
-
[f"输出扰动 (σ={s})" for s in OP_SIGMAS]
|
| 474 |
-
)
|
| 475 |
-
EVAL_KEY_MAP = {"基线模型": "baseline"}
|
| 476 |
-
for e in [0.02, 0.05, 0.1, 0.2]: EVAL_KEY_MAP[f"标签平滑 (ε={e})"] = f"smooth_eps_{e}"
|
| 477 |
-
for s in OP_SIGMAS: EVAL_KEY_MAP[f"输出扰动 (σ={s})"] = "baseline"
|
| 478 |
-
|
| 479 |
-
def cb_eval(model_choice):
|
| 480 |
-
k = EVAL_KEY_MAP.get(model_choice, "baseline")
|
| 481 |
-
acc = gu(k) if "输出扰动" not in model_choice else bl_acc
|
| 482 |
-
q = EVAL_POOL[np.random.randint(len(EVAL_POOL))]
|
| 483 |
-
ok = q.get(k, q.get('baseline', False))
|
| 484 |
-
ic = "✅ 正确" if ok else "❌ 错误"
|
| 485 |
-
note = "\n\n> 输出扰动不改变模型参数,准确率与基线一致。" if "输出扰动" in model_choice else ""
|
| 486 |
-
table_html = f"""
|
| 487 |
-
<table style="width:100%; border-collapse: collapse; margin-top: 15px; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 488 |
-
<tbody>
|
| 489 |
-
<tr><td style="padding: 12px; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0; width: 100px;">类型</td><td style="padding: 12px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{q['type_cn']}</td></tr>
|
| 490 |
-
<tr><td style="padding: 12px; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">题目</td><td style="padding: 12px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{q['question']}</td></tr>
|
| 491 |
-
<tr><td style="padding: 12px; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">正确答案</td><td style="padding: 12px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{q['answer']}</td></tr>
|
| 492 |
-
<tr><td style="padding: 12px; color: #86868B; font-weight: 600;">判定</td><td style="padding: 12px; color: #1D1D1F;">{ic}</td></tr>
|
| 493 |
-
</tbody>
|
| 494 |
-
</table>
|
| 495 |
-
"""
|
| 496 |
-
return (f"<div style='font-weight: 600; margin-bottom: 10px;'>🤖 模型: {model_choice} <span style='margin-left: 20px; color: #86868B;'>🎯 准确率: {acc:.1f}%</span></div>" + table_html + note)
|
| 497 |
-
|
| 498 |
def build_full_table():
|
| 499 |
rows = []
|
| 500 |
for k, l in zip(LS_KEYS, LS_LABELS_MD):
|
|
@@ -511,56 +312,25 @@ def build_full_table():
|
|
| 511 |
return header + "\n" + "\n".join(rows)
|
| 512 |
|
| 513 |
# ================================================================
|
| 514 |
-
#
|
| 515 |
# ================================================================
|
| 516 |
CSS = """
|
| 517 |
-
:root {
|
| 518 |
-
--primary-blue: #007AFF;
|
| 519 |
-
--bg-light: #F5F5F7;
|
| 520 |
-
--card-bg: #FFFFFF;
|
| 521 |
-
--text-dark: #1D1D1F;
|
| 522 |
-
--text-gray: #86868B;
|
| 523 |
-
--border-color: #D2D2D7;
|
| 524 |
-
}
|
| 525 |
-
|
| 526 |
body { background-color: var(--bg-light) !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important; color: var(--text-dark) !important; }
|
| 527 |
.gradio-container { max-width: 1350px !important; margin: 40px auto !important; }
|
| 528 |
.title-area { background-color: var(--card-bg); padding: 32px 40px; border-radius: 18px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); margin-bottom: 30px; text-align: center; }
|
| 529 |
-
.title-area h1 { color: var(--text-dark) !important; font-size: 2.2rem !important; font-weight: 700 !important; margin-bottom: 10px !important;
|
| 530 |
.title-area p { color: var(--text-gray) !important; font-size: 1.1rem !important; margin-bottom: 15px !important; }
|
| 531 |
.title-area .badge { display: inline-block; background-color: #E5F1FF; color: var(--primary-blue); padding: 6px 16px; border-radius: 20px; font-size: 0.9rem; font-weight: 600; }
|
| 532 |
-
.tabitem { background-color: var(--card-bg) !important; border-radius: 18px !important; border: none !important; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08) !important; padding: 40px !important; margin-top: 20px !important; }
|
| 533 |
-
.tab-nav { border-bottom: none !important; gap: 10px !important; background: transparent !important; padding-bottom: 5px !important; }
|
| 534 |
-
.tab-nav button { font-size: 15px !important; padding: 10px 20px !important; font-weight: 500 !important; color: var(--text-gray) !important; background: rgba(0,0,0,0.03) !important; border: none !important; border-radius: 12px !important; transition: all 0.2s ease !important; }
|
| 535 |
-
.tab-nav button:hover { background: rgba(0,0,0,0.06) !important; color: var(--text-dark) !important; }
|
| 536 |
-
.tab-nav button.selected { color: var(--primary-blue) !important; background: #E5F1FF !important; font-weight: 600 !important; }
|
| 537 |
-
.prose { color: var(--text-dark) !important; }
|
| 538 |
-
.prose h2 { color: var(--text-dark) !important; font-weight: 700 !important; border-bottom: 1px solid var(--border-color) !important; padding-bottom: 12px !important; margin-top: 30px !important; }
|
| 539 |
-
.prose h3 { color: var(--text-dark) !important; font-weight: 600 !important; margin-top: 24px !important; }
|
| 540 |
-
.prose h4 { color: var(--text-gray) !important; font-weight: 600 !important; margin-bottom: 12px !important; }
|
| 541 |
-
.prose table { border-collapse: separate !important; border-spacing: 0 !important; width: 100% !important; border: 1px solid var(--border-color) !important; border-radius: 12px !important; overflow: hidden !important; box-shadow: 0 2px 8px rgba(0,0,0,0.04) !important; }
|
| 542 |
-
.prose th { background: #F9F9F9 !important; color: var(--text-gray) !important; font-weight: 600 !important; padding: 14px 18px !important; text-align: left !important; border-bottom: 1px solid var(--border-color) !important; white-space: nowrap !important; }
|
| 543 |
-
.prose td { padding: 14px 18px !important; color: var(--text-dark) !important; border-bottom: 1px solid var(--border-color) !important; background: var(--card-bg) !important; white-space: nowrap !important; }
|
| 544 |
-
.prose tr:last-child td { border-bottom: none !important; }
|
| 545 |
-
.prose tr:hover td { background: #F5F7FA !important; }
|
| 546 |
-
button.primary { background-color: var(--primary-blue) !important; color: white !important; border: none !important; border-radius: 10px !important; font-weight: 600 !important; padding: 12px 24px !important; box-shadow: 0 2px 6px rgba(0, 122, 255, 0.25) !important; transition: all 0.2s !important; }
|
| 547 |
-
button.primary:hover { background-color: #0062CC !important; box-shadow: 0 4px 10px rgba(0, 122, 255, 0.35) !important; transform: translateY(-1px) !important; }
|
| 548 |
.card-wrap { background: var(--card-bg) !important; border: 1px solid var(--border-color) !important; border-radius: 14px !important; padding: 24px !important; box-shadow: 0 2px 8px rgba(0,0,0,0.04) !important; }
|
| 549 |
-
.block.svelte-12cmxck { border-radius: 12px !important; border-color: var(--border-color) !important; }
|
| 550 |
-
.input-label { color: var(--text-gray) !important; font-weight: 500 !important; }
|
| 551 |
.dim-label { display:inline-block; padding:3px 10px; border-radius:6px; font-size:12px; font-weight:700; letter-spacing:0.05em; margin-right:8px; }
|
| 552 |
-
.dim1 { background:#FEF3F2; color:#B42318; }
|
| 553 |
-
.dim2 { background:#FFFAEB; color:#B54708; }
|
| 554 |
-
.dim3 { background:#F4F3FF; color:#5925DC; }
|
| 555 |
-
.dim4 { background:#EFF8FF; color:#175CD3; }
|
| 556 |
-
.dim5 { background:#F0FDF9; color:#107569; }
|
| 557 |
-
footer { display: none !important; }
|
| 558 |
"""
|
| 559 |
|
| 560 |
# ================================================================
|
| 561 |
-
# UI 布局
|
| 562 |
# ================================================================
|
| 563 |
-
with gr.Blocks(title="MIA攻防研究") as demo:
|
| 564 |
|
| 565 |
gr.HTML("""<div class="title-area">
|
| 566 |
<h1>🎓 教育大模型中的成员推理攻击及其防御研究</h1>
|
|
@@ -571,28 +341,28 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 571 |
with gr.Tab("📊 实验总览"):
|
| 572 |
gr.Markdown(f"""
|
| 573 |
## 📌 研究背景:为什么教育大模型需要防范 MIA?
|
| 574 |
-
|
| 575 |
在教育领域,大模型(如虚拟辅导老师)的训练往往离不开学生真实的互动数据,而这些数据中包含了大量**极度敏感的个人隐私**。本研究基于 **{model_name}** 微调的数学辅导模型,系统揭示并解决这一安全隐患。
|
| 576 |
|
| 577 |
### 1️⃣ 什么是成员推理攻击 (MIA)?
|
| 578 |
**成员推理攻击 (Membership Inference Attack)** 的核心目的,是判断“某一条特定的数据,到底有没有被用来训练过这个AI?”
|
| 579 |
* **测谎仪原理**:大模型有一种“偷懒”的天性,对于它在训练时见过的“旧题”(成员数据),它回答得会极其顺畅,**损失值(Loss)非常低**;而面对没见过的“新题”(非成员数据),Loss 会偏高。攻击者正是利用这个 Loss 差距来做判定。
|
| 580 |
|
| 581 |
-
### 2️⃣ 教育大模型中的 MIA 危害有多大?
|
| 582 |
-
想象
|
| 583 |
> *“老师您好,我是**李明(学号20231001)**。我上次数学只考了**55分**,计算题老是错,请问 25+37 等于多少?”*
|
| 584 |
|
| 585 |
-
如果
|
| 586 |
|
| 587 |
### 3️⃣ 我们如何进行防御?
|
| 588 |
-
|
| 589 |
-
* 🛡️ **
|
| 590 |
-
* 🛡️ **输出扰动 (Output Perturbation, 推理期)**:给 AI 的输出加上“变声器”。在攻击者探查 Loss 值时,强行混入高斯噪声(加沙子),让攻击者看到的 Loss 忽高忽低,彻底瞎掉,但普通用户看到的文字回答依然绝对正确。
|
| 591 |
""")
|
| 592 |
|
|
|
|
| 593 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png")):
|
| 594 |
-
gr.Image(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png"), label="实验体系总览", show_label=
|
| 595 |
|
|
|
|
| 596 |
gr.HTML(f"""<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin:30px 0;">
|
| 597 |
<div class="card-wrap" style="text-align:center;">
|
| 598 |
<div style="font-size:32px;font-weight:700;color:{COLORS['accent']};margin-bottom:8px;">5</div>
|
|
@@ -615,6 +385,10 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 615 |
<div style="font-size:30px;margin-top:10px;">📄</div>
|
| 616 |
</div>
|
| 617 |
</div>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
|
| 619 |
with gr.Tab("📁 数据与模型"):
|
| 620 |
gr.HTML("""<div style="display:flex; flex-direction:row; gap:25px; margin-bottom:30px; align-items:stretch;">
|
|
@@ -636,147 +410,71 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 636 |
<tr><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">✏️ 错题订正</td><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">200</td><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">10%</td></tr>
|
| 637 |
</table>
|
| 638 |
</div></div>""")
|
| 639 |
-
gr.HTML(f'<div style="background:#FFF4E5; border-left:4px solid {COLORS["warning"]}; padding:16px; border-radius:12px; margin-bottom:30px; font-size:14px; color:#663C00; box-shadow: 0 2px 6px rgba(0,0,0,0.05);">⚠️ <b>注意:</b>两组数据格式完全相同(均含隐私字段),这是MIA实验的标准设置——攻击者无法从格式区分。</div>')
|
| 640 |
gr.Markdown("### 🔍 数据样例提取")
|
| 641 |
with gr.Row():
|
| 642 |
with gr.Column(scale=1):
|
| 643 |
-
gr.Markdown("#### ⚙️ 提取控制台")
|
| 644 |
d_src = gr.Radio(["成员数据(训练集)", "非成员数据(测试集)"], value="成员数据(训练集)", label="目标数据源")
|
| 645 |
d_btn = gr.Button("🎲 随机提取样本", variant="primary")
|
| 646 |
d_meta = gr.HTML()
|
| 647 |
with gr.Column(scale=2):
|
| 648 |
-
gr.Markdown("#### 📄 样本详情")
|
| 649 |
d_q = gr.Textbox(label="🧑🎓 学生提问 (Prompt)", lines=6, interactive=False)
|
| 650 |
d_a = gr.Textbox(label="💡 标准回答 (Ground Truth)", lines=6, interactive=False)
|
| 651 |
d_btn.click(cb_sample, [d_src], [d_meta, d_q, d_a])
|
| 652 |
|
| 653 |
with gr.Tab("🧠 算法原理"):
|
| 654 |
gr.Markdown("## 算法流程图与伪代码")
|
| 655 |
-
|
| 656 |
-
gr.Markdown("### Algorithm 1: 基于Loss的成员推理攻击 (MIA)")
|
| 657 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo1_mia_attack.png")):
|
| 658 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo1_mia_attack.png"), show_label=False)
|
| 659 |
-
gr.Markdown(f"""\
|
| 660 |
-
> **原理讲解:** MIA利用了“模型对训练数据记忆更深”这一现象。当模型“见过”某条数据时,它的预测不确定性更低,表现为**Loss偏低**。攻击者正是利用这个差异来判断数据是否属于训练集。
|
| 661 |
-
>
|
| 662 |
-
> 本实验中,基线模型的成员平均Loss={bl_m_mean:.4f},非成员平均Loss={bl_nm_mean:.4f},差距{bl_nm_mean-bl_m_mean:.4f},足以被攻击者利用。
|
| 663 |
-
""")
|
| 664 |
-
|
| 665 |
-
gr.Markdown("---\n### Algorithm 2: 标签平滑防御(训练期)")
|
| 666 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo2_label_smoothing.png")):
|
| 667 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo2_label_smoothing.png"), show_label=False)
|
| 668 |
-
gr.Markdown("""\
|
| 669 |
-
> **原理讲解:** 标签平滑将one-hot硬标签软化为概率分布。例如,原始标签[0,0,1,0]变为[0.033,0.033,0.9,0.033]。这迫使模型不再“100%确定”某个答案,从而降低对训练数据的过度记忆。
|
| 670 |
-
>
|
| 671 |
-
> 副作用:正则化效应还能防止过拟合,提升泛化能力。这就是为什么效用会反升的原因。
|
| 672 |
-
""")
|
| 673 |
-
|
| 674 |
-
gr.Markdown("---\n### Algorithm 3: 输出扰动防御(推理期)")
|
| 675 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png")):
|
| 676 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png"), show_label=False)
|
| 677 |
-
gr.Markdown("""\
|
| 678 |
-
> **原理讲解:** 输出扰动不修改模型本身,而是在返回给攻击者的Loss值上加入随机噪声。攻击者看到的是被噪声污染的Loss,无法精确判断是否低于阈值。
|
| 679 |
-
>
|
| 680 |
-
> 优势:①不需重新训练 ②即插即用 ③不影响模型回答质量(因为只扰动Loss,不扰动生成结果)
|
| 681 |
-
""")
|
| 682 |
|
| 683 |
-
with gr.Tab("🎯 攻击验证"):
|
| 684 |
-
gr.Markdown("## 🕵️ 成员推理攻击交互演示\n\n配置攻击目标与数据源,系统将执行 Loss 计算并映射判定边界。")
|
| 685 |
-
with gr.Row():
|
| 686 |
-
with gr.Column(scale=1):
|
| 687 |
-
gr.Markdown("#### ⚙️ 攻击配置台")
|
| 688 |
-
a_t = gr.Dropdown(choices=ATK_CHOICES, value=ATK_CHOICES[0], label="🎯 选择被攻击模型", interactive=True)
|
| 689 |
-
a_s = gr.Radio(["成员数据(训练集)", "非成员数据(测试集)"], value="成员数据(训练集)", label="📂 输入数据源")
|
| 690 |
-
a_i = gr.Slider(0, 999, step=1, value=12, label="📌 定位样本 ID")
|
| 691 |
-
a_b = gr.Button("⚡ 执行成员推理攻击", variant="primary")
|
| 692 |
-
a_qt = gr.HTML()
|
| 693 |
-
with gr.Column(scale=2):
|
| 694 |
-
gr.Markdown("#### 📉 攻击结果与 Loss 边界")
|
| 695 |
-
a_g = gr.Plot(label="Loss位置判定 (Decision Boundary)")
|
| 696 |
-
a_r = gr.HTML()
|
| 697 |
-
a_b.click(cb_attack, [a_i, a_s, a_t], [a_qt, a_g, a_r])
|
| 698 |
-
|
| 699 |
-
# ================================================================
|
| 700 |
-
# 🌟 核心:五维度攻防分析 (结合之前的详细数据表)
|
| 701 |
-
# ================================================================
|
| 702 |
with gr.Tab("🛡️ 五维度攻防分析"):
|
| 703 |
gr.Markdown("## 多维度攻防效果完整论证")
|
| 704 |
|
| 705 |
-
# 维度一:宏观评价
|
| 706 |
gr.HTML('<div style="margin:20px 0 8px;"><span class="dim-label dim1">D1</span><strong style="font-size:18px;color:#1D2939;">宏观评价维度 — 证明“总体攻防能力”</strong></div>')
|
| 707 |
gr.Markdown(f"""\
|
| 708 |
-
> **
|
| 709 |
-
>
|
| 710 |
-
> **证明防御有效:** 施加防御后(无论是 LS 还是 OP),随着参数强度的增加,AUC 柱子显著变矮,且 ROC 曲线几乎被完全压平(贴近对角线)。这证明防御从根本上瓦解了攻击的有效性。
|
| 711 |
""")
|
| 712 |
gr.Plot(value=fig_auc_bar())
|
| 713 |
gr.Plot(value=fig_roc_curves())
|
| 714 |
|
| 715 |
-
# 维度二:极限实战
|
| 716 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim2">D2</span><strong style="font-size:18px;color:#1D2939;">极限实战维度 — 证明“极低误报下的安全底线”</strong></div>')
|
| 717 |
gr.Markdown(f"""\
|
| 718 |
-
> **实战意义:**
|
| 719 |
-
>
|
| 720 |
-
> 开启 OP(σ=0.03) 防御后,这个成功率被死死压制到了 **{gm('perturbation_0.03','tpr_at_1fpr')*100:.1f}%**(绿柱子极矮)。这证明我们的防御在最极端的实战条件下依然坚如磐石。
|
| 721 |
""")
|
| 722 |
gr.Plot(value=fig_tpr_at_low_fpr())
|
| 723 |
|
| 724 |
-
# 维度三:机制溯源
|
| 725 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim3">D3</span><strong style="font-size:18px;color:#1D2939;">机制溯源维度 — 证明“底层物理逻辑”</strong></div>')
|
| 726 |
gr.Markdown(f"""\
|
| 727 |
-
> **攻击
|
| 728 |
-
>
|
| 729 |
-
> **
|
| 730 |
-
> **输出扰动(OP)的防御本质:** 均值差距虽然没变,但强行加入的高斯噪声导致分布变得极其扁平宽阔,红蓝区域被完全搅混,彻底蒙蔽了攻击者的双眼。
|
| 731 |
""")
|
| 732 |
-
|
|
|
|
| 733 |
gr.Plot(value=fig_loss_gap_waterfall())
|
| 734 |
-
|
| 735 |
-
with gr.Accordion("📉 查看所有模型详细 Loss 分布直方图", open=False):
|
| 736 |
-
gr.Plot(value=fig_loss_dist())
|
| 737 |
-
gr.Plot(value=fig_perturb_dist())
|
| 738 |
|
| 739 |
-
# 维度四:无死角压制
|
| 740 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim4">D4</span><strong style="font-size:18px;color:#1D2939;">无死角压制维度 — 证明“防御没有偏科”</strong></div>')
|
| 741 |
gr.Markdown("""\
|
| 742 |
-
> **为什么
|
| 743 |
-
>
|
| 744 |
-
> 但无论是看左图的 LS 还是右图的 OP,随着参数增加,**整个多边形在极其均匀地向内收缩**。这证明我们的防线是 360 度无死角的,不是靠操纵某一个单一指标得出的结论。
|
| 745 |
""")
|
| 746 |
gr.Plot(value=fig_radar())
|
| 747 |
|
| 748 |
-
# 维度五:落地代价
|
| 749 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim5">D5</span><strong style="font-size:18px;color:#1D2939;">落地代价维度 — 证明“隐私与效用的完美平衡”</strong></div>')
|
| 750 |
gr.Markdown(f"""\
|
| 751 |
-
>
|
| 752 |
-
>
|
| 753 |
-
> **
|
| 754 |
-
> **标签平滑 (LS):** 打出了出人意料的 **双赢 (Win-Win)** 局面,蓝色的效用曲线逆势上扬到了 {gu('smooth_eps_0.2'):.1f}%。不仅堵住了隐私漏洞,还治好了模型的过拟合!
|
| 755 |
""")
|
| 756 |
gr.Plot(value=fig_auc_trend())
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
with gr.Column(): gr.Plot(value=fig_tradeoff())
|
| 760 |
-
|
| 761 |
-
# 完整数据表 (保留修复版)
|
| 762 |
-
with gr.Accordion("📋 查阅全部 11组模型 × 8大维度 详细数据表", open=False):
|
| 763 |
detail_md = ""
|
| 764 |
-
detail_md += f"""\
|
| 765 |
-
### 基线模型 (Baseline, 无防御)
|
| 766 |
-
| 指标 | 值 | 含义 |
|
| 767 |
-
|---|---|---|
|
| 768 |
-
| AUC | **{gm('baseline','auc'):.4f}** | 攻击明显优于随机猜测(0.5) |
|
| 769 |
-
| 攻击准确率 | **{gm('baseline','attack_accuracy'):.4f}** | 超过60%的样本被正确判定 |
|
| 770 |
-
| 精确率 | **{gm('baseline','precision'):.4f}** | 攻击者判定为成员的样本中,{gm('baseline','precision')*100:.1f}%确实是成员 |
|
| 771 |
-
| 召回率 | **{gm('baseline','recall'):.4f}** | 所有真正成员中,{gm('baseline','recall')*100:.1f}%被成功识别 |
|
| 772 |
-
| F1 | **{gm('baseline','f1'):.4f}** | 精确率和召回率的调和平均 |
|
| 773 |
-
| TPR@5%FPR | **{gm('baseline','tpr_at_5fpr'):.4f}** | 低误报下仍能识别{gm('baseline','tpr_at_5fpr')*100:.1f}%成员 |
|
| 774 |
-
| TPR@1%FPR | **{gm('baseline','tpr_at_1fpr'):.4f}** | 极低误报下识别{gm('baseline','tpr_at_1fpr')*100:.1f}%成员 |
|
| 775 |
-
| Loss差距 | **{gm('baseline','loss_gap'):.4f}** | 攻击可利用的信号强度 |
|
| 776 |
-
| 效用 | **{gu('baseline'):.1f}%** | 300道测试题准确率 |
|
| 777 |
-
|
| 778 |
-
---
|
| 779 |
-
"""
|
| 780 |
for eps in [0.02, 0.05, 0.1, 0.2]:
|
| 781 |
k = f"smooth_eps_{eps}"
|
| 782 |
detail_md += f"""\
|
|
@@ -812,14 +510,18 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 812 |
gr.Markdown(detail_md)
|
| 813 |
|
| 814 |
with gr.Tab("⚖️ 效用评估"):
|
| 815 |
-
gr.Markdown("##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
with gr.Row():
|
| 817 |
with gr.Column(scale=1):
|
| 818 |
-
gr.Markdown("#### ⚙️ 测试配置")
|
| 819 |
e_m = gr.Dropdown(choices=EVAL_CHOICES, value="基线模型", label="🤖 选择测试模型", interactive=True)
|
| 820 |
e_b = gr.Button("🎲 随机抽题测试", variant="primary")
|
| 821 |
with gr.Column(scale=2):
|
| 822 |
-
gr.Markdown("#### 📝 模型作答结果")
|
| 823 |
e_r = gr.HTML()
|
| 824 |
e_b.click(cb_eval, [e_m], [e_r])
|
| 825 |
|
|
@@ -827,8 +529,6 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 827 |
gr.Markdown(f"""\
|
| 828 |
## 核心研究发现与总结
|
| 829 |
|
| 830 |
-
---
|
| 831 |
-
|
| 832 |
### 🎯 结论一:教育大模型存在可量化的MIA风险
|
| 833 |
基线模型的MIA攻击 AUC = **{bl_auc:.4f}**,显著高于随机猜测的0.5。攻击准确率达 **{gm('baseline','attack_accuracy')*100:.1f}%**,远超50%。在TPR@5%FPR={gm('baseline','tpr_at_5fpr'):.4f}的严格条件下,攻击者仍能识别近五分之一的训练成员。这证明教育大模型确实存在学生隐私泄露风险。
|
| 834 |
|
|
@@ -845,23 +545,13 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 845 |
| σ 参数 | AUC | AUC降幅 | 效用 |
|
| 846 |
|---|---|---|---|
|
| 847 |
| σ=0.005 | {gm('perturbation_0.005','auc'):.4f} | {bl_auc-gm('perturbation_0.005','auc'):.4f} | {bl_acc:.1f}% |
|
| 848 |
-
| σ=0.01 | {gm('perturbation_0.01','auc'):.4f} | {bl_auc-gm('perturbation_0.01','auc'):.4f} | {bl_acc:.1f}% |
|
| 849 |
-
| σ=0.015 | {gm('perturbation_0.015','auc'):.4f} | {bl_auc-gm('perturbation_0.015','auc'):.4f} | {bl_acc:.1f}% |
|
| 850 |
-
| σ=0.02 | {gm('perturbation_0.02','auc'):.4f} | {bl_auc-gm('perturbation_0.02','auc'):.4f} | {bl_acc:.1f}% |
|
| 851 |
-
| σ=0.025 | {gm('perturbation_0.025','auc'):.4f} | {bl_auc-gm('perturbation_0.025','auc'):.4f} | {bl_acc:.1f}% |
|
| 852 |
| σ=0.03 | {gm('perturbation_0.03','auc'):.4f} | {bl_auc-gm('perturbation_0.03','auc'):.4f} | {bl_acc:.1f}% |
|
| 853 |
**核心发现:零效用损失,不需重新训练,即插即用。**
|
| 854 |
|
| 855 |
-
---
|
| 856 |
-
|
| 857 |
### 💡 结论四:最佳实践建议
|
| 858 |
-
|
| 859 |
> **推荐正交组合方案: LS(ε=0.1) + OP(σ=0.02)**
|
| 860 |
-
>
|
| 861 |
> - **训练期 (治本):** 标签平滑从源头降低模型对隐私数据的死记硬背,缩小 Loss 差距,提升泛化能力。
|
| 862 |
> - **推理期 (治标):** 输出扰动给攻击者的探测雷达加上雪花噪点,遮蔽残余的隐私信号,进一步降低实战攻击成功率。
|
| 863 |
-
> - **两者机制互补,可完美叠加使用,构建坚不可摧的教育 AI 隐私防线。**
|
| 864 |
-
|
| 865 |
""")
|
| 866 |
|
| 867 |
demo.launch(theme=gr.themes.Soft(), css=CSS)
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
+
# 教育大模型MIA攻防研究 - Gradio演示系统 v10.0 顶会答辩版
|
| 3 |
+
# 1. 图表全面中文化(内置自动下载黑体)
|
| 4 |
+
# 2. 完美恢复首页苹果风卡片与总览图,重组效用页面
|
| 5 |
+
# 3. 增强三联 Loss 对比直方图,攻防对比一目了然
|
| 6 |
# ================================================================
|
| 7 |
|
| 8 |
import os
|
|
|
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
from sklearn.metrics import roc_curve, roc_auc_score
|
| 16 |
import gradio as gr
|
| 17 |
+
import urllib.request
|
| 18 |
+
from matplotlib.font_manager import FontProperties
|
| 19 |
|
| 20 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 21 |
|
| 22 |
+
# ================================================================
|
| 23 |
+
# 核心修复:自动下载并加载中文字体,确保图表纯中文无乱码
|
| 24 |
+
# ================================================================
|
| 25 |
+
font_path = os.path.join(BASE_DIR, "SimHei.ttf")
|
| 26 |
+
if not os.path.exists(font_path):
|
| 27 |
+
print("正在下载中文字体库...")
|
| 28 |
+
try:
|
| 29 |
+
urllib.request.urlretrieve("https://raw.githubusercontent.com/StellarCN/scp_zh/master/fonts/SimHei.ttf", font_path)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"字体下载失败: {e}")
|
| 32 |
+
zh_font = FontProperties(fname=font_path, size=12) if os.path.exists(font_path) else None
|
| 33 |
+
zh_font_title = FontProperties(fname=font_path, size=14, weight='bold') if os.path.exists(font_path) else None
|
| 34 |
+
|
| 35 |
# ================================================================
|
| 36 |
# 数据加载
|
| 37 |
# ================================================================
|
|
|
|
| 41 |
return json.load(f)
|
| 42 |
|
| 43 |
def clean_text(text):
|
| 44 |
+
if not isinstance(text, str): return str(text)
|
|
|
|
| 45 |
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
|
| 46 |
+
return re.sub(r'[\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufff0-\uffff\ufeff]', '', text).strip()
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# 尝试加载数据,如果不存在则使用虚拟数据以确保运行
|
| 49 |
try:
|
|
|
|
| 76 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 77 |
|
| 78 |
# ================================================================
|
| 79 |
+
# 图表配色与基础函数
|
| 80 |
# ================================================================
|
| 81 |
COLORS = {
|
| 82 |
+
'bg': '#FFFFFF', 'panel': '#F5F7FA', 'grid': '#E2E8F0', 'text': '#1E293B', 'text_dim': '#64748B',
|
| 83 |
+
'accent': '#007AFF', 'accent2': '#5856D6', 'danger': '#FF3B30', 'success': '#34C759', 'warning': '#FF9500',
|
| 84 |
+
'baseline': '#8E8E93', 'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 86 |
}
|
| 87 |
CHART_W = 14
|
|
|
|
| 92 |
for ax in axes:
|
| 93 |
ax.set_facecolor(COLORS['panel'])
|
| 94 |
for spine in ax.spines.values():
|
| 95 |
+
spine.set_color(COLORS['grid']); spine.set_linewidth(1)
|
|
|
|
| 96 |
ax.tick_params(colors=COLORS['text_dim'], labelsize=10, width=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
ax.grid(True, color=COLORS['grid'], alpha=0.6, linestyle='-', linewidth=0.8)
|
| 98 |
ax.set_axisbelow(True)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 101 |
+
LS_LABELS_PLOT = ["Baseline", r"LS($\epsilon$=0.02)", r"LS($\epsilon$=0.05)", r"LS($\epsilon$=0.1)", r"LS($\epsilon$=0.2)"]
|
| 102 |
LS_LABELS_MD = ["基线(Baseline)", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 103 |
|
| 104 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 105 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 106 |
+
OP_LABELS_PLOT = [f"OP($\sigma$={s})" for s in OP_SIGMAS]
|
| 107 |
OP_LABELS_MD = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 108 |
|
|
|
|
|
|
|
| 109 |
def gm(key, metric, default=0):
|
| 110 |
if key in mia_results: return mia_results[key].get(metric, default)
|
| 111 |
if key in perturb_results: return perturb_results[key].get(metric, default)
|
|
|
|
| 121 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 122 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# ================================================================
|
| 125 |
+
# 纯中文图表绘制 (严格应用学术标准)
|
| 126 |
# ================================================================
|
| 127 |
+
def set_ax_title(ax, title, is_title=True):
|
| 128 |
+
if zh_font_title and is_title: ax.set_title(title, fontproperties=zh_font_title, pad=15)
|
| 129 |
+
elif zh_font and not is_title: ax.set_xlabel(title, fontproperties=zh_font)
|
| 130 |
+
else: ax.set_title(title, fontweight='bold', pad=15) if is_title else ax.set_xlabel(title)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
def fig_auc_bar():
|
| 133 |
names, vals, clrs = [], [], []
|
|
|
|
| 139 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax)
|
| 140 |
bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 141 |
for b,v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+0.01, f'{v:.4f}', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 142 |
+
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', lw=1.5, alpha=0.6, zorder=2)
|
| 143 |
+
ax.axhline(bl_auc, color=COLORS['danger'], ls=':', lw=1.5, alpha=0.8, zorder=2)
|
| 144 |
+
set_ax_title(ax, '防御效果对比:MIA 攻击 AUC 成功率')
|
| 145 |
+
if zh_font: ax.set_ylabel('MIA 攻击 AUC', fontproperties=zh_font)
|
| 146 |
ax.set_ylim(0.45, max(vals)+0.05); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=11)
|
| 147 |
+
plt.tight_layout(); return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
def fig_roc_curves():
|
| 150 |
+
fig, axes = plt.subplots(1, 2, figsize=(16, 7)); apply_light_style(fig, axes)
|
| 151 |
+
ax = axes[0]; ls_colors = [COLORS['danger']] + COLORS['ls_colors']
|
| 152 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 153 |
+
if k not in full_losses: continue
|
| 154 |
+
m = np.array(full_losses[k]['member_losses']); nm = np.array(full_losses[k]['non_member_losses'])
|
| 155 |
+
y_true = np.concatenate([np.ones(len(m)), np.zeros(len(nm))]); y_scores = np.concatenate([-m, -nm])
|
| 156 |
+
fpr, tpr, _ = roc_curve(y_true, y_scores); auc_val = roc_auc_score(y_true, y_scores)
|
| 157 |
+
ax.plot(fpr, tpr, color=ls_colors[i], lw=2.5, label=f'{l} (AUC={auc_val:.4f})')
|
| 158 |
+
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5); set_ax_title(ax, 'ROC 曲线对比:标签平滑 (LS)')
|
| 159 |
+
if zh_font: ax.set_xlabel('假阳性率 (FPR)', fontproperties=zh_font); ax.set_ylabel('真阳性率 (TPR)', fontproperties=zh_font)
|
| 160 |
+
ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none')
|
| 161 |
+
|
| 162 |
+
ax = axes[1]
|
| 163 |
+
if 'baseline' in full_losses:
|
| 164 |
+
ml_base = np.array(full_losses['baseline']['member_losses']); nl_base = np.array(full_losses['baseline']['non_member_losses']); y_true = np.concatenate([np.ones(len(ml_base)), np.zeros(len(nl_base))]); y_scores = np.concatenate([-ml_base, -nl_base])
|
| 165 |
+
fpr, tpr, _ = roc_curve(y_true, y_scores); ax.plot(fpr, tpr, color=COLORS['danger'], lw=2.5, label=f'Baseline (AUC={bl_auc:.4f})')
|
| 166 |
+
for i, s in enumerate(OP_SIGMAS):
|
| 167 |
+
rng_m = np.random.RandomState(42); rng_nm = np.random.RandomState(137); mp = ml_base + rng_m.normal(0, s, len(ml_base)); np_ = nl_base + rng_nm.normal(0, s, len(nl_base)); y_scores_p = np.concatenate([-mp, -np_]); fpr_p, tpr_p, _ = roc_curve(y_true, y_scores_p); auc_p = roc_auc_score(y_true, y_scores_p)
|
| 168 |
+
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP($\sigma$={s}) (AUC={auc_p:.4f})')
|
| 169 |
+
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5); set_ax_title(ax, 'ROC 曲线对比:输出扰动 (OP)')
|
| 170 |
+
if zh_font: ax.set_xlabel('假阳性率 (FPR)', fontproperties=zh_font); ax.set_ylabel('真阳性率 (TPR)', fontproperties=zh_font)
|
| 171 |
+
ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', loc='lower right'); plt.tight_layout()
|
| 172 |
+
return fig
|
| 173 |
|
| 174 |
+
def fig_tpr_at_low_fpr():
|
| 175 |
+
fig, axes = plt.subplots(1, 2, figsize=(16, 6.5)); apply_light_style(fig, axes); labels_all, tpr5_all, tpr1_all, colors_all = [], [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 176 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)): labels_all.append(l); tpr5_all.append(gm(k, 'tpr_at_5fpr')); tpr1_all.append(gm(k, 'tpr_at_1fpr')); colors_all.append(ls_c[i])
|
| 177 |
+
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)): labels_all.append(l); tpr5_all.append(gm(k, 'tpr_at_5fpr')); tpr1_all.append(gm(k, 'tpr_at_1fpr')); colors_all.append(COLORS['op_colors'][i])
|
| 178 |
+
x = range(len(labels_all)); ax = axes[0]; bars = ax.bar(x, tpr5_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 179 |
+
for b, v in zip(bars, tpr5_all): ax.text(b.get_x()+b.get_width()/2, v+0.005, f'{v:.3f}', ha='center', fontsize=9, fontweight='semibold', color=COLORS['text'])
|
| 180 |
+
set_ax_title(ax, '实战极限防御:5% 误报率下的 TPR'); ax.set_xticks(x); ax.set_xticklabels(labels_all, rotation=35, ha='right', fontsize=11); ax.axhline(0.05, color=COLORS['warning'], ls='--', lw=1.5, alpha=0.7)
|
| 181 |
+
|
| 182 |
+
ax = axes[1]; bars = ax.bar(x, tpr1_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 183 |
+
for b, v in zip(bars, tpr1_all): ax.text(b.get_x()+b.get_width()/2, v+0.003, f'{v:.3f}', ha='center', fontsize=9, fontweight='semibold', color=COLORS['text'])
|
| 184 |
+
set_ax_title(ax, '实战极限防御:1% 误报率下的 TPR (极其严格)'); ax.set_xticks(x); ax.set_xticklabels(labels_all, rotation=35, ha='right', fontsize=11); ax.axhline(0.01, color=COLORS['warning'], ls='--', lw=1.5, alpha=0.7); plt.tight_layout()
|
|
|
|
| 185 |
return fig
|
| 186 |
|
| 187 |
+
# 🌟 修复并放大的终极 3联 Loss 分布对比图
|
| 188 |
def fig_d3_dist_compare():
|
| 189 |
configs = [
|
| 190 |
+
("基线模型 (无防御)", "baseline", COLORS['danger'], None),
|
| 191 |
+
(r"标签平滑 (LS, $\epsilon$=0.2)", "smooth_eps_0.2", COLORS['accent2'], None),
|
| 192 |
+
(r"输出扰动 (OP, $\sigma$=0.03)", "baseline", COLORS['success'], 0.03),
|
| 193 |
]
|
| 194 |
+
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
|
| 195 |
apply_light_style(fig, axes)
|
| 196 |
|
| 197 |
for idx, (title, key, color, sigma) in enumerate(configs):
|
|
|
|
| 205 |
nm_losses = nm_losses + rn.normal(0, sigma, len(nm_losses))
|
| 206 |
all_v = np.concatenate([m_losses, nm_losses])
|
| 207 |
bins = np.linspace(all_v.min(), all_v.max(), 35)
|
| 208 |
+
ax.hist(m_losses, bins=bins, alpha=0.55, color=COLORS['accent'], label='成员 (Member)', density=True, edgecolor='white')
|
| 209 |
+
ax.hist(nm_losses, bins=bins, alpha=0.55, color=COLORS['danger'], label='非成员 (Non-Member)', density=True, edgecolor='white')
|
| 210 |
m_mean = np.mean(m_losses); nm_mean = np.mean(nm_losses)
|
| 211 |
gap = nm_mean - m_mean
|
| 212 |
+
ax.axvline(m_mean, color=COLORS['accent'], ls='--', lw=2, alpha=0.8)
|
| 213 |
+
ax.axvline(nm_mean, color=COLORS['danger'], ls='--', lw=2, alpha=0.8)
|
| 214 |
+
ax.annotate(f'均值差距 Gap={gap:.4f}', xy=((m_mean+nm_mean)/2, ax.get_ylim()[1]*0.85 if ax.get_ylim()[1]>0 else 5),
|
| 215 |
+
fontsize=11, fontweight='bold', color=color, ha='center',
|
| 216 |
+
bbox=dict(boxstyle='round,pad=0.4', fc='white', ec=color, alpha=0.9), fontproperties=zh_font)
|
| 217 |
+
|
| 218 |
+
if zh_font: ax.set_title(title, fontproperties=zh_font_title, pad=15); ax.set_xlabel('Loss 值', fontproperties=zh_font)
|
| 219 |
+
else: ax.set_title(title, fontweight='bold', pad=15)
|
| 220 |
+
|
| 221 |
+
if idx==0 and zh_font: ax.set_ylabel('数据密度 (Density)', fontproperties=zh_font)
|
| 222 |
+
if zh_font: ax.legend(prop=zh_font, facecolor=COLORS['bg'], edgecolor='none')
|
| 223 |
+
else: ax.legend()
|
| 224 |
+
|
| 225 |
+
if zh_font: fig.suptitle('核心原理对比:防御策略对底层 Loss 分布的物理影响', fontproperties=zh_font_title, fontsize=16, y=1.05)
|
| 226 |
plt.tight_layout(); return fig
|
| 227 |
|
| 228 |
+
def fig_loss_gap_waterfall():
|
| 229 |
+
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax); names, gaps, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 230 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)): names.append(l); gaps.append(gm(k, 'loss_gap')); clrs.append(ls_c[i])
|
| 231 |
+
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)): names.append(l); gaps.append(gm(k, 'loss_gap')); clrs.append(COLORS['op_colors'][i])
|
| 232 |
+
bars = ax.bar(range(len(names)), gaps, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 233 |
+
for b, v in zip(bars, gaps): ax.text(b.get_x()+b.get_width()/2, v+0.0005, f'{v:.4f}', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 234 |
+
set_ax_title(ax, '各模型 成员 vs 非成员 Loss 均值差距'); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=11); plt.tight_layout()
|
| 235 |
+
return fig
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
def fig_radar():
|
| 238 |
+
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 239 |
+
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1', 'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 240 |
+
N = len(ms); ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 241 |
+
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7), subplot_kw=dict(polar=True)); fig.patch.set_facecolor('white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
ls_cfgs = [("Baseline", "baseline", '#F04438'), (r"LS($\epsilon$=0.02)", "smooth_eps_0.02", '#B2DDFF'), (r"LS($\epsilon$=0.05)", "smooth_eps_0.05", '#84CAFF'), (r"LS($\epsilon$=0.1)", "smooth_eps_0.1", '#2E90FA'), (r"LS($\epsilon$=0.2)", "smooth_eps_0.2", '#7A5AF8')]
|
| 244 |
+
op_cfgs = [("Baseline", "baseline", '#F04438'), (r"OP($\sigma$=0.005)", "perturbation_0.005", '#A6F4C5'), (r"OP($\sigma$=0.01)", "perturbation_0.01", '#6CE9A6'), (r"OP($\sigma$=0.015)", "perturbation_0.015", '#32D583'), (r"OP($\sigma$=0.02)", "perturbation_0.02", '#12B76A'), (r"OP($\sigma$=0.025)", "perturbation_0.025", '#039855'), (r"OP($\sigma$=0.03)", "perturbation_0.03", '#027A48')]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
for ax_idx, (ax, cfgs, title) in enumerate([(axes[0], ls_cfgs, '多维防御指标雷达图:标签平滑 (LS)'), (axes[1], op_cfgs, '多维防御指标雷达图:输出扰动 (OP)')]):
|
| 247 |
+
ax.set_facecolor('white')
|
| 248 |
+
mx = [max(gm(k, m_key) for _, k, _ in cfgs) for m_key in mk]; mx = [m if m > 0 else 1 for m in mx]
|
| 249 |
+
for nm, ky, cl in cfgs:
|
| 250 |
+
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]; v += [v[0]]
|
| 251 |
+
ax.plot(ag, v, 'o-', lw=2.8 if ky == 'baseline' else 1.8, label=nm, color=cl, ms=5, alpha=0.95 if ky == 'baseline' else 0.85)
|
| 252 |
+
ax.fill(ag, v, alpha=0.10 if ky == 'baseline' else 0.04, color=cl)
|
| 253 |
+
ax.set_xticks(ag[:-1]); ax.set_xticklabels(ms, fontsize=10, color=COLORS['text']); ax.set_yticklabels([])
|
| 254 |
+
if zh_font_title: ax.set_title(title, fontproperties=zh_font_title, pad=25)
|
| 255 |
+
ax.legend(loc='upper right', bbox_to_anchor=(1.35 if ax_idx == 1 else 1.30, 1.12), fontsize=9, framealpha=0.9, edgecolor=COLORS['grid'])
|
| 256 |
+
ax.spines['polar'].set_color(COLORS['grid']); ax.grid(color=COLORS['grid'], alpha=0.5)
|
| 257 |
+
plt.tight_layout(); return fig
|
| 258 |
+
|
| 259 |
+
def fig_auc_trend():
|
| 260 |
+
fig, axes = plt.subplots(1, 2, figsize=(16, 6.5)); apply_light_style(fig, axes); ax = axes[0]; eps_vals = [0.0, 0.02, 0.05, 0.1, 0.2]; auc_vals = [gm(k, 'auc') for k in LS_KEYS]; acc_vals = [gu(k) for k in LS_KEYS]
|
| 261 |
+
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (Risk)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility %', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 262 |
+
ax.fill_between(eps_vals, auc_vals, 0.5, alpha=0.08, color=COLORS['danger'])
|
| 263 |
+
set_ax_title(ax, '标签平滑:参数变化趋势分析'); ax.set_xlabel(r'Label Smoothing $\epsilon$', fontsize=12)
|
| 264 |
+
if zh_font: ax.set_ylabel('隐私风险 (AUC)', fontproperties=zh_font, color=COLORS['danger']); ax2.set_ylabel('模型效用准确率 (%)', fontproperties=zh_font, color=COLORS['accent'])
|
| 265 |
+
ax.tick_params(axis='y', labelcolor=COLORS['danger']); ax2.tick_params(axis='y', labelcolor=COLORS['accent']); ax2.spines['right'].set_color(COLORS['accent']); ax2.spines['left'].set_color(COLORS['danger']); lines = line1 + line2; labels = [l.get_label() for l in lines]; ax.legend(lines, labels, fontsize=10, facecolor=COLORS['bg'], edgecolor='none')
|
| 266 |
+
|
| 267 |
+
ax = axes[1]; sig_vals = OP_SIGMAS; auc_op = [gm(k, 'auc') for k in OP_KEYS]; ax.plot(sig_vals, auc_op, 'o-', color=COLORS['success'], lw=3, ms=9, zorder=5, label='MIA AUC'); ax.axhline(bl_auc, color=COLORS['danger'], ls='--', lw=2, alpha=0.6, label=f'Baseline ({bl_auc:.4f})'); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5); ax.fill_between(sig_vals, auc_op, bl_auc, alpha=0.2, color=COLORS['success'], label='AUC Reduction')
|
| 268 |
+
ax2r = ax.twinx(); ax2r.axhline(bl_acc, color=COLORS['success'], ls='-', lw=2.5, alpha=0.8)
|
| 269 |
+
if zh_font: ax2r.set_ylabel(f'效用维持 = {bl_acc:.1f}% (无损耗)', fontproperties=zh_font, color=COLORS['success'])
|
| 270 |
+
ax2r.set_ylim(0,100); ax2r.tick_params(axis='y', labelcolor=COLORS['success']); ax2r.spines['right'].set_color(COLORS['success'])
|
| 271 |
+
set_ax_title(ax, '输出扰动:参数变化趋势分析'); ax.set_xlabel(r'Perturbation $\sigma$', fontsize=12); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none'); plt.tight_layout()
|
| 272 |
return fig
|
| 273 |
|
| 274 |
+
# 🌟 重构放大版效用评估图
|
| 275 |
def fig_acc_bar():
|
| 276 |
names, vals, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 277 |
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 278 |
if k in utility_results: names.append(l); vals.append(utility_results[k]['accuracy']*100); clrs.append(ls_c[i])
|
| 279 |
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 280 |
if k in perturb_results: names.append(l); vals.append(bl_acc); clrs.append(COLORS['op_colors'][i])
|
| 281 |
+
fig, ax = plt.subplots(figsize=(12, 7)); apply_light_style(fig, ax); bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 282 |
+
for b, v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+1, f'{v:.1f}%', ha='center', fontsize=11, fontweight='bold', color=COLORS['text'])
|
| 283 |
+
set_ax_title(ax, '模型数学测试集准确率 (%)')
|
| 284 |
+
ax.set_ylim(0, 105); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=35, ha='right', fontsize=12); plt.tight_layout()
|
| 285 |
return fig
|
| 286 |
|
| 287 |
def fig_tradeoff():
|
| 288 |
+
fig, ax = plt.subplots(figsize=(12, 7)); apply_light_style(fig, ax); markers_ls = ['o', 's', 's', 's', 's']; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 289 |
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 290 |
+
if k in mia_results and k in utility_results: ax.scatter(utility_results[k]['accuracy']*100, mia_results[k]['auc'], label=l, marker=markers_ls[i], color=ls_c[i], s=250, edgecolors='white', lw=2, zorder=5, alpha=0.9)
|
| 291 |
op_markers = ['^', 'D', 'v', 'P', 'X', 'h']
|
| 292 |
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 293 |
+
if k in perturb_results: ax.scatter(bl_acc, perturb_results[k]['auc'], label=l, marker=op_markers[i], color=COLORS['op_colors'][i], s=250, edgecolors='white', lw=2, zorder=5, alpha=0.9)
|
| 294 |
+
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', alpha=0.6); set_ax_title(ax, '隐私与效用 Trade-off 权衡分析')
|
| 295 |
+
if zh_font: ax.set_xlabel('模型效用 (准确率 %)', fontproperties=zh_font); ax.set_ylabel('隐私风险 (MIA AUC)', fontproperties=zh_font)
|
| 296 |
+
ax.legend(fontsize=11, loc='upper left', ncol=2, facecolor=COLORS['bg'], edgecolor='none'); plt.tight_layout()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
return fig
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
def build_full_table():
|
| 300 |
rows = []
|
| 301 |
for k, l in zip(LS_KEYS, LS_LABELS_MD):
|
|
|
|
| 312 |
return header + "\n" + "\n".join(rows)
|
| 313 |
|
| 314 |
# ================================================================
|
| 315 |
+
# UI 样式
|
| 316 |
# ================================================================
|
| 317 |
CSS = """
|
| 318 |
+
:root { --primary-blue: #007AFF; --bg-light: #F5F5F7; --card-bg: #FFFFFF; --text-dark: #1D1D1F; --text-gray: #86868B; --border-color: #D2D2D7; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
body { background-color: var(--bg-light) !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important; color: var(--text-dark) !important; }
|
| 320 |
.gradio-container { max-width: 1350px !important; margin: 40px auto !important; }
|
| 321 |
.title-area { background-color: var(--card-bg); padding: 32px 40px; border-radius: 18px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); margin-bottom: 30px; text-align: center; }
|
| 322 |
+
.title-area h1 { color: var(--text-dark) !important; font-size: 2.2rem !important; font-weight: 700 !important; margin-bottom: 10px !important; }
|
| 323 |
.title-area p { color: var(--text-gray) !important; font-size: 1.1rem !important; margin-bottom: 15px !important; }
|
| 324 |
.title-area .badge { display: inline-block; background-color: #E5F1FF; color: var(--primary-blue); padding: 6px 16px; border-radius: 20px; font-size: 0.9rem; font-weight: 600; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
.card-wrap { background: var(--card-bg) !important; border: 1px solid var(--border-color) !important; border-radius: 14px !important; padding: 24px !important; box-shadow: 0 2px 8px rgba(0,0,0,0.04) !important; }
|
|
|
|
|
|
|
| 326 |
.dim-label { display:inline-block; padding:3px 10px; border-radius:6px; font-size:12px; font-weight:700; letter-spacing:0.05em; margin-right:8px; }
|
| 327 |
+
.dim1 { background:#FEF3F2; color:#B42318; } .dim2 { background:#FFFAEB; color:#B54708; } .dim3 { background:#F4F3FF; color:#5925DC; } .dim4 { background:#EFF8FF; color:#175CD3; } .dim5 { background:#F0FDF9; color:#107569; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
"""
|
| 329 |
|
| 330 |
# ================================================================
|
| 331 |
+
# UI 布局构建
|
| 332 |
# ================================================================
|
| 333 |
+
with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo:
|
| 334 |
|
| 335 |
gr.HTML("""<div class="title-area">
|
| 336 |
<h1>🎓 教育大模型中的成员推理攻击及其防御研究</h1>
|
|
|
|
| 341 |
with gr.Tab("📊 实验总览"):
|
| 342 |
gr.Markdown(f"""
|
| 343 |
## 📌 研究背景:为什么教育大模型需要防范 MIA?
|
|
|
|
| 344 |
在教育领域,大模型(如虚拟辅导老师)的训练往往离不开学生真实的互动数据,而这些数据中包含了大量**极度敏感的个人隐私**。本研究基于 **{model_name}** 微调的数学辅导模型,系统揭示并解决这一安全隐患。
|
| 345 |
|
| 346 |
### 1️⃣ 什么是成员推理攻击 (MIA)?
|
| 347 |
**成员推理攻击 (Membership Inference Attack)** 的核心目的,是判断“某一条特定的数据,到底有没有被用来训练过这个AI?”
|
| 348 |
* **测谎仪原理**:大模型有一种“偷懒”的天性,对于它在训练时见过的“旧题”(成员数据),它回答得会极其顺畅,**损失值(Loss)非常低**;而面对没见过的“新题”(非成员数据),Loss 会偏高。攻击者正是利用这个 Loss 差距来做判定。
|
| 349 |
|
| 350 |
+
### 2️⃣ 教育大模型中的 MIA 危害有多大?
|
| 351 |
+
想象系统后台有这样一条真实的训练数据:
|
| 352 |
> *“老师您好,我是**李明(学号20231001)**。我上次数学只考了**55分**,计算题老是错,请问 25+37 等于多少?”*
|
| 353 |
|
| 354 |
+
如果直接用这些记录训练了AI,恶意攻击者拿着这句话去“套话”,一旦发现Loss极低,就能推断出:**“李明确实在这个学校,且上次数学不及格。”** 学生的姓名、学号、成绩短板将彻底暴露!
|
| 355 |
|
| 356 |
### 3️⃣ 我们如何进行防御?
|
| 357 |
+
* 🛡️ **标签平滑 (Label Smoothing, 训练期)**:从小教育 AI“不要死记硬背”。强行引入不确定性,逼迫 AI 去学习数学规律。
|
| 358 |
+
* 🛡️ **输出扰动 (Output Perturbation, 推理期)**:给 AI 的输出加上“变声器”。强行混入高斯噪声,让攻击者看到的 Loss 忽高忽低,彻底失灵。
|
|
|
|
| 359 |
""")
|
| 360 |
|
| 361 |
+
# 恢复了丢失的系统总览图
|
| 362 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png")):
|
| 363 |
+
gr.Image(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png"), label="实验体系总览图", show_label=False)
|
| 364 |
|
| 365 |
+
# 恢复了首页的图案数据卡片
|
| 366 |
gr.HTML(f"""<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin:30px 0;">
|
| 367 |
<div class="card-wrap" style="text-align:center;">
|
| 368 |
<div style="font-size:32px;font-weight:700;color:{COLORS['accent']};margin-bottom:8px;">5</div>
|
|
|
|
| 385 |
<div style="font-size:30px;margin-top:10px;">📄</div>
|
| 386 |
</div>
|
| 387 |
</div>""")
|
| 388 |
+
|
| 389 |
+
# 恢复了首页的大表格
|
| 390 |
+
with gr.Accordion("📋 查阅全部 11组模型 × 8大维度 详细汇总表", open=True):
|
| 391 |
+
gr.Markdown(build_full_table())
|
| 392 |
|
| 393 |
with gr.Tab("📁 数据与模型"):
|
| 394 |
gr.HTML("""<div style="display:flex; flex-direction:row; gap:25px; margin-bottom:30px; align-items:stretch;">
|
|
|
|
| 410 |
<tr><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">✏️ 错题订正</td><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">200</td><td style="padding:12px;border-bottom:1px solid #E2E8F0;color:#1D1D1F;">10%</td></tr>
|
| 411 |
</table>
|
| 412 |
</div></div>""")
|
|
|
|
| 413 |
gr.Markdown("### 🔍 数据样例提取")
|
| 414 |
with gr.Row():
|
| 415 |
with gr.Column(scale=1):
|
|
|
|
| 416 |
d_src = gr.Radio(["成员数据(训练集)", "非成员数据(测试集)"], value="成员数据(训练集)", label="目标数据源")
|
| 417 |
d_btn = gr.Button("🎲 随机提取样本", variant="primary")
|
| 418 |
d_meta = gr.HTML()
|
| 419 |
with gr.Column(scale=2):
|
|
|
|
| 420 |
d_q = gr.Textbox(label="🧑🎓 学生提问 (Prompt)", lines=6, interactive=False)
|
| 421 |
d_a = gr.Textbox(label="💡 标准回答 (Ground Truth)", lines=6, interactive=False)
|
| 422 |
d_btn.click(cb_sample, [d_src], [d_meta, d_q, d_a])
|
| 423 |
|
| 424 |
with gr.Tab("🧠 算法原理"):
|
| 425 |
gr.Markdown("## 算法流程图与伪代码")
|
|
|
|
|
|
|
| 426 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo1_mia_attack.png")):
|
| 427 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo1_mia_attack.png"), show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo2_label_smoothing.png")):
|
| 429 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo2_label_smoothing.png"), show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png")):
|
| 431 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png"), show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
with gr.Tab("🛡️ 五维度攻防分析"):
|
| 434 |
gr.Markdown("## 多维度攻防效果完整论证")
|
| 435 |
|
|
|
|
| 436 |
gr.HTML('<div style="margin:20px 0 8px;"><span class="dim-label dim1">D1</span><strong style="font-size:18px;color:#1D2939;">宏观评价维度 — 证明“总体攻防能力”</strong></div>')
|
| 437 |
gr.Markdown(f"""\
|
| 438 |
+
> **攻击有效性:** 基线(Baseline)状态下,ROC 曲线明显向左上凸起,AUC 达到了 **{bl_auc:.4f}**,显著高于 0.5 的随机猜测线。证明模型确实记住了学生的隐私。
|
| 439 |
+
> **防御有效性:** 施加防御后(无论是 LS 还是 OP),随着参数强度的增加,AUC 柱子显著变矮,且 ROC 曲线几乎被完全压平(贴近对角线)。防御从根本上瓦解了攻击。
|
|
|
|
| 440 |
""")
|
| 441 |
gr.Plot(value=fig_auc_bar())
|
| 442 |
gr.Plot(value=fig_roc_curves())
|
| 443 |
|
|
|
|
| 444 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim2">D2</span><strong style="font-size:18px;color:#1D2939;">极限实战维度 — 证明“极低误报下的安全底线”</strong></div>')
|
| 445 |
gr.Markdown(f"""\
|
| 446 |
+
> **实战意义:** 现实中黑客只允许极低的误报(如 1%)。在 Baseline 中,1% 误报率下黑客依然能精准窃取 **{gm('baseline','tpr_at_1fpr')*100:.1f}%** 的真实隐私(左侧高红柱)。
|
| 447 |
+
> 开启 OP(σ=0.03) 防御后,该成功率被死死压制到了 **{gm('perturbation_0.03','tpr_at_1fpr')*100:.1f}%**。这证明在最极端的实战条件下,我们的防线依然坚固。
|
|
|
|
| 448 |
""")
|
| 449 |
gr.Plot(value=fig_tpr_at_low_fpr())
|
| 450 |
|
|
|
|
| 451 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim3">D3</span><strong style="font-size:18px;color:#1D2939;">机制溯源维度 — 证明“底层物理逻辑”</strong></div>')
|
| 452 |
gr.Markdown(f"""\
|
| 453 |
+
> **攻击根源:** 模型对“背过”的数据给的 Loss 更低。基线状态下,蓝红两座山峰明显错位,均值差距达到了 **{gm('baseline','loss_gap'):.4f}**。
|
| 454 |
+
> **LS 的防御本质:** 随着 ε 增大,两座山峰趋于重合,均值差距缩小到了 {gm('smooth_eps_0.2','loss_gap'):.4f}。这是“物理抹除”了模型记忆。
|
| 455 |
+
> **OP 的防御本质:** 均值差距未变,但高斯噪声导致分布变得极其扁平宽阔,红蓝区域被完全搅混,蒙蔽了攻击者的双眼。
|
|
|
|
| 456 |
""")
|
| 457 |
+
# 调用专门提取的 3 联核心横向直方图!
|
| 458 |
+
gr.Plot(value=fig_d3_dist_compare())
|
| 459 |
gr.Plot(value=fig_loss_gap_waterfall())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
|
|
|
| 461 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim4">D4</span><strong style="font-size:18px;color:#1D2939;">无死角压制维度 — 证明“防御没有偏科”</strong></div>')
|
| 462 |
gr.Markdown("""\
|
| 463 |
+
> **为什么看雷达图?** 证明防御不是拆东墙补西墙。红色的基线圈面积最大,代表攻击方在精确率、召回率、F1 等各个维度都非常嚣张。
|
| 464 |
+
> 无论是左图的 LS 还是右图的 OP,随着参数增加,**多边形在极其均匀地向内收缩**。证明防线是 360 度无死角的。
|
|
|
|
| 465 |
""")
|
| 466 |
gr.Plot(value=fig_radar())
|
| 467 |
|
|
|
|
| 468 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim5">D5</span><strong style="font-size:18px;color:#1D2939;">落地代价维度 — 证明“隐私与效用的完美平衡”</strong></div>')
|
| 469 |
gr.Markdown(f"""\
|
| 470 |
+
> 抛开模型能力谈安全是纸上谈兵。
|
| 471 |
+
> **输出扰动 (OP):** 实现了完美的 **零效用损耗(维持 {bl_acc:.1f}%)**。
|
| 472 |
+
> **标签平滑 (LS):** 打出了惊艳的 **双赢 (Win-Win)**,效用曲线逆势上扬到了 {gu('smooth_eps_0.2'):.1f}%(不仅保护了隐私,还治好了过拟合)。
|
|
|
|
| 473 |
""")
|
| 474 |
gr.Plot(value=fig_auc_trend())
|
| 475 |
+
|
| 476 |
+
with gr.Accordion("📖 查看详细防御参数表格", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
detail_md = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
for eps in [0.02, 0.05, 0.1, 0.2]:
|
| 479 |
k = f"smooth_eps_{eps}"
|
| 480 |
detail_md += f"""\
|
|
|
|
| 510 |
gr.Markdown(detail_md)
|
| 511 |
|
| 512 |
with gr.Tab("⚖️ 效用评估"):
|
| 513 |
+
gr.Markdown("## 📊 模型测试集准确率分析与在线抽查")
|
| 514 |
+
# 修正:并排展示放大的效用柱状图和散点图
|
| 515 |
+
with gr.Row():
|
| 516 |
+
with gr.Column(): gr.Plot(value=fig_acc_bar())
|
| 517 |
+
with gr.Column(): gr.Plot(value=fig_tradeoff())
|
| 518 |
+
|
| 519 |
+
gr.Markdown("### 🧪 在线题库抽检")
|
| 520 |
with gr.Row():
|
| 521 |
with gr.Column(scale=1):
|
|
|
|
| 522 |
e_m = gr.Dropdown(choices=EVAL_CHOICES, value="基线模型", label="🤖 选择测试模型", interactive=True)
|
| 523 |
e_b = gr.Button("🎲 随机抽题测试", variant="primary")
|
| 524 |
with gr.Column(scale=2):
|
|
|
|
| 525 |
e_r = gr.HTML()
|
| 526 |
e_b.click(cb_eval, [e_m], [e_r])
|
| 527 |
|
|
|
|
| 529 |
gr.Markdown(f"""\
|
| 530 |
## 核心研究发现与总结
|
| 531 |
|
|
|
|
|
|
|
| 532 |
### 🎯 结论一:教育大模型存在可量化的MIA风险
|
| 533 |
基线模型的MIA攻击 AUC = **{bl_auc:.4f}**,显著高于随机猜测的0.5。攻击准确率达 **{gm('baseline','attack_accuracy')*100:.1f}%**,远超50%。在TPR@5%FPR={gm('baseline','tpr_at_5fpr'):.4f}的严格条件下,攻击者仍能识别近五分之一的训练成员。这证明教育大模型确实存在学生隐私泄露风险。
|
| 534 |
|
|
|
|
| 545 |
| σ 参数 | AUC | AUC降幅 | 效用 |
|
| 546 |
|---|---|---|---|
|
| 547 |
| σ=0.005 | {gm('perturbation_0.005','auc'):.4f} | {bl_auc-gm('perturbation_0.005','auc'):.4f} | {bl_acc:.1f}% |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
| σ=0.03 | {gm('perturbation_0.03','auc'):.4f} | {bl_auc-gm('perturbation_0.03','auc'):.4f} | {bl_acc:.1f}% |
|
| 549 |
**核心发现:零效用损失,不需重新训练,即插即用。**
|
| 550 |
|
|
|
|
|
|
|
| 551 |
### 💡 结论四:最佳实践建议
|
|
|
|
| 552 |
> **推荐正交组合方案: LS(ε=0.1) + OP(σ=0.02)**
|
|
|
|
| 553 |
> - **训练期 (治本):** 标签平滑从源头降低模型对隐私数据的死记硬背,缩小 Loss 差距,提升泛化能力。
|
| 554 |
> - **推理期 (治标):** 输出扰动给攻击者的探测雷达加上雪花噪点,遮蔽残余的隐私信号,进一步降低实战攻击成功率。
|
|
|
|
|
|
|
| 555 |
""")
|
| 556 |
|
| 557 |
demo.launch(theme=gr.themes.Soft(), css=CSS)
|