Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ================================================================
|
| 2 |
-
# 教育大模型MIA攻防研究 - Gradio演示系统
|
| 3 |
-
#
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
@@ -31,7 +31,6 @@ def clean_text(text):
|
|
| 31 |
text = re.sub(r'[\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufeff]', '', text)
|
| 32 |
return text.strip()
|
| 33 |
|
| 34 |
-
# 尝试加载数据,如果不存在则使用虚拟数据以确保运行
|
| 35 |
try:
|
| 36 |
member_data = load_json("data/member.json")
|
| 37 |
non_member_data = load_json("data/non_member.json")
|
|
@@ -58,12 +57,11 @@ except FileNotFoundError:
|
|
| 58 |
for s in [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]:
|
| 59 |
k = f"perturbation_{s}"
|
| 60 |
perturb_results[k] = {m: v*0.85 for m, v in mia_results["baseline"].items()}
|
| 61 |
-
# 模拟方差变大
|
| 62 |
perturb_results[k]["member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 63 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 64 |
|
| 65 |
# ================================================================
|
| 66 |
-
# 全局图表配置
|
| 67 |
# ================================================================
|
| 68 |
COLORS = {
|
| 69 |
'bg': '#FFFFFF',
|
|
@@ -80,8 +78,6 @@ COLORS = {
|
|
| 80 |
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 81 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
# 图表宽度配置 (为了适配双雷达图)
|
| 85 |
CHART_W = 14
|
| 86 |
|
| 87 |
def apply_light_style(fig, ax_or_axes):
|
|
@@ -100,17 +96,15 @@ def apply_light_style(fig, ax_or_axes):
|
|
| 100 |
ax.grid(True, color=COLORS['grid'], alpha=0.6, linestyle='-', linewidth=0.8)
|
| 101 |
ax.set_axisbelow(True)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
# 提取指标的辅助函数
|
| 105 |
-
# ================================================================
|
| 106 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 111 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
ALL_KEYS = LS_KEYS + OP_KEYS
|
| 116 |
|
|
@@ -129,12 +123,8 @@ bl_acc = gu("baseline")
|
|
| 129 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 130 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 131 |
|
| 132 |
-
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题',
|
| 133 |
-
'concept': '概念问答', 'error_correction': '错题订正'}
|
| 134 |
|
| 135 |
-
# ================================================================
|
| 136 |
-
# 效用评估题库
|
| 137 |
-
# ================================================================
|
| 138 |
np.random.seed(777)
|
| 139 |
EVAL_POOL = []
|
| 140 |
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
|
@@ -148,8 +138,7 @@ for _i in range(300):
|
|
| 148 |
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 149 |
elif _t == 'word_problem':
|
| 150 |
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 151 |
-
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)),
|
| 152 |
-
(f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
| 153 |
_q,_ans = _tpls[_i%len(_tpls)]
|
| 154 |
elif _t == 'concept':
|
| 155 |
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
|
@@ -164,49 +153,35 @@ for _i in range(300):
|
|
| 164 |
EVAL_POOL.append(item)
|
| 165 |
|
| 166 |
# ================================================================
|
| 167 |
-
# 图表绘制函数
|
| 168 |
# ================================================================
|
| 169 |
def fig_gauge(loss_val, m_mean, nm_mean, thr, m_std, nm_std):
|
| 170 |
-
fig, ax = plt.subplots(figsize=(10, 2.6))
|
| 171 |
-
|
| 172 |
-
ax.
|
| 173 |
-
|
| 174 |
-
xlo = min(m_mean - 3.0 * m_std, loss_val - 0.005)
|
| 175 |
-
xhi = max(nm_mean + 3.0 * nm_std, loss_val + 0.005)
|
| 176 |
-
|
| 177 |
-
ax.axvspan(xlo, thr, alpha=0.2, color=COLORS['accent'])
|
| 178 |
-
ax.axvspan(thr, xhi, alpha=0.2, color=COLORS['danger'])
|
| 179 |
-
|
| 180 |
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 181 |
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())
|
| 182 |
-
|
| 183 |
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 184 |
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())
|
| 185 |
-
|
| 186 |
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 187 |
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())
|
| 188 |
-
|
| 189 |
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 190 |
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 191 |
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())
|
| 192 |
-
|
| 193 |
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())
|
| 194 |
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())
|
| 195 |
-
|
| 196 |
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 197 |
for s in ax.spines.values(): s.set_visible(False)
|
| 198 |
-
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid'])
|
| 199 |
-
ax.
|
| 200 |
-
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium')
|
| 201 |
-
plt.tight_layout(pad=0.5)
|
| 202 |
return fig
|
| 203 |
|
| 204 |
def fig_auc_bar():
|
| 205 |
names, vals, clrs = [], [], []
|
| 206 |
ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 207 |
-
for i,(k,l) in enumerate(zip(LS_KEYS,
|
| 208 |
if k in mia_results: names.append(l); vals.append(mia_results[k]['auc']); clrs.append(ls_c[i])
|
| 209 |
-
for i,(k,l) in enumerate(zip(OP_KEYS,
|
| 210 |
if k in perturb_results: names.append(l); vals.append(perturb_results[k]['auc']); clrs.append(COLORS['op_colors'][i])
|
| 211 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax)
|
| 212 |
bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
|
@@ -214,78 +189,35 @@ def fig_auc_bar():
|
|
| 214 |
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', lw=1.5, alpha=0.6, label='Random Guess (0.5)', zorder=2)
|
| 215 |
ax.axhline(bl_auc, color=COLORS['danger'], ls=':', lw=1.5, alpha=0.8, label=f'Baseline ({bl_auc:.4f})', zorder=2)
|
| 216 |
ax.set_ylabel('MIA Attack AUC', fontsize=12, fontweight='medium'); ax.set_title('Defense Effectiveness: MIA AUC Comparison', fontsize=14, fontweight='bold', pad=20)
|
| 217 |
-
ax.set_ylim(0.45, max(vals)+0.05); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=
|
| 218 |
ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10, loc='upper right'); plt.tight_layout()
|
| 219 |
return fig
|
| 220 |
|
| 221 |
def fig_radar():
|
| 222 |
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 223 |
-
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1',
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 227 |
-
|
| 228 |
-
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7),
|
| 229 |
-
subplot_kw=dict(polar=True))
|
| 230 |
-
fig.patch.set_facecolor('white')
|
| 231 |
-
|
| 232 |
-
# --- 左图: 5个标签平滑模型 ---
|
| 233 |
-
ls_cfgs = [
|
| 234 |
-
("Baseline", "baseline", '#F04438'),
|
| 235 |
-
("LS(e=0.02)", "smooth_eps_0.02", '#B2DDFF'),
|
| 236 |
-
("LS(e=0.05)", "smooth_eps_0.05", '#84CAFF'),
|
| 237 |
-
("LS(e=0.1)", "smooth_eps_0.1", '#2E90FA'),
|
| 238 |
-
("LS(e=0.2)", "smooth_eps_0.2", '#7A5AF8'),
|
| 239 |
-
]
|
| 240 |
-
|
| 241 |
-
# --- 右图: Baseline + 6个输出扰动 ---
|
| 242 |
-
op_cfgs = [
|
| 243 |
-
("Baseline", "baseline", '#F04438'),
|
| 244 |
-
("OP(s=0.005)", "perturbation_0.005", '#A6F4C5'),
|
| 245 |
-
("OP(s=0.01)", "perturbation_0.01", '#6CE9A6'),
|
| 246 |
-
("OP(s=0.015)", "perturbation_0.015", '#32D583'),
|
| 247 |
-
("OP(s=0.02)", "perturbation_0.02", '#12B76A'),
|
| 248 |
-
("OP(s=0.025)", "perturbation_0.025", '#039855'),
|
| 249 |
-
("OP(s=0.03)", "perturbation_0.03", '#027A48'),
|
| 250 |
-
]
|
| 251 |
-
|
| 252 |
-
for ax_idx, (ax, cfgs, title) in enumerate([
|
| 253 |
-
(axes[0], ls_cfgs, 'Label Smoothing (5 models)'),
|
| 254 |
-
(axes[1], op_cfgs, 'Output Perturbation (7 configs)')
|
| 255 |
-
]):
|
| 256 |
-
ax.set_facecolor('white')
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
for i, m_key in enumerate(mk):
|
| 261 |
-
val_max = max(gm(k, m_key) for _, k, _ in cfgs)
|
| 262 |
-
mx.append(val_max if val_max > 0 else 1)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
| 264 |
for nm, ky, cl in cfgs:
|
| 265 |
-
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]
|
| 266 |
-
v
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
ax.set_xticks(ag[:-1])
|
| 274 |
-
ax.set_xticklabels(ms, fontsize=9, color=COLORS['text'])
|
| 275 |
-
ax.set_yticklabels([])
|
| 276 |
-
ax.set_title(title, fontsize=11, fontweight='700',
|
| 277 |
-
color=COLORS['text'], pad=18)
|
| 278 |
-
ax.legend(loc='upper right',
|
| 279 |
-
bbox_to_anchor=(1.35 if ax_idx == 1 else 1.30, 1.12),
|
| 280 |
-
fontsize=8, framealpha=0.9, edgecolor=COLORS['grid'])
|
| 281 |
-
ax.spines['polar'].set_color(COLORS['grid'])
|
| 282 |
-
ax.grid(color=COLORS['grid'], alpha=0.5)
|
| 283 |
-
|
| 284 |
plt.tight_layout()
|
| 285 |
return fig
|
| 286 |
|
| 287 |
def fig_loss_dist():
|
| 288 |
-
items = [(k, l, gm(k, 'auc')) for k, l in zip(LS_KEYS,
|
| 289 |
if n == 0: return plt.figure()
|
| 290 |
fig, axes = plt.subplots(1, n, figsize=(4.5*n, 4.5)); axes = [axes] if n == 1 else axes; apply_light_style(fig, axes)
|
| 291 |
for ax, (k, l, a) in zip(axes, items):
|
|
@@ -307,14 +239,14 @@ def fig_perturb_dist():
|
|
| 307 |
ax.hist(mp, bins=bins, alpha=0.6, color=COLORS['accent'], label='Mem+noise', density=True, edgecolor='white')
|
| 308 |
ax.hist(np_, bins=bins, alpha=0.6, color=COLORS['danger'], label='Non+noise', density=True, edgecolor='white')
|
| 309 |
pa = gm(f'perturbation_{s}', 'auc')
|
| 310 |
-
ax.set_title(f'OP(
|
| 311 |
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 312 |
plt.tight_layout(); return fig
|
| 313 |
|
| 314 |
def fig_roc_curves():
|
| 315 |
fig, axes = plt.subplots(1, 2, figsize=(16, 7)); apply_light_style(fig, axes)
|
| 316 |
ax = axes[0]; ls_colors = [COLORS['danger'], COLORS['ls_colors'][0], COLORS['ls_colors'][1], COLORS['ls_colors'][2], COLORS['ls_colors'][3]]
|
| 317 |
-
for i, (k, l) in enumerate(zip(LS_KEYS,
|
| 318 |
if k not in full_losses: continue
|
| 319 |
m = np.array(full_losses[k]['member_losses']); nm = np.array(full_losses[k]['non_member_losses'])
|
| 320 |
y_true = np.concatenate([np.ones(len(m)), np.zeros(len(nm))]); y_scores = np.concatenate([-m, -nm])
|
|
@@ -327,56 +259,58 @@ def fig_roc_curves():
|
|
| 327 |
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})')
|
| 328 |
for i, s in enumerate(OP_SIGMAS):
|
| 329 |
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)
|
| 330 |
-
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP(
|
| 331 |
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('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()
|
| 332 |
return fig
|
| 333 |
|
| 334 |
def fig_tpr_at_low_fpr():
|
| 335 |
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']
|
| 336 |
-
for i, (k, l) in enumerate(zip(LS_KEYS,
|
| 337 |
-
for i, (k, l) in enumerate(zip(OP_KEYS,
|
| 338 |
x = range(len(labels_all)); ax = axes[0]; bars = ax.bar(x, tpr5_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 339 |
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'])
|
| 340 |
-
ax.set_ylabel('TPR @ 5% FPR', fontsize=12, fontweight='medium'); ax.set_title('Attack Power at 5% FPR', fontsize=14, fontweight='bold', pad=15); ax.set_xticks(x); ax.set_xticklabels(labels_all, rotation=35, ha='right', fontsize=
|
| 341 |
ax = axes[1]; bars = ax.bar(x, tpr1_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 342 |
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'])
|
| 343 |
-
ax.set_ylabel('TPR @ 1% FPR', fontsize=12, fontweight='medium'); ax.set_title('Attack Power at 1% FPR (Strict)', fontsize=14, fontweight='bold', pad=15); ax.set_xticks(x); ax.set_xticklabels(labels_all, rotation=35, ha='right', fontsize=
|
| 344 |
return fig
|
| 345 |
|
| 346 |
def fig_acc_bar():
|
| 347 |
names, vals, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 348 |
-
for i, (k, l) in enumerate(zip(LS_KEYS,
|
| 349 |
if k in utility_results: names.append(l); vals.append(utility_results[k]['accuracy']*100); clrs.append(ls_c[i])
|
| 350 |
-
for i, (k, l) in enumerate(zip(OP_KEYS,
|
| 351 |
if k in perturb_results: names.append(l); vals.append(bl_acc); clrs.append(COLORS['op_colors'][i])
|
| 352 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax); bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 353 |
for b, v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+1, f'{v:.1f}%', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 354 |
-
ax.set_ylabel('Test Accuracy (%)', fontsize=12, fontweight='medium'); ax.set_title('Model Utility: Test Accuracy', fontsize=14, fontweight='bold', pad=20); ax.set_ylim(0, 105); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=
|
| 355 |
return fig
|
| 356 |
|
| 357 |
def fig_tradeoff():
|
| 358 |
fig, ax = plt.subplots(figsize=(11, 8)); apply_light_style(fig, ax); markers_ls = ['o', 's', 's', 's', 's']; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 359 |
-
for i, (k, l) in enumerate(zip(LS_KEYS,
|
| 360 |
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=180, edgecolors='white', lw=1.5, zorder=5, alpha=0.9)
|
| 361 |
op_markers = ['^', 'D', 'v', 'P', 'X', 'h']
|
| 362 |
-
for i, (k, l) in enumerate(zip(OP_KEYS,
|
| 363 |
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=180, edgecolors='white', lw=1.5, zorder=5, alpha=0.9)
|
| 364 |
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', alpha=0.6, label='Random (AUC=0.5)'); ax.annotate('IDEAL ZONE\nHigh Utility, Low Risk', xy=(85, 0.51), fontsize=11, fontweight='bold', color=COLORS['success'], alpha=0.7, ha='center', backgroundcolor=COLORS['bg']); ax.annotate('HIGH RISK ZONE\nLow Utility, High Risk', xy=(62, 0.61), fontsize=11, fontweight='bold', color=COLORS['danger'], alpha=0.7, ha='center', backgroundcolor=COLORS['bg']); ax.set_xlabel('Model Utility (Accuracy %)', fontsize=12, fontweight='medium'); ax.set_ylabel('Privacy Risk (MIA AUC)', fontsize=12, fontweight='medium'); ax.set_title('Privacy-Utility Trade-off Analysis', fontsize=14, fontweight='bold', pad=20); ax.legend(fontsize=10, loc='upper left', ncol=2, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text']); plt.tight_layout()
|
| 365 |
return fig
|
| 366 |
|
| 367 |
def fig_auc_trend():
|
| 368 |
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]
|
| 369 |
-
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (left)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility % (right)', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 370 |
-
ax
|
|
|
|
|
|
|
| 371 |
return fig
|
| 372 |
|
| 373 |
def fig_loss_gap_waterfall():
|
| 374 |
fig, ax = plt.subplots(figsize=(14, 6.5)); apply_light_style(fig, ax); names, gaps, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 375 |
-
for i, (k, l) in enumerate(zip(LS_KEYS,
|
| 376 |
-
for i, (k, l) in enumerate(zip(OP_KEYS,
|
| 377 |
bars = ax.bar(range(len(names)), gaps, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 378 |
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'])
|
| 379 |
-
ax.set_ylabel('Loss Gap', fontsize=12, fontweight='medium'); ax.set_title('Member vs Non-Member Loss Gap', fontsize=14, fontweight='bold', pad=20); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=
|
| 380 |
return fig
|
| 381 |
|
| 382 |
# ================================================================
|
|
@@ -401,6 +335,7 @@ def cb_sample(src):
|
|
| 401 |
"""
|
| 402 |
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 403 |
|
|
|
|
| 404 |
ATK_CHOICES = (
|
| 405 |
["基线模型 (Baseline)"] +
|
| 406 |
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
|
@@ -517,12 +452,12 @@ def cb_eval(model_choice):
|
|
| 517 |
|
| 518 |
def build_full_table():
|
| 519 |
rows = []
|
| 520 |
-
for k, l in zip(LS_KEYS,
|
| 521 |
if k in mia_results:
|
| 522 |
m = mia_results[k]; u = gu(k)
|
| 523 |
t = "—" if k == "baseline" else "训练期"; d = "" if k == "baseline" else f"{m['auc']-bl_auc:+.4f}"
|
| 524 |
rows.append(f"| {l} | {t} | {m['auc']:.4f} | {m['attack_accuracy']:.4f} | {m['precision']:.4f} | {m['recall']:.4f} | {m['f1']:.4f} | {m['tpr_at_5fpr']:.4f} | {m['tpr_at_1fpr']:.4f} | {m['loss_gap']:.4f} | {u:.1f}% | {d} |")
|
| 525 |
-
for k, l in zip(OP_KEYS,
|
| 526 |
if k in perturb_results:
|
| 527 |
m = perturb_results[k]; d = f"{m['auc']-bl_auc:+.4f}"
|
| 528 |
rows.append(f"| {l} | 推理期 | {m['auc']:.4f} | {m['attack_accuracy']:.4f} | {m['precision']:.4f} | {m['recall']:.4f} | {m['f1']:.4f} | {m['tpr_at_5fpr']:.4f} | {m['tpr_at_1fpr']:.4f} | {m['loss_gap']:.4f} | {bl_acc:.1f}% | {d} |")
|
|
@@ -605,8 +540,8 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 605 |
""")
|
| 606 |
|
| 607 |
# 实验体系总览图 (如果在目录里则显示)
|
| 608 |
-
if os.path.exists(os.path.join(BASE_DIR, "figures", "
|
| 609 |
-
gr.Image(os.path.join(BASE_DIR, "figures", "
|
| 610 |
|
| 611 |
gr.HTML(f"""<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin:30px 0;">
|
| 612 |
<div class="card-wrap" style="text-align:center;">
|
|
@@ -719,24 +654,22 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 719 |
gr.Markdown(f"### 1️⃣ 攻击成功率全景对比 (AUC)\n\n> 柱子越短 = AUC越低 = 防御越有效。基线AUC={bl_auc:.4f},标签平滑最低降至{gm('smooth_eps_0.2','auc'):.4f},输出扰动最低降至{gm('perturbation_0.03','auc'):.4f}。")
|
| 720 |
gr.Plot(value=fig_auc_bar())
|
| 721 |
|
| 722 |
-
# --- 整合双雷达图及配套讲解文本 ---
|
| 723 |
gr.Markdown(f"""\
|
| 724 |
### 2️⃣ 多指标雷达图对比(全部11组实验)
|
| 725 |
|
| 726 |
> **左图:标签平滑系列5个模型**
|
| 727 |
> - 红色(Baseline)面积最大 = 攻击全面有效
|
| 728 |
-
> - 随着ε从0.02
|
| 729 |
> - 特别注意 TPR@1%FPR 和 LossGap 两个轴,缩小最显著
|
| 730 |
>
|
| 731 |
> **右图:输出扰动系列7个配置**
|
| 732 |
> - 红色(Baseline)同样是最大的
|
| 733 |
-
> - 随着σ从0.005
|
| 734 |
> - OP在LossGap和TPR@5%维度上降幅尤其明显
|
| 735 |
>
|
| 736 |
> **结论:** 两种防御均在所有维度上全面压制攻击能力,不是只降低了某一个指标。
|
| 737 |
""")
|
| 738 |
gr.Plot(value=fig_radar())
|
| 739 |
-
# ---------------------------------
|
| 740 |
|
| 741 |
gr.Markdown("### 3️⃣ ROC曲线对比\n\n> 曲线越贴近对角线=攻击越接近随机猜测=防御越有效。左图标签平滑,右图输出扰动。")
|
| 742 |
gr.Plot(value=fig_roc_curves())
|
|
@@ -862,4 +795,4 @@ with gr.Blocks(title="MIA攻防研究") as demo:
|
|
| 862 |
|
| 863 |
""")
|
| 864 |
|
| 865 |
-
demo.launch(
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
+
# 教育大模型MIA攻防研究 - Gradio演示系统 v7.0 学术巅峰版
|
| 3 |
+
# 彻底消灭普通 e/s,全量启用 LaTeX 原生数学斜体 $\epsilon$ 和 $\sigma$
|
| 4 |
# ================================================================
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 31 |
text = re.sub(r'[\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufeff]', '', text)
|
| 32 |
return text.strip()
|
| 33 |
|
|
|
|
| 34 |
try:
|
| 35 |
member_data = load_json("data/member.json")
|
| 36 |
non_member_data = load_json("data/non_member.json")
|
|
|
|
| 57 |
for s in [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]:
|
| 58 |
k = f"perturbation_{s}"
|
| 59 |
perturb_results[k] = {m: v*0.85 for m, v in mia_results["baseline"].items()}
|
|
|
|
| 60 |
perturb_results[k]["member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 61 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 62 |
|
| 63 |
# ================================================================
|
| 64 |
+
# 全局图表配置
|
| 65 |
# ================================================================
|
| 66 |
COLORS = {
|
| 67 |
'bg': '#FFFFFF',
|
|
|
|
| 78 |
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 79 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 80 |
}
|
|
|
|
|
|
|
| 81 |
CHART_W = 14
|
| 82 |
|
| 83 |
def apply_light_style(fig, ax_or_axes):
|
|
|
|
| 96 |
ax.grid(True, color=COLORS['grid'], alpha=0.6, linestyle='-', linewidth=0.8)
|
| 97 |
ax.set_axisbelow(True)
|
| 98 |
|
| 99 |
+
# 🌟🌟🌟 核心修改:专门为画图准备的 LaTeX 格式标签 🌟🌟🌟
|
|
|
|
|
|
|
| 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_UI = ["基线(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_UI = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 108 |
|
| 109 |
ALL_KEYS = LS_KEYS + OP_KEYS
|
| 110 |
|
|
|
|
| 123 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 124 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 125 |
|
| 126 |
+
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题', 'concept': '概念问答', 'error_correction': '错题订正'}
|
|
|
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
np.random.seed(777)
|
| 129 |
EVAL_POOL = []
|
| 130 |
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
|
|
|
| 138 |
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 139 |
elif _t == 'word_problem':
|
| 140 |
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 141 |
+
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)), (f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
|
|
|
| 142 |
_q,_ans = _tpls[_i%len(_tpls)]
|
| 143 |
elif _t == 'concept':
|
| 144 |
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
|
|
|
| 153 |
EVAL_POOL.append(item)
|
| 154 |
|
| 155 |
# ================================================================
|
| 156 |
+
# 图表绘制函数 (全部更换为 LaTeX 渲染)
|
| 157 |
# ================================================================
|
| 158 |
def fig_gauge(loss_val, m_mean, nm_mean, thr, m_std, nm_std):
|
| 159 |
+
fig, ax = plt.subplots(figsize=(10, 2.6)); fig.patch.set_facecolor(COLORS['bg']); ax.set_facecolor(COLORS['panel'])
|
| 160 |
+
xlo = min(m_mean - 3.0 * m_std, loss_val - 0.005); xhi = max(nm_mean + 3.0 * nm_std, loss_val + 0.005)
|
| 161 |
+
ax.axvspan(xlo, thr, alpha=0.2, color=COLORS['accent']); ax.axvspan(thr, xhi, alpha=0.2, color=COLORS['danger'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 163 |
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())
|
|
|
|
| 164 |
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 165 |
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())
|
|
|
|
| 166 |
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 167 |
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())
|
|
|
|
| 168 |
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 169 |
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 170 |
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())
|
|
|
|
| 171 |
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())
|
| 172 |
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())
|
|
|
|
| 173 |
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 174 |
for s in ax.spines.values(): s.set_visible(False)
|
| 175 |
+
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid']); ax.tick_params(colors=COLORS['text_dim'], width=1)
|
| 176 |
+
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium'); plt.tight_layout(pad=0.5)
|
|
|
|
|
|
|
| 177 |
return fig
|
| 178 |
|
| 179 |
def fig_auc_bar():
|
| 180 |
names, vals, clrs = [], [], []
|
| 181 |
ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 182 |
+
for i,(k,l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 183 |
if k in mia_results: names.append(l); vals.append(mia_results[k]['auc']); clrs.append(ls_c[i])
|
| 184 |
+
for i,(k,l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 185 |
if k in perturb_results: names.append(l); vals.append(perturb_results[k]['auc']); clrs.append(COLORS['op_colors'][i])
|
| 186 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax)
|
| 187 |
bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
|
|
|
| 189 |
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', lw=1.5, alpha=0.6, label='Random Guess (0.5)', zorder=2)
|
| 190 |
ax.axhline(bl_auc, color=COLORS['danger'], ls=':', lw=1.5, alpha=0.8, label=f'Baseline ({bl_auc:.4f})', zorder=2)
|
| 191 |
ax.set_ylabel('MIA Attack AUC', fontsize=12, fontweight='medium'); ax.set_title('Defense Effectiveness: MIA AUC Comparison', fontsize=14, fontweight='bold', pad=20)
|
| 192 |
+
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)
|
| 193 |
ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10, loc='upper right'); plt.tight_layout()
|
| 194 |
return fig
|
| 195 |
|
| 196 |
def fig_radar():
|
| 197 |
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 198 |
+
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1', 'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 199 |
+
N = len(ms); ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 200 |
+
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7), subplot_kw=dict(polar=True)); fig.patch.set_facecolor('white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
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')]
|
| 203 |
+
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')]
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
+
for ax_idx, (ax, cfgs, title) in enumerate([(axes[0], ls_cfgs, 'Label Smoothing (5 models)'), (axes[1], op_cfgs, 'Output Perturbation (7 configs)')]):
|
| 206 |
+
ax.set_facecolor('white')
|
| 207 |
+
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]
|
| 208 |
for nm, ky, cl in cfgs:
|
| 209 |
+
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]; v += [v[0]]
|
| 210 |
+
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)
|
| 211 |
+
ax.fill(ag, v, alpha=0.10 if ky == 'baseline' else 0.04, color=cl)
|
| 212 |
+
ax.set_xticks(ag[:-1]); ax.set_xticklabels(ms, fontsize=10, color=COLORS['text']); ax.set_yticklabels([])
|
| 213 |
+
ax.set_title(title, fontsize=12, fontweight='700', color=COLORS['text'], pad=18)
|
| 214 |
+
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'])
|
| 215 |
+
ax.spines['polar'].set_color(COLORS['grid']); ax.grid(color=COLORS['grid'], alpha=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
plt.tight_layout()
|
| 217 |
return fig
|
| 218 |
|
| 219 |
def fig_loss_dist():
|
| 220 |
+
items = [(k, l, gm(k, 'auc')) for k, l in zip(LS_KEYS, LS_LABELS_PLOT) if k in full_losses]; n = len(items)
|
| 221 |
if n == 0: return plt.figure()
|
| 222 |
fig, axes = plt.subplots(1, n, figsize=(4.5*n, 4.5)); axes = [axes] if n == 1 else axes; apply_light_style(fig, axes)
|
| 223 |
for ax, (k, l, a) in zip(axes, items):
|
|
|
|
| 239 |
ax.hist(mp, bins=bins, alpha=0.6, color=COLORS['accent'], label='Mem+noise', density=True, edgecolor='white')
|
| 240 |
ax.hist(np_, bins=bins, alpha=0.6, color=COLORS['danger'], label='Non+noise', density=True, edgecolor='white')
|
| 241 |
pa = gm(f'perturbation_{s}', 'auc')
|
| 242 |
+
ax.set_title(f'OP($\sigma$={s})\nAUC={pa:.4f}', fontsize=11, fontweight='semibold'); ax.set_xlabel('Loss', fontsize=10)
|
| 243 |
ax.legend(fontsize=9, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'])
|
| 244 |
plt.tight_layout(); return fig
|
| 245 |
|
| 246 |
def fig_roc_curves():
|
| 247 |
fig, axes = plt.subplots(1, 2, figsize=(16, 7)); apply_light_style(fig, axes)
|
| 248 |
ax = axes[0]; ls_colors = [COLORS['danger'], COLORS['ls_colors'][0], COLORS['ls_colors'][1], COLORS['ls_colors'][2], COLORS['ls_colors'][3]]
|
| 249 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 250 |
if k not in full_losses: continue
|
| 251 |
m = np.array(full_losses[k]['member_losses']); nm = np.array(full_losses[k]['non_member_losses'])
|
| 252 |
y_true = np.concatenate([np.ones(len(m)), np.zeros(len(nm))]); y_scores = np.concatenate([-m, -nm])
|
|
|
|
| 259 |
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})')
|
| 260 |
for i, s in enumerate(OP_SIGMAS):
|
| 261 |
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)
|
| 262 |
+
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP($\sigma$={s}) (AUC={auc_p:.4f})')
|
| 263 |
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('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()
|
| 264 |
return fig
|
| 265 |
|
| 266 |
def fig_tpr_at_low_fpr():
|
| 267 |
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']
|
| 268 |
+
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])
|
| 269 |
+
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])
|
| 270 |
x = range(len(labels_all)); ax = axes[0]; bars = ax.bar(x, tpr5_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 271 |
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'])
|
| 272 |
+
ax.set_ylabel('TPR @ 5% FPR', fontsize=12, fontweight='medium'); ax.set_title('Attack Power at 5% FPR', fontsize=14, fontweight='bold', pad=15); 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, label='Random (0.05)'); ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10)
|
| 273 |
ax = axes[1]; bars = ax.bar(x, tpr1_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 274 |
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'])
|
| 275 |
+
ax.set_ylabel('TPR @ 1% FPR', fontsize=12, fontweight='medium'); ax.set_title('Attack Power at 1% FPR (Strict)', fontsize=14, fontweight='bold', pad=15); 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, label='Random (0.01)'); ax.legend(facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text'], fontsize=10); plt.tight_layout()
|
| 276 |
return fig
|
| 277 |
|
| 278 |
def fig_acc_bar():
|
| 279 |
names, vals, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 280 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 281 |
if k in utility_results: names.append(l); vals.append(utility_results[k]['accuracy']*100); clrs.append(ls_c[i])
|
| 282 |
+
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 283 |
if k in perturb_results: names.append(l); vals.append(bl_acc); clrs.append(COLORS['op_colors'][i])
|
| 284 |
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax); bars = ax.bar(range(len(names)), vals, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 285 |
for b, v in zip(bars, vals): ax.text(b.get_x()+b.get_width()/2, v+1, f'{v:.1f}%', ha='center', fontsize=10, fontweight='semibold', color=COLORS['text'])
|
| 286 |
+
ax.set_ylabel('Test Accuracy (%)', fontsize=12, fontweight='medium'); ax.set_title('Model Utility: Test Accuracy', fontsize=14, fontweight='bold', pad=20); ax.set_ylim(0, 105); ax.set_xticks(range(len(names))); ax.set_xticklabels(names, rotation=30, ha='right', fontsize=11); plt.tight_layout()
|
| 287 |
return fig
|
| 288 |
|
| 289 |
def fig_tradeoff():
|
| 290 |
fig, ax = plt.subplots(figsize=(11, 8)); apply_light_style(fig, ax); markers_ls = ['o', 's', 's', 's', 's']; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 291 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 292 |
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=180, edgecolors='white', lw=1.5, zorder=5, alpha=0.9)
|
| 293 |
op_markers = ['^', 'D', 'v', 'P', 'X', 'h']
|
| 294 |
+
for i, (k, l) in enumerate(zip(OP_KEYS, OP_LABELS_PLOT)):
|
| 295 |
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=180, edgecolors='white', lw=1.5, zorder=5, alpha=0.9)
|
| 296 |
ax.axhline(0.5, color=COLORS['text_dim'], ls='--', alpha=0.6, label='Random (AUC=0.5)'); ax.annotate('IDEAL ZONE\nHigh Utility, Low Risk', xy=(85, 0.51), fontsize=11, fontweight='bold', color=COLORS['success'], alpha=0.7, ha='center', backgroundcolor=COLORS['bg']); ax.annotate('HIGH RISK ZONE\nLow Utility, High Risk', xy=(62, 0.61), fontsize=11, fontweight='bold', color=COLORS['danger'], alpha=0.7, ha='center', backgroundcolor=COLORS['bg']); ax.set_xlabel('Model Utility (Accuracy %)', fontsize=12, fontweight='medium'); ax.set_ylabel('Privacy Risk (MIA AUC)', fontsize=12, fontweight='medium'); ax.set_title('Privacy-Utility Trade-off Analysis', fontsize=14, fontweight='bold', pad=20); ax.legend(fontsize=10, loc='upper left', ncol=2, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text']); plt.tight_layout()
|
| 297 |
return fig
|
| 298 |
|
| 299 |
def fig_auc_trend():
|
| 300 |
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]
|
| 301 |
+
ax2 = ax.twinx(); line1 = ax.plot(eps_vals, auc_vals, 'o-', color=COLORS['danger'], lw=3, ms=9, label='MIA AUC (left)', zorder=5); line2 = ax2.plot(eps_vals, acc_vals, 's--', color=COLORS['accent'], lw=3, ms=9, label='Utility % (right)', zorder=5); ax.axhline(0.5, color=COLORS['text_dim'], ls=':', alpha=0.5)
|
| 302 |
+
ax.set_xlabel(r'Label Smoothing $\epsilon$', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC', fontsize=12, fontweight='medium', color=COLORS['danger']); ax2.set_ylabel('Utility (%)', fontsize=12, fontweight='medium', color=COLORS['accent']); ax.set_title('Label Smoothing Trends', fontsize=14, fontweight='bold', pad=15); 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'])
|
| 303 |
+
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')
|
| 304 |
+
ax.set_xlabel(r'Perturbation $\sigma$', fontsize=12, fontweight='medium'); ax.set_ylabel('MIA AUC', fontsize=12, fontweight='medium'); ax.set_title('Output Perturbation Trends', fontsize=14, fontweight='bold', pad=15); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', labelcolor=COLORS['text']); plt.tight_layout()
|
| 305 |
return fig
|
| 306 |
|
| 307 |
def fig_loss_gap_waterfall():
|
| 308 |
fig, ax = plt.subplots(figsize=(14, 6.5)); apply_light_style(fig, ax); names, gaps, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 309 |
+
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])
|
| 310 |
+
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])
|
| 311 |
bars = ax.bar(range(len(names)), gaps, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 312 |
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'])
|
| 313 |
+
ax.set_ylabel('Loss Gap', fontsize=12, fontweight='medium'); ax.set_title('Member vs Non-Member Loss Gap', 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()
|
| 314 |
return fig
|
| 315 |
|
| 316 |
# ================================================================
|
|
|
|
| 335 |
"""
|
| 336 |
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 337 |
|
| 338 |
+
# 🌟 下拉框选项也全部替换为 ε 和 σ
|
| 339 |
ATK_CHOICES = (
|
| 340 |
["基线模型 (Baseline)"] +
|
| 341 |
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
|
|
|
| 452 |
|
| 453 |
def build_full_table():
|
| 454 |
rows = []
|
| 455 |
+
for k, l in zip(LS_KEYS, LS_LABELS_UI):
|
| 456 |
if k in mia_results:
|
| 457 |
m = mia_results[k]; u = gu(k)
|
| 458 |
t = "—" if k == "baseline" else "训练期"; d = "" if k == "baseline" else f"{m['auc']-bl_auc:+.4f}"
|
| 459 |
rows.append(f"| {l} | {t} | {m['auc']:.4f} | {m['attack_accuracy']:.4f} | {m['precision']:.4f} | {m['recall']:.4f} | {m['f1']:.4f} | {m['tpr_at_5fpr']:.4f} | {m['tpr_at_1fpr']:.4f} | {m['loss_gap']:.4f} | {u:.1f}% | {d} |")
|
| 460 |
+
for k, l in zip(OP_KEYS, OP_LABELS_UI):
|
| 461 |
if k in perturb_results:
|
| 462 |
m = perturb_results[k]; d = f"{m['auc']-bl_auc:+.4f}"
|
| 463 |
rows.append(f"| {l} | 推理期 | {m['auc']:.4f} | {m['attack_accuracy']:.4f} | {m['precision']:.4f} | {m['recall']:.4f} | {m['f1']:.4f} | {m['tpr_at_5fpr']:.4f} | {m['tpr_at_1fpr']:.4f} | {m['loss_gap']:.4f} | {bl_acc:.1f}% | {d} |")
|
|
|
|
| 540 |
""")
|
| 541 |
|
| 542 |
# 实验体系总览图 (如果在目录里则显示)
|
| 543 |
+
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png")):
|
| 544 |
+
gr.Image(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png"), label="实验体系总览", show_label=True)
|
| 545 |
|
| 546 |
gr.HTML(f"""<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin:30px 0;">
|
| 547 |
<div class="card-wrap" style="text-align:center;">
|
|
|
|
| 654 |
gr.Markdown(f"### 1️⃣ 攻击成功率全景对比 (AUC)\n\n> 柱子越短 = AUC越低 = 防御越有效。基线AUC={bl_auc:.4f},标签平滑最低降至{gm('smooth_eps_0.2','auc'):.4f},输出扰动最低降至{gm('perturbation_0.03','auc'):.4f}。")
|
| 655 |
gr.Plot(value=fig_auc_bar())
|
| 656 |
|
|
|
|
| 657 |
gr.Markdown(f"""\
|
| 658 |
### 2️⃣ 多指标雷达图对比(全部11组实验)
|
| 659 |
|
| 660 |
> **左图:标签平滑系列5个模型**
|
| 661 |
> - 红色(Baseline)面积最大 = 攻击全面有效
|
| 662 |
+
> - 随着 ε 从 0.02 增至 0.2,雷达面积逐步缩小 = 防御逐步增强
|
| 663 |
> - 特别注意 TPR@1%FPR 和 LossGap 两个轴,缩小最显著
|
| 664 |
>
|
| 665 |
> **右图:输出扰动系列7个配置**
|
| 666 |
> - 红色(Baseline)同样是最大的
|
| 667 |
+
> - 随着 σ 从 0.005 增至 0.03,绿色系雷达逐步缩小
|
| 668 |
> - OP在LossGap和TPR@5%维度上降幅尤其明显
|
| 669 |
>
|
| 670 |
> **结论:** 两种防御均在所有维度上全面压制攻击能力,不是只降低了某一个指标。
|
| 671 |
""")
|
| 672 |
gr.Plot(value=fig_radar())
|
|
|
|
| 673 |
|
| 674 |
gr.Markdown("### 3️⃣ ROC曲线对比\n\n> 曲线越贴近对角线=攻击越接近随机猜测=防御越有效。左图标签平滑,右图输出扰动。")
|
| 675 |
gr.Plot(value=fig_roc_curves())
|
|
|
|
| 795 |
|
| 796 |
""")
|
| 797 |
|
| 798 |
+
demo.launch()
|