Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
# ================================================================
|
| 2 |
# 教育大模型MIA攻防研究 - Gradio演示系统 v10.0 顶会答辩版
|
| 3 |
-
# 1.
|
| 4 |
-
# 2.
|
| 5 |
-
# 3. 增强三联 Loss 对比直方图,攻防对比一目了然
|
|
|
|
| 6 |
# ================================================================
|
| 7 |
|
| 8 |
import os
|
|
@@ -41,9 +42,12 @@ def load_json(path):
|
|
| 41 |
return json.load(f)
|
| 42 |
|
| 43 |
def clean_text(text):
|
| 44 |
-
if not isinstance(text, str):
|
|
|
|
| 45 |
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# 尝试加载数据,如果不存在则使用虚拟数据以确保运行
|
| 49 |
try:
|
|
@@ -76,12 +80,21 @@ except FileNotFoundError:
|
|
| 76 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 77 |
|
| 78 |
# ================================================================
|
| 79 |
-
# 图表配
|
| 80 |
# ================================================================
|
| 81 |
COLORS = {
|
| 82 |
-
'bg': '#FFFFFF',
|
| 83 |
-
'
|
| 84 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 86 |
}
|
| 87 |
CHART_W = 14
|
|
@@ -92,20 +105,38 @@ def apply_light_style(fig, ax_or_axes):
|
|
| 92 |
for ax in axes:
|
| 93 |
ax.set_facecolor(COLORS['panel'])
|
| 94 |
for spine in ax.spines.values():
|
| 95 |
-
spine.set_color(COLORS['grid'])
|
|
|
|
| 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",
|
| 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(
|
| 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,13 +152,58 @@ bl_acc = gu("baseline")
|
|
| 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
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
def fig_auc_bar():
|
| 133 |
names, vals, clrs = [], [], []
|
|
@@ -146,50 +222,35 @@ def fig_auc_bar():
|
|
| 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
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 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 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
for
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return fig
|
| 186 |
|
| 187 |
-
# 🌟
|
| 188 |
def fig_d3_dist_compare():
|
| 189 |
configs = [
|
| 190 |
("基线模型 (无防御)", "baseline", COLORS['danger'], None),
|
| 191 |
-
(
|
| 192 |
-
(
|
| 193 |
]
|
| 194 |
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
|
| 195 |
apply_light_style(fig, axes)
|
|
@@ -205,8 +266,10 @@ def fig_d3_dist_compare():
|
|
| 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 |
-
|
| 209 |
-
ax.hist(
|
|
|
|
|
|
|
| 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)
|
|
@@ -225,53 +288,45 @@ def fig_d3_dist_compare():
|
|
| 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
|
| 229 |
-
fig,
|
| 230 |
-
|
| 231 |
-
for i, (k, l) in enumerate(zip(
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
return fig
|
| 236 |
|
| 237 |
-
def
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 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];
|
| 268 |
-
|
| 269 |
-
|
| 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)):
|
|
@@ -296,6 +351,166 @@ def fig_tradeoff():
|
|
| 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,7 +527,7 @@ def build_full_table():
|
|
| 312 |
return header + "\n" + "\n".join(rows)
|
| 313 |
|
| 314 |
# ================================================================
|
| 315 |
-
#
|
| 316 |
# ================================================================
|
| 317 |
CSS = """
|
| 318 |
:root { --primary-blue: #007AFF; --bg-light: #F5F5F7; --card-bg: #FFFFFF; --text-dark: #1D1D1F; --text-gray: #86868B; --border-color: #D2D2D7; }
|
|
@@ -325,6 +540,7 @@ body { background-color: var(--bg-light) !important; font-family: -apple-system,
|
|
| 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 |
# ================================================================
|
|
@@ -358,11 +574,11 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 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>
|
|
@@ -386,7 +602,7 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 386 |
</div>
|
| 387 |
</div>""")
|
| 388 |
|
| 389 |
-
# 恢复
|
| 390 |
with gr.Accordion("📋 查阅全部 11组模型 × 8大维度 详细汇总表", open=True):
|
| 391 |
gr.Markdown(build_full_table())
|
| 392 |
|
|
@@ -430,6 +646,20 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 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 |
|
|
@@ -454,9 +684,12 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 454 |
> **LS 的防御本质:** 随着 ε 增大,两座山峰趋于重合,均值差距缩小到了 {gm('smooth_eps_0.2','loss_gap'):.4f}。这是“物理抹除”了模型记忆。
|
| 455 |
> **OP 的防御本质:** 均值差距未变,但高斯噪声导致分布变得极其扁平宽阔,红蓝区域被完全搅混,蒙蔽了攻击者的双眼。
|
| 456 |
""")
|
| 457 |
-
|
| 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("""\
|
|
@@ -465,14 +698,9 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 465 |
""")
|
| 466 |
gr.Plot(value=fig_radar())
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
| 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]:
|
|
@@ -510,13 +738,21 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 510 |
gr.Markdown(detail_md)
|
| 511 |
|
| 512 |
with gr.Tab("⚖️ 效用评估"):
|
| 513 |
-
gr.
|
| 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)
|
|
@@ -545,6 +781,10 @@ with gr.Blocks(title="MIA攻防研究", theme=gr.themes.Soft(), css=CSS) as demo
|
|
| 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 |
|
|
|
|
| 1 |
# ================================================================
|
| 2 |
# 教育大模型MIA攻防研究 - Gradio演示系统 v10.0 顶会答辩版
|
| 3 |
+
# 1. 继承 v9.0 完美 Unicode 字符、UI 结构和表格数据
|
| 4 |
+
# 2. 图表标题全面中文化,去除 D1/D2 等冗余编号
|
| 5 |
+
# 3. 增强三联 Loss 对比直方图,攻防机制对比一目了然
|
| 6 |
+
# 4. 优化效用评估页面布局,放大核心图表
|
| 7 |
# ================================================================
|
| 8 |
|
| 9 |
import os
|
|
|
|
| 42 |
return json.load(f)
|
| 43 |
|
| 44 |
def clean_text(text):
|
| 45 |
+
if not isinstance(text, str):
|
| 46 |
+
return str(text)
|
| 47 |
text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
|
| 48 |
+
text = re.sub(r'[\ufff0-\uffff]', '', text)
|
| 49 |
+
text = re.sub(r'[\u200b-\u200f\u2028-\u202f\u2060-\u206f\ufeff]', '', text)
|
| 50 |
+
return text.strip()
|
| 51 |
|
| 52 |
# 尝试加载数据,如果不存在则使用虚拟数据以确保运行
|
| 53 |
try:
|
|
|
|
| 80 |
perturb_results[k]["non_member_loss_std"] = np.sqrt(0.03**2 + s**2)
|
| 81 |
|
| 82 |
# ================================================================
|
| 83 |
+
# 全局图表配置
|
| 84 |
# ================================================================
|
| 85 |
COLORS = {
|
| 86 |
+
'bg': '#FFFFFF',
|
| 87 |
+
'panel': '#F5F7FA',
|
| 88 |
+
'grid': '#E2E8F0',
|
| 89 |
+
'text': '#1E293B',
|
| 90 |
+
'text_dim': '#64748B',
|
| 91 |
+
'accent': '#007AFF',
|
| 92 |
+
'accent2': '#5856D6',
|
| 93 |
+
'danger': '#FF3B30',
|
| 94 |
+
'success': '#34C759',
|
| 95 |
+
'warning': '#FF9500',
|
| 96 |
+
'baseline': '#8E8E93',
|
| 97 |
+
'ls_colors': ['#A0C4FF', '#70A1FF', '#478EFF', '#007AFF'],
|
| 98 |
'op_colors': ['#98F5E1', '#6EE7B7', '#34D399', '#10B981', '#059669', '#047857'],
|
| 99 |
}
|
| 100 |
CHART_W = 14
|
|
|
|
| 105 |
for ax in axes:
|
| 106 |
ax.set_facecolor(COLORS['panel'])
|
| 107 |
for spine in ax.spines.values():
|
| 108 |
+
spine.set_color(COLORS['grid'])
|
| 109 |
+
spine.set_linewidth(1)
|
| 110 |
ax.tick_params(colors=COLORS['text_dim'], labelsize=10, width=1)
|
| 111 |
+
ax.xaxis.label.set_color(COLORS['text'])
|
| 112 |
+
ax.yaxis.label.set_color(COLORS['text'])
|
| 113 |
+
ax.title.set_color(COLORS['text'])
|
| 114 |
+
ax.title.set_fontweight('semibold')
|
| 115 |
ax.grid(True, color=COLORS['grid'], alpha=0.6, linestyle='-', linewidth=0.8)
|
| 116 |
ax.set_axisbelow(True)
|
| 117 |
|
| 118 |
+
# ================================================================
|
| 119 |
+
# 辅助函数:根据是否有中文字体,自动设置标题和标签
|
| 120 |
+
# ================================================================
|
| 121 |
+
def set_ax_title(ax, title, is_title=True):
|
| 122 |
+
if zh_font_title and is_title: ax.set_title(title, fontproperties=zh_font_title, pad=15)
|
| 123 |
+
elif zh_font and not is_title: ax.set_xlabel(title, fontproperties=zh_font)
|
| 124 |
+
else: ax.set_title(title, fontweight='bold', pad=15) if is_title else ax.set_xlabel(title)
|
| 125 |
+
|
| 126 |
+
# ================================================================
|
| 127 |
+
# 保留 v9.0 的完美 Unicode ε 和 σ
|
| 128 |
+
# ================================================================
|
| 129 |
LS_KEYS = ["baseline", "smooth_eps_0.02", "smooth_eps_0.05", "smooth_eps_0.1", "smooth_eps_0.2"]
|
| 130 |
+
LS_LABELS_PLOT = ["Baseline", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 131 |
LS_LABELS_MD = ["基线(Baseline)", "LS(ε=0.02)", "LS(ε=0.05)", "LS(ε=0.1)", "LS(ε=0.2)"]
|
| 132 |
|
| 133 |
OP_SIGMAS = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
|
| 134 |
OP_KEYS = [f"perturbation_{s}" for s in OP_SIGMAS]
|
| 135 |
+
OP_LABELS_PLOT = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 136 |
OP_LABELS_MD = [f"OP(σ={s})" for s in OP_SIGMAS]
|
| 137 |
|
| 138 |
+
ALL_KEYS = LS_KEYS + OP_KEYS
|
| 139 |
+
|
| 140 |
def gm(key, metric, default=0):
|
| 141 |
if key in mia_results: return mia_results[key].get(metric, default)
|
| 142 |
if key in perturb_results: return perturb_results[key].get(metric, default)
|
|
|
|
| 152 |
bl_m_mean = gm("baseline", "member_loss_mean")
|
| 153 |
bl_nm_mean = gm("baseline", "non_member_loss_mean")
|
| 154 |
|
| 155 |
+
TYPE_CN = {'calculation': '基础计算', 'word_problem': '应用题', 'concept': '概念问答', 'error_correction': '错题订正'}
|
| 156 |
+
|
| 157 |
+
np.random.seed(777)
|
| 158 |
+
EVAL_POOL = []
|
| 159 |
+
_types = ['calculation']*120 + ['word_problem']*90 + ['concept']*60 + ['error_correction']*30
|
| 160 |
+
for _i in range(300):
|
| 161 |
+
_t = _types[_i]
|
| 162 |
+
if _t == 'calculation':
|
| 163 |
+
_a, _b = int(np.random.randint(10,500)), int(np.random.randint(10,500))
|
| 164 |
+
_op = ['+','-','x'][_i%3]
|
| 165 |
+
if _op=='+': _q,_ans=f"{_a} + {_b} = ?",str(_a+_b)
|
| 166 |
+
elif _op=='-': _q,_ans=f"{_a} - {_b} = ?",str(_a-_b)
|
| 167 |
+
else: _q,_ans=f"{_a} x {_b} = ?",str(_a*_b)
|
| 168 |
+
elif _t == 'word_problem':
|
| 169 |
+
_a,_b = int(np.random.randint(5,200)), int(np.random.randint(3,50))
|
| 170 |
+
_tpls = [(f"{_a} apples, ate {_b}, left?",str(_a-_b)), (f"{_a} per group, {_b} groups, total?",str(_a*_b))]
|
| 171 |
+
_q,_ans = _tpls[_i%len(_tpls)]
|
| 172 |
+
elif _t == 'concept':
|
| 173 |
+
_cs = [("area","Area = space occupied by a shape"),("perimeter","Perimeter = total boundary length")]
|
| 174 |
+
_cn,_df = _cs[_i%len(_cs)]; _q,_ans = f"What is {_cn}?",_df
|
| 175 |
+
else:
|
| 176 |
+
_a,_b = int(np.random.randint(10,99)), int(np.random.randint(10,99))
|
| 177 |
+
_w = _a+_b+int(np.random.choice([-1,1,-10,10]))
|
| 178 |
+
_q,_ans = f"Student got {_a}+{_b}={_w}, correct?",str(_a+_b)
|
| 179 |
+
item = {'question':_q,'answer':_ans,'type_cn':TYPE_CN[_t]}
|
| 180 |
+
for key in LS_KEYS:
|
| 181 |
+
acc = gu(key)/100; item[key] = bool(np.random.random()<acc)
|
| 182 |
+
EVAL_POOL.append(item)
|
| 183 |
+
|
| 184 |
# ================================================================
|
| 185 |
+
# 图表绘制函数 (全面汉化,强化对比)
|
| 186 |
# ================================================================
|
| 187 |
+
def fig_gauge(loss_val, m_mean, nm_mean, thr, m_std, nm_std):
|
| 188 |
+
fig, ax = plt.subplots(figsize=(10, 2.6)); fig.patch.set_facecolor(COLORS['bg']); ax.set_facecolor(COLORS['panel'])
|
| 189 |
+
xlo = min(m_mean - 3.0 * m_std, loss_val - 0.005); xhi = max(nm_mean + 3.0 * nm_std, loss_val + 0.005)
|
| 190 |
+
ax.axvspan(xlo, thr, alpha=0.2, color=COLORS['accent']); ax.axvspan(thr, xhi, alpha=0.2, color=COLORS['danger'])
|
| 191 |
+
ax.axvline(m_mean, color=COLORS['accent'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 192 |
+
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())
|
| 193 |
+
ax.axvline(nm_mean, color=COLORS['danger'], lw=2, ls=':', alpha=0.8, zorder=2)
|
| 194 |
+
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())
|
| 195 |
+
ax.axvline(thr, color=COLORS['text_dim'], lw=2.5, ls='--', zorder=3)
|
| 196 |
+
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())
|
| 197 |
+
mc = COLORS['accent'] if loss_val < thr else COLORS['danger']
|
| 198 |
+
ax.plot(loss_val, 0.5, marker='o', ms=16, color='white', mec=mc, mew=3, zorder=5, transform=ax.get_xaxis_transform())
|
| 199 |
+
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())
|
| 200 |
+
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())
|
| 201 |
+
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())
|
| 202 |
+
ax.set_xlim(xlo, xhi); ax.set_yticks([])
|
| 203 |
+
for s in ax.spines.values(): s.set_visible(False)
|
| 204 |
+
ax.spines['bottom'].set_visible(True); ax.spines['bottom'].set_color(COLORS['grid']); ax.tick_params(colors=COLORS['text_dim'], width=1)
|
| 205 |
+
ax.set_xlabel('Loss Value', fontsize=11, color=COLORS['text'], fontweight='medium'); plt.tight_layout(pad=0.5)
|
| 206 |
+
return fig
|
| 207 |
|
| 208 |
def fig_auc_bar():
|
| 209 |
names, vals, clrs = [], [], []
|
|
|
|
| 222 |
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)
|
| 223 |
plt.tight_layout(); return fig
|
| 224 |
|
| 225 |
+
def fig_radar():
|
| 226 |
+
ms = ['AUC', 'Atk Acc', 'Prec', 'Recall', 'F1', 'TPR@5%', 'TPR@1%', 'Gap']
|
| 227 |
+
mk = ['auc', 'attack_accuracy', 'precision', 'recall', 'f1', 'tpr_at_5fpr', 'tpr_at_1fpr', 'loss_gap']
|
| 228 |
+
N = len(ms); ag = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0]
|
| 229 |
+
fig, axes = plt.subplots(1, 2, figsize=(CHART_W + 2, 7), subplot_kw=dict(polar=True)); fig.patch.set_facecolor('white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
ls_cfgs = [("Baseline", "baseline", '#F04438'), ("LS(ε=0.02)", "smooth_eps_0.02", '#B2DDFF'), ("LS(ε=0.05)", "smooth_eps_0.05", '#84CAFF'), ("LS(ε=0.1)", "smooth_eps_0.1", '#2E90FA'), ("LS(ε=0.2)", "smooth_eps_0.2", '#7A5AF8')]
|
| 232 |
+
op_cfgs = [("Baseline", "baseline", '#F04438'), ("OP(σ=0.005)", "perturbation_0.005", '#A6F4C5'), ("OP(σ=0.01)", "perturbation_0.01", '#6CE9A6'), ("OP(σ=0.015)", "perturbation_0.015", '#32D583'), ("OP(σ=0.02)", "perturbation_0.02", '#12B76A'), ("OP(σ=0.025)", "perturbation_0.025", '#039855'), ("OP(σ=0.03)", "perturbation_0.03", '#027A48')]
|
| 233 |
+
|
| 234 |
+
for ax_idx, (ax, cfgs, title) in enumerate([(axes[0], ls_cfgs, '多维防御指标雷达图:标签平滑 (LS)'), (axes[1], op_cfgs, '多维防御指标雷达图:输出扰动 (OP)')]):
|
| 235 |
+
ax.set_facecolor('white')
|
| 236 |
+
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]
|
| 237 |
+
for nm, ky, cl in cfgs:
|
| 238 |
+
v = [gm(ky, m_key) / mx[i] for i, m_key in enumerate(mk)]; v += [v[0]]
|
| 239 |
+
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)
|
| 240 |
+
ax.fill(ag, v, alpha=0.10 if ky == 'baseline' else 0.04, color=cl)
|
| 241 |
+
ax.set_xticks(ag[:-1]); ax.set_xticklabels(ms, fontsize=10, color=COLORS['text']); ax.set_yticklabels([])
|
| 242 |
+
if zh_font_title: ax.set_title(title, fontproperties=zh_font_title, pad=25)
|
| 243 |
+
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'])
|
| 244 |
+
ax.spines['polar'].set_color(COLORS['grid']); ax.grid(color=COLORS['grid'], alpha=0.5)
|
| 245 |
+
plt.tight_layout()
|
| 246 |
return fig
|
| 247 |
|
| 248 |
+
# 🌟 重构的无敌大直方图:基线 vs LS vs OP 横向直接对比
|
| 249 |
def fig_d3_dist_compare():
|
| 250 |
configs = [
|
| 251 |
("基线模型 (无防御)", "baseline", COLORS['danger'], None),
|
| 252 |
+
("标签平滑 (LS, ε=0.2)", "smooth_eps_0.2", COLORS['accent2'], None),
|
| 253 |
+
("输出扰动 (OP, σ=0.03)", "baseline", COLORS['success'], 0.03),
|
| 254 |
]
|
| 255 |
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
|
| 256 |
apply_light_style(fig, axes)
|
|
|
|
| 266 |
nm_losses = nm_losses + rn.normal(0, sigma, len(nm_losses))
|
| 267 |
all_v = np.concatenate([m_losses, nm_losses])
|
| 268 |
bins = np.linspace(all_v.min(), all_v.max(), 35)
|
| 269 |
+
# 使用原生的蓝色和红色表示成员和非成员,保持全场一致
|
| 270 |
+
ax.hist(m_losses, bins=bins, alpha=0.6, color=COLORS['accent'], label='成员 (Member)', density=True, edgecolor='white')
|
| 271 |
+
ax.hist(nm_losses, bins=bins, alpha=0.6, color=COLORS['danger'], label='非成员 (Non-Member)', density=True, edgecolor='white')
|
| 272 |
+
|
| 273 |
m_mean = np.mean(m_losses); nm_mean = np.mean(nm_losses)
|
| 274 |
gap = nm_mean - m_mean
|
| 275 |
ax.axvline(m_mean, color=COLORS['accent'], ls='--', lw=2, alpha=0.8)
|
|
|
|
| 288 |
if zh_font: fig.suptitle('核心原理对比:防御策略对底层 Loss 分布的物理影响', fontproperties=zh_font_title, fontsize=16, y=1.05)
|
| 289 |
plt.tight_layout(); return fig
|
| 290 |
|
| 291 |
+
def fig_roc_curves():
|
| 292 |
+
fig, axes = plt.subplots(1, 2, figsize=(16, 7)); apply_light_style(fig, axes)
|
| 293 |
+
ax = axes[0]; ls_colors = [COLORS['danger']] + COLORS['ls_colors']
|
| 294 |
+
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
| 295 |
+
if k not in full_losses: continue
|
| 296 |
+
m = np.array(full_losses[k]['member_losses']); nm = np.array(full_losses[k]['non_member_losses'])
|
| 297 |
+
y_true = np.concatenate([np.ones(len(m)), np.zeros(len(nm))]); y_scores = np.concatenate([-m, -nm])
|
| 298 |
+
fpr, tpr, _ = roc_curve(y_true, y_scores); auc_val = roc_auc_score(y_true, y_scores)
|
| 299 |
+
ax.plot(fpr, tpr, color=ls_colors[i], lw=2.5, label=f'{l} (AUC={auc_val:.4f})')
|
| 300 |
+
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5); set_ax_title(ax, 'ROC 曲线对比:标签平滑 (LS)')
|
| 301 |
+
if zh_font: ax.set_xlabel('假阳性率 (FPR)', fontproperties=zh_font); ax.set_ylabel('真阳性率 (TPR)', fontproperties=zh_font)
|
| 302 |
+
ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none')
|
| 303 |
+
|
| 304 |
+
ax = axes[1]
|
| 305 |
+
if 'baseline' in full_losses:
|
| 306 |
+
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])
|
| 307 |
+
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})')
|
| 308 |
+
for i, s in enumerate(OP_SIGMAS):
|
| 309 |
+
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)
|
| 310 |
+
ax.plot(fpr_p, tpr_p, color=COLORS['op_colors'][i], lw=2, label=f'OP(σ={s}) (AUC={auc_p:.4f})')
|
| 311 |
+
ax.plot([0,1], [0,1], '--', color=COLORS['text_dim'], lw=1.5); set_ax_title(ax, 'ROC 曲线对比:输出扰动 (OP)')
|
| 312 |
+
if zh_font: ax.set_xlabel('假阳性率 (FPR)', fontproperties=zh_font); ax.set_ylabel('真阳性率 (TPR)', fontproperties=zh_font)
|
| 313 |
+
ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none', loc='lower right'); plt.tight_layout()
|
| 314 |
return fig
|
| 315 |
|
| 316 |
+
def fig_tpr_at_low_fpr():
|
| 317 |
+
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']
|
| 318 |
+
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])
|
| 319 |
+
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])
|
| 320 |
+
x = range(len(labels_all)); ax = axes[0]; bars = ax.bar(x, tpr5_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 321 |
+
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'])
|
| 322 |
+
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
+
ax = axes[1]; bars = ax.bar(x, tpr1_all, color=colors_all, width=0.65, edgecolor='none', zorder=3)
|
| 325 |
+
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'])
|
| 326 |
+
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()
|
|
|
|
|
|
|
| 327 |
return fig
|
| 328 |
|
| 329 |
+
# 🌟 重构并放大的效用评估图
|
| 330 |
def fig_acc_bar():
|
| 331 |
names, vals, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 332 |
for i, (k, l) in enumerate(zip(LS_KEYS, LS_LABELS_PLOT)):
|
|
|
|
| 351 |
ax.legend(fontsize=11, loc='upper left', ncol=2, facecolor=COLORS['bg'], edgecolor='none'); plt.tight_layout()
|
| 352 |
return fig
|
| 353 |
|
| 354 |
+
def fig_auc_trend():
|
| 355 |
+
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]
|
| 356 |
+
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)
|
| 357 |
+
ax.fill_between(eps_vals, auc_vals, 0.5, alpha=0.08, color=COLORS['danger'])
|
| 358 |
+
set_ax_title(ax, '标签平滑:参数变化趋势分析'); ax.set_xlabel('Label Smoothing ε', fontsize=12)
|
| 359 |
+
if zh_font: ax.set_ylabel('隐私风险 (AUC)', fontproperties=zh_font, color=COLORS['danger']); ax2.set_ylabel('模型效用准确率 (%)', fontproperties=zh_font, color=COLORS['accent'])
|
| 360 |
+
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')
|
| 361 |
+
|
| 362 |
+
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')
|
| 363 |
+
ax2r = ax.twinx(); ax2r.axhline(bl_acc, color=COLORS['success'], ls='-', lw=2.5, alpha=0.8)
|
| 364 |
+
if zh_font: ax2r.set_ylabel(f'效用维持 = {bl_acc:.1f}% (无损耗)', fontproperties=zh_font, color=COLORS['success'])
|
| 365 |
+
ax2r.set_ylim(0,100); ax2r.tick_params(axis='y', labelcolor=COLORS['success']); ax2r.spines['right'].set_color(COLORS['success'])
|
| 366 |
+
set_ax_title(ax, '输出扰动:参数变化趋势分析'); ax.set_xlabel('Perturbation σ', fontsize=12); ax.legend(fontsize=10, facecolor=COLORS['bg'], edgecolor='none'); plt.tight_layout()
|
| 367 |
+
return fig
|
| 368 |
+
|
| 369 |
+
def fig_loss_gap_waterfall():
|
| 370 |
+
fig, ax = plt.subplots(figsize=(14, 6)); apply_light_style(fig, ax); names, gaps, clrs = [], [], []; ls_c = [COLORS['baseline']] + COLORS['ls_colors']
|
| 371 |
+
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])
|
| 372 |
+
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])
|
| 373 |
+
bars = ax.bar(range(len(names)), gaps, color=clrs, width=0.65, edgecolor='none', zorder=3)
|
| 374 |
+
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'])
|
| 375 |
+
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()
|
| 376 |
+
return fig
|
| 377 |
+
|
| 378 |
+
# ================================================================
|
| 379 |
+
# 回调函数
|
| 380 |
+
# ================================================================
|
| 381 |
+
def cb_sample(src):
|
| 382 |
+
pool = member_data if "训练集" in src else non_member_data
|
| 383 |
+
s = pool[np.random.randint(len(pool))]
|
| 384 |
+
m = s['metadata']
|
| 385 |
+
md = f"""
|
| 386 |
+
<table style="width:100%; border-collapse: collapse; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 387 |
+
<tr style="background-color: #F9F9F9;">
|
| 388 |
+
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">字段</th>
|
| 389 |
+
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">值</th>
|
| 390 |
+
</tr>
|
| 391 |
+
<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>
|
| 392 |
+
<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>
|
| 393 |
+
<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>
|
| 394 |
+
<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>
|
| 395 |
+
<tr><td style="padding: 10px; color: #1D1D1F;">类型</td><td style="padding: 10px; color: #1D1D1F;">{TYPE_CN.get(s.get('task_type',''), '')}</td></tr>
|
| 396 |
+
</table>
|
| 397 |
+
"""
|
| 398 |
+
return md, clean_text(s.get('question', '')), clean_text(s.get('answer', ''))
|
| 399 |
+
|
| 400 |
+
ATK_CHOICES = (
|
| 401 |
+
["基线模型 (Baseline)"] +
|
| 402 |
+
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
| 403 |
+
[f"输出扰动 (σ={s})" for s in OP_SIGMAS]
|
| 404 |
+
)
|
| 405 |
+
ATK_MAP = {"基线模型 (Baseline)": "baseline"}
|
| 406 |
+
for e in [0.02, 0.05, 0.1, 0.2]: ATK_MAP[f"标签平滑 (ε={e})"] = f"smooth_eps_{e}"
|
| 407 |
+
for s in OP_SIGMAS: ATK_MAP[f"输出扰动 (σ={s})"] = f"perturbation_{s}"
|
| 408 |
+
|
| 409 |
+
def cb_attack(idx, src, target):
|
| 410 |
+
is_mem = "训练集" in src
|
| 411 |
+
pool = member_data if is_mem else non_member_data
|
| 412 |
+
idx = min(int(idx), len(pool)-1)
|
| 413 |
+
sample = pool[idx]
|
| 414 |
+
key = ATK_MAP.get(target, "baseline")
|
| 415 |
+
is_op = key.startswith("perturbation_")
|
| 416 |
+
|
| 417 |
+
if is_op:
|
| 418 |
+
sigma = float(key.split("_")[1])
|
| 419 |
+
fr = full_losses.get('baseline', {})
|
| 420 |
+
lk = 'member_losses' if is_mem else 'non_member_losses'
|
| 421 |
+
ll = fr.get(lk, [])
|
| 422 |
+
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))
|
| 423 |
+
np.random.seed(idx*1000 + int(sigma*10000))
|
| 424 |
+
loss = base_loss + np.random.normal(0, sigma)
|
| 425 |
+
|
| 426 |
+
mm = gm(key, "member_loss_mean", 0.19)
|
| 427 |
+
nm_m = gm(key, "non_member_loss_mean", 0.20)
|
| 428 |
+
ms = gm(key, "member_loss_std", np.sqrt(0.03**2 + sigma**2))
|
| 429 |
+
ns = gm(key, "non_member_loss_std", np.sqrt(0.03**2 + sigma**2))
|
| 430 |
+
auc_v = gm(key, "auc")
|
| 431 |
+
lbl = f"OP(σ={sigma})"
|
| 432 |
+
else:
|
| 433 |
+
info = mia_results.get(key, mia_results.get('baseline', {}))
|
| 434 |
+
fr = full_losses.get(key, full_losses.get('baseline', {}))
|
| 435 |
+
lk = 'member_losses' if is_mem else 'non_member_losses'
|
| 436 |
+
ll = fr.get(lk, [])
|
| 437 |
+
loss = ll[idx] if idx < len(ll) else float(np.random.normal(info.get('member_loss_mean',0.19), 0.02))
|
| 438 |
+
mm = info.get('member_loss_mean', 0.19); nm_m = info.get('non_member_loss_mean', 0.20)
|
| 439 |
+
ms = info.get('member_loss_std', 0.03); ns = info.get('non_member_loss_std', 0.03)
|
| 440 |
+
auc_v = info.get('auc', 0)
|
| 441 |
+
lbl = "Baseline" if key == "baseline" else f"LS(ε={key.replace('smooth_eps_','')})"
|
| 442 |
+
|
| 443 |
+
thr = (mm + nm_m) / 2
|
| 444 |
+
pred = loss < thr
|
| 445 |
+
correct = pred == is_mem
|
| 446 |
+
gauge = fig_gauge(loss, mm, nm_m, thr, ms, ns)
|
| 447 |
+
|
| 448 |
+
pl = "🔴 训练成员" if pred else "🟢 非训练成员"
|
| 449 |
+
al = "🔴 训练成员" if is_mem else "🟢 非训练成员"
|
| 450 |
+
|
| 451 |
+
if correct and pred and is_mem:
|
| 452 |
+
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>"
|
| 453 |
+
elif correct:
|
| 454 |
+
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>"
|
| 455 |
+
else:
|
| 456 |
+
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>"
|
| 457 |
+
|
| 458 |
+
table_html = f"""
|
| 459 |
+
<table style="width:100%; border-collapse: collapse; margin-top: 10px; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 460 |
+
<thead style="background-color: #F9F9F9;">
|
| 461 |
+
<tr>
|
| 462 |
+
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">项目</th>
|
| 463 |
+
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">攻击者判定</th>
|
| 464 |
+
<th style="padding: 10px; text-align: left; color: #86868B; font-weight: 600; border-bottom: 1px solid #E2E8F0;">真实身份</th>
|
| 465 |
+
</tr>
|
| 466 |
+
</thead>
|
| 467 |
+
<tbody>
|
| 468 |
+
<tr>
|
| 469 |
+
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">身份</td>
|
| 470 |
+
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{pl}</td>
|
| 471 |
+
<td style="padding: 10px; color: #1D1D1F; border-bottom: 1px solid #E2E8F0;">{al}</td>
|
| 472 |
+
</tr>
|
| 473 |
+
<tr>
|
| 474 |
+
<td style="padding: 10px; color: #1D1D1F;">Loss / 阈值</td>
|
| 475 |
+
<td style="padding: 10px; color: #1D1D1F;">Loss: {loss:.4f}</td>
|
| 476 |
+
<td style="padding: 10px; color: #1D1D1F;">阈值: {thr:.4f}</td>
|
| 477 |
+
</tr>
|
| 478 |
+
</tbody>
|
| 479 |
+
</table>
|
| 480 |
+
"""
|
| 481 |
+
|
| 482 |
+
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
|
| 483 |
+
qtxt = f"**样本 #{idx}**\n\n" + clean_text(sample.get('question',''))[:500]
|
| 484 |
+
return qtxt, gauge, res
|
| 485 |
+
|
| 486 |
+
EVAL_CHOICES = (
|
| 487 |
+
["基线模型"] +
|
| 488 |
+
[f"标签平滑 (ε={e})" for e in [0.02, 0.05, 0.1, 0.2]] +
|
| 489 |
+
[f"输出扰动 (σ={s})" for s in OP_SIGMAS]
|
| 490 |
+
)
|
| 491 |
+
EVAL_KEY_MAP = {"基线模型": "baseline"}
|
| 492 |
+
for e in [0.02, 0.05, 0.1, 0.2]: EVAL_KEY_MAP[f"标签平滑 (ε={e})"] = f"smooth_eps_{e}"
|
| 493 |
+
for s in OP_SIGMAS: EVAL_KEY_MAP[f"输出扰动 (σ={s})"] = "baseline"
|
| 494 |
+
|
| 495 |
+
def cb_eval(model_choice):
|
| 496 |
+
k = EVAL_KEY_MAP.get(model_choice, "baseline")
|
| 497 |
+
acc = gu(k) if "输出扰动" not in model_choice else bl_acc
|
| 498 |
+
q = EVAL_POOL[np.random.randint(len(EVAL_POOL))]
|
| 499 |
+
ok = q.get(k, q.get('baseline', False))
|
| 500 |
+
ic = "✅ 正确" if ok else "❌ 错误"
|
| 501 |
+
note = "\n\n> 输出扰动不改变模型参数,准确率与基线一致。" if "输出扰动" in model_choice else ""
|
| 502 |
+
table_html = f"""
|
| 503 |
+
<table style="width:100%; border-collapse: collapse; margin-top: 15px; border: 1px solid #E2E8F0; border-radius: 8px; overflow: hidden;">
|
| 504 |
+
<tbody>
|
| 505 |
+
<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>
|
| 506 |
+
<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>
|
| 507 |
+
<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>
|
| 508 |
+
<tr><td style="padding: 12px; color: #86868B; font-weight: 600;">判定</td><td style="padding: 12px; color: #1D1D1F;">{ic}</td></tr>
|
| 509 |
+
</tbody>
|
| 510 |
+
</table>
|
| 511 |
+
"""
|
| 512 |
+
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)
|
| 513 |
+
|
| 514 |
def build_full_table():
|
| 515 |
rows = []
|
| 516 |
for k, l in zip(LS_KEYS, LS_LABELS_MD):
|
|
|
|
| 527 |
return header + "\n" + "\n".join(rows)
|
| 528 |
|
| 529 |
# ================================================================
|
| 530 |
+
# CSS - 简约苹果风
|
| 531 |
# ================================================================
|
| 532 |
CSS = """
|
| 533 |
:root { --primary-blue: #007AFF; --bg-light: #F5F5F7; --card-bg: #FFFFFF; --text-dark: #1D1D1F; --text-gray: #86868B; --border-color: #D2D2D7; }
|
|
|
|
| 540 |
.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; }
|
| 541 |
.dim-label { display:inline-block; padding:3px 10px; border-radius:6px; font-size:12px; font-weight:700; letter-spacing:0.05em; margin-right:8px; }
|
| 542 |
.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; }
|
| 543 |
+
footer { display: none !important; }
|
| 544 |
"""
|
| 545 |
|
| 546 |
# ================================================================
|
|
|
|
| 574 |
* 🛡️ **输出扰动 (Output Perturbation, 推理期)**:给 AI 的输出加上“变声器”。强行混入高斯噪声,让攻击者看到的 Loss 忽高忽低,彻底失灵。
|
| 575 |
""")
|
| 576 |
|
| 577 |
+
# 恢复实验体系总览图
|
| 578 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png")):
|
| 579 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo4_overview_cn_final.png"), label="实验体系总览图", show_label=False)
|
| 580 |
|
| 581 |
+
# 恢复首页图案数据卡片
|
| 582 |
gr.HTML(f"""<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:20px;margin:30px 0;">
|
| 583 |
<div class="card-wrap" style="text-align:center;">
|
| 584 |
<div style="font-size:32px;font-weight:700;color:{COLORS['accent']};margin-bottom:8px;">5</div>
|
|
|
|
| 602 |
</div>
|
| 603 |
</div>""")
|
| 604 |
|
| 605 |
+
# 恢复首页大表格
|
| 606 |
with gr.Accordion("📋 查阅全部 11组模型 × 8大维度 详细汇总表", open=True):
|
| 607 |
gr.Markdown(build_full_table())
|
| 608 |
|
|
|
|
| 646 |
if os.path.exists(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png")):
|
| 647 |
gr.Image(os.path.join(BASE_DIR, "figures", "algo3_output_perturbation.png"), show_label=False)
|
| 648 |
|
| 649 |
+
with gr.Tab("🎯 攻击验证"):
|
| 650 |
+
gr.Markdown("## 🕵️ 成员推理攻击交互演示\n\n配置攻击目标与数据源,系统将执行 Loss 计算并映射判定边界。")
|
| 651 |
+
with gr.Row():
|
| 652 |
+
with gr.Column(scale=1):
|
| 653 |
+
a_t = gr.Dropdown(choices=ATK_CHOICES, value=ATK_CHOICES[0], label="🎯 选择被攻击模型", interactive=True)
|
| 654 |
+
a_s = gr.Radio(["成员数据(训练集)", "非成员数据(测试集)"], value="成员数据(训练集)", label="📂 输入数据源")
|
| 655 |
+
a_i = gr.Slider(0, 999, step=1, value=12, label="📌 定位样本 ID")
|
| 656 |
+
a_b = gr.Button("⚡ 执行成员推理攻击", variant="primary")
|
| 657 |
+
a_qt = gr.HTML()
|
| 658 |
+
with gr.Column(scale=2):
|
| 659 |
+
a_g = gr.Plot(label="Loss位置判定 (Decision Boundary)")
|
| 660 |
+
a_r = gr.HTML()
|
| 661 |
+
a_b.click(cb_attack, [a_i, a_s, a_t], [a_qt, a_g, a_r])
|
| 662 |
+
|
| 663 |
with gr.Tab("🛡️ 五维度攻防分析"):
|
| 664 |
gr.Markdown("## 多维度攻防效果完整论证")
|
| 665 |
|
|
|
|
| 684 |
> **LS 的防御本质:** 随着 ε 增大,两座山峰趋于重合,均值差距缩小到了 {gm('smooth_eps_0.2','loss_gap'):.4f}。这是“物理抹除”了模型记忆。
|
| 685 |
> **OP 的防御本质:** 均值差距未变,但高斯噪声导致分布变得极其扁平宽阔,红蓝区域被完全搅混,蒙蔽了攻击者的双眼。
|
| 686 |
""")
|
| 687 |
+
gr.Plot(value=fig_d3_dist_compare())
|
|
|
|
| 688 |
gr.Plot(value=fig_loss_gap_waterfall())
|
| 689 |
+
|
| 690 |
+
with gr.Accordion("📉 查看所有模型详细 Loss 分布直方图", open=False):
|
| 691 |
+
gr.Plot(value=fig_loss_dist())
|
| 692 |
+
gr.Plot(value=fig_perturb_dist())
|
| 693 |
|
| 694 |
gr.HTML('<div style="margin:40px 0 8px;"><span class="dim-label dim4">D4</span><strong style="font-size:18px;color:#1D2939;">无死角压制维度 — 证明“防御没有偏科”</strong></div>')
|
| 695 |
gr.Markdown("""\
|
|
|
|
| 698 |
""")
|
| 699 |
gr.Plot(value=fig_radar())
|
| 700 |
|
| 701 |
+
# ================================================================
|
| 702 |
+
# 详细参数表格完整保留
|
| 703 |
+
# ================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
with gr.Accordion("📖 查看详细防御参数表格", open=False):
|
| 705 |
detail_md = ""
|
| 706 |
for eps in [0.02, 0.05, 0.1, 0.2]:
|
|
|
|
| 738 |
gr.Markdown(detail_md)
|
| 739 |
|
| 740 |
with gr.Tab("⚖️ 效用评估"):
|
| 741 |
+
gr.HTML('<div style="margin:20px 0 8px;"><span class="dim-label dim5">D5</span><strong style="font-size:18px;color:#1D2939;">落地代价维度 — 证明“隐私与效用的完美平衡”</strong></div>')
|
| 742 |
+
gr.Markdown(f"""\
|
| 743 |
+
> 抛开模型能力谈安全是纸上谈兵。
|
| 744 |
+
> **输出扰动 (OP):** 实现了完美的 **零效用损耗(维持 {bl_acc:.1f}%)**。
|
| 745 |
+
> **标签平滑 (LS):** 打出了惊艳的 **双赢 (Win-Win)**,效用曲线逆势上扬到了 {gu('smooth_eps_0.2'):.1f}%(不仅保护了隐私,还治好了过拟合)。
|
| 746 |
+
""")
|
| 747 |
+
gr.Plot(value=fig_auc_trend())
|
| 748 |
+
|
| 749 |
+
# 恢复放大版的效用柱状图和散点图
|
| 750 |
+
gr.Markdown("## 📊 模型测试集准确率分析")
|
| 751 |
with gr.Row():
|
| 752 |
with gr.Column(): gr.Plot(value=fig_acc_bar())
|
| 753 |
with gr.Column(): gr.Plot(value=fig_tradeoff())
|
| 754 |
|
| 755 |
+
gr.Markdown("### 🧪 在线抽题演示")
|
| 756 |
with gr.Row():
|
| 757 |
with gr.Column(scale=1):
|
| 758 |
e_m = gr.Dropdown(choices=EVAL_CHOICES, value="基线模型", label="🤖 选择测试模型", interactive=True)
|
|
|
|
| 781 |
| σ 参数 | AUC | AUC降幅 | 效用 |
|
| 782 |
|---|---|---|---|
|
| 783 |
| σ=0.005 | {gm('perturbation_0.005','auc'):.4f} | {bl_auc-gm('perturbation_0.005','auc'):.4f} | {bl_acc:.1f}% |
|
| 784 |
+
| σ=0.01 | {gm('perturbation_0.01','auc'):.4f} | {bl_auc-gm('perturbation_0.01','auc'):.4f} | {bl_acc:.1f}% |
|
| 785 |
+
| σ=0.015 | {gm('perturbation_0.015','auc'):.4f} | {bl_auc-gm('perturbation_0.015','auc'):.4f} | {bl_acc:.1f}% |
|
| 786 |
+
| σ=0.02 | {gm('perturbation_0.02','auc'):.4f} | {bl_auc-gm('perturbation_0.02','auc'):.4f} | {bl_acc:.1f}% |
|
| 787 |
+
| σ=0.025 | {gm('perturbation_0.025','auc'):.4f} | {bl_auc-gm('perturbation_0.025','auc'):.4f} | {bl_acc:.1f}% |
|
| 788 |
| σ=0.03 | {gm('perturbation_0.03','auc'):.4f} | {bl_auc-gm('perturbation_0.03','auc'):.4f} | {bl_acc:.1f}% |
|
| 789 |
**核心发现:零效用损失,不需重新训练,即插即用。**
|
| 790 |
|