File size: 5,578 Bytes
45c2952 9fa22a1 45c2952 0d312b8 45c2952 9fa22a1 45c2952 de70591 45c2952 de70591 9fa22a1 a299d07 0f94be1 a299d07 0f94be1 a299d07 0f94be1 a299d07 0f94be1 de70591 b44dcc4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | ---
title: LTF Image Detector
emoji: 🔎
colorFrom: blue
colorTo: indigo
sdk: gradio
language: en
sdk_version: 6.31.0
python_version: '3.11'
app_file: app.py
pinned: false
short_description: Test LTF on your original image files
---
# LTF online demo
Upload an original image file to obtain real / AI-generated scores. The model
uses the original RGB image and its full set of 224-pixel tiles; it does not
resize the entire input to a fixed resolution before inference. The preview
is reduced for display only. Fusion at threshold 0.5 is the default.
This custom PyTorch model uses frozen CLIP ViT-L/14, two-level MIL, cross-shuffle
during training, and gated fusion. The published checkpoint is epoch 20 from
the historical GenImage run, whose logged macro accuracy is 98.39%.
These are historical results, not an accuracy guarantee for uploaded images.
Scores are model outputs and are not calibrated authenticity probabilities.
## Configuration
Space variables (Settings → Variables and secrets):
| Variable | Meaning | Default |
| --- | --- | --- |
| `LTF_MODEL_REPO` | Model repository containing runtime and weights | Required on Hub |
| `LTF_MODEL_REVISION` | Model commit SHA | `main`; deployment pins a commit |
| `LTF_MODEL_DIR` | Local model folder, for offline testing | Unset |
| `LTF_DEVICE` | `auto`, `cpu`, or `cuda:0` | `auto` |
| `LTF_CHUNK_SIZE` | Number of CLIP views per forward chunk | `8` |
| `LTF_CPU_THREADS` | PyTorch CPU threads | `2` |
| `LTF_MAX_PIXELS` | Largest accepted image, in pixels | `16000000` |
| `LTF_MAX_VIEWS` | Largest accepted count of local tiles + global view | `400` |
Oversized images are rejected explicitly; their pixels or tiles are not silently
removed. Gradio queues one inference at a time. Original image paths and tokens
are not returned in prediction JSON. Uploaded files use Gradio temporary storage
with periodic cleanup; they are not added to either repository.
The runtime downloads the model at the configured revision, verifies its asset
and source hashes, and then loads it once. No training runs on the Space.
For a private model repository, set an appropriately scoped **read** token as
the Space secret `HF_TOKEN`. Do not put tokens in app.py or README.md.
## Local run
Use Python 3.11 and install `requirements.txt`. From this directory:
```bash
LTF_MODEL_DIR=../model_repo python app.py
```
The web UI and its `predict` API both call the same original-file inference path.
## Attention and tile inspection
The Analyze button additionally requests the model's existing `return_viz`
outputs during the same forward pass. Temporary observation hooks collect the
eight L1 across-tile aggregation heads. No model parameters are changed.
- Exact, numbered local crop boxes, including edge-aligned overlap and small-image padding.
- L1 tile-attention overlay: mean or any of the eight aggregation heads.
- L2 local-view overlay with the global-view attention weight listed separately.
- Four ranked local crop cards; clicking a card selects its patch inspector.
- Side-by-side original crop, patch overlay and interactive 16×16 weight matrix.
- Large score card and branch comparison, plus per-view attention ranking charts.
- Responsive desktop/mobile layout; replacing an image clears previous analysis.
- A tile selector for inspecting any local tile without rerunning inference.
- Exact view-weight table, embedding fusion gate and read-only model/inference parameters.
- Historical training settings from the original saved configuration, explicitly labeled Training only.
Colors use a fixed log2 scale relative to uniform attention (`weight × N`):
0.25×, 0.5×, 1×, 2× and 4×. Neutral gray represents the uniform reference;
blue is below and orange-red above that reference. Small differences are not
stretched into strong colors. L2 uses the count of all views, including Global,
for its reference. Overlap uses mean tile weight. Raw weights are available in
the ranking charts, weight table and prediction JSON. A displayed percentage is
an attention share, not a calibrated class probability.
These are model attention values, not pixel-level forgery probabilities or
causal explanations. The eight selectable heads belong to the L1 across-tile
aggregator; L1 patch pooling and L2 view pooling use gated MIL attention.
The original five-output `predict` API is preserved. The new `explain` API
returns prediction results plus visualization images, tables and attention metadata.
Changing a head or inspected tile uses per-session cached data, retained for up
to an hour. Changing the decision branch or threshold updates the result from
the cached scores. Uploading a new image clears previous results. The unchanged
checkpoint is used for both endpoints.
## Clickable GenImage examples
The gallery includes four real and four generated validation images. Click an
image to run the same prediction and attention analysis as an uploaded file.
Captions show dataset labels, separately from model predictions. Original files,
source paths and checksums are recorded in `examples/manifest.json`; attribution
is in `examples/SOURCE.md`.
## Tiling policy
Full 224×224 blocks are taken without overlap. If pixels remain at the right
or bottom, a final block is aligned to that boundary; only boundary blocks
may overlap. For width 500, horizontal starts are 0, 224, 276. The global view
is separate. This corrects earlier uniformly overlapping tiling; weights are
unchanged, but predictions can change. Historical benchmark results have not
been recomputed with this preprocessing.
|