MuskAI commited on
Commit
0f94be1
·
verified ·
1 Parent(s): a299d07

Add original-image LTF Gradio demo

Browse files
Files changed (5) hide show
  1. README.md +15 -7
  2. app.py +146 -77
  3. attention.py +46 -24
  4. presentation.py +138 -0
  5. styles.css +38 -0
README.md CHANGED
@@ -62,22 +62,28 @@ The web UI and its `predict` API both call the same original-file inference path
62
 
63
  ## Attention and tile inspection
64
 
65
- The Detect button additionally requests the model's existing `return_viz`
66
  outputs during the same forward pass. Temporary observation hooks collect the
67
  eight L1 across-tile aggregation heads. No model parameters are changed.
68
 
69
  - Exact, numbered local crop boxes, including edge-aligned overlap and small-image padding.
70
  - L1 tile-attention overlay: mean or any of the eight aggregation heads.
71
  - L2 local-view overlay with the global-view attention weight listed separately.
72
- - Top four local tiles, original crops paired with their 16×16 MIL patch attention.
 
 
 
73
  - A tile selector for inspecting any local tile without rerunning inference.
74
  - Exact view-weight table, embedding fusion gate and read-only model/inference parameters.
75
  - Historical training settings from the original saved configuration, explicitly labeled Training only.
76
 
77
- Colors use a separate min-max scale for each map (blue→yellow is low→high).
78
- Overlapping image pixels use the mean of covering tile weights. A patch map is
79
- normalized within its own tile. Raw normalized view weights are available in
80
- the table and prediction JSON; colors cannot be compared across images or tiles.
 
 
 
81
  These are model attention values, not pixel-level forgery probabilities or
82
  causal explanations. The eight selectable heads belong to the L1 across-tile
83
  aggregator; L1 patch pooling and L2 view pooling use gated MIL attention.
@@ -85,4 +91,6 @@ aggregator; L1 patch pooling and L2 view pooling use gated MIL attention.
85
  The original five-output `predict` API is preserved. The new `explain` API
86
  returns prediction results plus visualization images, tables and attention metadata.
87
  Changing a head or inspected tile uses per-session cached data, retained for up
88
- to an hour. The unchanged checkpoint is used for both endpoints.
 
 
 
62
 
63
  ## Attention and tile inspection
64
 
65
+ The Analyze button additionally requests the model's existing `return_viz`
66
  outputs during the same forward pass. Temporary observation hooks collect the
67
  eight L1 across-tile aggregation heads. No model parameters are changed.
68
 
69
  - Exact, numbered local crop boxes, including edge-aligned overlap and small-image padding.
70
  - L1 tile-attention overlay: mean or any of the eight aggregation heads.
71
  - L2 local-view overlay with the global-view attention weight listed separately.
72
+ - Four ranked local crop cards; clicking a card selects its patch inspector.
73
+ - Side-by-side original crop, patch overlay and interactive 16×16 weight matrix.
74
+ - Large score card and branch comparison, plus per-view attention ranking charts.
75
+ - Responsive desktop/mobile layout; replacing an image clears previous analysis.
76
  - A tile selector for inspecting any local tile without rerunning inference.
77
  - Exact view-weight table, embedding fusion gate and read-only model/inference parameters.
78
  - Historical training settings from the original saved configuration, explicitly labeled Training only.
79
 
80
+ Colors use a fixed log2 scale relative to uniform attention (`weight × N`):
81
+ 0.25×, 0.5×, 1×, 2× and 4×. Neutral gray represents the uniform reference;
82
+ blue is below and orange-red above that reference. Small differences are not
83
+ stretched into strong colors. L2 uses the count of all views, including Global,
84
+ for its reference. Overlap uses mean tile weight. Raw weights are available in
85
+ the ranking charts, weight table and prediction JSON. A displayed percentage is
86
+ an attention share, not a calibrated class probability.
87
  These are model attention values, not pixel-level forgery probabilities or
88
  causal explanations. The eight selectable heads belong to the L1 across-tile
89
  aggregator; L1 patch pooling and L2 view pooling use gated MIL attention.
 
91
  The original five-output `predict` API is preserved. The new `explain` API
92
  returns prediction results plus visualization images, tables and attention metadata.
93
  Changing a head or inspected tile uses per-session cached data, retained for up
94
+ to an hour. Changing the decision branch or threshold updates the result from
95
+ the cached scores. Uploading a new image clears previous results. The unchanged
96
+ checkpoint is used for both endpoints.
app.py CHANGED
@@ -25,6 +25,7 @@ import torch
25
  sys.path.insert(0, str(Path(__file__).absolute().parent))
26
  from attention import (capture_prediction, draw_grid, head_view, parameter_rows,
27
  patch_view, public_attention, tile_boxes, tile_overlay, top_tiles)
 
28
 
29
  LOG = logging.getLogger(__name__)
30
  MAX_PIXELS = int(os.environ.get('LTF_MAX_PIXELS', '16000000'))
@@ -147,16 +148,13 @@ def _predict_request(image_path, branch='fusion', threshold=0.5, with_attention=
147
  elapsed_seconds=round(time.perf_counter() - start, 3))
148
  assert result['views'] == views, 'Preview and model tile counts disagree'
149
  scores = result['probabilities'][branch]
150
- verdict = 'AI-generated / 生成图' if result['label'] else 'Real / 真实图'
151
- summary = (f'### {verdict}\n\n生成分数 / AI-generated score: '
152
- f'**{result["score_fake"]:.4f}** · threshold: **{float(threshold):.2f}**\n\n'
153
- f'{width:,} × {height:,} pixels · {views} views · '
154
- f'{result["elapsed_seconds"]:.2f} s (includes first-time model loading)')
155
  rows = [[name, result['probabilities'][name]['real'],
156
  result['probabilities'][name]['fake']]
157
  for name in ('fusion', 'level1', 'level2')]
158
  if state:
159
  result['attention'] = public_attention(state)
 
160
  return (preview, summary, {'Real': scores['real'], 'AI-generated': scores['fake']}, rows, result), state
161
 
162
 
@@ -173,14 +171,12 @@ def explain(image_path, branch='fusion', threshold=0.5):
173
  raise gr.Error('Attention and original image tile counts disagree.')
174
  grid = draw_grid(state['path'], state['boxes'])
175
  l1_image, l1_description = head_view(state, 'mean')
176
- l2_image = tile_overlay(state['path'], state['boxes'], attention['l2'][1:])
177
- description = (f'**L2 global-view weight: {attention["l2"][0]:.6f}** · '
178
- f'local-view weights sum: {float(attention["l2"][1:].sum()):.6f}.\n\n'
179
- 'L2 图仅着色局部分块;全局权重单列,不铺满整张图。'
180
- ' / The L2 overlay shows local views only; the global weight is listed separately.\n\n'
181
- f'Fusion embedding gate: L1 × **{attention["gate"]:.4f}**, '
182
- f'L2 × **{1 - attention["gate"]:.4f}**. '
183
- '这是特征缩放系数,不是分支概率 / These scale embeddings, not branch probabilities.')
184
  weight_rows = [['Global', 'whole image', None, float(attention['l2'][0])]]
185
  weight_rows.extend([f'T{i + 1}', str(list(box)), float(attention['l1'][i]), float(attention['l2'][i + 1])]
186
  for i, box in enumerate(state['boxes']))
