AleksanderObuchowski
commited on
Create README.md
Browse files
README.md
CHANGED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
tags:
|
4 |
+
- medical
|
5 |
+
---
|
6 |
+
# MedImageInsight: Open-Source Medical Image Embedding Model
|
7 |
+
|
8 |
+
This repository provides a simplified implementation for using the MedImageInsight model, an open-source medical imaging embedding model presented in the paper [MedImageInsight: An Open-Source Embedding Model for General Domain Medical Imaging](https://arxiv.org/abs/2410.06542) by Noel C. F. Codella et al. The official guide to access the model from Microsoft is quite complicated, and it is arguable whether the model is truly open-source. This repository aims to make it easier to use the MedImageInsight model for various tasks, such as zero-shot classification, image embedding, and text embedding.
|
9 |
+
|
10 |
+
What we have done:
|
11 |
+
|
12 |
+
- Downloaded the models from azure
|
13 |
+
- Got rid of all the uncecessary files
|
14 |
+
- Got rid of uncecessary mlflow code to make standalone implementation
|
15 |
+
- Moved to uv for dependecy management
|
16 |
+
- Added multi-label classification
|
17 |
+
- Create example FastAPI service
|
18 |
+
|
19 |
+
## Usage
|
20 |
+
|
21 |
+
1. Clone the repository and navigate to the project directory.
|
22 |
+
|
23 |
+
Make sure you have git-lfs installed (https://git-lfs.com)
|
24 |
+
```bash
|
25 |
+
git lfs install
|
26 |
+
```
|
27 |
+
|
28 |
+
```bash
|
29 |
+
git clone https://huggingface.co/lion-ai/MedImageInsights
|
30 |
+
```
|
31 |
+
3. Install the required dependencies
|
32 |
+
We are using [uv](https://github.com/astral-sh/uv) package manager to simplyfy the installation.
|
33 |
+
|
34 |
+
To create a virual env simply run
|
35 |
+
```bash
|
36 |
+
uv sync
|
37 |
+
```
|
38 |
+
Or to run a single script just run
|
39 |
+
```bash
|
40 |
+
uv run example.py
|
41 |
+
```
|
42 |
+
|
43 |
+
Thats it!
|
44 |
+
|
45 |
+
## Examples
|
46 |
+
See to the `example.py` file.
|
47 |
+
### Zero-shot image classification
|
48 |
+
|
49 |
+
Here's an example of how to use the `MedImageInsight` class for zero-shot classification:
|
50 |
+
|
51 |
+
```python
|
52 |
+
# Initialize classifier
|
53 |
+
classifier = MedImageInsight(
|
54 |
+
model_dir="2024.09.27",
|
55 |
+
vision_model_name="medimageinsigt-v1.0.0.pt",
|
56 |
+
language_model_name="language_model.pth"
|
57 |
+
)
|
58 |
+
|
59 |
+
# Load model
|
60 |
+
classifier.load_model()
|
61 |
+
|
62 |
+
# Read image
|
63 |
+
image = base64.encodebytes(read_image("image.png")).decode("utf-8")
|
64 |
+
|
65 |
+
# Zero-shot classification
|
66 |
+
images = [image]
|
67 |
+
labels = ["normal", "Pneumonia", "unclear"]
|
68 |
+
results = classifier.predict(images, labels)
|
69 |
+
print(results)
|
70 |
+
```
|
71 |
+
### Multi-label zero-shot image classification
|
72 |
+
Run multi-label image classification (without softmax at the end)
|
73 |
+
```python
|
74 |
+
# Multilabel classification example
|
75 |
+
images = [image]
|
76 |
+
labels = ["normal", "Pneumonia", "Fracture", "Tumor"]
|
77 |
+
results = classifier.predict(images, labels, multilabel=True)
|
78 |
+
print(results)
|
79 |
+
|
80 |
+
```
|
81 |
+
### Image embeddings
|
82 |
+
|
83 |
+
```python
|
84 |
+
results = classifier.encode(images=images)
|
85 |
+
print(results["image_embeddings"])
|
86 |
+
```
|
87 |
+
|
88 |
+
### Text embeddings
|
89 |
+
```python
|
90 |
+
results = classifier.encode(texts=labels)
|
91 |
+
print(results["text_embeddings"])
|
92 |
+
```
|
93 |
+
### Flask FastAPI server
|
94 |
+
```bash
|
95 |
+
uv run flask_app.py
|
96 |
+
```
|
97 |
+
Go to to localhost:8000/docs to see the swagger.
|
98 |
+
|
99 |
+
The application provides endpoints for classification and image embeddings. Images have to be base64 encoded
|
100 |
+
|
101 |
+
|
102 |
+
## Roadmap
|
103 |
+
- [x] Basic implementation
|
104 |
+
- [x] Multilabel classification
|
105 |
+
- [x] FastAPI service
|
106 |
+
- [ ] HF compatible API (from_pretrained())
|
107 |
+
- [ ] Explainability
|
108 |
+
|
109 |
+
## Acknowledgments
|
110 |
+
|
111 |
+
This repository is based on the work presented in the paper "MedImageInsight: An Open-Source Embedding Model for General Domain Medical Imaging" by Noel C. F. Codella et al. ([arXiv:2410.06542](https://arxiv.org/abs/2410.06542)).
|