π§ Deepfake Detection using Swin Transformer
A robust deepfake detection model leveraging the microsoft/swin-base-patch4-window7-224 architecture. Built and trained on the "Hemg/deepfake-and-real-images" dataset with oversampling and image augmentations to ensure class balance and improved generalization.
π Table of Contents
- π¦ Cell1: Install Required Packages
- π₯ Cell2: Load Dataset & Apply Oversampling
- π·οΈ Cell3: Label Mapping + Split Dataset
- π§ Cell4: Load Swin Transformer + Transforms
- π Cell5: Collator + Metrics + CSV/Plot Export
- βοΈ Cell6: Training Arguments + Trainer Setup
- π Cell7: Train and Evaluate
- π Cell8: Loss & Accuracy Plots
π¦ Cell1: Install Required Packages
pip install -q datasets transformers evaluate imbalanced-learn
pip install -U transformers
π₯ Cell2: Load Dataset & Apply Oversampling
Used the Hemg/deepfake-and-real-images dataset and applied RandomOverSampler from imblearn to balance the Real and Fake class distribution.
π·οΈ Cell3: Label Mapping + Split Dataset
Mapped class labels to integers using ClassLabel and split the dataset using stratification into 60% training and 40% testing.
π§ Cell4: Load Swin Transformer + Transforms
- Loaded Swin Transformer from Hugging Face Transformers.
- Defined different transforms for training and evaluation using
torchvision.transforms.
π Cell5: Collator + Metrics + CSV/Plot Export
- Used a custom collator to batch image tensors and labels.
- Saved evaluation metrics (
accuracy,precision,recall,f1) and confusion matrix as.csvand.png.
βοΈ Cell6: Training Arguments + Trainer Setup
- Trained using Hugging Face
Trainer. - Setup to run for 2 epochs with the best model saved based on the highest F1 score.
π Cell7: Train and Evaluate
Executed training and evaluation using the following:
trainer.train()
trainer.evaluate()
π Cell8: Loss & Accuracy Plots
Post-training visualizations generated:

π Files and Artifacts
classification_report.csvconfusion_matrix.csvconfusion_matrix_plot.pngloss_vs_steps.pngaccuracy_vs_steps.pngloss_vs_epoch.pngaccuracy_vs_epoch.pngloss_vs_walltime.pngaccuracy_vs_walltime.png
π§© Model Details
- Backbone:
microsoft/swin-base-patch4-window7-224 - Task: Binary Image Classification (Real vs Fake)
- Augmentation: Rotation, Sharpness
- Metrics: Accuracy, Precision, Recall, F1
π License
MIT License