@@ -190,81 +186,151 @@ def explain(image_path, branch='fusion', threshold=0.5):
190
  state['parameters'], weight_rows, state,
191
  gr.Dropdown(choices=[str(i + 1) for i in range(count)], value=str(selected)),
192
  tile, patch, patch_description,
193
- gr.Dropdown(choices=['mean'] + [f'head_{i + 1}' for i in range(attention['l1_heads'].shape[0])], value='mean'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
 
196
  def build_demo():
197
- with gr.Blocks(title='LTF · Image Detector', delete_cache=(3600, 3600)) as demo:
198
- gr.Markdown('# LTF · Image Detector\n上传原图,测试 LTF v3.3 / Test your original image with LTF v3.3.')
199
- gr.Markdown('默认 Fusion 分支、阈值 0.5。保留原始图像和完整分块。'
200
- ' / Default: fusion, threshold 0.5. Original pixels and all tiles are preserved.')
201
  with gr.Row():
202
- with gr.Column():
203
- upload = gr.File(label='上传原图 / Original image file', type='filepath',
204
- file_count='single', file_types=['image'])
205
- with gr.Accordion('高级选项 / Advanced', open=False):
206
- branch = gr.Dropdown(['fusion', 'level1', 'level2'], value='fusion', label='Branch')
207
- threshold = gr.Slider(0, 1, value=0.5, step=0.01, label='Decision threshold')
208
- button = gr.Button('开始检测 / Detect', variant='primary')
209
- gr.Markdown(f'在线测试上限 / Demo limits: {MAX_PIXELS:,} pixels, '
210
- f'{MAX_VIEWS} views. CPU inference may take time for large images.')
211
- with gr.Column():
212
- preview = gr.Image(label='图片预览 / Preview', interactive=False)
213
- summary = gr.Markdown('等待图片 / Ready for an image.')
214
- scores = gr.Label(label='模型分数 / Model scores', num_top_classes=2)
215
- with gr.Accordion('分支分数与 JSON / Branch scores and JSON', open=False):
216
- table = gr.Dataframe(headers=['Branch', 'Real score', 'AI-generated score'],
217
- datatype=['str', 'number', 'number'], interactive=False)
218
- details = gr.JSON(label='Prediction')
219
  state = gr.State(value=None, time_to_live=3600)
220
- with gr.Accordion('分块与 MIL Attention / Tiles and model attention', open=True):
221
- gr.Markdown('以下权重直接来自本次模型前向计算。Attention 是相对关注程度,'
222
- '不能直接作为伪造区域或因果解释。'
223
- ' / These are actual forward-pass weights, not forgery probabilities or causal explanations.')
224
- with gr.Tabs():
225
- with gr.Tab('原图分块 / Tile grid'):
226
- grid = gr.Image(label='224×224 local tiles · T1…Tn · edge-aligned', interactive=False)
227
- gr.Markdown('分块编号按行排列,边缘对齐时允许重叠。全局视图独立于局部分块。'
228
- ' / Row-major IDs; edge-aligned tiles may overlap. The global view is separate.')
229
- with gr.Tab('L1 分块注意力'):
230
- head = gr.Dropdown(['mean'], value='mean', label='Across-tile aggregation head')
231
- l1_image = gr.Image(label='L1 tile attention', interactive=False)
232
- l1_description = gr.Markdown()
233
- with gr.Tab('L2 视图注意力'):
234
- l2_image = gr.Image(label='L2 local-view attention', interactive=False)
235
- attention_description = gr.Markdown()
236
- weight_table = gr.Dataframe(headers=['View', 'Original crop box', 'L1 weight', 'L2 weight'],
237
- datatype=['str', 'str', 'number', 'number'], interactive=False)
238
- with gr.Tab('Top-K 与块内 Patch Attention'):
239
- gallery = gr.Gallery(label='Top 4 L1 tiles: original + within-tile patch attention', columns=4,
240
- object_fit='contain', height='auto')
241
- tile_number = gr.Dropdown(choices=[], label='Inspect local tile / 分块编号', allow_custom_value=False)
242
  with gr.Row():
243
- selected_tile = gr.Image(label='Original 224×224 crop', interactive=False)
244
- selected_patch = gr.Image(label='MIL patch attention (16×16)', interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  patch_description = gr.Markdown()
246
- gr.Markdown('热图分别归一化:蓝色低、黄色高,不能跨图片按颜色比较。'
247
- '重叠像素显示覆盖分块的平均权重,精确权重见表格。'
248
- ' / Heatmaps use separate min-max scales; overlap uses mean weights.')
249
- with gr.Accordion('模型与超参数 / Model and hyperparameters (read only)', open=False):
 
 
 
 
250
  parameters = gr.Dataframe(headers=['Scope', 'Parameter', 'Value'],
251
  datatype=['str', 'str', 'str'], interactive=False)
252
- gr.Markdown('Training only 是历史训练配置;Cross-shuffle、随机增强和 dropout '
253
- '在本次推理中不启用。Head 数量与网络结构随权重固定。')
254
- button.click(explain, [upload, branch, threshold],
255
- [preview, summary, scores, table, details, grid, l1_image, l1_description,
256
- l2_image, attention_description, gallery, parameters, weight_table, state,
257
- tile_number, selected_tile, selected_patch, patch_description, head],
258
- api_name='explain', concurrency_limit=1, concurrency_id='ltf-inference')
259
- head.change(head_view, [state, head], [l1_image, l1_description], api_name=False)
260
- tile_number.change(patch_view, [state, tile_number],
261
- [selected_tile, selected_patch, patch_description], api_name=False)
262
- # Preserve the original prediction endpoint for existing API clients.
 
 
 
 
 
263
  api_button = gr.Button(visible=False)
264
  api_button.click(predict, [upload, branch, threshold], [preview, summary, scores, table, details],
265
  api_name='predict', concurrency_limit=1, concurrency_id='ltf-inference')
266
- gr.Markdown('分数是模型输出,不能单独作为图片真伪的证明。'
267
- ' / Model scores are not calibrated probabilities or proof of authenticity.')
268
  return demo.queue(max_size=16, default_concurrency_limit=1)
269
 
270
 
@@ -272,4 +338,7 @@ if __name__ == '__main__':
272
  logging.basicConfig(level=logging.INFO)
273
  build_demo().launch(server_name=os.environ.get('GRADIO_SERVER_NAME', '0.0.0.0'),
274
  server_port=int(os.environ.get('PORT', '7860')), share=False,
275
- max_file_size='30mb')
 
 
 
 
25
  sys.path.insert(0, str(Path(__file__).absolute().parent))
26
  from attention import (capture_prediction, draw_grid, head_view, parameter_rows,
27
  patch_view, public_attention, tile_boxes, tile_overlay, top_tiles)
28
+ from presentation import EMPTY_RESULT, HEADER, patch_matrix, result_card, stats_card, weight_chart
29
 
30
  LOG = logging.getLogger(__name__)
31
  MAX_PIXELS = int(os.environ.get('LTF_MAX_PIXELS', '16000000'))
 
148
  elapsed_seconds=round(time.perf_counter() - start, 3))
149
  assert result['views'] == views, 'Preview and model tile counts disagree'
150
  scores = result['probabilities'][branch]
151
+ summary = result_card(result)
 
 
 
 
152
  rows = [[name, result['probabilities'][name]['real'],
153
  result['probabilities'][name]['fake']]
154
  for name in ('fusion', 'level1', 'level2')]
155
  if state:
156
  result['attention'] = public_attention(state)
157
+ state['result'] = dict(result)
158
  return (preview, summary, {'Real': scores['real'], 'AI-generated': scores['fake']}, rows, result), state
159
 
160
 
 
171
  raise gr.Error('Attention and original image tile counts disagree.')
172
  grid = draw_grid(state['path'], state['boxes'])
173
  l1_image, l1_description = head_view(state, 'mean')
174
+ l2_image = tile_overlay(state['path'], state['boxes'], attention['l2'][1:], reference_count=count+1)
175
+ description = (f'全局视图权重 **{attention["l2"][0]*100:.2f}%**;局部视图合计 '
176
+ f'**{float(attention["l2"][1:].sum())*100:.2f}%**。'
177
+ '全局权重单列,叠图仅表示局部分块。\n\n'
178
+ f'特征融合系数:L1 × {attention["gate"]:.4f},L2 × {1-attention["gate"]:.4f}。'
179
+ '它们缩放分支特征,不是分支概率。')
 
 
180
  weight_rows = [['Global', 'whole image', None, float(attention['l2'][0])]]
181
  weight_rows.extend([f'T{i + 1}', str(list(box)), float(attention['l1'][i]), float(attention['l2'][i + 1])]
182
  for i, box in enumerate(state['boxes']))
 
186
  state['parameters'], weight_rows, state,
187
  gr.Dropdown(choices=[str(i + 1) for i in range(count)], value=str(selected)),
188
  tile, patch, patch_description,
189
+ gr.Dropdown(choices=[('平均(8 Heads)', 'mean')] + [(f'Head {i + 1}', f'head_{i + 1}')
190
+ for i in range(attention['l1_heads'].shape[0])], value='mean'),
191
+ weight_chart(state), weight_chart(state, level='l2'), patch_matrix(state, str(selected)),
192
+ stats_card(state), gr.update(visible=True))
193
+
194
+
195
+ def inspect_head(state, selection):
196
+ image, description = head_view(state, selection)
197
+ return image, description, weight_chart(state, selection)
198
+
199
+
200
+ def inspect_patch(state, tile_number):
201
+ tile, image, description = patch_view(state, tile_number)
202
+ return tile, image, description, patch_matrix(state, tile_number)
203
+
204
+
205
+ def select_top_tile(state, event: gr.SelectData):
206
+ if not state:
207
+ return gr.skip()
208
+ index = event.index[0] if isinstance(event.index, (tuple, list)) else event.index
209
+ order = sorted(range(len(state['boxes'])), key=lambda i: -float(state['attention']['l1'][i]))[:4]
210
+ return str(order[int(index)] + 1)
211
+
212
+
213
+ def update_decision(state, branch, threshold):
214
+ """Threshold and branch selection use cached logits, without another forward."""
215
+ if not state:
216
+ return tuple(gr.skip() for _ in range(5))
217
+ result = dict(state['result'])
218
+ score = result['probabilities'][branch]['fake']
219
+ label = int(score >= float(threshold))
220
+ result.update(branch=branch, threshold=float(threshold), score_fake=score,
221
+ label=label, prediction='fake' if label else 'real')
222
+ scores = result['probabilities'][branch]
223
+ rows = [[name, result['probabilities'][name]['real'], result['probabilities'][name]['fake']]
224
+ for name in ('fusion', 'level1', 'level2')]
225
+ parameters = [list(row) for row in state['parameters']]
226
+ parameters[0][2] = f'{branch} / {float(threshold):.2f}'
227
+ return result_card(result), {'Real': scores['real'], 'AI-generated': scores['fake']}, rows, result, parameters
228
+
229
+
230
+ def reset_upload(path):
231
+ image = None
232
+ if path:
233
+ try:
234
+ source, *_ = validate_upload(path)
235
+ with Image.open(source) as original:
236
+ image = original.convert('RGB')
237
+ image.thumbnail((1024, 1024))
238
+ except gr.Error:
239
+ pass
240
+ return image, EMPTY_RESULT, None, [], None, [], None, gr.update(visible=False)
241
 
