Image Classification
Transformers
Safetensors
convnext
ai-generated-image-detection
image-forensics

AI-generated image detector (ConvNeXt-Tiny)

A binary classifier that labels an image real (a photograph taken by a camera) or ai (AI-generated). It is a ConvNeXt-Tiny CNN fine-tuned from facebook/convnext-tiny-224 for a Bachelor of Cybersecurity graduation project. Code, a small web interface, the lists of test images, every prediction and the evaluation protocol: https://github.com/mkk1135/graduation-project

Try it in your browser: https://huggingface.co/spaces/mkk1135/ai-image-detector (the image is analysed on your own device and is not uploaded).

Read the limitations before relying on it. On a held-out test set of 384 images it classified 81.5% correctly, and its answer is an indication, not proof.

Use

from transformers import pipeline

detector = pipeline("image-classification", model="mkk1135/ai-image-detector")
print(detector("photo.jpg"))

The processor does not resize: the model looks at the whole image at its original resolution, because shrinking an image removes the fine traces that generators leave. Very large images need more memory and time; the project's interface analyses at most the central 2048 x 2048 pixels. The decision threshold that was chosen on development images is built into the model, so the label with the higher score is the model's answer. That score is the model's score for its answer, not a calibrated probability; treat a score below 0.75 as "undetermined". The project's interfaces show it as "detector score" and state that it is not a probability.

The project's repository also contains a combination of this model with the published detector OwensLab/commfor-model-384 (fusion.py), which was more accurate than either part on the held-out test set (95.3% against 81.5% and 92.2%, and 94.0% for the published detector with a threshold tuned on the development images).

Training data

36,562 images, of which a random 10% was held out for validation.

Class Source Images
Generated Community Forensics-Small: 377 community Stable Diffusion models (latent diffusion) 14,592
Generated Community Forensics-Small: GLIDE, DeepFloyd IF (pixel diffusion), BigGAN 2,992
Real Community Forensics-Small: COCO (4,831), VISION (1,153), Landscapes HQ (2,992), FFHQ (2,992) 11,968
Real DataSeeds.AI Sample Dataset: peer-ranked photographs of varied subjects 7,010

Images flagged NSFW by Community Forensics were removed.

Training

Three epochs, AdamW, learning rate 5e-5, batch size 32, seed 42, on 256 x 256 crops taken at the images' original resolution. Augmentation: random shrinking (seven times in ten, by any factor down to the crop size), horizontal flip, Gaussian blur (radius 0.3 to 1.2 px, probability 0.3), Gaussian noise (standard deviation 1 to 5, probability 0.3), and JPEG re-compression applied zero, one or two times at quality 30 to 95. The generated training images are uncompressed PNG and most real ones are JPEG, so the augmentation is there to keep the model from using an image's size, sharpness or compression history as the answer.

This is the fifth training setting of the project. The model published before it (setting 4) called small, lightly compressed photographs generated; the strong shrinking was added to remove that shortcut. After training, the decision threshold was set to 0.69 on the "AI" score, the value with the highest accuracy on 541 development images.

Results

"Real" is the positive class. None of the named generators of the evaluation sets is in the training data, but some open-source generators of the development and final test sets (three LCM-LoRA models and a Midjourney-style adapter) are built on Stable Diffusion, the family that supplies most of the generated training images. The generators of the Reddit images are not known. The training setting and the threshold were chosen on development images only; the three held-out sets were scored once, after all decisions had been committed to the public repository.

Set Images Accuracy Real correct AI correct
Validation split (same sources as training; default threshold) 3,657 95.3%
Check set (development) 157 88.5% 66 / 69 73 / 88
Development test set 384 82.6% 167 / 192 150 / 192
Final test set (held out) 384 81.5% (95% CI 77.3 to 85.1) 171 / 192 142 / 192
Final test set, every image re-compressed as JPEG quality 90 384 78.1% 174 / 192 126 / 192
ChatGPT and Gemini set (held out) 168 68.5% 61 / 72 54 / 96
Reddit set (held out; labels may be wrong in some cases) 120 68.3% 56 / 60 26 / 60

The final test set holds 192 photographs (Wikimedia Commons, Unsplash and Open Images, 64 each) and 192 images from the Community Forensics evaluation set: 132 from 11 commercial generators (Midjourney v5.2 and v6.1, DALL-E 2 and 3, Firefly Image 2 and 3, Imagen 3, Ideogram v1 and v2, FLUX dev and schnell) and 60 from 10 open-source generators. By source: Wikimedia Commons 56 / 64, Unsplash 62 / 64, Open Images 53 / 64, commercial generators 105 / 132, open-source generators 37 / 60.

Compared on the same 384 held-out images: Claude Sonnet 5.5 (asked in a blind test) 96.6%, the combination of this model with OwensLab/commfor-model-384 95.3%, OwensLab/commfor-model-384 alone 92.2%, this model 81.5%, the model published before it (setting 4) 82.3%, and the three other public detectors compared in the project's report between 50.0% and 66.4%.

Three training runs that differ only in the seed scored between 81.5% and 83.9% on the final test set (mean 83.0%, standard deviation 1.3). The published model is the run with seed 42, which was fixed in advance.

Limitations

  • Treat its answer as a hint, not as proof that an image is real or generated. On the held-out final test set 71 of 384 answers were wrong.
  • Its main error is missing generated images: on the final test set it called 50 of the 192 generated images real, and 21 of the 192 photographs generated.
  • It is weak on images from the generators of current chat assistants. On the held-out ChatGPT and Gemini set it recognised 12 of 24 images from GPT Image 2, 10 of 24 from GPT Image 1.5, 23 of 24 from Nano Banana and 9 of 24 from Nano Banana Pro.
  • JPEG re-compression lowers its accuracy on the final test set from 81.5% to 78.1%: of the 192 generated images it then recognises 126.
  • It still reacts to processing. After a slight Gaussian blur (radius 0.6 px) it recognised 58 of the 69 photographs of the check set (without blur: 66) and 73 of the 88 generated images (without blur: 73).
  • Its score is not a calibrated probability. On the final test set, answers with a score of 0.75 or more were right in 86.4% of 324 cases, answers with a lower score in 55.0% of 60.
  • This model is not better than the one published here before it (training setting 4, default threshold), and on some images it is worse. On the final test set the two cannot be told apart (81.5% against 82.3%). On the ChatGPT and Gemini set the earlier model scored 73.2% against 68.5%, and on the Reddit set 79.2% against 68.3%: the stronger shrinking and the tuned threshold make this model slower to call an image generated. This model was published because the rule for choosing it had been fixed before the held-out sets were scored. For images from recent generators or from social media, prefer the earlier model, which is kept in this repository under the tag round-1: pipeline("image-classification", model="mkk1135/ai-image-detector", revision="round-1").
  • It was trained and tested on photographs and on unedited generator output. Screenshots, scans, drawings, heavily edited photographs, and partly synthetic images are untested.
  • Generators released after the training data was collected may not be detected.

Licence

CC BY-NC-SA 4.0, following the licence of the Community Forensics training data: non-commercial use only.

Downloads last month
92
Safetensors
Model size
27.8M params
Tensor type
F32
Β·
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for mkk1135/ai-image-detector

Finetuned
(62)
this model

Datasets used to train mkk1135/ai-image-detector

Space using mkk1135/ai-image-detector 1