deep-detect-api / README.md
AdeelHassan's picture
files updated
c907362 verified
|
Raw
History Blame Contribute Delete
4.59 kB
metadata
title: Deep Detect Api
emoji: πŸ¦€
colorFrom: red
colorTo: blue
sdk: docker
app_port: 7860

Image_Detector β€” FastAPI Backend 🐍

This directory contains the complete Python backend for Deep-Detect. It exposes a REST API that accepts image uploads and returns an AI vs. Real classification result using a custom-trained PyTorch CNN.


πŸ“ Directory Structure

Image_Detector/
β”œβ”€β”€ app.py              # FastAPI application β€” server entry point, routes, CORS
β”œβ”€β”€ inference.py        # Model loading, image preprocessing, prediction pipeline
β”œβ”€β”€ model.py            # Custom CNN architecture (PyTorch nn.Module definition)
β”œβ”€β”€ predict.py          # Standalone Tkinter desktop GUI for local inference
β”œβ”€β”€ requirements.txt    # All Python package dependencies
β”œβ”€β”€ models/             # Trained model weights (gitignored β€” download separately)
β”‚   └── custom_cnn_standalone.pt   # TorchScript model (~103 MB)
└── notebooks/          # Jupyter notebooks for research and training
    β”œβ”€β”€ Preprocessing.ipynb         # Dataset loading, augmentation, visualization
    β”œβ”€β”€ Model_training.ipynb        # Full training loop with loss/accuracy tracking
    β”œβ”€β”€ Model_evaluation.ipynb      # Confusion matrix, ROC, per-class metrics
    └── Pretrained_Models.ipynb     # Experiments with ResNet50, EfficientNet, ViT

βš™οΈ Setup

1. Create Virtual Environment

cd Image_Detector

python -m venv venv

# Windows
.\venv\Scripts\activate

# macOS / Linux
source venv/bin/activate

2. Install Dependencies

pip install -r requirements.txt

Dependencies:

Package Version Purpose
fastapi β‰₯0.100.0 REST API framework
uvicorn β‰₯0.20.0 ASGI server
python-multipart β‰₯0.0.6 File upload parsing
torch β‰₯2.0.0 PyTorch deep learning
torchvision β‰₯0.15.0 Image transforms
pillow β‰₯9.5.0 Image I/O

3. Download Model Weights

The model file custom_cnn_standalone.pt is too large for GitHub (103 MB > 100 MB limit). Download it from the project's Releases page and place it at:

Image_Detector/models/custom_cnn_standalone.pt

πŸš€ Running the Server

python app.py

The server starts on http://0.0.0.0:8000. Verify it's healthy:

curl http://localhost:8000/

Expected response:

{
  "status": "healthy",
  "api_name": "Deep-Detect Image Classification Service",
  "model_architecture": "Custom CNN Standalone (PyTorch)",
  "device_running": "cpu"
}

πŸ”Œ API Endpoints

POST /predict

Classifies an uploaded image as Deep-Fake or Real.

curl -X POST http://localhost:8000/predict \
  -F "file=@/path/to/image.jpg"

Success Response:

{
  "prediction": "ai",
  "confidence": 94.85,
  "status": "success"
}
  • prediction: "ai" or "real"
  • confidence: percentage confidence (0–100)

🧠 Model Details

Property Value
Architecture Custom CNN (defined in model.py)
Input Size 224 Γ— 224 (RGB)
Normalization ImageNet mean/std
Output Single logit β†’ Sigmoid β†’ binary probability
Format TorchScript (.pt) β€” runs without class definition at load time
Inference Device CPU (configurable)

Inference Pipeline (see inference.py):

  1. Load image bytes β†’ convert to RGB PIL Image
  2. Resize to 224Γ—224
  3. Normalize with ImageNet statistics
  4. Run forward pass through TorchScript model
  5. Apply Sigmoid to raw logit β†’ confidence score
  6. Threshold at 0.5: < 0.5 β†’ "real", β‰₯ 0.5 β†’ "ai"

πŸ–₯️ Desktop Utility

Run local inference via a GUI without starting the API server:

python predict.py

Opens a Tkinter window where you can browse and classify local image files directly.


πŸ““ Notebooks

The notebooks/ directory contains the full ML research workflow:

Notebook Description
Preprocessing.ipynb Dataset exploration, augmentation strategy, class balance analysis
Model_training.ipynb Custom CNN training from scratch with loss curves and checkpointing
Model_evaluation.ipynb Confusion matrix, ROC-AUC, precision/recall metrics
Pretrained_Models.ipynb Transfer learning experiments: ResNet50, EfficientNet, ViT