242
 
243
  def build_demo():
244
+ with gr.Blocks(title='LTF · 图像检测与注意力分析', fill_width=True, delete_cache=(3600, 3600)) as demo:
245
+ gr.HTML(HEADER)
 
 
246
  with gr.Row():
247
+ with gr.Column(scale=3, min_width=260, elem_id='upload-panel'):
248
+ gr.Markdown('### 01 · 上传原图\n保留原始文件进行推理,预览缩放不影响模型输入。')
249
+ upload = gr.File(label='图片文件', type='filepath', file_count='single', file_types=['image'], height=150)
250
+ gr.ClearButton([upload], value='更换图片', size='sm')
251
+ branch = gr.Dropdown([('Fusion · 融合分支', 'fusion'), ('L1 · 局部分支', 'level1'),
252
+ ('L2 · 视图分支', 'level2')], value='fusion', label='预测分支', elem_id='branch-select')
253
+ threshold = gr.Slider(0, 1, value=0.5, step=0.01, label='判定阈值', elem_id='threshold-select')
254
+ button = gr.Button('开始分析', variant='primary', elem_id='run-button')
255
+ gr.Markdown(f'上限 {MAX_PIXELS/1_000_000:g} MP · {MAX_VIEWS} 个视图 · 30 MB。大图在 CPU 上处理较慢。')
256
+ with gr.Column(scale=4, min_width=300):
257
+ preview = gr.Image(label='原图预览', height=390, interactive=False,
258
+ buttons=['download', 'fullscreen'], elem_id='original-preview')
259
+ with gr.Column(scale=4, min_width=300):
260
+ summary = gr.HTML(EMPTY_RESULT, label='模型分析结果')
261
+ scores = gr.Label(label='模型分数', num_top_classes=2, visible=False)
 
 
262
  state = gr.State(value=None, time_to_live=3600)
263
+ with gr.Column(visible=False, elem_id='analysis-section') as analysis:
264
+ statistics = gr.HTML()
265
+ gr.Markdown('## 02 · 注意力分析\n热图使用统一的「均匀权重」参照;权重接近均匀时,颜色也保持接近。Attention 不代表伪造概率。')
266
+ with gr.Tabs(selected='l1', elem_id='analysis-tabs'):
267
+ with gr.Tab('L1 · 分块注意力', id='l1'):
268
+ with gr.Row():
269
+ with gr.Column(scale=7, min_width=320):
270
+ l1_image = gr.Image(label='L1 分块级注意力叠图', height=500, interactive=False,
271
+ buttons=['download', 'fullscreen'], elem_classes='attention-image')
272
+ l1_description = gr.Markdown()
273
+ with gr.Column(scale=5, min_width=300):
274
+ head = gr.Dropdown([('平均(8 Heads)', 'mean')], value='mean', label='聚合 Head', elem_id='head-select')
275
+ l1_chart = gr.HTML()
276
+ with gr.Tab('L2 · 视图权重', id='l2'):
 
 
 
 
 
 
 
 
277
  with gr.Row():
