š X-Ray Image Search System
A production-ready prototype for searching medical X-ray images using both text descriptions and visual similarity. Built with state-of-the-art deep learning models for medical imaging applications.
š Features
ā Core Functionality
- Text-Based Search: Find X-rays using natural language (e.g., "chest X-ray with pneumonia")
- Image-Based Search: Upload an X-ray to find visually similar images
- Dual Model Architecture:
- CLIP (ViT-B/32) for text-to-image search
- ResNet50 for image-to-image similarity
- GPU Acceleration: Automatic CUDA detection and usage
- Smart Caching: Embeddings cached for fast subsequent searches
- Category Filtering: Filter results by anatomical categories
- Similarity Thresholding: Control result quality with adjustable thresholds
š Bonus Features
- Ranking scores displayed for all results
- Category filters (chest, spine, dental, etc.)
- Real-time similarity metrics
- Responsive grid layout
- Dataset statistics dashboard
š Project Structure
xray-search-system/
āāā data_processor.py # Dataset handling & embedding generation
āāā search_engine.py # Search logic (text & image)
āāā app.py # Streamlit UI
āāā requirements.txt # Python dependencies
āāā README.md # This file
āāā dataset/ # Dataset directory (you create this)
ā āāā images/ # X-ray images (.png)
ā āāā metadata.csv # Auto-generated metadata
ā āāā resnet_embeddings.pkl # Cached ResNet embeddings
ā āāā clip_embeddings.pkl # Cached CLIP embeddings
āāā screenshots/ # Demo screenshots (optional)
š Quick Start
1ļøā£ Installation
# Clone or download this project
cd xray-search-system
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\\Scripts\\activate
# Install dependencies
pip install -r requirements.txt
2ļøā£ Dataset Setup
Option A: NIH Chest X-ray Dataset (Recommended)
Visit: https://nihcc.app.box.com/v/ChestXray-NIHCC
Download images_001.tar.gz (~7GB, contains ~4,600 images)
Extract to dataset/images/
Option B: Custom Dataset
Create this structure:
dataset/
āāā images/
ā āāā image1.png
ā āāā image2.png
ā āāā ...
āāā metadata.csv (optional)
3ļøā£ Run the Application
streamlit run app.py
š» Usage
Text Search
- Select "Text Search" mode
- Enter a query like:
- "chest x-ray with pneumonia"
- "normal chest radiograph"
- "lung nodule"
- Adjust filters (number of results, similarity threshold, category)
- Click "Search"
Image Search
- Select "Image Search" mode
- Upload an X-ray image (PNG, JPG, JPEG)
- Adjust filters
- Click "Find Similar Images"
š§ Technical Details
Architecture
Text Search Pipeline:
Text Query ā CLIP Text Encoder ā Text Embedding (512-dim)
ā
Cosine Similarity
ā
Database CLIP Image Embeddings (N x 512)
ā
Top-K Results
Image Search Pipeline:
Query Image ā ResNet50 ā Feature Embedding (2048-dim)
ā
Cosine Similarity
ā
Database ResNet Embeddings (N x 2048)
ā
Top-K Results
š Troubleshooting
Issue: "Dataset not found"
Solution: Ensure dataset/images/ directory exists with PNG/JPG files
Issue: "CUDA out of memory"
Solution: System will auto-fallback to CPU
Issue: "No module named 'clip'"
Solution: Install CLIP from GitHub:
pip install git+https://github.com/openai/CLIP.git
š License & Attribution
Dataset: NIH Chest X-ray Dataset
- License: Public Domain
- Source: NIH Clinical Center
Code: MIT License
š¤ Contributing
For improvements or bug fixes:
- Test on subset (100 images) first
- Verify GPU and CPU modes both work
- Check embeddings cache correctly
- Ensure UI remains responsive
š§ Support
For issues:
- Check "Troubleshooting" section above
- Verify dataset setup is correct
- Ensure all dependencies installed
- Check GPU drivers if using CUDA
š Acknowledgments
- OpenAI for CLIP model
- NIH Clinical Center for chest X-ray dataset
- PyTorch and Streamlit teams
Built with ā¤ļø for medical image retrieval
