21amd21's picture
Move the test link under the intro; drop the credit line
1f7d2da verified
|
Raw History Blame Contribute Delete
5.67 kB
metadata
license: apache-2.0
base_model: abacusai/Smaug-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
  - deepfake-detection
  - quantized
  - 4-bit

Smaug AI Image Detector, web build (181 MB)

A browser-ready, quantised ONNX version of abacusai/Smaug-AI-Image-Detector by Abacus.AI: is this image AI-generated? It runs in the browser with onnxruntime-web (WebGPU or WebAssembly), so images can be checked on the user's own device. 181 MB instead of 880 MB, with almost the same results.

Test it for free here : https://utilitydone.com/ai-image-detector

Results

Tested on 300 images from OpenFake (130 real, 170 AI-generated, including GPT-Image 2, Nano Banana Pro, Midjourney 7, FLUX.2, Seedream 5, Sora 2 and Veo 3 frames), against the original model:

Original (880 MB) This build (181 MB)
AUC 0.950 0.937
AI images caught (score > 0.5) 110 / 170 110 / 170
Real photos flagged as AI (score > 0.5) 2 / 130 1 / 130

Verdicts changed on 5 of 300 images, all close to the 0.5 line. Mean probability shift: 0.022.

Speed in Chrome on a mid-range PC: about 24 s to load once, then about 1.7 s per image with WebGPU (3 crops) or 0.65 s (1 crop). The WebAssembly fallback is much slower (about 19 s for 1 crop on one thread), so use 1 crop and several threads there.

What changed from the original

  • Image only. The model is built on EmbeddingGemma-2, whose 262K-token text vocabulary isn't used for images. It's removed: the 4 fixed tokens around the image (BOS, start of image, end of image, EOS) are baked into the graph. The patch position table is cut to the 33 rows a 528 px crop uses. 289M parameters instead of 439M.
  • Mixed 4-bit / 8-bit weights. 4-bit (MatMulNBits, block 64, symmetric) everywhere except vision layers 8 to 11, which are 8-bit (DequantizeLinear, per output channel). Testing showed the language part is unaffected by 4-bit, while those four vision layers were the ones that lost quality.
  • The graph is pre-optimised with ONNX Runtime (basic level). One input, one output.

Use

  • Input pixels: float32 [n, 3, 528, 528], RGB values from 0 to 1.
  • Output logits: float32 [n]. Average the logits, then apply a sigmoid to get the probability that the image is AI-generated.

Make the crops like the original: scale the shorter side to 576 px (or up to 528 px if it's smaller), then take 528 × 528 crops at the start, centre and end of the long side (n = 3), or the centre only (n = 1, about 3× faster, slightly less accurate).

Use onnxruntime-web 1.31.1 or newer for WebGPU. Older builds (such as 1.22-dev) return wrong scores on WebGPU; WebAssembly is correct in both. The graph is already optimised, so graphOptimizationLevel: "disabled" loads it faster.

import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.31.1/dist/ort.webgpu.min.mjs";

const session = await ort.InferenceSession.create(MODEL_URL, {
  executionProviders: ["webgpu", "wasm"],
  graphOptimizationLevel: "disabled",
});

// Shorter side to 576 px (or up to 528), then 528 x 528 crops at the start, centre and end of the long side.
function crops(img, count = 3) {
  const C = 528, s = Math.max(C, Math.min(576, Math.min(img.width, img.height))) / Math.min(img.width, img.height);
  const w = Math.round(img.width * s), h = Math.round(img.height * s);
  const canvas = new OffscreenCanvas(w, h), ctx = canvas.getContext("2d");
  ctx.imageSmoothingQuality = "high";
  ctx.drawImage(img, 0, 0, w, h);
  const cx = (w - C) >> 1, cy = (h - C) >> 1;
  const spots = count === 1 ? [[cx, cy]] : w >= h ? [[0, cy], [cx, cy], [w - C, cy]] : [[cx, 0], [cx, cy], [cx, h - C]];
  const data = new Float32Array(spots.length * 3 * C * C);
  spots.forEach(([x, y], i) => {
    const px = ctx.getImageData(x, y, C, C).data;
    for (let p = 0; p < C * C; p++)
      for (let c = 0; c < 3; c++) data[i * 3 * C * C + c * C * C + p] = px[p * 4 + c] / 255;
  });
  return new ort.Tensor("float32", data, [spots.length, 3, C, C]);
}

const bitmap = await createImageBitmap(file, { imageOrientation: "from-image" });
const { logits } = await session.run({ pixels: crops(bitmap) });
const mean = logits.data.reduce((a, b) => a + b, 0) / logits.data.length;
const probabilityAI = 1 / (1 + Math.exp(-mean));

The original resizes with Lanczos; the browser's high-quality smoothing is close but not identical, so scores can differ slightly.

Limitations

A score is a likelihood, not proof. Like every detector, it can miss images from new generators, heavily edited or compressed images, and screenshots, and it can flag some real photos (about 1 in 130 in the test above). Don't use it as the only evidence for decisions about people.

Rebuild

scripts/wrap.py wraps the original model as [n, 3, 528, 528] → [n] logits (matches the original exactly), scripts/export.py exports it to ONNX (eager attention, opset 18), and scripts/mixed.py quantises it. Then run ONNX Runtime at ORT_ENABLE_BASIC with optimized_model_filepath to save the optimised graph. Made with torch 2.15, transformers 5.23, sentence-transformers 6.3, onnx 1.23 and onnxruntime 1.32.

License

Apache-2.0, same as the original. See LICENSE. Changes from the original are described above.