278
+ with gr.Column(scale=7, min_width=320):
279
+ l2_image = gr.Image(label='L2 局部视图注意力叠图', height=500, interactive=False,
280
+ buttons=['download', 'fullscreen'], elem_classes='attention-image')
281
+ attention_description = gr.Markdown()
282
+ with gr.Column(scale=5, min_width=300):
283
+ l2_chart = gr.HTML()
284
+ with gr.Tab('原图 · 分块位置', id='grid'):
285
+ grid = gr.Image(label='原始 224 × 224 分块边界', height=570, interactive=False,
286
+ buttons=['download', 'fullscreen'], elem_classes='attention-image')
287
+ gr.Markdown('编号按行排列,边缘对齐会产生重叠。分块很多时仅标注部分编号,完整坐标见下方数据表。')
288
+ with gr.Tab('分块 · Patch 细节', id='patch'):
289
+ gr.Markdown('### 按 L1 权重排列的前 4 个分块\n点击分块,查看其原始裁剪、注意力叠图和 Patch 权重。Top-4 仅用于展示,不参与预测。')
290
+ gallery = gr.Gallery(label='高权重分块', columns=4, object_fit='contain', height=250,
291
+ elem_id='top-crops', show_label=False, preview=False, allow_preview=False,
292
+ interactive=False, buttons=['download_all'])
293
+ tile_number = gr.Dropdown(choices=[], label='查看分块编号', allow_custom_value=False, elem_id='tile-select')
294
+ with gr.Row():
295
+ with gr.Column(min_width=250):
296
+ selected_tile = gr.Image(label='原始 224 × 224 裁剪', height=330, interactive=False,
297
+ buttons=['download', 'fullscreen'])
298
+ with gr.Column(min_width=250):
299
+ selected_patch = gr.Image(label='Patch 注意力叠图', height=330, interactive=False,
300
+ buttons=['download', 'fullscreen'])
301
+ with gr.Column(min_width=280):
302
+ patch_chart = gr.HTML()
303
  patch_description = gr.Markdown()
304
+ with gr.Accordion('精确权重、参数与原始输出', open=False, elem_id='technical-details'):
305
+ gr.Markdown('### 分支分数')
306
+ table = gr.Dataframe(headers=['Branch', 'Real score', 'Generated score'],
307
+ datatype=['str', 'number', 'number'], interactive=False)
308
+ gr.Markdown('### 视图权重\nL1 列为 8 Heads 平均;L2 的第一项为独立的全局视图。')
309
+ weight_table = gr.Dataframe(headers=['View', 'Original crop box', 'L1 mean weight', 'L2 weight'],
310
+ datatype=['str', 'str', 'number', 'number'], interactive=False)
311
+ gr.Markdown('### 模型与超参数\nTraining only 为历史训练配置;推理时不启用随机增强、Cross-shuffle 和 Dropout。')
312
  parameters = gr.Dataframe(headers=['Scope', 'Parameter', 'Value'],
313
  datatype=['str', 'str', 'str'], interactive=False)
314
+ details = gr.JSON(label='Prediction JSON')
315
+ run_event = button.click(explain, [upload, branch, threshold],
316
+ [preview, summary, scores, table, details, grid, l1_image, l1_description,
317
+ l2_image, attention_description, gallery, parameters, weight_table, state,
318
+ tile_number, selected_tile, selected_patch, patch_description, head,
319
+ l1_chart, l2_chart, patch_chart, statistics, analysis],
320
+ api_name='explain', concurrency_limit=1, concurrency_id='ltf-inference')
321
+ head.change(inspect_head, [state, head], [l1_image, l1_description, l1_chart], api_name=False)
322
+ tile_number.change(inspect_patch, [state, tile_number],
323
+ [selected_tile, selected_patch, patch_description, patch_chart], api_name=False)
324
+ gallery.select(select_top_tile, [state], [tile_number], api_name=False)
325
+ for control in (branch, threshold):
326
+ control.change(update_decision, [state, branch, threshold], [summary, scores, table, details, parameters],
327
+ api_name=False, queue=False, trigger_mode='always_last')
328
+ upload.change(reset_upload, [upload], [preview, summary, scores, table, details, parameters, state, analysis],
329
+ api_name=False, queue=False, cancels=[run_event])
330
  api_button = gr.Button(visible=False)
331
  api_button.click(predict, [upload, branch, threshold], [preview, summary, scores, table, details],
332
  api_name='predict', concurrency_limit=1, concurrency_id='ltf-inference')
333
+ gr.Markdown('LTF v3.3 · 原始权重与推理流程 · 可视化不改变模型分数', elem_id='footer-note')
 
334
  return demo.queue(max_size=16, default_concurrency_limit=1)
335
 
336
 
 
338
  logging.basicConfig(level=logging.INFO)
339
  build_demo().launch(server_name=os.environ.get('GRADIO_SERVER_NAME', '0.0.0.0'),
340
  server_port=int(os.environ.get('PORT', '7860')), share=False,
341
+ max_file_size='30mb', theme=gr.themes.Soft(primary_hue='teal', neutral_hue='slate',
342
+ font=['ui-sans-serif', 'system-ui', 'sans-serif']).set(
343
+ block_label_background_fill='#eef3f8', block_label_text_color='#445b72'),
344
+ css_paths=Path(__file__).with_name('styles.css'))
attention.py CHANGED
@@ -73,14 +73,21 @@ def relative_scale(values):
73
 
74
 
75
  def colors(values):
76
- """Continuous dark-blue → cyan → yellow scale; no probability semantics."""
77
  x = np.clip(np.asarray(values), 0, 1)
78
- stops = np.array([[35, 47, 110], [31, 164, 172], [253, 225, 77]], dtype=np.float32)
79
- first = np.minimum((x * 2).astype(int), 1)
80
- fraction = (x * 2 - first)[..., None]
 
81
  return (stops[first] * (1 - fraction) + stops[first + 1] * fraction).astype(np.uint8)
82
 
83
 
 
 
 
 
 
 
84
  def preview_image(path):
85
  with Image.open(path) as source:
86
  image = source.convert('RGB')
@@ -93,17 +100,20 @@ def draw_grid(path, boxes):
93
  with Image.open(path) as original:
94
  sx, sy = image.width / original.width, image.height / original.height
95
  draw = ImageDraw.Draw(image)
96
- font = ImageFont.load_default(size=14)
97
  labels = []
98
  for index, (x0, y0, x1, y1) in enumerate(boxes, 1):
99
  rect = (int(x0 * sx), int(y0 * sy), min(image.width - 1, int(x1 * sx) - 1),
100
  min(image.height - 1, int(y1 * sy) - 1))
101
- draw.rectangle(rect, outline=(255, 212, 74), width=2)
 
 
 
102
  label = f'T{index}'
103
  left, top = rect[0] + 3, rect[1] + 3
104
  # Edge alignment can place two rows just one pixel apart. Keep their
105
  # exact crop borders while moving text labels to avoid hiding earlier IDs.
106
- for candidate_top in range(top, max(top + 1, rect[3] - 15), 18):
107
  extent = draw.textbbox((left, candidate_top), label, font=font)
