Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
# AI Image Classification Model
|
| 5 |
+
|
| 6 |
+
This repository contains two trained classifiers, **XGBoost** and **CatBoost**, for AI image classification. These models are trained to distinguish between AI-generated and real human faces using embeddings extracted from the **AuraFace** model.
|
| 7 |
+
|
| 8 |
+
## Model Overview
|
| 9 |
+
|
| 10 |
+
- **AuraFace**: Used for extracting face embeddings from input images.
|
| 11 |
+
- **CatBoost & XGBoost**: Trained classifiers to predict if an image is AI-generated or real.
|
| 12 |
+
- **Dataset**: Trained using the [Real vs AI Generated Faces Dataset](https://www.kaggle.com/datasets/philosopher0808/real-vs-ai-generated-faces-dataset).
|
| 13 |
+
- **Preferred Model**: While both classifiers yield similar results, **CatBoost** is the preferred model.
|
| 14 |
+
|
| 15 |
+
## Pipeline
|
| 16 |
+
|
| 17 |
+
1. An image is passed to **AuraFace** to extract a 512-dimensional face embedding.
|
| 18 |
+
2. The embedding is converted into a pandas DataFrame.
|
| 19 |
+
3. The trained classifier (CatBoost/XGBoost) is used to make predictions.
|
| 20 |
+
|
| 21 |
+
## Model Usage
|
| 22 |
+
|
| 23 |
+
### Dependencies
|
| 24 |
+
|
| 25 |
+
```bash
|
| 26 |
+
pip install opencv-python catboost xgboost pandas numpy pillow huggingface_hub
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
### Loading AuraFace
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
from huggingface_hub import snapshot_download
|
| 33 |
+
from insightface.app import FaceAnalysis
|
| 34 |
+
import numpy as np
|
| 35 |
+
import cv2
|
| 36 |
+
|
| 37 |
+
# Download AuraFace model
|
| 38 |
+
snapshot_download(
|
| 39 |
+
"fal/AuraFace-v1",
|
| 40 |
+
local_dir="models/auraface",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Initialize AuraFace
|
| 44 |
+
face_app = FaceAnalysis(
|
| 45 |
+
name="auraface",
|
| 46 |
+
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
|
| 47 |
+
root="."
|
| 48 |
+
)
|
| 49 |
+
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Loading CatBoost Model
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
from catboost import CatBoostClassifier
|
| 56 |
+
|
| 57 |
+
# Load trained CatBoost model
|
| 58 |
+
ai_image_classifier = CatBoostClassifier()
|
| 59 |
+
ai_image_classifier.load_model('models/ai_image_classifier/cat_classifier.cbm')
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
### Classifying an Image
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
def classify_image(image_path):
|
| 66 |
+
# Load image
|
| 67 |
+
img = Image.open(image_path).convert("RGB")
|
| 68 |
+
img_array = np.array(img)[:, :, ::-1] # Convert to BGR for processing
|
| 69 |
+
|
| 70 |
+
# Detect faces and extract embedding
|
| 71 |
+
faces = face_app.get(img_array)
|
| 72 |
+
if not faces:
|
| 73 |
+
return "No face detected."
|
| 74 |
+
|
| 75 |
+
embedding = faces[0].normed_embedding
|
| 76 |
+
|
| 77 |
+
# Convert embedding to DataFrame
|
| 78 |
+
feature_columns = [f'feature_{i}' for i in range(512)]
|
| 79 |
+
embedding_df = pd.DataFrame([embedding], columns=feature_columns)
|
| 80 |
+
|
| 81 |
+
# Predict class
|
| 82 |
+
prediction = ai_image_classifier.predict(embedding_df)[0]
|
| 83 |
+
return "AI-generated" if prediction == 1 else "Real Face"
|
| 84 |
+
|
| 85 |
+
# Example Usage
|
| 86 |
+
image_path = "path/to/image.jpg"
|
| 87 |
+
result = classify_image(image_path)
|
| 88 |
+
print(f"Classification: {result}")
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Using XGBoost
|
| 92 |
+
|
| 93 |
+
XGBoost follows the same process. To use XGBoost instead, replace the `CatBoostClassifier` loading step with:
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
from xgboost import XGBClassifier
|
| 97 |
+
|
| 98 |
+
# Load trained XGBoost model
|
| 99 |
+
ai_image_classifier = XGBClassifier()
|
| 100 |
+
ai_image_classifier.load_model('models/ai_image_classifier/xgb_classifier.json')
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
## Acknowledgments
|
| 104 |
+
|
| 105 |
+
- **[AuraFace-v1](https://huggingface.co/fal/AuraFace-v1)** for face embeddings.
|
| 106 |
+
- **[Real vs AI Generated Faces Dataset](https://www.kaggle.com/datasets/philosopher0808/real-vs-ai-generated-faces-dataset)** for training data.
|