Skip to main content

Spectral Artifacts in Diffusion Images: What Detectors Actually Learn

Updated 5 min readMarkdown
Spectral Artifacts in Diffusion Images: What Detectors Actually Learn

Short answer: Almost all image detection models for artificial intelligence fail to "see" the anomalies in the appearance of a fake face and fake hand. On the contrary, they identify small patterns that generators leave in the frequency domain when upscaling and decoding images. These patterns are indeed very much present, but they are rather fragile – they differ from one generator to another, and are destroyed when images are resized or compressed in JPEG format. That is why AI generated image detection that looks perfect in the lab often fails online.

Anyone who has ever trained an AI to distinguish between real and fake images knows how easy it is for the algorithm to hit 99% accuracy when running tests on its validation dataset and completely fail the very first time an image is sent into a WhatsApp chat group. The reason? The model does precisely what it was taught to do!

GAN is where the story starts. In 2020, Frank et al. investigated GAN-generated images in the DCT domain and discovered severe artifacts that kept popping up regardless of the architecture, data, and resolution. It was discovered that these artifacts were the result of the presence of upsampling layers in all GANs, which allow a small feature map to be upscaled into an entire image. Transposed convolutions and nearest-neighbour upsampling copy values on a regular grid, which results in bright periodic peaks in the spectrum, similar to a checkerboard.

Generation procedure is specific to diffusion models; however, the need for switching from compact to pixel form is still important for them. Stable Diffusion works by performing denoising in a very small latent space and then applying the decoder from VAE with up-sampling operations that make its own trace. Corvi et al. examined the traces of diffusion models and found that they appear in the spectrum of noise residual in a specific manner, different from GANs', and individual for each specific model. Bammey designed Synthbuster, a detector relying on artefacts that appear in the Fourier transform of the residual image.

To analyze such artefacts, a trained model is not required. One must de-noise the image to extract the image data and obtain a residual, and then average the power spectrum of the residual from a couple of hundred images created by the same model. Images created using a real camera will produce a smooth, isotropic power spectrum. Generated images will often create a pattern of bright spots in the power spectrum.

import numpy as np
from PIL import Image
from scipy.ndimage import median_filter

def residual_spectrum(path, size=512):
    img = Image.open(path).convert("L").resize((size, size))
    x = np.asarray(img, dtype=np.float32)
    residual = x - median_filter(x, size=3)          # remove scene content
    f = np.fft.fftshift(np.fft.fft2(residual))
    return np.log1p(np.abs(f))

def average_spectrum(paths):
    return np.mean([residual_spectrum(p) for p in paths], axis=0)

The graph of the average spectrum of your real data set and that of your generated data set will show a clear distinction between the two. Export both of them as a JPEG file of quality 75 and then plot again and most of the differences would be gone.

Side-by-side residual spectra: smooth camera-like image vs upsampled generator-like image

Image created with AI assistance.

CNNs trained on real or generated data follow the easiest path towards low loss. These high-frequency peaks form such an easy path. Wang et al. have proved that the ResNet, trained on only images created by ProGAN and with blurring and JPEG transformation applied to it, was able to generalise extremely well across other CNN generators.

However, the above approach presents an Achilles' heel of the classifier. According to Ojha et al., the classifier built on the data generated using GANs turns into an asymmetric classifier as, even though the "fake" class recognizes the pattern of one specific GAN generator, the "real" class acts like the sink class for all other generators, even for newly emerged ones such as diffusion models. Therefore, the classifier developed based on the generated spectra with GANs can recognize an image generated using a diffusion model as a real one with high probability. The solution proposed by Ojha et al. was to use features of a large vision-language model rather than learning low-level artifacts from scratch.

The following three elements affect the signal by taking it away or modifying it. Resizing gets rid of the high frequencies on which the detector relies. Both the JPEG and WebP compression schemes quantize those exact high frequencies and add their own 8x8-grid structure, either masking the signal coming from the generator's grid structure or adding extra peaks. Also, each time a new generator or a new VAE decoder appears, peaks move around, which means that detectors trained on last year's generators fail quietly.

Another issue would be the question of fair play with regards to cameras. Some smartphones have a camera system which has some processing steps to sharpen up the image and resample, which itself adds a periodicity to the image and pushes real photos towards the fake side.

Think of spectral signatures as the signal, but not the decision. Do your training on degradations that you get from your users, like downsizing, recompression, and screen grabs. Work on combining frequency-domain and semantic signals and work on whatever metadata / provenance validation you have. Have a test set of the most recent generator models for drift detection. And output a score with uncertainty bounds, not a decision.

If you want to know more about how these types of models produce images, read this article explaining diffusion models. Additional details about the effects of recompression can be found in the topic of compression and detection, and here is an article about image analysis architecture concerning frequency features. If you want to learn more about it for the layman, here is the information about how to tell if an image is AI-generated.

Note: I used AI tools while drafting this post and reviewed it before publishing. The cover and images were made with AI assistance.

About the author

  • AI intern at Neural Defend. I write plain-English guides on deepfakes, voice cloning and AI-powered scams: how they work, how to spot them, and how to check whether a photo, video or call is real before you trust it.

No comments yet. Be the first to comment.