108
  if not any(left < old[2] + 3 and extent[2] + 3 > old[0]
109
  and candidate_top < old[3] + 3 and extent[3] + 3 > old[1]
@@ -112,12 +122,12 @@ def draw_grid(path, boxes):
112
  break
113
  extent = draw.textbbox((left, top), label, font=font)
114
  labels.append((left, top, extent[2], extent[3]))
115
- draw.rectangle((left - 1, top - 1, extent[2] + 2, extent[3] + 2), fill=(18, 28, 45))
116
  draw.text((left, top), label, fill='white', font=font)
117
  return image
118
 
119
 
120
- def tile_overlay(path, boxes, weights):
121
  image = preview_image(path)
122
  with Image.open(path) as original:
123
  sx, sy = image.width / original.width, image.height / original.height
@@ -130,8 +140,21 @@ def tile_overlay(path, boxes, weights):
130
  summed[y0:y1, x0:x1] += float(weight)
131
  coverage[y0:y1, x0:x1] += 1
132
  averaged = summed / np.maximum(coverage, 1)
133
- heat = Image.fromarray(colors(relative_scale(averaged)))
134
- return Image.blend(image, heat, 0.48)
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
 
137
  def patch_view(state, tile_number):
@@ -144,14 +167,13 @@ def patch_view(state, tile_number):
144
  with Image.open(state['path']) as source:
145
  tile = source.convert('RGB').crop(box)
146
  weights = state['attention']['patch'][index]
147
- heat = Image.fromarray(colors(relative_scale(weights))).resize(tile.size, Image.Resampling.NEAREST)
148
- overlay = Image.blend(tile, heat, 0.48)
149
  description = (f'T{index + 1} · original box {list(box)} · patch grid '
150
  f'{weights.shape[0]} × {weights.shape[1]} · '
151
  f'patch weights sum = {float(weights.sum()):.6f}. '
152
- '颜色在当前块内归一化;不能按颜色跨块比较。'
153
- ' / Colors are scaled within this tile only. '
154
- 'Small images retain the model’s original black padding.')
155
  return tile, overlay, description
156
 
157
 
@@ -159,10 +181,9 @@ def top_tiles(state, k=4):
159
  order = np.argsort(-state['attention']['l1'], kind='stable')[:k]
160
  gallery = []
161
  for index in order:
162
- tile, overlay, _ = patch_view(state, int(index) + 1)
163
  weight = float(state['attention']['l1'][index])
164
- gallery.extend([(tile, f'T{index + 1} · L1 weight {weight:.6f}'),
165
- (overlay, f'T{index + 1} · within-tile patch attention')])
166
  return gallery
167
 
168
 
@@ -177,10 +198,9 @@ def head_view(state, selection):
177
  weights = state['attention']['l1_heads'][index]
178
  label = f'L1 aggregation head {index + 1}'
179
  return tile_overlay(state['path'], state['boxes'], weights), (
180
- f'{label} · sum = {float(weights.sum()):.6f}. '
181
- '蓝→黄表示本图内相对权重低→高;重叠区域取覆盖分块权重的平均值。'
182
- ' / Blue→yellow: lower→higher within this image; overlap uses mean weight. '
183
- '精确权重见表格 / Exact weights are in the table.')
184
 
185
 
186
  def parameter_rows(detector, branch, threshold, views):
@@ -195,6 +215,8 @@ def parameter_rows(detector, branch, threshold, views):
195
  ['Architecture', 'Local tile size', f'{int(detector.model.input_resolution)} × {int(detector.model.input_resolution)}'],
196
  ['Architecture', 'CLIP patch size', str(visual.conv1.kernel_size[0])],
197
  ['Architecture', 'Feature dimension', str(detector.model.embed_dim)],
 
 
198
  ['Architecture', 'L1 across-tile aggregation heads', str(core.level1_aggregate_attn.num_heads)],
199
  ['Architecture', 'L1 MIL pooling / hidden', f'{mil1.pooling} / {mil1.attn_V.out_features} ({mil1.attn_bag_mode})'],
200
  ['Architecture', 'L2 MIL pooling / hidden', f'{mil2.pooling} / {mil2.attn_V.out_features} ({mil2.attn_bag_mode})'],
@@ -214,7 +236,7 @@ def public_attention(state):
214
  weights = state['attention']
215
  return dict(
216
  meaning='Relative model attention, not a forgery probability or causal explanation.',
217
- display='Each heatmap is min-max scaled independently; overlap uses mean tile weight.',
218
  local_tile_boxes=[list(box) for box in state['boxes']],
219
  l1_tile_weights=weights['l1'].tolist(),
220
  l1_aggregation_head_weights=weights['l1_heads'].tolist(),
 
73
 
74
 
75
  def colors(values):
76
+ """Fixed blue → neutral → orange-red scale, centered on uniform attention."""
77
  x = np.clip(np.asarray(values), 0, 1)
78
+ stops = np.array([[35, 57, 93], [104, 134, 171], [180, 191, 203],
79
+ [247, 163, 66], [238, 80, 60]], dtype=np.float32)
80
+ first = np.minimum((x * 4).astype(int), 3)
81
+ fraction = (x * 4 - first)[..., None]
82
  return (stops[first] * (1 - fraction) + stops[first + 1] * fraction).astype(np.uint8)
83
 
84
 
85
+ def attention_levels(values, reference_count):
86
+ """A common reference: 1/N is neutral; clip only outside [0.25×, 4×]."""
87
+ ratios = np.maximum(np.asarray(values, dtype=np.float32) * reference_count, 1e-12)
88
+ return np.clip((np.log2(ratios) + 2) / 4, 0, 1)
89
+
90
+
91
  def preview_image(path):
92
  with Image.open(path) as source:
93
  image = source.convert('RGB')
 
100
  with Image.open(path) as original:
101
  sx, sy = image.width / original.width, image.height / original.height
102
  draw = ImageDraw.Draw(image)
103
+ font = ImageFont.load_default(size=18)
104
  labels = []
105
  for index, (x0, y0, x1, y1) in enumerate(boxes, 1):
106
  rect = (int(x0 * sx), int(y0 * sy), min(image.width - 1, int(x1 * sx) - 1),
107
  min(image.height - 1, int(y1 * sy) - 1))
108
+ draw.rectangle(rect, outline=(11, 35, 59), width=4)
109
+ draw.rectangle(rect, outline=(234, 252, 255), width=2)
110
+ if len(boxes) > 80 and (index - 1) % math.ceil(len(boxes) / 80):
111
+ continue
112
  label = f'T{index}'
113
  left, top = rect[0] + 3, rect[1] + 3
114
  # Edge alignment can place two rows just one pixel apart. Keep their
115
  # exact crop borders while moving text labels to avoid hiding earlier IDs.
116
+ for candidate_top in range(top, max(top + 1, rect[3] - 21), 24):
117
  extent = draw.textbbox((left, candidate_top), label, font=font)
118
  if not any(left < old[2] + 3 and extent[2] + 3 > old[0]
119
  and candidate_top < old[3] + 3 and extent[3] + 3 > old[1]
 
122
  break
123
  extent = draw.textbbox((left, top), label, font=font)
124
  labels.append((left, top, extent[2], extent[3]))
125
+ draw.rectangle((left - 2, top - 2, extent[2] + 4, extent[3] + 4), fill=(11, 35, 59))
126
  draw.text((left, top), label, fill='white', font=font)
127
  return image
128
 
129
 
130
+ def tile_overlay(path, boxes, weights, reference_count=None):
131
  image = preview_image(path)
132
  with Image.open(path) as original:
133
  sx, sy = image.width / original.width, image.height / original.height
 
140
  summed[y0:y1, x0:x1] += float(weight)
141
  coverage[y0:y1, x0:x1] += 1
142
  averaged = summed / np.maximum(coverage, 1)
143
+ count = reference_count or len(weights)
144
+ ratio = np.maximum(averaged * count, 1e-12)
145
+ heat = colors(attention_levels(averaged, count)).astype(np.float32)
146
+ alpha = np.clip(0.15 + np.abs(np.log2(ratio)) * 0.3, 0.15, 0.75)[..., None]
147
+ pixels = np.asarray(image, dtype=np.float32) * (1 - alpha) + heat * alpha
148
+ overlay = Image.fromarray(np.clip(pixels, 0, 255).astype(np.uint8))
149
+ # Rank outlines are useful only when attention differences are meaningful.
150
+ if float(np.ptp(weights)) * count >= 0.10:
151
+ draw = ImageDraw.Draw(overlay)
152
+ for rank, index in enumerate(np.argsort(-np.asarray(weights), kind='stable')[:3], 1):
153
+ x0, y0, x1, y1 = boxes[index]
154
+ rect = (int(x0*sx), int(y0*sy), min(image.width-1, int(x1*sx)-1), min(image.height-1, int(y1*sy)-1))
155
+ draw.rectangle(rect, outline=(12, 26, 42), width=7)
156
+ draw.rectangle(rect, outline=(255, 198, 65), width=4)
157
+ return overlay
158
 
159
 
160
  def patch_view(state, tile_number):
 
167
  with Image.open(state['path']) as source:
168
  tile = source.convert('RGB').crop(box)
169
  weights = state['attention']['patch'][index]
170
+ heat = Image.fromarray(colors(attention_levels(weights, weights.size))).resize(tile.size, Image.Resampling.NEAREST)
171
+ overlay = Image.blend(tile, heat, 0.65)
172
  description = (f'T{index + 1} · original box {list(box)} · patch grid '
173
  f'{weights.shape[0]} × {weights.shape[1]} · '
174
  f'patch weights sum = {float(weights.sum()):.6f}. '
175
+ f' 均匀权重 = {100/weights.size:.4f}%。'
176
+ '所有块采用同一色标:相对均匀权重 0.25×–4×。小图保留模型实际使用的黑色补边。')
 
177
  return tile, overlay, description
178
 
179
 
 
181
  order = np.argsort(-state['attention']['l1'], kind='stable')[:k]
182
  gallery = []
183
  for index in order:
184
+ tile, _, _ = patch_view(state, int(index) + 1)
185
  weight = float(state['attention']['l1'][index])
186
+ gallery.append((tile, f'#{len(gallery)+1} · T{index + 1} · 权重 {weight*100:.2f}%'))
 
187
  return gallery
188
 
189
 
 
198
  weights = state['attention']['l1_heads'][index]
199
  label = f'L1 aggregation head {index + 1}'
200
  return tile_overlay(state['path'], state['boxes'], weights), (
201
+ f'{label} · 均匀参照 = {100/len(weights):.2f}%。'
202
+ '蓝色低于参照,灰色接近参照,橙红色高于参照;重叠区域取覆盖分块的平均权重。'
203
+ '分块权重不能直接解释为像素级伪造概率。')
 
204
 
205
 
206
  def parameter_rows(detector, branch, threshold, views):
 
215
  ['Architecture', 'Local tile size', f'{int(detector.model.input_resolution)} × {int(detector.model.input_resolution)}'],
216
  ['Architecture', 'CLIP patch size', str(visual.conv1.kernel_size[0])],
217
  ['Architecture', 'Feature dimension', str(detector.model.embed_dim)],
218
+ ['Architecture', 'Feature extraction layer', str(detector.model.clip_mil_stop_after_block_1based
219
+ if detector.model.use_clip_intermediate_for_mil else len(visual.transformer.resblocks))],
220
  ['Architecture', 'L1 across-tile aggregation heads', str(core.level1_aggregate_attn.num_heads)],
221
  ['Architecture', 'L1 MIL pooling / hidden', f'{mil1.pooling} / {mil1.attn_V.out_features} ({mil1.attn_bag_mode})'],
222
  ['Architecture', 'L2 MIL pooling / hidden', f'{mil2.pooling} / {mil2.attn_V.out_features} ({mil2.attn_bag_mode})'],
 
236
  weights = state['attention']
237
  return dict(
238
  meaning='Relative model attention, not a forgery probability or causal explanation.',
239
+ display='Fixed log2 scale relative to uniform 1/N attention: 0.25x to 4x; overlap uses mean tile weight.',
240
  local_tile_boxes=[list(box) for box in state['boxes']],
241
  l1_tile_weights=weights['l1'].tolist(),
242
  l1_aggregation_head_weights=weights['l1_heads'].tolist(),
presentation.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Readable presentation of model scores and quantitative attention weights."""
2
+ from html import escape
3
+ import math
4
+
5
+ import numpy as np
6
+
7
+ CARD_CSS = '''
8
+ .ltf-card{font-family:ui-sans-serif,system-ui,sans-serif;color:#15243c;line-height:1.5}
9
+ .ltf-card *{box-sizing:border-box}.ltf-eyebrow{font-size:12px;font-weight:750;letter-spacing:.11em;color:#60748c;text-transform:uppercase}
10
+ .ltf-decision{border:1px solid #dce5ef;border-radius:20px;padding:25px;background:#fff;min-height:325px}
11
+ .ltf-decision h2{font-size:25px;line-height:1.25;margin:10px 0 20px;letter-spacing:-.03em}
12
+ .ltf-score{font-size:58px;line-height:1;font-weight:800;letter-spacing:-.06em;color:var(--accent,#087f85)}
13
+ .ltf-score-label{font-size:13px;color:#60748c;margin:8px 0 18px}.ltf-chip{display:inline-block;padding:5px 10px;border-radius:8px;background:#eef3f8;color:#42566e;font-size:12px;font-weight:650;margin-right:5px}
14
+ .ltf-mini{display:flex;align-items:center;gap:8px;margin:12px 0;font-size:13px}.ltf-mini b{width:57px}.ltf-mini>span{flex:1;background:#edf1f6;height:7px;border-radius:5px;overflow:hidden}.ltf-mini i{display:block;background:#14888e;height:100%;border-radius:5px}.ltf-mini em{width:45px;text-align:right;font-style:normal;font-variant-numeric:tabular-nums}
15
+ .ltf-note{font-size:12px;color:#60748c;margin-top:18px}.ltf-wait{color:#405772;font-size:16px;margin:30px 0 10px}
16
+ .ltf-stat-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin:2px 0 20px}.ltf-stat{background:#fff;border:1px solid #dce5ef;padding:17px 20px;border-radius:13px}.ltf-stat strong{display:block;font-size:27px;letter-spacing:-.04em}.ltf-stat span{font-size:12px;color:#60748c}
17
+ .ltf-chart{background:#fff;border:1px solid #dce5ef;border-radius:14px;padding:18px 16px}.ltf-chart h3{font-size:17px;margin:0 0 5px}.ltf-chart p{font-size:12px;color:#60748c;margin:0 0 12px}.ltf-scale{display:flex;justify-content:space-between;font-size:11px;color:#52687e;margin-top:5px}.ltf-colorbar{height:11px;border-radius:5px;background:linear-gradient(90deg,#23395d,#6886ab,#b4bfcb,#f7a342,#ee503c);margin-top:12px}
18
+ .ltf-callout{padding:11px 13px;background:#edf5f8;border-left:3px solid #14888e;border-radius:6px;font-size:12px;color:#33506b;margin-top:12px}
19
+ .ltf-hero{font-family:ui-sans-serif,system-ui,sans-serif;padding:29px 32px;border-radius:20px;background:linear-gradient(110deg,#10243e,#153c51);color:white;margin:0 0 24px}.ltf-card .ltf-hero h1{font-size:31px;margin:10px 0;letter-spacing:-.04em;color:#fff!important}.ltf-card .ltf-hero p{font-size:14px;color:#bed2e3!important;margin:7px 0}.ltf-hero .ltf-eyebrow{color:#75dcd2}.ltf-hero .ltf-chip{background:#ffffff12;color:#d9e9f1;border:1px solid #ffffff18;margin-top:12px}
20
+ @media(max-width:650px){.ltf-stat-grid{grid-template-columns:repeat(2,1fr)}.ltf-hero{padding:22px}.ltf-hero h1{font-size:25px}.ltf-decision{min-height:250px}.ltf-score{font-size:47px}}
21
+ @media(max-width:760px){
22
+ .gradio-container{padding:14px 12px 35px!important;min-width:0!important;width:100%!important;max-width:100vw!important}
23
+ .gradio-container .main{padding:0!important;min-width:0!important;width:100%!important}
24
+ .gradio-container .wrap,.gradio-container .contain{min-width:0!important;max-width:100%!important}
25
+ .gradio-container .row,.gradio-container .column,.gradio-container .block{min-width:0!important;max-width:100%!important}
26
+ }
27
+ '''
28
+
29
+
30
+ def card(content):
31
+ return f'<style>{CARD_CSS}</style><div class="ltf-card">{content}</div>'
32
+
33
+
34
+ HEADER = card('''<div class="ltf-hero"><div class="ltf-eyebrow">LTF / MODEL EXPLORER</div>
35
+ <h1>图像检测与注意力分析</h1><p>上传原图,查看模型分数、分块权重与块内细节。</p>
36
+ <span class="ltf-chip">CLIP ViT-L/14</span><span class="ltf-chip">第 24 层特征</span>
37
+ <span class="ltf-chip">224 × 224 分块</span><span class="ltf-chip">研究演示</span></div>''')
38
+
39
+ EMPTY_RESULT = card('''<div class="ltf-decision"><div class="ltf-eyebrow">ANALYSIS RESULT</div>
40
+ <h2>等待分析</h2><div class="ltf-wait">上传图片后,点击「开始分析」。</div>
41
+ <p class="ltf-note">这里将显示模型判定、生成分数和三个分支的对比。注意力分析会在下方展开。</p></div>''')
42
+
43
+
44
+ def result_card(result):
45
+ score = float(result['score_fake'])
46
+ generated = result['label'] == 1
47
+ accent = '#d75536' if generated else '#087f85'
48
+ verdict = '生成图' if generated else '真实图'
49
+ branch = {'fusion': 'Fusion 融合', 'level1': 'L1 局部', 'level2': 'L2 视图'}[result['branch']]
50
+ bars = ''
51
+ for key, label in (('fusion', 'Fusion'), ('level1', 'L1'), ('level2', 'L2')):
52
+ value = result['probabilities'][key]['fake']
53
+ bars += (f'<div class="ltf-mini"><b>{label}</b><span><i style="width:{value*100:.3f}%"></i></span>'
54
+ f'<em>{value:.3f}</em></div>')
55
+ return card(f'''<div class="ltf-decision" style="--accent:{accent};border-top:4px solid {accent}">
56
+ <div class="ltf-eyebrow">模型判定 · {branch}</div><h2>{verdict}</h2>
57
+ <div class="ltf-score">{score:.3f}</div><div class="ltf-score-label">生成分数 · 阈值 {result['threshold']:.2f}</div>
58
+ {bars}<div class="ltf-note">{result['width']:,} × {result['height']:,} px · {result['views']-1} 个局部分块
59
+ <br>分数是模型输出,未经概率校准;不代表图片来源的确定结论。</div></div>''')
60
+
61
+
62
+ def stats_card(state):
63
+ if not state:
64
+ return ''
65
+ attention = state['attention']
66
+ layer = next((row[2] for row in state['parameters'] if row[1] == 'Feature extraction layer'), '24')
67
+ stats = [(str(len(state['boxes'])), '局部分块 · 另含 1 个全局视图'),
68
+ (str(attention['l1_heads'].shape[0]), 'L1 分块聚合 Heads'),
69
+ (f"{attention['patch'].shape[1]} × {attention['patch'].shape[2]}", '每块 Patch 网格'),
70
+ (str(layer), 'CLIP 特征层')]
71
+ return card('<div class="ltf-stat-grid">' + ''.join(
72
+ f'<div class="ltf-stat"><strong>{escape(value)}</strong><span>{label}</span></div>'
73
+ for value, label in stats) + '</div>')
74
+
75
+
76
+ def scale_legend():
77
+ return ('<div class="ltf-colorbar"></div><div class="ltf-scale"><span>≤0.25×</span><span>0.5×</span>'
78
+ '<span>1× 均匀参照</span><span>2×</span><span>≥4×</span></div>')
79
+
80
+
81
+ def weight_chart(state, selection='mean', level='l1'):
82
+ if not state:
83
+ return ''
84
+ attention = state['attention']
85
+ if level == 'l2':
86
+ weights = attention['l2']
87
+ labels = ['Global'] + [f'T{i+1}' for i in range(len(weights)-1)]
88
+ title = 'L2 视图权重'
89
+ else:
90
+ weights = attention['l1'] if selection == 'mean' else attention['l1_heads'][int(selection.split('_')[-1])-1]
91
+ labels = [f'T{i+1}' for i in range(len(weights))]
92
+ title = 'L1 分块权重 · ' + ('8 Heads 平均' if selection == 'mean' else selection.replace('_', ' '))
93
+ n = len(weights)
94
+ order = np.argsort(-weights, kind='stable')[:10]
95
+ maximum = max(float(weights.max()), 1 / n) * 1.12
96
+ width, left, track, row_height = 430, 63, 290, 32
97
+ height = 38 + row_height * len(order)
98
+ baseline = left + track * (1/n) / maximum
99
+ svg = [f'<svg viewBox="0 0 {width} {height}" role="img" aria-label="{escape(title)}" style="width:100%;font-family:system-ui,sans-serif">',
100
+ f'<line x1="{baseline:.2f}" y1="8" x2="{baseline:.2f}" y2="{height-22}" stroke="#8394a8" stroke-dasharray="3 4"/>']
101
+ for rank, index in enumerate(order):
102
+ y, value = 12 + rank * row_height, float(weights[index])
103
+ fill = '#102e4b' if labels[index] == 'Global' else ('#078b93' if rank == 0 else '#66aab1')
104
+ svg.extend([f'<text x="0" y="{y+15}" fill="#30465e" font-size="13" font-weight="650">{labels[index]}</text>',
105
+ f'<rect x="{left}" y="{y}" width="{track}" height="21" rx="4" fill="#edf2f7"/>',
106
+ f'<rect x="{left}" y="{y}" width="{track*value/maximum:.3f}" height="21" rx="4" fill="{fill}">'
107
+ f'<title>{labels[index]}: {value:.8f} · {value*n:.4f}× uniform</title></rect>',
108
+ f'<text x="{left+track+8}" y="{y+15}" fill="#30465e" font-size="12">{value*100:.2f}%</text>'])
109
+ svg.append(f'<text x="{left}" y="{height-4}" fill="#60748c" font-size="10">虚线 = 均匀分配 {100/n:.2f}% · 最多显示前 10 项</text></svg>')
110
+ ratio = float(weights.max()) * n
111
+ message = ('权重接近均匀,当前没有明显的集中区域。热图不会放大微小差异。'
112
+ if float(np.ptp(weights)) * n < 0.10 else
113
+ f'最高权重为均匀参照的 {ratio:.2f}×。排名表示注意力权重,不等同于伪造证据。')
114
+ if level == 'l2':
115
+ message = f'Global 全局视图占 {float(weights[0])*100:.2f}%;原图叠图仅展示局部分块。' + message
116
+ return card(f'<div class="ltf-chart"><h3>{title}</h3><p>数值是归一化注意力权重,所有项目之和为 100%。</p>'
117
+ + ''.join(svg) + scale_legend() + f'<div class="ltf-callout">{message}</div></div>')
118
+
119
+
120
+ def patch_matrix(state, tile_number):
121
+ if not state:
122
+ return ''
123
+ from attention import attention_levels, colors
124
+ index = int(tile_number)-1
125
+ weights = state['attention']['patch'][index]
126
+ palette = colors(attention_levels(weights, weights.size))
127
+ h, w = weights.shape
128
+ cells = []
129
+ for y in range(h):
130
+ for x in range(w):
131
+ color = '#%02x%02x%02x' % tuple(palette[y, x])
132
+ value = float(weights[y, x])
133
+ cells.append(f'<rect x="{x*20}" y="{y*20}" width="20" height="20" fill="{color}" stroke="#ffffff40" stroke-width=".6">'
134
+ f'<title>行 {y+1},列 {x+1} · {value*100:.5f}% · {value*weights.size:.3f}× uniform</title></rect>')
135
+ return card(f'<div class="ltf-chart"><h3>T{index+1} · Patch 权重矩阵</h3><p>悬停查看原始权重;每格对应 14 × 14 输入像素。</p>'
136
+ f'<svg viewBox="0 0 {w*20} {h*20}" role="img" aria-label="Patch attention matrix" style="width:100%;max-height:420px">'
137
+ + ''.join(cells) + '</svg>' + scale_legend()
138
+ + '<div class="ltf-callout">统一使用「相对均匀权重」色标。灰色接近均匀,蓝色偏低,橙红色偏高。</div></div>')
styles.css ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root{color-scheme:light}
2
+ .gradio-container{max-width:1440px!important;margin:auto!important;padding:30px 28px 60px!important;background:#f4f7fb!important;color:#16263d!important;font-family:ui-sans-serif,system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif!important}
3
+ .block{border-color:#dce5ef!important;border-radius:13px!important;box-shadow:none!important}
4
+ .form{border-color:#dce5ef!important}
5
+ label,.label-wrap{font-weight:650!important}
6
+ #upload-panel{background:#fff;border:1px solid #dce5ef;border-radius:18px;padding:21px;gap:14px;min-width:260px}
7
+ #upload-panel h3{font-size:18px;margin-bottom:6px;color:#18314c}
8
+ #upload-panel .prose p{color:#60748c;font-size:13px}
9
+ #run-button{min-height:51px!important;border-radius:11px!important;font-size:16px!important;font-weight:750!important;background:#087f85!important;color:white!important;border-color:#087f85!important;box-shadow:0 4px 12px #087f8520!important}
10
+ #run-button:hover{background:#06666b!important}
11
+ #original-preview{background:#e9eff5!important}
12
+ #original-preview img,.attention-image img{width:100%!important;height:100%!important;object-fit:contain!important}
13
+ #analysis-section{margin-top:28px;gap:18px}
14
+ #analysis-tabs{background:#fff;border:1px solid #dce5ef;border-radius:16px;padding:16px;overflow:hidden}
15
+ #analysis-tabs .tab-nav{gap:5px;border-bottom:1px solid #e4ebf3;padding-bottom:12px;margin-bottom:18px}
16
+ #analysis-tabs .tab-nav button{padding:12px 18px;border-radius:9px;font-weight:650;color:#4c627b;border:0}
17
+ #analysis-tabs .tab-nav button[aria-selected="true"]{background:#e1f3f2;color:#006d73;box-shadow:inset 0 0 0 1px #b5dcda}
18
+ #analysis-tabs [role="tablist"]{gap:5px;border-bottom:1px solid #e4ebf3;padding-bottom:12px;margin-bottom:18px;flex-wrap:wrap}
19
+ #analysis-tabs [role="tab"]{padding:12px 18px!important;border-radius:9px!important;font-weight:650!important;color:#4c627b!important;border:0!important}
20
+ #analysis-tabs [role="tab"][aria-selected="true"]{background:#e1f3f2!important;color:#006d73!important;box-shadow:inset 0 0 0 1px #b5dcda}
21
+ #analysis-section .prose p{font-size:13px;color:#52687e;line-height:1.65}
22
+ #analysis-section .prose h2{font-size:23px;color:#18314c;margin-top:5px}
23
+ .section-kicker{font-size:13px;color:#13868a;font-weight:750;letter-spacing:.04em;margin-bottom:3px}
24
+ .attention-image{background:#e9eff5!important}
25
+ #top-crops{background:#fff!important}
26
+ #top-crops .caption{font-weight:650!important;font-size:13px!important;background:#fff!important;color:#17334e!important}
27
+ #technical-details{margin-top:22px}
28
+ #footer-note{margin-top:18px;text-align:center;color:#6b7f93;font-size:12px}
29
+ .setting-help{font-size:12px;color:#60748c}
30
+ @media(max-width:760px){.gradio-container{padding:14px 12px 35px!important}#upload-panel{padding:16px}#analysis-tabs{padding:9px}#analysis-tabs .tab-nav button{padding:10px;font-size:12px}#analysis-section{margin-top:18px}}
31
+ @media(max-width:760px){
32
+ .gradio-container{width:100%!important;min-width:0!important;max-width:100vw!important}
33
+ .gradio-container .main{min-width:0!important;padding:0!important;width:100%!important}
34
+ .row,.column,.block{min-width:0!important;max-width:100%!important}
35
+ #analysis-tabs [role="tablist"]{flex-wrap:wrap!important;min-width:0!important;width:100%}
36
+ #analysis-tabs [role="tab"]{white-space:normal!important;padding:9px!important;font-size:12px!important}
37
+ #top-crops [style*="grid-template-columns"]{grid-template-columns:repeat(2,minmax(0,1fr))!important}
38
+ }