Update README.md
Browse files
README.md
CHANGED
@@ -1,147 +1,59 @@
|
|
1 |
---
|
|
|
|
|
|
|
2 |
tags:
|
3 |
-
- image-classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
AI
|
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 |
-
return result, confidence
|
42 |
-
|
43 |
-
# Example usage
|
44 |
-
# result, confidence = detect_image("path/to/image.jpg")
|
45 |
-
# print(f"Result: {result} (Confidence: {confidence:.2f}%)")
|
46 |
-
```
|
47 |
-
|
48 |
-
### Python Code Example for AI Source Detector
|
49 |
```python
|
50 |
-
from transformers import ViTImageProcessor, ViTForImageClassification
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
def detect_source(image_path):
|
59 |
-
image = Image.open(image_path)
|
60 |
-
inputs = processor(images=image, return_tensors="pt")
|
61 |
-
outputs = model(**inputs)
|
62 |
-
predictions = outputs.logits.softmax(dim=-1)
|
63 |
-
prediction_id = torch.argmax(predictions).item()
|
64 |
-
confidence = predictions[0][prediction_id].item() * 100
|
65 |
-
sources = ["Real Image", "Midjourney", "DALL-E", "Stable Diffusion"]
|
66 |
-
result = sources[prediction_id]
|
67 |
-
return result, confidence
|
68 |
-
|
69 |
-
# Example usage
|
70 |
-
# result, confidence = detect_source("path/to/image.jpg")
|
71 |
-
# print(f"Source: {result} (Confidence: {confidence:.2f}%)")
|
72 |
-
```
|
73 |
-
|
74 |
-
### Combined Analysis Code Example
|
75 |
-
```python
|
76 |
-
def analyze_image(image_path):
|
77 |
-
"""
|
78 |
-
Analyzes an image to detect if it is AI-generated and its source.
|
79 |
-
|
80 |
-
Args:
|
81 |
-
image_path: Path to the image file
|
82 |
-
|
83 |
-
Returns:
|
84 |
-
dict: Results containing detection and source information
|
85 |
-
"""
|
86 |
-
# Load models
|
87 |
-
detector = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
88 |
-
source_detector = ViTForImageClassification.from_pretrained("yaya36095/ai-source-detector")
|
89 |
-
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
90 |
-
|
91 |
-
# Open and process image
|
92 |
-
image = Image.open(image_path)
|
93 |
-
inputs = processor(images=image, return_tensors="pt")
|
94 |
-
|
95 |
-
# Get AI detection result
|
96 |
-
with torch.no_grad():
|
97 |
-
outputs = detector(**inputs)
|
98 |
-
predictions = outputs.logits.softmax(dim=-1)
|
99 |
-
|
100 |
-
is_ai = torch.argmax(predictions).item() == 1
|
101 |
-
ai_confidence = predictions[0][1].item() * 100
|
102 |
-
|
103 |
-
# Get source detection if it is AI
|
104 |
-
source = "Not AI Generated"
|
105 |
-
source_confidence = 0
|
106 |
-
|
107 |
-
if is_ai:
|
108 |
-
with torch.no_grad():
|
109 |
-
outputs = source_detector(**inputs)
|
110 |
-
predictions = outputs.logits.softmax(dim=-1)
|
111 |
-
|
112 |
-
source_id = torch.argmax(predictions).item()
|
113 |
-
sources = ["Real Image", "Midjourney", "DALL-E", "Stable Diffusion"]
|
114 |
-
source = sources[source_id]
|
115 |
-
source_confidence = predictions[0][source_id].item() * 100
|
116 |
-
|
117 |
-
return {
|
118 |
-
"is_ai": is_ai,
|
119 |
-
"ai_confidence": ai_confidence,
|
120 |
-
"source": source,
|
121 |
-
"source_confidence": source_confidence
|
122 |
-
}
|
123 |
-
|
124 |
-
# Example usage:
|
125 |
-
# result = analyze_image("path/to/image.jpg")
|
126 |
-
# print(f"AI Generated: {result['is_ai']}")
|
127 |
-
# print(f"AI Confidence: {result['ai_confidence']:.2f}%")
|
128 |
-
# print(f"Source: {result['source']}")
|
129 |
-
# print(f"Source Confidence: {result['source_confidence']:.2f}%")
|
130 |
-
```
|
131 |
-
|
132 |
-
FEATURES
|
133 |
-
--------
|
134 |
-
- Supports all image formats.
|
135 |
-
- Automatic image resizing.
|
136 |
-
- Confidence scores for predictions.
|
137 |
-
- Combined analysis (AI detection + Source).
|
138 |
-
|
139 |
-
LIMITATIONS
|
140 |
-
-----------
|
141 |
-
- Best with clear, high-quality images.
|
142 |
-
- May vary with heavily edited images.
|
143 |
-
- Requires good internet connection for first load.
|
144 |
-
|
145 |
-
For more information visit:
|
146 |
-
- [AI Image Detector](https://huggingface.co/yaya36095/ai-image-detector)
|
147 |
-
- [AI Source Detector](https://huggingface.co/yaya36095/ai-source-detector)
|
|
|
1 |
---
|
2 |
+
license: apache-2.0
|
3 |
+
library_name: timm
|
4 |
+
pipeline_tag: image-classification
|
5 |
tags:
|
6 |
+
- image-classification
|
7 |
+
- ai-detection
|
8 |
+
- vit
|
9 |
+
datasets:
|
10 |
+
- your-username/ai-generated-vs-real
|
11 |
+
metrics:
|
12 |
+
- accuracy
|
13 |
+
- f1
|
14 |
---
|
15 |
|
16 |
+
# AI Source Detector (ViT-Base)
|
17 |
+
|
18 |
+
Detects *and* classifies the source of AI-generated images into **five** classes
|
19 |
+
(`stable_diffusion`, `midjourney`, `dalle`, `real`, `other_ai`).
|
20 |
+
|
21 |
+
## Model Details
|
22 |
+
* **Architecture:** ViT-Base Patch-16 × 224
|
23 |
+
* **Parameters:** 86 M
|
24 |
+
* **Fine-tuning epochs:** 10
|
25 |
+
* **Optimizer:** AdamW (lr = 3e-5, wd = 0.01)
|
26 |
+
* **Hardware:** 1× NVIDIA RTX 4090 (24 GB)
|
27 |
+
|
28 |
+
## Training Data
|
29 |
+
| Class | Images |
|
30 |
+
|-------|-------:|
|
31 |
+
| Stable Diffusion | 12 000 |
|
32 |
+
| Midjourney | 10 500 |
|
33 |
+
| DALL-E 3 | 9 400 |
|
34 |
+
| Real | 11 800 |
|
35 |
+
| Other AI | 8 200 |
|
36 |
+
|
37 |
+
Total ≈ 52 k images - 80 % train / 10 % val / 10 % test.
|
38 |
+
|
39 |
+
## Evaluation
|
40 |
+
| Metric | Top-1 | Macro F1 |
|
41 |
+
|--------|------:|---------:|
|
42 |
+
| Validation | 92.8 % | 0.928 |
|
43 |
+
| Test | 91.6 % | 0.914 |
|
44 |
+
|
45 |
+
<details>
|
46 |
+
<summary>Confusion Matrix (click to open)</summary>
|
47 |
+
<img src="confusion_matrix.png" width="480">
|
48 |
+
</details>
|
49 |
+
|
50 |
+
## Usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
```python
|
52 |
+
from transformers import ViTImageProcessor, ViTForImageClassification, pipeline
|
53 |
+
classifier = pipeline(
|
54 |
+
task="image-classification",
|
55 |
+
model="yaya36095/ai-source-detector",
|
56 |
+
top_k=1
|
57 |
+
)
|
58 |
+
classifier("demo.jpg")
|
59 |
+
# → [{'label': 'stable_diffusion', 'score': 0.97}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|