|
|
--- |
|
|
language: en |
|
|
license: apache-2.0 |
|
|
tags: |
|
|
- medical |
|
|
- chest-xray |
|
|
- tuberculosis |
|
|
- vision-transformer |
|
|
- pytorch |
|
|
- image-classification |
|
|
metrics: |
|
|
- accuracy |
|
|
- precision |
|
|
- recall |
|
|
- f1 |
|
|
pipeline_tag: image-classification |
|
|
--- |
|
|
|
|
|
# Tuberculosis Detection ViT Model |
|
|
|
|
|
This repository contains a Vision Transformer (ViT) model for classifying chest X-ray images as **Normal** or **Tuberculosis**. |
|
|
|
|
|
## Model Details |
|
|
|
|
|
- **Model Type**: Vision Transformer (ViT) |
|
|
- **Framework**: PyTorch 2.0.1 |
|
|
- **Architecture**: |
|
|
- Patch size: 16 |
|
|
- Number of patches: 196 |
|
|
- Projection dimension: 512 |
|
|
- Number of attention heads: 8 |
|
|
- Number of encoder blocks: 6 |
|
|
- **Input Size**: 224x224 RGB images |
|
|
- **Classes**: Normal, Tuberculosis |
|
|
|
|
|
## Dataset |
|
|
- **Source**: TB Chest Radiography Database |
|
|
- **Preprocessing**: Grayscale conversion, CLAHE enhancement, Gaussian blur, RGB conversion, standardization |
|
|
- **Training**: Early stopping with validation loss monitoring |
|
|
|
|
|
## Usage |
|
|
|
|
|
### Quick Start |
|
|
```python |
|
|
from huggingface_hub import hf_hub_download |
|
|
import torch |
|
|
import cv2 |
|
|
import numpy as np |
|
|
|
|
|
# Download the handler |
|
|
handler_path = hf_hub_download(repo_id="sukhmani1303/tuberculosis-vit-model", filename="handler.py") |
|
|
|
|
|
# Import and use |
|
|
exec(open(handler_path).read()) |
|
|
classifier = TBClassifier() |
|
|
|
|
|
# Load and predict |
|
|
image = cv2.imread("path/to/chest_xray.jpg") |
|
|
result = classifier.predict(image) |
|
|
print(f"Prediction: {result['prediction']}, Confidence: {result['confidence']:.4f}") |
|
|
``` |
|
|
|
|
|
### Loading the PyTorch Model |
|
|
```python |
|
|
from huggingface_hub import hf_hub_download |
|
|
import torch |
|
|
import json |
|
|
|
|
|
# Download model files |
|
|
config_path = hf_hub_download(repo_id="sukhmani1303/tuberculosis-vit-model", filename="config.json") |
|
|
model_path = hf_hub_download(repo_id="sukhmani1303/tuberculosis-vit-model", filename="pytorch_model.bin") |
|
|
|
|
|
# Load configuration |
|
|
with open(config_path, 'r') as f: |
|
|
config = json.load(f) |
|
|
|
|
|
# Initialize your ViT model class with config |
|
|
# model = ViT(**config) # You'll need to have your ViT class available |
|
|
# model.load_state_dict(torch.load(model_path, map_location='cpu')) |
|
|
# model.eval() |
|
|
``` |
|
|
|
|
|
## Performance |
|
|
- **Training**: Early stopping with validation monitoring |
|
|
- **Evaluation**: Tested on held-out validation set |
|
|
- **Metrics**: Accuracy, Precision, Recall, F1-score available upon request |
|
|
|
|
|
## Preprocessing Pipeline |
|
|
1. Convert RGB to grayscale |
|
|
2. Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) |
|
|
3. Apply Gaussian blur (5x5 kernel) |
|
|
4. Resize to 224x224 |
|
|
5. Convert back to RGB format |
|
|
6. Normalize with z-score standardization |
|
|
|
|
|
## Medical Disclaimer |
|
|
⚠️ **IMPORTANT**: This model is for educational and research purposes only. It should not be used for actual medical diagnosis. Always consult qualified healthcare professionals for medical advice and diagnosis. |
|
|
|
|
|
## Citation |
|
|
If you use this model in your research, please cite: |
|
|
``` |
|
|
@misc{tuberculosis-vit-model, |
|
|
author = {Sukhmani}, |
|
|
title = {Tuberculosis Detection using Vision Transformer}, |
|
|
year = {2025}, |
|
|
publisher = {Hugging Face}, |
|
|
url = {https://huggingface.co/sukhmani1303/tuberculosis-vit-model} |
|
|
} |
|
|
``` |
|
|
|
|
|
## License |
|
|
Apache 2.0 |
|
|
|
|
|
## Contact |
|
|
For questions or issues, please open an issue in the repository. |
|
|
|