Nicolò Trevisan

← Case studiesCasi studio

01DataShortcuts and twins

Less data, better results: how I made an AI image detector reliable

The problem wasn't missing data. It was a shortcut hidden in the data I already had. How I found it and closed it.

The case

Imagine you are looking for a flat and find a listing with a perfect kitchen. Too perfect: the photo was made by an AI, and the flat doesn't exist. A detector has to catch it.

One of the early versions of my detector caught it almost every time. Its test scores were excellent. But a high score doesn't tell you how the model reaches its answer. And I wanted to know that before I could trust it.

The discovery: the blind check

To understand what a model has learned, I run a simple test. I build a second, small model and don't show it the photo at all. I only give it information that shouldn't matter: how big the image is, what shape it has, how the file is saved. It's a "blindfolded" model.

If the "blindfolded" model gets it right, the data contains a shortcut: a clue that separates real photos from fake ones without even looking at them.

The result surprised me. Looking only at how many megapixels the image had, the "blindfolded" model spotted the AI-made one 91 times out of 100.

The rule of 64

So I looked at the sizes, one by one. The test set contained 629 AI-generated images: all 629 had sides divisible by 64. Generators produce images in standard sizes, multiples of 64 pixels. Cameras and smartphones don't.

A very basic rule, "if the sides divide by 64, it's fake", would have caught them all. My model had learned something very similar. Instead of looking at the photo, it was measuring the frame.

The fake kitchen from the listing, generated by AI

1024 × 768 pixels

1024 ÷ 64 = 16768 ÷ 64 = 12

Fake

0 pixels cropped

The shortcut rule looks only at the size of the image: if both sides are divisible by 64, it calls the image fake. Cropping the edge doesn't change the content, but it changes the verdict: the image is "fake" with 0, 64 or 128 pixels cropped, and "real" in every other case. In the same way, a real photo resized to 1024 × 768 would be called fake. An illustration of the shortcut, not of the detector; the kitchen is the AI-generated image that also appears on the site.

The damage

A shortcut works as long as the world looks like the training data. In reality, real photos come in a thousand sizes: cropped, resized, taken with different phones.

On a public collection of over 3,000 photos taken with real cameras, that model mistook 20 out of 100 for fakes. One real photo in five. For anyone who has to decide whether a piece of evidence is authentic, that is an unacceptable error.

What didn't work

Faced with a problem like this, the usual answer is: more data, or a bigger model. I tried both.

  • More data. One of the early versions was trained on about 610,000 images from 7 different collections. It recognised AI images, but mistook half of the real photos for fakes.
  • A huge model. I tried a model with 7 billion parameters. Good numbers, but expensive, and only usable with a high-end graphics card.

The problem wasn't quantity. The data contained an easy answer, and any model, big or small, would have found it.

The idea: twins

If real and fake photos can be told apart "from the outside", they have to become identical from the outside. Every AI image gets its real twin: same size, same crop, same compression, same file format. Only one difference is left, the one that matters: who made the pixels.

I created the twins myself, systematically. I started from real photos and, from each one, generated its fake version with AI. Then I applied exactly the same size, crop, compression and format to every pair. One fixed rule, the same for every pair, no exceptions.

Before told apart by the frame real photos · many sizes · JPEG AI · fixed size PNG With twins identical outside, different only inside each pair: same size, crop,compression and format
Plain outline: real photos; red: AI images. On the left, the shape alone is enough to separate them. On the right, the shape tells you nothing.
Real photoOriginal photo
Its twin: jacket redone by AIThe same photo with the jacket regenerated by AI
A pair of twins: same photo, same size, same compression. Only the jacket changes, regenerated by AI. Photo: GilPe, Wikimedia Commons, CC BY-SA 4.0; edited for testing.

The first attempt wasn't enough. With only the crop matched, the "blindfolded" model still got it right 100 times out of 100, just by reading the file header: every program leaves its signature there. I had to match everything at once: size, crop, compression and format.

In the end the "blindfolded" model got it right 51 times out of 100. Like flipping a coin. The shortcut was closed, and the real model had only one thing left to learn: the pixels.

The "blindfolded" model: how often it guesses real or fake from the file alone, out of 100

Only the crop matched100
Full twins51
The dashed line is 50: guessing

The proof

