File size: 5,064 Bytes
98e9c51
5c3384d
 
 
 
 
982ac6c
65582b8
 
5c3384d
 
 
 
65582b8
 
 
 
 
 
 
 
 
5c3384d
 
 
 
 
 
982ac6c
5c3384d
 
 
 
 
 
 
 
bb06753
5c3384d
 
 
65582b8
 
 
 
fadf1fb
 
 
 
 
 
 
65582b8
 
 
 
 
8a7acf8
65582b8
fadf1fb
8a7acf8
65582b8
fadf1fb
ca73993
 
 
 
 
 
 
65582b8
 
fadf1fb
65582b8
 
fadf1fb
 
8a7acf8
 
65582b8
fadf1fb
 
65582b8
ddbe6bd
982ac6c
5c3384d
 
982ac6c
bb06753
 
 
 
 
982ac6c
93544dd
5c3384d
 
 
072dec6
2ff7019
072dec6
 
 
 
 
 
 
2ff7019
65582b8
 
2ff7019
fadf1fb
072dec6
 
 
982ac6c
072dec6
 
65582b8
2ff7019
fadf1fb
 
 
 
982ac6c
 
 
fadf1fb
 
 
982ac6c
 
 
fadf1fb
 
 
2ff7019
fadf1fb
65582b8
9ef5fd1
 
982ac6c
 
 
65582b8
 
fadf1fb
982ac6c
 
 
65582b8
 
5c3384d
 
93544dd
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import spaces
import torch
from torchvision.transforms import v2
import gradio
from PIL import Image
from huggingface_hub import hf_hub_download
from models.linear_predictor import Predictor
import numpy
import os

device = "cuda" if torch.cuda.is_available() else "cpu"

LABELS = [
    "Adipose",
    "Background",
    "Debris",
    "Lymphocytes",
    "Mucus",
    "Smooth Muscle",
    "Normal Colon Mucosa",
    "Cancer-associated Stroma",
    "Colorectal Adenocarcinoma Epithelium",
]

model = Predictor(n_labels=len(LABELS))

model_file = hf_hub_download(
    repo_id="Hali5/Mae-Model-MedMNIST-Predictor",
    filename="checkpoints/model_linear_v2_epoch_100.pt"
)

model.load_state_dict(torch.load(model_file,map_location=device))
model.to(device)
model.eval()

tf = v2.Compose([
    v2.ToImage(),
    v2.Resize((64, 64), antialias=True),
    v2.ToDtype(torch.float32, scale=True),
])

dataset = numpy.load("test_samples.npz")
images = dataset["images"]
labels = dataset["labels"]

val_dataset = numpy.load("val_samples.npz")
val_images = val_dataset["images"]
val_labels = val_dataset["labels"]

example_rows_test = []
example_rows_val = []

number_of_examples = len(labels)

os.makedirs("ui_examples", exist_ok=True)

for i in range(number_of_examples):

    img_array = images[i]
    val_img_arr = val_images[i]

    label_index = int(labels[i].item() if hasattr(labels[i], 'item') else labels[i])
    val_label_index = int(val_labels[i].item() if hasattr(val_labels[i], 'item') else val_labels[i])

    if img_array.max() <= 1.0:
        img_array = (img_array * 255).astype(numpy.uint8)
        val_img_arr = (val_img_arr * 255).astype(numpy.uint8)
    else:
        img_array = img_array.astype(numpy.uint8)
        val_img_arr = val_img_arr.astype(numpy.uint8)
    
    truth_label_text = LABELS[label_index] if label_index < len(LABELS) else f"Class {label_index}"
    val_truth_label_text = LABELS[val_label_index] if val_label_index < len(LABELS) else f"Class {val_label_index}"
    
    file_path = f"ui_examples/sample_{i}.jpg"
    val_file_path = f"ui_examples/val_sample_{i}.jpg"

    Image.fromarray(img_array, "RGB").save(file_path)
    Image.fromarray(val_img_arr, "RGB").save(val_file_path)

    example_rows_test.append([file_path, truth_label_text])
    example_rows_val.append([val_file_path, val_truth_label_text])

@spaces.GPU
def predict(image,truth_labels=True):
    if image is None:
        return None

    # uplouded image
    img_tensor = tf(image).unsqueeze(0).to(device)

    with torch.no_grad():
        outputs = model(img_tensor)
        print(outputs.shape)
        probabilities = torch.nn.functional.softmax(outputs.squeeze(0), dim=0)
    
    return {LABELS[i]: float(probabilities[i]) for i in range(len(LABELS))}

custom_css = """
.tab img{
    object-fit: fill !important;
    width: 100% !important;
    height: 100% !important;
    image-rendering: pixelated !important; /* Forces crisp pixel lines */
}
"""

with gradio.Blocks(css=custom_css) as demo:
    gradio.Markdown("# PathMNIST Image Classification")

    with gradio.Tab("Predict", elem_classes="tab"):
        gradio.Markdown("## Upload a tissue image for classification")
        with gradio.Row():
            input_img = gradio.Image(height=512, width=512)
            with gradio.Column():
                output_lbl = gradio.Label(num_top_classes=9)
                btn = gradio.Button("Predict")
                btn.click(fn=predict, inputs=input_img, outputs=output_lbl)

    with gradio.Tab("Examples (Validation)", elem_classes="tab"):
        gradio.Markdown("## Select an example below to test the model against the PathMNIST validation dataset")
        
        with gradio.Row():
            with gradio.Column():
                input_img_val_ex = gradio.Image(label="Selected Test Image", height=512, width=512)
                truth_box_val = gradio.Textbox(label="Ground Truth Label", interactive=False)
            output_lbl_val_ex = gradio.Label(num_top_classes=9, label="Model Prediction")
        
        gradio.Examples(
            examples=example_rows_val, 
            inputs=[input_img_val_ex, truth_box_val],
            outputs=output_lbl_val_ex,
            fn=predict,
            cache_examples=True,
        )

    with gradio.Tab("Examples (Test)", elem_classes="tab"):
        gradio.Markdown("## Select an example below to test the model against the PathMNIST test dataset")
        
        with gradio.Row():
            with gradio.Column():
                input_img_test_ex = gradio.Image(label="Selected Test Image", height=512, width=512)
                truth_box_test = gradio.Textbox(label="Ground Truth Label", interactive=False)
            output_lbl_test_ex = gradio.Label(num_top_classes=9, label="Model Prediction")
        
        gradio.Examples(
            examples=example_rows_test, 
            inputs=[input_img_test_ex, truth_box_test],
            outputs=output_lbl_test_ex,
            fn=predict,
            cache_examples=True,
        )

if __name__ == "__main__":
    demo.launch()