RomanShnurov commited on
Commit
0af351c
1 Parent(s): f57e905
Files changed (11) hide show
  1. README.md +137 -0
  2. __init__.py +0 -0
  3. config.json +12 -0
  4. diffusions5M.pt +3 -0
  5. diffusions5M.py +17 -0
  6. images/1.jpg +0 -0
  7. images/2.jpg +0 -0
  8. images/3.jpg +0 -0
  9. images/4.webp +0 -0
  10. pipeline.py +51 -0
  11. requirements.txt +3 -0
README.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: generic
3
+ license: cc-by-sa-3.0
4
+ pipeline_tag: image-classification
5
+ tags:
6
+ - ai_or_not
7
+ - sumsub
8
+ - image_classification
9
+ - sumsubaiornot
10
+ - aiornot
11
+ - deepfake
12
+ - synthetic
13
+ - generated
14
+ - pytorch
15
+ metrics:
16
+ - accuracy
17
+ ---
18
+
19
+ # For Fake's Sake: a set of models for detecting generated and synthetic images
20
+
21
+ Many people on the internet have recently been tricked by fake images of Pope Francis wearing a coat or of Donald Trump's arrest.
22
+ To help combat this issue, we provide detectors for such images generated by popular tools like Midjourney and Stable Diffusion.
23
+
24
+ | ![Image1](images/3.jpg) | ![Image2](images/2.jpg) | ![Image3](images/4.webp) |
25
+ |-------------------------|-------------------------|--------------------------|
26
+
27
+ ## Model Details
28
+
29
+ ### Model Description
30
+
31
+ - **Developed by:** [Sumsub AI team](https://sumsub.com/)
32
+ - **Model type:** Image classification
33
+ - **License:** CC-By-SA-3.0
34
+ - **Types:** *diffusions_5m*(Size: 5M parameters, Description: Designed to detect photos created using different versions of Stable Diffusion (1.4, 1.5, 2.1))
35
+ - **Finetuned from model:** *tf_mobilenetv3_large_100.in1k*
36
+
37
+ ## Demo
38
+
39
+ The demo page can be found [here](https://huggingface.co/spaces/Sumsub/Sumsub-ffs-demo).
40
+
41
+ ## How to Get Started with the Model & Model Sources
42
+
43
+ Use the code below to get started with the model:
44
+
45
+ ```bash
46
+ git lfs install
47
+ git clone https://huggingface.co/Sumsub/Sumsub-ffs-synthetic-1.0_sd_5 sumsub_synthetic_sd_5
48
+ ```
49
+
50
+ ```python
51
+ from sumsub_synthetic_sd_5.pipeline import PreTrainedPipeline
52
+ from PIL import Image
53
+
54
+ pipe = PreTrainedPipeline("sumsub_synthetic_sd_5/")
55
+
56
+ img = Image.open("sumsub_synthetic_sd_5/images/2.jpg")
57
+
58
+ result = pipe(img)
59
+ print(result)
60
+ ```
61
+
62
+ You may need these prerequsites installed:
63
+
64
+ ```bash
65
+ pip install -r requirements.txt
66
+ pip install "git+https://github.com/rwightman/pytorch-image-models"
67
+ pip install "git+https://github.com/huggingface/huggingface_hub"
68
+ ```
69
+
70
+ ## Training Details
71
+
72
+ ### Training Data
73
+
74
+ The models were trained on the following datasets:
75
+
76
+ **Stable Diffusion datasets:**
77
+
78
+ - *Real photos* : [MS COCO](https://cocodataset.org/#home).
79
+ - *AI photos* : [aiornot HuggingFace contest data](https://huggingface.co/datasets/competitions/aiornot), [Stable Diffusion Wordnet Dataset](https://www.kaggle.com/datasets/astoeckl/stable-diffusion-wordnet-dataset).
80
+
81
+ ### Training Procedure
82
+
83
+ To improve the performance metrics, we used data augmentations such as rotation, crop, Mixup and CutMix. Each model was trained for 30 epochs using early stopping with batch size equal to 32.
84
+
85
+ ## Evaluation
86
+
87
+ For evaluation we used the following datasets:
88
+
89
+ **Stable Diffusion datasets:**
90
+
91
+ - [DiffusionDB](https://github.com/poloclub/diffusiondb): a set of 2 million images generated by Stable Diffusion using prompts and hyperparameters specified by real users.
92
+ - [Kaggel SD Faces](https://www.kaggle.com/datasets/bwandowando/faces-dataset-using-stable-diffusion-v14): set of 4k human face images generated using Stable Diffusion 1.4.
93
+ - [Stable Diffusion Wordnet Dataset](https://www.kaggle.com/datasets/astoeckl/stable-diffusion-wordnet-dataset): set of 200K images generated by Stable Diffusion.
94
+
95
+ **Realistic images:**
96
+
97
+ - [MS COCO](https://cocodataset.org/#home): set of 120k real world images.
98
+
99
+ ## Metrics
100
+
101
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
102
+
103
+ | Model | Dataset | Accuracy |
104
+ |-----------------|---------------------------------------------------------------------------------------------------------------|----------|
105
+ | diffusions_5M | [Kaggel SD Faces](https://www.kaggle.com/datasets/bwandowando/faces-dataset-using-stable-diffusion-v14) | 0.984 |
106
+ | diffusions_5M | [DiffusionDB](https://github.com/poloclub/diffusiondb) | 0.912 |
107
+ | diffusions_5M | [Stable Diffusion Wordnet Dataset](https://www.kaggle.com/datasets/astoeckl/stable-diffusion-wordnet-dataset) | 0.919 |
108
+ | diffusions_5M | [MS COCO](https://cocodataset.org/#home) | 0.877 |
109
+
110
+ ## Limitations
111
+
112
+ - It should be noted that achieving 100% accuracy is not possible. Therefore, the model output should only be used as an indication that an image may have been (but not definitely) artificially generated.
113
+ - Our models may face challenges in accurately predicting the class for real-world examples that are extremely vibrant and of exceptionally high quality. In such cases, the richness of colors and fine details may lead to misclassifications due to the complexity of the input. This could potentially cause the model to focus on visual aspects that are not necessarily indicative of the true class.
114
+
115
+ ![Image1](images/1.jpg)
116
+
117
+ ## Citation
118
+
119
+ If you find this useful, please cite as:
120
+
121
+ ```text
122
+ @misc{sumsubaiornot,
123
+ publisher = {Sumsub},
124
+ url = {https://huggingface.co/Sumsub/Sumsub-ffs-synthetic-1.0_sd_5},
125
+ year = {2023},
126
+ author = {Savelyev, Alexander and Toropov, Alexey and Goldman-Kalaydin, Pavel and Samarin, Alexey},
127
+ title = {For Fake's Sake: a set of models for detecting deepfakes, generated images and synthetic images}
128
+ }
129
+ ```
130
+
131
+ ## References
132
+
133
+ - Stöckl, Andreas. (2022). Evaluating a Synthetic Image Dataset Generated with Stable Diffusion. 10.48550/arXiv.2211.01777.
134
+ - Lin, Tsung-Yi & Maire, Michael & Belongie, Serge & Hays, James & Perona, Pietro & Ramanan, Deva & Dollár, Piotr & Zitnick, C.. (2014). Microsoft COCO: Common Objects in Context.
135
+ - Howard, Andrew & Zhu, Menglong & Chen, Bo & Kalenichenko, Dmitry & Wang, Weijun & Weyand, Tobias & Andreetto, Marco & Adam, Hartwig. (2017). MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications.
136
+ - Liu, Zhuang & Mao, Hanzi & Wu, Chao-Yuan & Feichtenhofer, Christoph & Darrell, Trevor & Xie, Saining. (2022). A ConvNet for the 2020s.
137
+ - Wang, Zijie & Montoya, Evan & Munechika, David & Yang, Haoyang & Hoover, Benjamin & Chau, Polo. (2022). DiffusionDB: A Large-scale Prompt Gallery Dataset for Text-to-Image Generative Models. 10.48550/arXiv.2210.14896.
__init__.py ADDED
File without changes
config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id2label": {
3
+ "0": "by AI",
4
+ "1": "by human"
5
+ },
6
+ "label2id": {
7
+ "by AI": "0",
8
+ "by human": "1"
9
+ },
10
+ "pretrained": false,
11
+ "timm_model": "timm/tf_mobilenetv3_large_100.in1k"
12
+ }
diffusions5M.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d2ad16a05ba72f6a5fadae6a7788f2e0c370f02e24e7fc629f3fa526059c435b
3
+ size 17339597
diffusions5M.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import timm
2
+ import torch
3
+ from torch import nn
4
+
5
+ class Model5M(torch.nn.Module):
6
+ def __init__(self):
7
+ super().__init__()
8
+ self.model = timm.create_model('timm/tf_mobilenetv3_large_100.in1k', pretrained=False, num_classes=0)
9
+
10
+ self.clf = nn.Sequential(
11
+ nn.Linear(1280, 128),
12
+ nn.ReLU(inplace=True),
13
+ nn.Linear(128, 2))
14
+
15
+ def forward(self, image):
16
+ image_features = self.model(image)
17
+ return self.clf(image_features)
images/1.jpg ADDED
images/2.jpg ADDED
images/3.jpg ADDED
images/4.webp ADDED
pipeline.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from PIL import Image
3
+
4
+ import os
5
+ import json
6
+ import torch
7
+ import torchvision
8
+ from torch.nn import functional as F
9
+
10
+ from diffusions5M import Model5M
11
+
12
+ class PreTrainedPipeline():
13
+ def __init__(self, path=""):
14
+ self.model = Model5M()
15
+ ckpt = torch.load(os.path.join(path, "diffusions5M.pt"), map_location=torch.device('cpu'))
16
+ self.model.load_state_dict(ckpt)
17
+ self.model.eval()
18
+
19
+ with open(os.path.join(path, "config.json")) as config:
20
+ config = json.load(config)
21
+ self.id2label = config["id2label"]
22
+
23
+ self.tfm = torchvision.transforms.Compose([
24
+ torchvision.transforms.Resize((224, 224)),
25
+ torchvision.transforms.ToTensor(),
26
+ torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406],
27
+ std=[0.229, 0.224, 0.225]),
28
+ ])
29
+
30
+ def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
31
+ """
32
+ Args:
33
+ inputs (:obj:`PIL.Image`):
34
+ The raw image representation as PIL.
35
+ No transformation made whatsoever from the input. Make all necessary transformations here.
36
+ Return:
37
+ A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
38
+ It is preferred if the returned list is in decreasing `score` order
39
+ """
40
+ img = self.tfm(inputs)
41
+ return self.predict_from_model(img)
42
+
43
+ def predict_from_model(self, img):
44
+ y = self.model.forward(img[None, ...])
45
+ y_1 = F.softmax(y, dim=1)[:, 1].cpu().detach().numpy()
46
+ y_2 = F.softmax(y, dim=1)[:, 0].cpu().detach().numpy()
47
+ labels = [
48
+ {"label": str(self.id2label["0"]), "score": y_1.tolist()[0]},
49
+ {"label": str(self.id2label["1"]), "score": y_2.tolist()[0]},
50
+ ]
51
+ return labels
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ timm==0.9.5
2
+ torch==2.0.1
3
+ torchvision==0.15.2