File size: 4,159 Bytes
e6cb34f
 
 
 
 
 
 
 
 
 
 
 
d611a6e
 
 
 
 
 
 
 
 
 
e6cb34f
 
 
d611a6e
 
 
 
e6cb34f
 
 
d611a6e
 
 
 
e6cb34f
 
 
d611a6e
 
 
 
 
 
 
e6cb34f
d611a6e
 
e6cb34f
d611a6e
e6cb34f
d611a6e
e6cb34f
 
 
d611a6e
e6cb34f
 
 
 
 
 
 
 
 
 
 
d611a6e
 
 
 
 
 
 
 
 
 
e6cb34f
 
 
 
 
d611a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cb34f
 
 
 
 
d611a6e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---

title: Medical Image Analysis Tool
emoji: 🏥
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: "4.0.0"
app_file: app.py
pinned: false
license: mit
---


# 🏥 Medical Image Analysis Tool

An AI-powered medical image analysis application using advanced detection models and large language models for medical image interpretation.

## Features

- **Advanced Object Detection**: Uses RF-DETR (Real-time Fine-grained Detection Transformer) for precise object detection
- **Medical AI Analysis**: Integrates MedGemma, a specialized medical vision-language model
- **Interactive Interface**: Built with Gradio for easy web-based interaction
- **Configurable Thresholds**: Adjustable confidence thresholds for detection sensitivity
- **Model Size Selection**: Choose between MedGemma 4B (faster) or 27B (more accurate) models
- **GPU Acceleration**: Optimized for GPU usage when available with 4-bit quantization
- **Automatic Model Downloads**: Models download automatically from Hugging Face Hub

## Models Used

- **RF-DETR Medium**: State-of-the-art object detection model
- **MedGemma 4B/27B**: Medical-specialized vision-language models for analysis and descriptions
  - 4B model: Faster inference, lower memory usage
  - 27B model: Higher accuracy, requires more resources

## Usage

1. **Upload Image**: Click on the image upload area or drag and drop a medical image
2. **Adjust Settings**:
   - Use the confidence threshold slider to control detection sensitivity
   - Select model size (4B for speed, 27B for accuracy)
3. **Analyze**: Click "Analyze Image" to run the AI analysis
4. **View Results**: See the annotated image with detected objects and AI-generated descriptions

## Installation & Setup

This application is designed to run on Hugging Face Spaces. The following files are required:

- `app.py` - Main application file (optimized for Spaces)
- `requirements.txt` - Python dependencies
- `packages.txt` - System packages
- `README.md` - This documentation

## Model Loading

**RF-DETR Model:**
- Upload your trained `rf-detr-medium.pth` file to the Space
- The application will automatically find and load it

**MedGemma Models:**
- Models download automatically from Hugging Face Hub on first use
- No manual installation required
- Choose between 4B (faster) or 27B (more accurate) models

## Space Configuration

For optimal performance, configure your Space settings:
- **Hardware**: GPU (T4 minimum, A100 recommended for 27B models)
- **Storage**: Enable persistent storage for model caching
- **Timeout**: 30+ minutes for large model downloads

## Technical Details

- **Framework**: PyTorch + Transformers
- **Interface**: Gradio
- **Computer Vision**: OpenCV, PIL, Supervision
- **Hardware**: Optimized for both CPU and GPU inference

## Performance Tips

- **Model Selection**: Use MedGemma 4B for faster processing or 27B for higher accuracy
- **Confidence Thresholds**: Higher values reduce false positives but may miss subtle findings
- **GPU Acceleration**: The application automatically uses GPU acceleration when available
- **Memory Optimization**: Uses 4-bit quantization to reduce memory usage
- **Model Caching**: Models are cached after first load for faster subsequent analyses

## Limitations

- Requires significant computational resources for optimal performance
- Best suited for medical imaging applications
- Results should be verified by qualified medical professionals

## Development

To run locally:

```bash

pip install -r requirements.txt

python app.py

```

**Note**: For local development, you'll need to:
1. Install the RF-DETR package or ensure it's available
2. Place your `rf-detr-medium.pth` file in the project directory
3. Models will download automatically on first run

## License

This project is for research and educational purposes. Medical applications should be developed and validated according to appropriate regulatory standards.

## Support

For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.