SadraCoding commited on
Commit
270c326
·
1 Parent(s): b1250d2

Frequency Analysis Added

Browse files
Files changed (5) hide show
  1. README.md +36 -146
  2. config.json +9 -51
  3. model.safetensors +2 -2
  4. preprocessor_config.json +1 -2
  5. training_args.bin +2 -2
README.md CHANGED
@@ -3,173 +3,63 @@ license: mit
3
  tags:
4
  - image-classification
5
  - deepfake-detection
6
- - computer-vision
7
- - vision-transformer
8
- - sdxl
9
- - fake-face-detection
10
- datasets:
11
- - xhlulu/140k-real-and-fake-faces
12
- metrics:
13
- - accuracy
14
- - f1
15
- - precision
16
- - recall
17
- model-index:
18
- - name: SDXL-Deepfake-Detector
19
- results:
20
- - task:
21
- type: image-classification
22
- name: Image Classification
23
- dataset:
24
- name: 140k Real and Fake Faces
25
- type: xhlulu/140k-real-and-fake-faces
26
- metrics:
27
- - type: accuracy
28
- value: 0.97
29
- name: Accuracy
30
  ---
31
 
32
- # SDXL-Deepfake-Detector
33
- ### Detecting AI-Generated Faces with Precision and Purpose
34
 
