File size: 4,448 Bytes
202eff6
 
 
 
 
 
 
 
 
 
 
 
6ba63c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b45b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ba63c9
 
 
8b45b0c
 
 
 
6ba63c9
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import gradio as gr
import torch
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from huggingface_hub import hf_hub_download
from modeling.BaseModel import BaseModel
from modeling import build_model
from utilities.distributed import init_distributed
from utilities.arguments import load_opt_from_config_files
from utilities.constants import BIOMED_CLASSES
from inference_utils.inference import interactive_infer_image


def overlay_masks(image, masks, colors):
    overlay = image.copy()
    overlay = np.array(overlay, dtype=np.uint8)
    for mask, color in zip(masks, colors):
        overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype(
            np.uint8
        )
    return Image.fromarray(overlay)


def generate_colors(n):
    cmap = plt.get_cmap("tab10")
    colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)]
    return colors


def init_model():
    # Download model
    model_file = hf_hub_download(
        repo_id="microsoft/BiomedParse",
        filename="biomedparse_v1.pt",
        token=os.getenv("HF_TOKEN"),
    )

    # Initialize model
    conf_files = "configs/biomedparse_inference.yaml"
    opt = load_opt_from_config_files([conf_files])
    opt = init_distributed(opt)

    model = BaseModel(opt, build_model(opt)).from_pretrained(model_file).eval().cuda()
    with torch.no_grad():
        model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(
            BIOMED_CLASSES + ["background"], is_eval=True
        )

    return model


def predict(image, prompts):
    if not prompts:
        return None

    # Convert string input to list
    prompts = [p.strip() for p in prompts.split(",")]

    # Convert to RGB if needed
    if image.mode != "RGB":
        image = image.convert("RGB")

    # Get predictions
    pred_mask = interactive_infer_image(model, image, prompts)

    # Generate visualization
    colors = generate_colors(len(prompts))
    pred_overlay = overlay_masks(
        image, [1 * (pred_mask[i] > 0.5) for i in range(len(prompts))], colors
    )

    return pred_overlay


def run():
    global model
    model = init_model()

    demo = gr.Interface(
        fn=predict,
        inputs=[
            gr.Image(type="pil", label="Input Image"),
            gr.Textbox(
                label="Prompts",
                placeholder="Enter prompts separated by commas (e.g., neoplastic cells, inflammatory cells)",
            ),
        ],
        outputs=gr.Image(type="pil", label="Prediction"),
        title="BiomedParse Demo",
        description="Upload a biomedical image and enter prompts (separated by commas) to detect specific features.",
        examples=[
            ["examples/144DME_as_F.jpeg", "edema"],
            ["examples/C3_EndoCV2021_00462.jpg", "polyp"],
            ["examples/covid_1585.png", "left lung"],
            ["examples/covid_1585.png", "right lung"],
            ["examples/covid_1585.png", "COVID-19 infection"],
            ["examples/ISIC_0015551.jpg", "lesion"],
            ["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "lung nodule"],
            ["examples/LIDC-IDRI-0140_143_280_CT_lung.png", "COVID-19 infection"],
            [
                "examples/Part_1_516_pathology_breast.png",
                "connective tissue cells",
            ],
            [
                "examples/Part_1_516_pathology_breast.png",
                "neoplastic cells",
            ],
            [
                "examples/Part_1_516_pathology_breast.png",
                "neoplastic cells, inflammatory cells",
            ],
            ["examples/T0011.jpg", "optic disc"],
            ["examples/T0011.jpg", "optic cup"],
            ["examples/TCGA_HT_7856_19950831_8_MRI-FLAIR_brain.png", "glioma"],
        ],
    )

    demo.launch(server_name="0.0.0.0", server_port=7860)


if __name__ == "__main__":
    run()