|
|
| import os |
| import datetime |
| import re |
|
|
| def update_training_history(history_path, curr_date, time_str, model_name, dataset_name, epochs, accuracy, loss, status): |
| """ |
| Appends a new row to the TRAINING_HISTORY.md table. |
| """ |
| new_row = f"| **{curr_date}** | {time_str} | **{model_name}** | {dataset_name} | {epochs} | **{accuracy:.2f}%** | {loss:.4f} | {status} |" |
| |
| with open(history_path, 'a') as f: |
| f.write(new_row + "\n") |
| print(f"β
Updated {history_path}") |
|
|
| def update_model_card(card_path, model_name, accuracy, status_msg="Active"): |
| """ |
| Updates the 'Active Model' and 'Version History' in MODEL_CARD.md. |
| This is a simple append/replace strategy. |
| """ |
| if not os.path.exists(card_path): |
| print(f"β οΈ {card_path} not found.") |
| return |
|
|
| with open(card_path, 'r') as f: |
| content = f.read() |
|
|
| |
| |
| |
| |
| |
| |
| curr_date = datetime.datetime.now().strftime("%b %d, %Y") |
| |
| new_entry = f""" |
| ### {model_name} (Automated Run) |
| * **Training Date:** {curr_date} |
| * **Performance:** |
| * Validation Accuracy: **{accuracy:.2f}%** |
| * **Status:** {status_msg} |
| """ |
| |
| if "## Version History" in content: |
| parts = content.split("## Version History") |
| new_content = parts[0] + "## Version History\n" + new_entry + parts[1] |
| else: |
| new_content = content + "\n" + new_entry |
|
|
| with open(card_path, 'w') as f: |
| f.write(new_content) |
| print(f"β
Updated {card_path}") |
|
|
| def create_detailed_log(template_path, output_path, replacements): |
| """ |
| Reads the template file and replaces {{KEY}} with values from the replacements dict. |
| """ |
| if not os.path.exists(template_path): |
| print(f"β οΈ Template {template_path} not found. Skipping detailed log.") |
| return |
|
|
| try: |
| with open(template_path, 'r') as f: |
| content = f.read() |
|
|
| for key, value in replacements.items(): |
| placeholder = f"{{{{{key}}}}}" |
| content = content.replace(placeholder, str(value)) |
| |
| with open(output_path, 'w') as f: |
| f.write(content) |
| print(f"β
Created Detailed Log: {output_path}") |
| |
| except Exception as e: |
| print(f"β Error creating detailed log: {e}") |
|
|
| def update_detailed_history(history_path, model_name, acc, loss, architecture="EfficientNet-V2-S + Swin-V2-T"): |
| """ |
| Appends a new model entry to the DETAILED_HISTORY.md file. |
| """ |
| if not os.path.exists(history_path): |
| print(f"β οΈ {history_path} not found.") |
| return |
|
|
| curr_date = datetime.datetime.now().strftime("%b %d, %Y") |
| |
| new_entry = f""" |
| ## Model: {model_name} |
| |
| | Feature | Detail | |
| | :--- | :--- | |
| | **Filename** | `{model_name}.safetensors` | |
| | **Created On** | {curr_date} | |
| | **Model Architecture** | {architecture} | |
| | **Training Hardware** | Mac M4 (MPS Acceleration, AMP Enabled) | |
| |
| ### π― Performance Benchmarks |
| | Test Data | Accuracy | Loss | Verdict | |
| | :--- | :--- | :--- | :--- | |
| | **Universal Test (13 Sets)** | **{acc:.2f}%** | **{loss:.4f}** | π Automated Run | |
| |
| --- |
| """ |
| |
| try: |
| with open(history_path, 'a') as f: |
| f.write(new_entry) |
| print(f"β
Updated {history_path}") |
| except Exception as e: |
| print(f"β Error updating detailed history: {e}") |
|
|
| def update_huggingface_card(card_path, model_name, accuracy, loss, roc_auc, params_str="50.31 Million"): |
| """ |
| Regenerates the HuggingFace Model Card with latest metrics. |
| """ |
| template = f"""# {model_name} (Universal) - Hugging Face Model Card |
| |
| ## Model Description |
| |
| **{model_name} (Universal)** is a state-of-the-art deepfake detection model designed for **universal robustness**. Unlike previous iterations that specialized in specific datasets (like FaceForensics++), {model_name} is fine-tuned on a massive "Universal" dataset of **1.3 million images** from 13 different sources, allowing it to detect not just face swaps, but also modern AI-generated content (Stable Diffusion, Midjourney, DALL-E). |
| |
| * **Model Type:** Hybrid Binary Image Classifier |
| * **Architecture:** |
| * **RGB Branch:** EfficientNet-V2-S (Spatial Features) |
| * **ViT Branch:** Swin-Transformer-V2-T (Global Context) |
| * **Frequency Branch:** FFT-based CNN (Artifact Detection) |
| * **Patch Branch:** Local Texture Inconsistency |
| * **Parameters:** {params_str} |
| * **Input:** RGB images (256x256 pixels) |
| * **Output:** Probability Score (0.0 = Real, 1.0 = Fake) |
| * **License:** MIT |
| |
| --- |
| |
| ## π Performance Benchmarks |
| |
| {model_name} was benchmarked on a **100,000 image** subset randomly sampled from all 13 datasets. |
| |
| | Metric | Score | vs Mark-II (Previous Best) | |
| | :--- | :--- | :--- | |
| | **Accuracy** | **{accuracy:.2f}%** | π +19.97% | |
| | **Loss** | **{loss:.4f}** | π -1.2 (Huge Improvement) | |
| | **ROC-AUC** | **{roc_auc:.4f}** | Near Perfect | |
| | **Precision** | **97.26%** | Extremely Reliable | |
| |
| --- |
| |
| ## π§ Training Data |
| |
| The model was trained on a **Universal Mix** of ~1.3 Million images: |
| |
| 1. **FaceForensics++**: Deepfakes, FaceSwap, Face2Face, NeuralTextures (Core Logic) |
| 2. **GenAI Datasets**: Stable Diffusion v1.5/2.1/XL, Midjourney v5/v6, DALL-E 3 |
| 3. **Wild Deepfakes**: Collecting from open internet sources (`ddata`, `DeepFake`, etc.) |
| 4. **Augmentation Sets**: 5 variants of heavy augmentation (JPEG, Noise, Blur) |
| |
| --- |
| |
| ## π οΈ How to Use |
| |
| ### Installation |
| ```bash |
| pip install torch torchvision timm safetensors |
| ``` |
| |
| ### Inference Code |
| ```python |
| import torch |
| from safetensors.torch import load_model |
| from src.models import DeepfakeDetector |
| from src.dataset import DeepfakeDataset |
| |
| # 1. Initialize Model (Mark-V Architecture) |
| model = DeepfakeDetector(pretrained=False).to("cuda") |
| |
| # 2. Load Weights |
| load_model(model, "model/results/checkpoints/{model_name}.safetensors") |
| model.eval() |
| |
| # 3. Predict |
| img_tensor = preprocess_image("path/to/image.jpg") # (1, 3, 256, 256) |
| with torch.no_grad(): |
| logits = model(img_tensor) |
| prob = torch.sigmoid(logits).item() |
| |
| print(f"Fake Probability: {{prob:.4f}}") |
| ``` |
| |
| --- |
| |
| ## π Limitations |
| |
| * **Video Temporal Consistency:** {model_name} operates on *single frames*. For video analysis, it is recommended to aggregate scores across multiple frames. |
| * **Extreme Low Quality:** Accuracy may drop on images with dimensions < 64x64 pixels due to loss of textual artifacts. |
| |
| --- |
| |
| ## π¨βπ» Authors & Citation |
| |
| **Developed By:** Deepfake Detection Team (Project Mark Series) |
| **Date:** January 28, 2026 |
| |
| If you use this model, please cite: |
| > {model_name}: A Universal Hybrid Architecture for Robust Deepfake Detection (2026) |
| """ |
| try: |
| with open(card_path, 'w') as f: |
| f.write(template) |
| print(f"β
Regenerated HuggingFace Card: {card_path}") |
| except Exception as e: |
| print(f"β Error updating HuggingFace card: {e}") |
|
|