CoolFace
Apppublic

Elliot89/Universal_Cross-Domain_Vision_Model

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
App README

๐Ÿฅ๐ŸŽพ Universal Cross-Domain Vision Model

A multi-backbone vision model that classifies images across medical X-ray pathologies and sports action domains using fine-tuned multi-modal attention fusion on top of four pretrained encoders.

![Hugging Face Space](https://huggingface.co/spaces/Elliot89/UniversalCross-DomainVision_Model) ![License: MIT](https://opensource.org/licenses/MIT)


๐Ÿง  Model Architecture

The model fuses features from four pretrained backbone encoders through a learned multi-head attention fusion layer:

BackboneSourceOutput Dim
BiomedCLIP ViT-B/16microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224512
ViT-B/16timm (ImageNet pretrained)512
ResNet-50timm (ImageNet pretrained)512
EfficientNet-B0timm (ImageNet pretrained)512

Each backbone's features are projected to a shared 512-dim space, then fused via an 8-head attention transformer block. The final classifier head outputs 14 class probabilities with an uncertainty estimate.

Image โ†’ [BiomedCLIP, ViT-B/16, ResNet-50, EfficientNet-B0]
      โ†’ Projection Adapters (per backbone)
      โ†’ 8-Head Attention Fusion
      โ†’ Classifier โ†’ 14 classes + Uncertainty estimate

๐Ÿท๏ธ Classes

DomainClasses
๐Ÿฅ Medical (X-ray)Normal, Pneumonia, COVID-19, Tuberculosis, Cardiomegaly, Rib Fracture, Lung Mass, Pleural Effusion
๐ŸŽพ SportsRunning, Jumping, Swimming, Cycling, Tennis, Football

๐Ÿš€ Running the Demo

Option 1 โ€” Hugging Face Spaces (live)

Visit the live demo โ€” no setup needed:

๐Ÿ‘‰ https://huggingface.co/spaces/Elliot89/Universal_Cross-Domain_Vision_Model

Upload any image and click Classify.

Option 2 โ€” Run locally

Requirements: Python 3.9+, ~4 GB RAM (CPU) or GPU recommended

bash
# 1. Clone this repo
git clone https://huggingface.co/spaces/Elliot89/Universal_Cross-Domain_Vision_Model
cd Universal_Cross-Domain_Vision_Model

# 2. Install dependencies
pip install -r requirements.txt

# 3. Launch
python app.py
# Opens at http://localhost:7860

Option 3 โ€” REST API

bash
# Start the API server
uvicorn api:app --host 0.0.0.0 --port 8000

# Classify an image file
curl -X POST http://localhost:8000/predict -F "file=@your_image.jpg"

# Classify from URL
curl -X POST http://localhost:8000/predict/url \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/xray.jpg"}'

Interactive API docs at http://localhost:8000/docs

Option 4 โ€” Google Colab

Open colab_deploy.ipynb in Colab, set runtime to T4 GPU, and run all cells.


๐Ÿ“ฆ Repository Structure

โ”œโ”€โ”€ app.py                  # Gradio web demo (main entry point)
โ”œโ”€โ”€ api.py                  # FastAPI REST inference server
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ”œโ”€โ”€ head_weights.pt         # Fine-tuned fusion + classifier weights (~25 MB)
โ”œโ”€โ”€ extract_head.py         # Utility: extract head weights from full checkpoint
โ”œโ”€โ”€ colab_deploy.ipynb      # One-click Google Colab notebook
โ””โ”€โ”€ README.md               # This file
Note on weights: The four backbone encoders (~1 GB total) are downloaded automatically from Hugging Face Hub at first startup and cached. Only the fine-tuned head (head_weights.pt, ~25 MB) is stored in this repo.

๐Ÿ”ง Training Details

SettingValue
Base modelBiomedCLIP (Microsoft), pretrained on PMC-15M medical image-text pairs
Additional backbonesViT-B/16, ResNet-50, EfficientNet-B0 (ImageNet pretrained via timm)
Medical dataSynthesized X-ray images across 8 pathology classes
Sports dataStanford40 action recognition dataset
Fusion8-head multi-head attention, 512-dim embedding space
OptimizerAdamW with cosine annealing LR schedule
RegularizationDropout (0.2), domain adversarial training

๐Ÿ“‹ API Response Format

json
{
  "top_prediction": {
    "label": "Pneumonia",
    "confidence": 0.412
  },
  "predictions": [
    { "label": "Pneumonia",       "confidence": 0.412 },
    { "label": "Normal",          "confidence": 0.238 },
    { "label": "COVID-19",        "confidence": 0.134 },
    { "label": "Tuberculosis",    "confidence": 0.089 },
    { "label": "Cardiomegaly",    "confidence": 0.061 },
    { "label": "Running",         "confidence": 0.044 },
    { "label": "Lung Mass",       "confidence": 0.031 },
    { "label": "Pleural Effusion","confidence": 0.021 }
  ]
}

โš™๏ธ Environment Variables

VariableDefaultDescription
PORT7860 (Gradio) / 8000 (API)Server port

๐Ÿ› ๏ธ Troubleshooting

Slow first startup โ€” The four backbones (~1 GB total) are downloaded from HF Hub on first run and cached. On HF Spaces this happens automatically during the build phase.

`head_weights.pt` not found โ€” The app still runs but uses random weights for the fusion and classifier layers. Predictions will not reflect actual training. Upload head_weights.pt to the repo to enable real predictions.

Out of memory โ€” The model runs on CPU if no GPU is detected. If memory is tight, reduce image resolution or comment out extra backbones in app.py.

Regenerating `head_weights.pt` from the original checkpoint โ€” If you have best_model_phase1.pt, run:

bash
python extract_head.py

This strips the large backbone weights (which are loaded from HF Hub) and saves only the fine-tuned layers (~25 MB) as head_weights.pt.


๐Ÿ“„ License

MIT โ€” see https://opensource.org/licenses/MIT


๐Ÿ™ Acknowledgements

  • โ€”Microsoft BiomedCLIP โ€” vision-language model pretrained on 15M medical image-text pairs from PubMed Central
  • โ€”Stanford40 โ€” sports and human action recognition dataset
  • โ€”timm โ€” PyTorch Image Models library
  • โ€”open_clip โ€” open source CLIP implementation
  • โ€”Gradio โ€” web demo framework
  • โ€”FastAPI โ€” REST API framework