File size: 16,396 Bytes
1051963
 
 
 
 
 
 
 
 
 
 
 
c623f55
1051963
 
 
 
6fc2104
 
54d4d55
 
 
6fc2104
54d4d55
1051963
 
 
 
 
 
6fc2104
1051963
 
 
 
 
 
 
 
 
 
 
54d4d55
1051963
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54d4d55
 
 
 
 
 
1051963
 
 
 
 
 
 
 
 
 
 
54d4d55
1051963
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54d4d55
 
 
 
 
 
 
 
 
 
1051963
 
 
 
 
54d4d55
1051963
 
 
 
 
 
 
 
 
54d4d55
1051963
 
 
 
 
54d4d55
 
1051963
 
 
 
 
 
 
 
54d4d55
 
 
 
 
 
 
 
1051963
 
 
 
 
 
 
 
 
 
 
 
54d4d55
 
1051963
 
 
 
 
54d4d55
 
1051963
 
 
 
 
 
 
54d4d55
1051963
 
54d4d55
 
1051963
 
 
 
 
54d4d55
1051963
 
 
 
 
54d4d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1051963
 
 
54d4d55
1051963
54d4d55
1051963
54d4d55
1051963
 
 
 
 
54d4d55
 
1051963
 
54d4d55
1051963
 
 
 
 
 
 
 
54d4d55
 
 
 
 
 
 
 
 
 
1051963
54d4d55
1051963
 
 
 
 
 
 
 
54d4d55
1051963
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
license: apache-2.0
datasets:
- HuggingFaceM4/COCO
- ehristoforu/dalle-3-images
- poloclub/diffusiondb
- ehristoforu/midjourney-images
- nateraw/midjourney-texttoimage
- duchaiten/duchaiten-realistic-sdxl
tags:
- vision
- image-classification
pipeline_tag: image-classification
---

# SuSy - Synthetic Image Detector

<img src="https://cdn-uploads.huggingface.co/production/uploads/62f7a16192950415b637e201/NobqlpFbFkTyBi1LsT9JE.png" alt="image" width="300" height="auto">


- **Repository:** https://github.com/HPAI-BSC/SuSy
- **Dataset:** https://huggingface.co/datasets/HPAI-BSC/SuSy-Dataset
- **Paper:** TBD

## Model Details

<!-- Provide a longer summary of what this model is. -->

SuSy is a Spatial-Based Synthetic Image Detection and Recognition Model, designed and trained to detect synthetic images and attribute them to a generative model (i.e., two StableDiffusion models, two Midjourney versions and DALL·E 3). The model takes image patches of size 224x224 as input, and outputs the probability of the image being authentic or having been created by each of the aforementioned generative models.

<img src="model_architecture.png" alt="image" width="900" height="auto">

The model is based on a CNN architecture and is trained using a supervised learning approach. It's design is based on [previous work](https://upcommons.upc.edu/handle/2117/395959), originally intended for video superresolution detection, adapted here for the tasks of synthetic image detection and recognition. The architecture consists of two modules: a feature extractor and a multi-layer perceptron (MLP), as it's quite light weight. SuSy has a total of 12.7M parameters, with the feature extractor accounting for 12.5M parameters and the MLP accounting for the remaining 197K.