Same task, same type of model. Only the data changes.

Real photos mistaken for fakes, out of 100

Before20
With twins0.5
Public collection of 3,085 photos taken with real cameras

AI images caught out of 100, with at most one false alarm per 100 real photos

Before55
With twins81
Held-out twin test · 7,002 AI images and 815 real photos

From one real photo in five mistaken for a fake, to one in two hundred.

The lesson

Today's model is trained on about 120,000 images: five times fewer than the 610,000 version. And the model it builds on is small, 22 million parameters: 300 times smaller than the 7-billion giant, with the same result. It even runs on an ordinary laptop processor.

More data and more computing power cost money, and often don't solve the problem. What made the difference was the method: first ask what the model is learning, then measure it, then fix the data. It's how a science lab works, applied to AI.

The same happened with my audio detector. On a public collection of almost 2,000 audio clips taken from social media, it went from 50 to 93 out of 100 at recognising the generated voice. Same model: I only changed the data.

In short

  • Before I can trust a number, I check how the model gets there.
  • I found a shortcut hidden in the data and closed it with twins.
  • The result came from the method, not from more data or a bigger model.

01DatiScorciatoie e gemelli

Meno dati, risultati migliori: come ho reso affidabile un detector di immagini AI

Il problema non erano i dati che mancavano, ma una scorciatoia nascosta in quelli che avevo. Come l'ho trovata e chiusa.

Il caso

Immagina di cercare casa e di trovare un annuncio con una cucina perfetta. Troppo perfetta: la foto l'ha fatta un'AI, e l'appartamento non esiste. Un detector deve accorgersene.

Una delle prime versioni del mio detector se ne accorgeva quasi sempre. Sul suo test i numeri erano ottimi. Ma un numero alto non dice come il modello arriva alla risposta. E io volevo saperlo prima di potermi fidare.

La scoperta: il controllo alla cieca

Per capire cosa ha imparato un modello faccio una prova semplice. Costruisco un secondo modello, piccolo, a cui non faccio vedere la foto in questione. Gli do solo le informazioni che non dovrebbero contare niente: quanto è grande l'immagine, che forma ha, come è salvato il file. È un modello "bendato".

Se il modello "bendato" indovina, nei dati c'è una scorciatoia: un indizio che separa le foto vere da quelle false senza nemmeno guardarle.

Il risultato mi ha sorpreso. Guardando solo quanti megapixel aveva l'immagine, il modello "bendato" riconosceva quella fatta dall'AI 91 volte su 100.

La regola del 64

Allora ho guardato le misure, una per una. Nel test c'erano 629 immagini generate dall'AI: tutte e 629 avevano i lati divisibili per 64. I generatori producono immagini in misure standard, multiple di 64 pixel. Le fotocamere e gli smartphone no.

Una regola molto elementare, "se i lati si dividono per 64, è falsa", le avrebbe scoperte tutte. Il mio modello aveva imparato qualcosa di molto simile. Invece di guardare la foto, ne misurava la cornice.

La cucina finta dell'annuncio, generata dall'AI

1024 × 768 pixel

1024 ÷ 64 = 16768 ÷ 64 = 12

Falsa

0 pixel tagliati

La regola della scorciatoia guarda solo le misure dell'immagine: se entrambi i lati sono divisibili per 64, la dichiara falsa. Ritagliando il bordo il contenuto non cambia, ma il verdetto sì: l'immagine risulta "falsa" con 0, 64 o 128 pixel tagliati e "vera" in tutti gli altri casi. Allo stesso modo, una foto vera ridimensionata a 1024 × 768 verrebbe dichiarata falsa. Esempio illustrativo della scorciatoia, non del detector; la cucina è l'immagine generata dall'AI che compare anche nel sito.

Il danno

Una scorciatoia funziona finché il mondo assomiglia ai dati di addestramento. Nella realtà le foto vere arrivano in mille misure: ritagliate, ridimensionate, scattate con telefoni diversi.

Su una raccolta pubblica di oltre 3.000 foto scattate con fotocamere vere, quel modello ne scambiava per false 20 su 100. Una foto vera su cinque. Per chi deve decidere se una prova è autentica, è un errore che non si può accettare.

Cosa non ha funzionato

