File size: 4,592 Bytes
011a287 4aa73af 011a287 | 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 | ---
license: cc-by-nc-sa-4.0
base_model: mesmertech/mesmer-ai-image-detector
base_model_relation: quantized
pipeline_tag: image-classification
library_name: onnx
tags:
- onnx
- onnxruntime-web
- webgpu
- ai-image-detection
- ai-generated-image-detection
- quantized
---
# Mesmer AI image detector, ONNX for WebGPU
[mesmertech/mesmer-ai-image-detector](https://huggingface.co/mesmertech/mesmer-ai-image-detector) converted to ONNX so it
runs in a browser with ONNX Runtime Web (WebGPU). Try it in [WeReCooking/AI-Image-Detector-WebGPU](https://huggingface.co/spaces/WeReCooking/AI-Image-Detector-WebGPU),
next to Smaug.
The original is a 2.4M-parameter adapter on two frozen Meta Perception Encoder backbones, so a faithful port needs both:
| member | backbone | input | here |
|---|---|---|---|
| PE-L | PE-Core-L14-336 (0.3B) with the LoRA merged in | 5 views, 336 px | `q8/pel.onnx`, 8-bit, 370 MB |
| PE-G | PE-Core-G14-448 (1.9B), split in 4 parts | 1 view, 448 px | `q4/peg_*.onnx`, 4-bit, 1.2 GB (or `q8/peg_*.onnx`, 8-bit, 2.2 GB) |
The default set (`manifest.json`) is 1.6 GB: PE-L 8-bit, PE-G 4-bit.
## Accuracy against the original
Scores on 7 test images (3 Unsplash photos, Midjourney 7, GPT-Image-2, FLUX.2 klein, DFlow2), original `inference.py` in fp32
on CPU against these files on ONNX Runtime CPU:
| set | size | largest score change | verdict changes |
|---|---|---|---|
| fp32 export | 7.5 GB | 0.000 (PE-G logit 0.001) | 0 / 7 |
| PE-L 8-bit + PE-G 8-bit | 2.6 GB | 0.083 | 0 / 7 |
| **PE-L 8-bit + PE-G 4-bit (default)** | **1.6 GB** | **0.177** | **0 / 7** |
| PE-L 4-bit + PE-G 4-bit | 1.4 GB | 0.829 | 0 / 7 |
The verdict lines are at 2.918 (uncertain) and 4.286 (likely AI), so the default set moves scores by at most an eighth of
the gap between them. All 4-bit was dropped: PE-L lost up to 1.6 logits. Seven images is a small test; treat scores within
about 0.2 of a line as on the line.
## Files
- `q8/pel.onnx`: `pixels` float32 `[n, 3, 336, 336]` → `logit` `[n]`. Average the 5 views' logits.
- `q4/peg_a.onnx` … `peg_d.onnx` (and the same in `q8/`): `peg_a` takes `pixels` `[1, 3, 448, 448]` and returns `x`
(`[1, 1024, 1536]` tokens); `peg_b` and `peg_c` take `x` and return `y` plus the block-25 and block-38 token statistics
`s25` / `s38` (`[1, 3072]`: mean, then population std); `peg_d` takes `x` and returns `s50` and `pooled` (`[1, 1280]`).
- `peg_head.bin`: the PE-G linear head, float32 little endian, `mu | sd | w` (10,496 each) then `b`. It runs outside
ONNX because ONNX Runtime Web has no fp16 `Cast` kernel and the original rounds the features to fp16: clamp each of
`s25[:1536]`, `s25[1536:]`, `s38[:1536]`, `s38[1536:]`, `s50[:1536]`, `s50[1536:]`, `pooled` to ±65000, round to fp16,
NaN to 0, L2-normalise each block, then `logit = ((x - mu) / sd) · w + b`. The Space's `pegLogit` does this in JavaScript.
- Pixels are `(x / 255 - 0.5) / 0.5`, RGB, NCHW.
- `fusion.json` and `policy.json` are the original's: rank-normalise each logit against the 4,998 real reference scores,
weight 0.6 PE-L + 0.4 PE-G, then apply the policy lines.
- `manifest.json`: file sizes and SHA-256.
The views must match the original's Pillow code (EXIF transpose, ICC to sRGB, long side capped at 1024 for PE-L with Lanczos,
bicubic resize and native crops). The Space ships a JavaScript port of Pillow's resampler that gives the same bytes as Python;
see its `app.js` (`mesmerViews`).
Weights are quantized with ONNX Runtime's `MatMulNBits` (block 32, asymmetric). The heads stay fp32.
## Rebuild
`scripts/export_mesmer.py` exports fp32 parts (legacy exporter, opset 18, LoRA merged in fp64 onto the bf16-rounded weights
the original uses), `scripts/quant_det.py` quantizes, `scripts/pack.py` writes single-file parts and the manifest,
`scripts/verify_mesmer.py` compares against `scripts/ref_mesmer.py` (the original `inference.py` with a low-memory loader).
Made with torch 2.15, timm 1.0.31, onnx 1.23 and onnxruntime 1.32.
## License
CC-BY-NC-SA-4.0, the license of mesmertech/mesmer-ai-image-detector: non-commercial, share alike, credit mesmertech.
The backbones are Meta's Perception Encoder ([PE-Core-L14-336](https://huggingface.co/timm/vit_pe_core_large_patch14_336.fb),
[PE-Core-G14-448](https://huggingface.co/timm/vit_pe_core_gigantic_patch14_448.fb)), Apache-2.0. Changes from the original:
format conversion, LoRA merged into the weights, weight quantization, the PE-G forward pass split into 4 graphs.
A score is a likelihood, not proof. Do not use it as the only evidence for decisions about people.
|