The CNN feature extractor consists of five stages following a [ResNet-18](https://arxiv.org/abs/1512.03385) scheme. The output of each of the blocks is used as input for various bottleneck modules that are arranged in a staircase pattern. The bottleneck modules consist of three 2D convolutional layers. Each level of bottlenecks takes input at a later stage than the previous level, and each bottleneck module takes input from the current stage and, except the first bottleneck of each level, from the previous bottleneck module.

The outputs of each level of bottlenecks and stage 4 are passed to a 2D adaptative average pooling layer and then concatenated to form the feature map feeding the MLP. The MLP consists of three fully connected layers with 512, 256 and 256 units, respectively. Between each layer, a dropout layer (rate of 0.5) prevents overfitting. The output of the MLP has 6 units, corresponding to the number of classes in the dataset (5 synthetic models and 1 real image class).

The model can be used as a detector by either taking the class with the highest probability as the output or summing the probabilities of the synthetic classes and comparing them to the real class. The model can also be used as an recognition model by taking the class with the highest probability as the output.

### Model Description

- **Developed by:** [Pablo Bernabeu Perez](https://huggingface.co/pabberpe), [Enrique Lopez Cuena](https://huggingface.co/Cuena) and [Dario Garcia Gasulla](https://huggingface.co/dariog) from [HPAI](https://hpai.bsc.es/)
- **Model type:** Spatial-Based Synthetic Image Detection and Recognition Convolutional Neural Network
- **License:** [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)

## Uses

This model can be used to detect synthetic images in a scalable manner, thanks to its small size. Since it operates on patches of 224x224, a moving window should be implemented in inference when applied on larger inputs (the most likely scenario, and the one it was trained under). This also enables the capacity for synthetic content localization within a high resolution input.

Any individual or organization seeking for support on the identification of synthetic content can use this model. However, it should not be used as the only source of evidence, particularly when applied to inputs produced by generative models not included in its training (see details in Training Data below).

### Intended Uses

Intended uses include the following:

* Detection of authentic and synthetic images
* Attribution of synthetic images to their generative model (if included in the training data)
* Localization of image patches likely to be synthetic or tampered.

### Out-of-Scope Uses

Out-of-scope uses include the following:

* Detection of manually edited images using traditional tools.
* Detection of images automatically downscaled and/or upscaled. These are considered as non-synthetic samples in the model training phase. 
* Detection of inpainted images.
* Detection of synthetic vs manually crafted illustrations. The model is trained mainly on photorealistic samples.
* Attribution of synthetic images to their generative model if the model was not included in the training data. AThis model may not be used to train generative models or tools aimed at lthough some generalization capabilities are expected, reliability in this case cannot be estimated.

### Forbidden Uses

This model may not be used to train generative models or tools aimed at purposefully deceiving the model or creating misleading content.

## Bias, Risks, and Limitations

The model may be biased in the following ways:

* The model may be biased towards the training data, which may not be representative of all authentic and synthetic images. Particularly for the class of real world images, which were obtained from a single source.
* The model may be biased towards the generative models included in the training data, which may not be representative of all possible generative models. Particularly new ones, since all models included were released between 2022 and 2023.
* The model may be biased towards certain type of images or contents. While it is trained using roughly 18K synthetic images, no assessment was made on which domains and profiles are included in those.

The model has the following technical limitations:

* The performance of the model may be influenced by transformations and editions performed on the images. While the model was trained on some alterations (blur, brightness, compression and gamma) there are other alterations applicable to images that could reduce the model accuracy.
* The model will not be able to attribute synthetic images to their generative model if the model was not included in the training data.
* The model is trained on patches with high gray-level contrast. For images composed entirely by low contrast regions, the model may not work as expected.

### Recommendations

<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->

## How to Get Started with the Model

Use the code below to get started with the model.

```python
import torch
from PIL import Image
from torchvision import transforms

# Load the model
model = torch.jit.load("SuSy.pt")

# Load patch
patch = Image.open("midjourney-images-example-patch0.png")

# Transform patch to tensor
patch = transforms.PILToTensor()(patch).unsqueeze(0) / 255.

# Predict patch
model.eval()
with torch.no_grad():
    preds = model(patch)

print(preds)
```

See `test_image.py` and `test_patch.py` for other examples on how to use the model.

## Training Details

### Training Data

<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->

The dataset is available at: https://huggingface.co/datasets/HPAI-BSC/SuSy-Dataset

| Dataset           | Year | Train | Validation | Test  |
|-------------------|------|-------|------------|-------|
| COCO              | 2017 | 2,967 | 1,234      | 1,234 |
| dalle-3-images    | 2023 | 987   | 330        | 330   |
| diffusiondb       | 2022 | 2,967 | 1,234      | 1,234 |
| realisticSDXL     | 2023 | 2,967 | 1,234      | 1,234 |
| midjourney-tti    | 2022 | 2,718 | 906        | 906   |
| midjourney-images | 2023 | 1,845 | 617        | 617   |

#### Authentic Images

- [COCO](https://cocodataset.org/)

We use a random subset of the COCO dataset, containing 5,435 images, for the authentic images in our training dataset. The partitions are made respecting the original COCO splits, with 2,967 images in the training partition and 1,234 in the validation and test partitions.

#### Synthetic Images

- [dalle-3-images](https://huggingface.co/datasets/ehristoforu/dalle-3-images)
- [diffusiondb](https://poloclub.github.io/diffusiondb/)
- [midjourney-images](https://huggingface.co/datasets/ehristoforu/midjourney-images)
- [midjourney-texttoimage](https://www.kaggle.com/datasets/succinctlyai/midjourney-texttoimage)
- [realistic-SDXL](https://huggingface.co/datasets/DucHaiten/DucHaiten-realistic-SDXL)

For the diffusiondb dataset, we use a random subset of 5,435 images, with 2,967 in the training partition and 1,234 in the validation and test partitions. We use only the realistic images from the realisticSDXL dataset, with images in the realistic-2.2 split in our training data and the realistic-1 split for our test partition. The remaining datasets are used in their entirety, with 60% of the images in the training partition, 20% in the validation partition and 20% in the test partition.

### Training Procedure

<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->

The training code is available at: https://github.com/HPAI-BSC/SuSy

#### Preprocessing

**Patch Extraction**

To prepare the training data, we extract 240x240 patches from the images, minimizing the overlap between them. We then select the most informative patches by calculating the gray-level co-occurrence matrix (GLCM) for each patch. Given the GLCM, we calculate the contrast and select the five patches with the highest contrast. These patches are then passed to the model in their original RGB format and cropped to 224x224.

**Data Augmentation**

| Technique                | Probability | Other Parameters                          |
|--------------------------|:-----------:|-------------------------------------------|
| HorizontalFlip           |     0.50    | -                                         |
| RandomBrightnessContrast |     0.20    | brightness\_limit=0.2 contrast\_limit=0.2 |
| RandomGamma              |     0.20    | gamma\_limit=(80, 120)                    |
| AdvancedBlur             |     0.20    |                                           |
| GaussianBlur             |     0.20    |                                           |
| JPEGCompression          |     0.20    | quality\_lower=75 quality\_upper=100      |


#### Training Hyperparameters

- Loss Function: Cross-Entropy Loss
- Optimizer: Adam
- Learning Rate: 0.0001
- Weight Decay: 0
- Scheduler: ReduceLROnPlateau
- Factor: 0.1
- Patience: 4
- Batch Size: 128
- Epochs: 10
- Early Stopping: 2

## Evaluation

<!-- This section describes the evaluation protocols and provides the results. -->

The evaluation code is available at: https://github.com/HPAI-BSC/SuSy

### Testing Data, Factors & Metrics

#### Testing Data

<!-- This should link to a Dataset Card if possible. -->

- Test Split of our Training Dataset
- Synthetic Images generated with [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium) and [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) using prompts from [Gustavosta/Stable-Diffusion-Prompts](https://huggingface.co/datasets/Gustavosta/Stable-Diffusion-Prompts)
- Synthetic Images in the Wild: Dataset containing 210 Authentic and Synthetic Images obtained from Social Media Platforms
- [Flickr 30k Dataset](https://www.kaggle.com/datasets/hsankesara/flickr-image-dataset)
- [Google Landmarks v2](https://github.com/cvdfoundation/google-landmark)
- [Synthbuster](https://zenodo.org/records/10066460)

#### Metrics

<!-- These are the evaluation metrics being used, ideally with a description of why. -->

- Recall: The proportion of correctly classified positive instances out of all actual positive instances in a dataset.

### Results

<!-- This section provides the results of the evaluation. -->

#### Authentic Sources

| Dataset             | Model | Year | Recall |
|---------------------|-------|------|--------|
| Flickr30k           | -     | 2014 | 90.53  |
| Google Landmarks v2 | -     | 2020 | 64.54  |
| In-the-wild         | -     | 2024 | 33.06  |

#### Synthetic Sources

| Dataset     | Model                     | Year | Recall |
|-------------|---------------------------|------|--------|
| Synthbuster | Glide                     | 2021 | 53.50  |
| Synthbuster | Stable Diffusion 1.3      | 2022 | 87.00  |
| Synthbuster | Stable Diffusion 1.4      | 2022 | 87.10  |
| Synthbuster | Stable Diffusion 2        | 2022 | 68.40  |
| Synthbuster | DALL-E 2                  | 2022 | 20.70  |
| Synthbuster | MidJourney V5             | 2023 | 73.10  |
| Synthbuster | Stable Diffusion XL       | 2023 | 79.50  |
| Synthbuster | Firefly                   | 2023 | 40.90  |
| Synthbuster | DALL-E 3                  | 2023 | 88.60  |
| Authors     | Stable Diffusion 3 Medium | 2024 | 93.23  |
| Authors     | Flux.1-dev                | 2024 | 96.46  |
| In-the-wild | Mixed/Unknown             | 2024 | 89.90  |

### Summary

The results for authentic image datasets reveal varying detection performance across different sources. Recall rates range from 33.06% for the In-the-wild dataset to 90.53% for the Flickr30k dataset. The Google Landmarks v2 dataset shows an intermediate recall rate of 64.54%. These results indicate a significant disparity in the detectability of authentic images across different datasets, with the In-the-wild dataset presenting the most challenging case for SuSy.

The results for synthetic image datasets show varying detection performance across different image generation models. Recall rates range from 20.70% for DALL-E 2 (2022) to 96.46% for Flux.1-dev (2024). Stable Diffusion models generally exhibited high detectability, with versions 1.3 and 1.4 (2022) showing recall rates above 87%. More recent models tested by the authors, such as Stable Diffusion 3 Medium (2024) and Flux.1-dev (2024), demonstrate even higher detectability with recall rates above 93%. The in-the-wild mixed/unknown synthetic dataset from 2024 showed a high recall of 89.90%, indicating effective detection across various unknown generation methods. These results suggest an overall trend of improving detection capabilities for synthetic images, with newer generation models generally being more easily detectable.

It must be noted that these metrics were computed using the center-patch of images, instead of using the patch voting mechanisms described previously. This strategy allows a more fair comparison with other state-of-the-art methods although it hinders the performance of SuSy.

## Environmental Impact

<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->

- **Hardware Type:** H100
- **Hours used:** 16
- **Hardware Provider:** Barcelona Supercomputing Center (BSC)
- **Compute Region:** Spain
- **Carbon Emitted:** 0.63kg

## Citation

<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->

**BibTeX:**

```bibtex
@misc{bernabeu2024susy,
    title={Present and Future Generalization of Synthetic Image Detectors},
    author={Bernabeu Perez, Pablo and Lopez Cuena, Enrique and Garcia Gasulla, Dario},
    year={2024},
    month={09}
}
```

```bibtex
@thesis{bernabeu2024aidetection,
    title={Detecting and Attributing AI-Generated Images with Machine Learning},
    author={Bernabeu Perez, Pablo},
    school={UPC, Facultat d'Informàtica de Barcelona, Departament de Ciències de la Computació},
    year={2024},
    month={06}
}
```

## Model Card Authors

[Pablo Bernabeu Perez](https://huggingface.co/pabberpe) and [Dario Garcia Gasulla](https://huggingface.co/dariog)

## Model Card Contact

For further inquiries, please contact [HPAI](mailto:[email protected])