Davanti a un problema così, la risposta più comune è: servono più dati, o un modello più grande. Le ho provate tutte e due.

  • Più dati. Una delle prime versioni era addestrata su circa 610.000 immagini, prese da 7 raccolte diverse. Riconosceva le immagini dell'AI, ma scambiava per false metà delle foto vere.
  • Un modello enorme. Ho provato un modello da 7 miliardi di parametri. Buoni numeri, ma costoso, e utilizzabile solo con una scheda grafica di fascia alta.

Il problema non era la quantità. Era che i dati contenevano una risposta facile, e qualsiasi modello, grande o piccolo, l'avrebbe trovata.

L'idea: i gemelli

Se le foto vere e quelle false si distinguono "da fuori", allora devono diventare identiche da fuori. Ogni immagine dell'AI ha la sua gemella vera: stessa misura, stesso ritaglio, stessa compressione, stesso formato del file. Resta una sola differenza, quella che conta: chi ha prodotto i pixel.

Le gemelle le ho create io, in modo sistematico. Sono partito da foto vere e da ognuna ho generato con l'AI la sua versione falsa. Poi a ogni coppia ho applicato esattamente la stessa misura, lo stesso ritaglio, la stessa compressione e lo stesso formato. Una regola fissa, uguale per tutte le coppie, senza eccezioni.

Prima si distinguono dalla cornice foto vere · tante misure · JPEG AI · misura fissa PNG Con i gemelli identiche fuori, diverse solo dentro ogni coppia: stessa misura, ritaglio,compressione e formato
Con il contorno semplice le foto vere, in rosso le immagini dell'AI. A sinistra basta guardare la forma per separarle. A destra la forma non dice più niente.
Foto veraFoto originale
La sua gemella: giacca rifatta dall'AIStessa foto con la giacca rigenerata dall'AI
Una coppia di gemelli: stessa foto, stessa misura, stessa compressione. Cambia solo la giacca, rigenerata dall'AI. Foto: GilPe, Wikimedia Commons, CC BY-SA 4.0; ritoccata per test.

Il primo tentativo non bastava. Pareggiando solo il ritaglio, il modello "bendato" indovinava ancora 100 volte su 100, leggendo soltanto l'intestazione del file: ogni programma lascia lì la sua firma. Ho dovuto pareggiare tutto insieme, misura, ritaglio, compressione e formato.

Alla fine il modello "bendato" indovinava 51 volte su 100. Come lanciare una moneta. La scorciatoia era chiusa, e al modello vero restava una sola cosa da imparare: i pixel.

Il modello "bendato": quante volte indovina vero o falso guardando solo il file, su 100

Pareggiato solo il ritaglio100
Gemelli completi51
La linea tratteggiata è 50: tirare a indovinare

La prova

Stesso compito, stesso tipo di modello. Cambiano i dati.

Foto vere scambiate per false, su 100

Prima20
Con i gemelli0,5
Raccolta pubblica di 3.085 foto scattate con fotocamere vere

Immagini dell'AI scoperte su 100, con al massimo un falso allarme ogni 100 foto vere

Prima55
Con i gemelli81
Test di gemelli tenuti da parte · 7.002 immagini dell'AI e 815 foto vere

Da una foto vera su cinque scambiata per falsa, a una su duecento.

La morale

Il modello di oggi è addestrato su circa 120.000 immagini: cinque volte meno della versione da 610.000. E il modello su cui si appoggia è piccolo, 22 milioni di parametri: 300 volte meno del gigante da 7 miliardi, con lo stesso risultato. È anche compatibile con un semplice processore da laptop.

Più dati e più potenza costano, e spesso non risolvono. La differenza l'ha fatta il metodo: prima chiedersi cosa sta imparando il modello, poi misurarlo, poi correggere i dati. È il modo di lavorare di un laboratorio scientifico, applicato all'AI.

Nel detector audio è andata allo stesso modo. Su una raccolta pubblica di quasi 2.000 audio presi dai social, è passato da 50 a 93 su 100 nel riconoscere la voce generata. Stesso modello: ho cambiato solo i dati.

In breve

  • Prima di potermi fidare di un numero, controllo come il modello ci arriva.
  • Ho trovato una scorciatoia nascosta nei dati e l'ho chiusa con i gemelli.
  • Il risultato è venuto dal metodo, non da più dati o da un modello più grande.