35
- >*Not just another classifier — a tool for digital truth.*
36
- >
37
- Developed by **[SadraCoding](https://github.com/SadraCoding)**
38
 
39
- ---
40
-
41
- ## Why This Matters
42
- As generative AI (like SDXL, DALL·E, and Midjourney) becomes more accessible, the line between real and synthetic media blurs — especially for vulnerable communities. This project started as a technical experiment but evolved into a **privacy-aware, open-source defense** against visual misinformation, with a focus on **ethical AI deployment**.
43
-
44
- ---
45
-
46
- ## Model Overview
47
-
48
- **SDXL-Deepfake-Detector** is a fine-tuned vision transformer that classifies human faces as **artificial (0)** or **human (1)**, achieving an accuracy of **97%**.
49
 
50
- ## Training Approach
51
 
52
- This model was obtained by **fine-tuning** the [`Organika/sdxl-detector`](https://huggingface.co/Organika/sdxl-detector) — a vision transformer pre-trained specifically to detect SDXL-generated faces — on the [140k Real and Fake Faces](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces) dataset.
53
-
54
- This approach leverages:
55
- - Prior knowledge of SDXL artifacts from the base model
56
- - Broader generalization from a large-scale real/fake face dataset
57
- - Efficient training on limited hardware (single RTX 3060)
58
-
59
- The result is a lightweight, high-accuracy detector optimized for **both SDXL and general diffusion-based deepfakes**.
60
-
61
- ### Key Highlights
62
- - **Architecture**: Fine-tuned Vision Transformer (ViT) via Hugging Face `transformers`
63
- - **Dataset**: 140k balanced real/fake face images
64
- - **License**: [MIT](https://opensource.org/licenses/MIT) — free for research and commercial use
65
- - **Hardware**: Trained on a single NVIDIA RTX 3060 (12GB VRAM) — proving high impact doesn’t require massive resources
66
-
67
- ---
68
 
69
- ## Quick Start
70
 
71
- ### Dependencies
72
- ```bash
73
- pip install transformers torch pillow
74
- ```
75
- ### Python Script (If you face any issues, try using Transformers 4.36.2)
76
- ```python
77
- #predict.py
78
- import argparse
79
- from transformers import AutoModelForImageClassification, AutoFeatureExtractor
80
- from PIL import Image
81
- import torch
82
- import os
83
-
84
- def main():
85
- parser = argparse.ArgumentParser(
86
- description="Classify an image as 'artificial' or 'human' using the SDXL-Deepfake-Detector."
87
- )
88
- parser.add_argument("--image", type=str, required=True, help="Path to the input image file")
89
- args = parser.parse_args()
90
-
91
- # Validate image path
92
- if not os.path.isfile(args.image):
93
- raise FileNotFoundError(f"Image file not found: {args.image}")
94
-
95
- # Load model and feature extractor from Hugging Face Hub
96
- model_name = "SADRACODING/SDXL-Deepfake-Detector"
97
- print(f"Loading model '{model_name}'...")
98
- model = AutoModelForImageClassification.from_pretrained(model_name)
99
- feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
100
-
101
- # Set device (GPU if available)
102
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
103
- model.to(device)
104
- model.eval()
105
- print(f"Running on device: {device}")
106
-
107
- # Load and preprocess image
108
- image = Image.open(args.image).convert("RGB")
109
- inputs = feature_extractor(images=image, return_tensors="pt").to(device)
110
-
111
- # Inference
112
- with torch.no_grad():
113
- outputs = model(**inputs)
114
-
115
- logits = outputs.logits
116
- predicted_class_idx = logits.argmax(-1).item()
117
- predicted_label = model.config.id2label[predicted_class_idx]
118
-
119
- # Output
120
- print(f"Prediction Result")
121
- print(f"Class Index: {predicted_class_idx}")
122
- print(f"Label : {predicted_label}")
123
-
124
- if __name__ == "__main__":
125
- main()
126
- ```
127
- ### How to use
128
- ```bash
129
- python predict.py --image path/to/image
130
- ```
131
 
132
- ## Performance & Limitations
133
 
134
- > **Note**: Final test accuracy will be reported after full evaluation. Preliminary results show strong generalization on SDXL- and diffusion-based face forgeries.
 
 
 
135
 
136
- ### Known Limitations
137
- - Trained primarily on **frontal, well-lit, aligned face crops** — may underperform on:
138
- - Low-resolution or blurry images
139
- - Heavily occluded or non-frontal faces
140
- - GAN-generated faces (e.g., StyleGAN2/3)
141
- - Label mapping:
142
- - `0` → `"artificial"` (AI-generated / Deepfake)
143
- - `1` → `"human"` (authentic human face)
144
 
145
- > ⚠️ This tool is **not a forensic proof**, but a probabilistic detector. Use responsibly.
146
 
147
- ---
148
 
149
- ## Philosophy & Ethics
150
 
151
- This model is open-source because:
152
- - **Transparency** is essential in the fight against synthetic media.
153
- - **Accessibility** ensures researchers, journalists, and civil society can audit and use detection tools without gatekeeping.
154
- - **Privacy matters**: The model runs **entirely offline** — your images never leave your device.
155
 
156
- As a developer from a vulnerable community, I believe AI safety tools must be **inclusive, ethical, and human-centered** — not just technically accurate.
157
 
158
- ---
159
 
160
- ## Acknowledgements
 
 
161
 
162
- - **Dataset**: [140k Real and Fake Faces](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces) by xhlulu
163
- - **Framework**: [Hugging Face Transformers](https://huggingface.co/docs/transformers)
164
- - **Model & Code**: [GitHub Repository](https://github.com/SadraCoding/SDXL-Deepfake-Detector) | [Hugging Face Hub](https://huggingface.co/SADRACODING/SDXL-Deepfake-Detector)
165
 
166
- ---
167
 
168
- ## How to Contribute
169
 
170
- Fine-tune this model on your domain-specific data using Hugging Face `Trainer`.
171
 
172
- ---
173
 
174
- > *Built with curiosity, ethics, and a 12GB GPU — because impactful AI doesn’t require a data center, just purpose.*
175
- > — SadraCoding
 
3
  tags:
4
  - image-classification
5
  - deepfake-detection
6
+ - synthetic-image-detection
7
+ - swin-transformer
8
+ - frequency-analysis
9
+ library_name: transformers
10
+ base_model: Organika/sdxl-detector
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
+ # Spatial–Frequency Hybrid Detector for Synthetic Face Images
 
14
 
15
+ This repository contains a fine-tuned checkpoint for binary image classification. Given an input image, the model predicts one of two labels: `artificial` or `human`.
 
 
16
 
17
+ > **Experimental research model.** This classifier is not an authenticity or provenance verifier. Its predictions reflect its training data and preprocessing; they do not establish who created an image, whether a face is real, or whether an image was manipulated. Do not use it as the sole basis for high-impact decisions.
 
 
 
 
 
 
 
 
 
18
 
19
+ ## Links
20
 
21
+ - **GitHub source, scripts, report, and protocol:** [SadraCoding/SDXL-Deepfake-Detector](https://github.com/SadraCoding/SDXL-Deepfake-Detector)
22
+ - **Live demo:** [sdxldd.ir](https://sdxldd.ir)
23
+ - **Base checkpoint:** [Organika/sdxl-detector](https://huggingface.co/Organika/sdxl-detector)
24
+ - **Dataset referenced by the project:** [140k Real and Fake Faces](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces)
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ ## Model details
27
 
28
+ The model starts from the Swin image-classification checkpoint `Organika/sdxl-detector`. It combines pooled spatial features from Swin with a learned encoding of the input's centered 2D Fourier log-magnitude. A fusion classifier predicts the final label, and an auxiliary classifier is trained on the Swin features.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ The image processor resizes inputs to 224 × 224, rescales pixel values, and applies ImageNet channel normalization. The class mapping stored with this checkpoint is:
31
 
32
+ | ID | Label |
33
+ | --- | --- |
34
+ | 0 | `artificial` |
35
+ | 1 | `human` |
36
 
37
+ An optional adaptive inference path predicts first from the Swin features and runs the frequency branch for samples below a confidence threshold. The checkpoint's default threshold is 0.90. Softmax confidence should not be interpreted as calibrated probability unless calibration has been evaluated separately.
 
 
 
 
 
 
 
38
 
39
+ ## Intended use and limitations
40
 
41
+ The project references the Kaggle 140k Real and Fake Faces collection, a face-image dataset commonly described as containing real and StyleGAN-generated images. This is a narrow benchmark domain. It does not establish performance on unseen generators, face swaps, video, current text-to-image systems, or arbitrary images in the wild. Dataset version, split construction, and complete run provenance are not included with the checkpoint.
42
 
43
+ The project's checked-in metrics artifact reports 93.27% clean accuracy on one run of 10,905 images. Split provenance and other details needed to reproduce or independently interpret this result are incomplete; treat it as a historical result, not a generalization claim. The Hugging Face model page's previously reported 97% accuracy has different or undocumented evaluation details and should not be compared directly with that artifact.
44
 
45
+ Potential dataset shortcuts, distribution shift, image compression, preprocessing, identity overlap, and generator-specific artifacts can affect predictions. The project has not established that the frequency branch improves performance over a matched Swin-only baseline. See the GitHub [research report](https://github.com/SadraCoding/SDXL-Deepfake-Detector/blob/main/REVISED_PROJECT_REPORT.md) and [research protocol](https://github.com/SadraCoding/SDXL-Deepfake-Detector/blob/main/docs/research_protocol.md) for details.
 
 
 
46
 
47
+ ## Using the checkpoint
48
 
49
+ This checkpoint uses a custom architecture (`SwinFrequencyHybridDetector`), so the model implementation and inference scripts are provided in the GitHub repository. Follow its [installation and quick-start instructions](https://github.com/SadraCoding/SDXL-Deepfake-Detector#installation). In brief, clone the source, install the requirements and a compatible PyTorch build, then run the prediction script from `model/`:
50
 
51
+ ```bash
52
+ python scripts/predict.py --image /path/to/image.jpg
53
+ ```
54
 
55
+ The script selects CUDA when available and otherwise uses CPU. It reports the predicted class, confidence, adaptive-path status, measured model-call latency, and descriptive image diagnostics. Only process images you have permission to use.
 
 
56
 
57
+ ## Training data
58
 
59
+ The dataset is not included in this repository. The source project describes downloading it separately from [Kaggle](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces) and arranging it into `train/{fake,real}`, `val/{fake,real}`, and `test/{fake,real}` directories. Review the dataset's current access terms and image rights before use. Dataset terms are separate from the code license.
60
 
61
+ ## License and attribution
62
 
63
+ The project declares MIT for its original code. The initialized upstream model, derived checkpoint, and dataset may have separate terms. The upstream `Organika/sdxl-detector` card identifies a CC BY-NC 3.0 license and non-commercial restrictions; review its current terms before using or redistributing this checkpoint. The MIT license does not grant rights to third-party weights or dataset images.
64
 
65
+ Please cite the [source project](https://github.com/SadraCoding/SDXL-Deepfake-Detector), the [base model](https://huggingface.co/Organika/sdxl-detector), and the [dataset](https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces) as relevant to your work.
 
config.json CHANGED
@@ -1,63 +1,21 @@
1
  {
2
- "_num_labels": 2,
3
  "architectures": [
4
- "SwinForImageClassification"
5
  ],
6
- "attention_probs_dropout_prob": 0.0,
7
- "depths": [
8
- 2,
9
- 2,
10
- 18,
11
- 2
12
- ],
13
- "drop_path_rate": 0.1,
14
- "embed_dim": 128,
15
- "encoder_stride": 32,
16
- "hidden_act": "gelu",
17
- "hidden_dropout_prob": 0.0,
18
- "hidden_size": 1024,
19
  "id2label": {
20
  "0": "artificial",
21
  "1": "human"
22
  },
23
- "image_size": 224,
24
- "initializer_range": 0.02,
25
  "label2id": {
26
  "artificial": 0,
27
  "human": 1
28
  },
29
- "layer_norm_eps": 1e-05,
30
- "max_length": 128,
31
- "mlp_ratio": 4.0,
32
- "model_type": "swin",
33
- "num_channels": 3,
34
- "num_heads": [
35
- 4,
36
- 8,
37
- 16,
38
- 32
39
- ],
40
- "num_layers": 4,
41
- "out_features": [
42
- "stage4"
43
- ],
44
- "out_indices": [
45
- 4
46
- ],
47
- "padding": "max_length",
48
- "patch_size": 4,
49
- "path_norm": true,
50
- "problem_type": "single_label_classification",
51
- "qkv_bias": true,
52
- "stage_names": [
53
- "stem",
54
- "stage1",
55
- "stage2",
56
- "stage3",
57
- "stage4"
58
- ],
59
- "torch_dtype": "float32",
60
- "transformers_version": "4.53.2",
61
- "use_absolute_embeddings": false,
62
- "window_size": 7
63
  }
 
1
  {
 
2
  "architectures": [
3
+ "SwinFrequencyHybridDetector"
4
  ],
5
+ "base_model_name": "Organika/sdxl-detector",
6
+ "confidence_threshold": 0.9,
7
+ "dtype": "float32",
8
+ "early_exit_loss_weight": 0.25,
 
 
 
 
 
 
 
 
 
9
  "id2label": {
10
  "0": "artificial",
11
  "1": "human"
12
  },
 
 
13
  "label2id": {
14
  "artificial": 0,
15
  "human": 1
16
  },
17
+ "model_type": "swin-frequency-hybrid",
18
+ "num_classes": 2,
19
+ "transformers_version": "5.18.0",
20
+ "use_cache": false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:2c3436c0aaddb5e2a024d86fdfaedcb96a30f779795d940650d1597604d024f1
3
- size 347498816
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2fdacd32fb5e09d511930644dd09b7c2d075286e956f53b2748b6bf2243472c7
3
+ size 350406456
preprocessor_config.json CHANGED
@@ -1,5 +1,4 @@
1
  {
2
- "do_convert_rgb": null,
3
  "do_normalize": true,
4
  "do_rescale": true,
5
  "do_resize": true,
@@ -8,7 +7,7 @@
8
  0.456,
9
  0.406
10
  ],
11
- "image_processor_type": "ViTFeatureExtractor",
12
  "image_std": [
13
  0.229,
14
  0.224,
 
1
  {
 
2
  "do_normalize": true,
3
  "do_rescale": true,
4
  "do_resize": true,
 
7
  0.456,
8
  0.406
9
  ],
10
+ "image_processor_type": "ViTImageProcessor",
11
  "image_std": [
12
  0.229,
13
  0.224,
training_args.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:856c7893002b2c5a5df9fc7bbf451b2058b5eac0ca77c719c2d3b463d31c4487
3
- size 5777
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c652b7a37918d0f29c76a9b471a8186106495fdcfbb2c3c719ebcb1f93f1a1ee
3
+ size 5201