Back to portfolio

Project · Generative Model Forensics

Deepfake Detection via Diffusion Reconstruction Error

Using a diffusion model's own inversion error as an engineered feature that turns a hard non-linear problem into a nearly linearly separable one

Harishwar Rao J.

IISER Bhopal · Nov 2025

DIRE maps for real and generated faces
DIRE maps. Top row: real faces, bright and structured, with high reconstruction error. Bottom row: generated faces, near-black, with almost no error. The classifier's job is now close to trivial.

Overview

A diffusion model reconstructs its own outputs far better than it reconstructs the real world. That asymmetry is the detection signal, and it is strong enough that most of the work goes into the feature rather than the classifier.

The core hypothesis, following DIRE[1], is that a partially-trained, "brittle" diffusion model can faithfully reconstruct images it generated itself, but will fail on real images that lie outside its learned manifold. The DIffusion Reconstruction Error (DIRE) exploits that failure directly: invert an image to its latent, sample back, and take the per-pixel absolute difference.

96.65% Test accuracy
on 2,000 held-out images
3 blocks CNN trained
from scratch
10,000 DIRE maps
5k real / 5k generated

The DIRE Signal

For an image x₀, the DIRE map is the per-pixel absolute error between the image and its reconstruction after a full inversion-and-sampling cycle:

D(x₀) = | x₀ − x₀^recon |
  1. Inversion (0 → T). The image is deterministically inverted through the DDIM[3] process (100 steps) to recover its corresponding latent x_T. DDIM's determinism is what makes this well-defined. A stochastic sampler would inject noise into the round trip and destroy the signal.
  2. Reconstruction (T → 0). That latent is sampled back to an image with deterministic DDIM (η = 0).
  3. Difference. The absolute per-pixel error becomes a new 64×64×3 image.

Real faces come back with high, structured error, giving bright maps. Faces generated by the same DDPM come back with near-zero error, giving almost black maps.

Data

Why This Works: The EDA

Before building any classifier, the exploratory analysis had already made the case. Pixel-intensity histograms of raw real vs. generated faces overlap almost perfectly, so global colour and brightness statistics carry essentially no signal. On DIRE maps the same histograms separate cleanly, with real images concentrated at high error and generated images decaying exponentially toward zero.

PCA makes it starker. Projected onto their first two principal components, raw images form one overlapping blob: the real/fake distinction is real but highly non-linear and subtle. The same projection on DIRE maps shows remarkable near-linear separability.

PCA of raw pixels vs PCA of DIRE maps
PCA projection of real vs. generated faces. Left: raw pixels, where classes overlap heavily. Right: DIRE maps, where the generated class collapses into a tight cluster, well separated from the real one.
Why the classifier is small. A large pretrained backbone like ResNet-50 is trained to find natural image features (fur, eyes, edges), not the abstract artefact patterns in a DIRE map. Given how separable the features already are, the right move is a lightweight 3-block CNN trained from scratch, whose only job is to distinguish "artefact-filled" from "near-black". The features did the work, so the model could stay simple.

Results

The DireClassifier: three conv/batchnorm/pool blocks (16 → 32 → 64 channels) into a 256-unit FC layer with dropout, was trained from scratch with BCEWithLogitsLoss and Adam. Validation accuracy held steadily in the 95–97% range with no significant overfitting.

ClassPrecisionRecallF1Support
Real0.960.980.971000
Generated0.980.950.971000
Overall accuracy96.65%2000

Confusion matrix: 979 true negatives and 21 false positives on real images; 954 true positives and 46 false negatives on generated ones, which is balanced performance across both classes.

Limitations

Two problems are intrinsic to the approach, and both matter more than the headline accuracy:

Future work would start with augmentation applied to the DIRE maps themselves (flips, rotations) to stabilise validation and reduce the minor overfitting observed.

References

  1. Z. He, et al. DIRE: Diffusion-based Representation for Deepfake Detection. ICCV, 2023.
  2. J. Ho, A. Jain, P. Abbeel. Denoising Diffusion Probabilistic Models. NeurIPS, 2020.
  3. J. Song, C. Meng, S. Ermon. Denoising Diffusion Implicit Models (DDIM). ICLR, 2021.
  4. Z. Liu, P. Luo, X. Wang, X. Tang. Deep Learning Face Attributes in the Wild (CelebA). ICCV, 2015.
DDIM Inversion Deepfake Detection Feature Engineering PCA Forensics