Spaces:
Running on Zero
Running on Zero
improved ui?
Browse files- .vscode/settings.json +3 -0
- TestSampleDataset.py +27 -0
- app.py +56 -18
- requirements.txt +1 -0
- test_samples.npz +3 -0
.vscode/settings.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"python-envs.defaultEnvManager": "ms-python.python:pipenv"
|
| 3 |
+
}
|
TestSampleDataset.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
from torch.utils.data import Dataset
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestDataset(Dataset):
|
| 7 |
+
|
| 8 |
+
def __init__(self, npz_path, transform=None):
|
| 9 |
+
data = np.load(npz_path)
|
| 10 |
+
|
| 11 |
+
self.images = data["images"]
|
| 12 |
+
self.labels = data["labels"]
|
| 13 |
+
self.transform = transform
|
| 14 |
+
|
| 15 |
+
def __len__(self):
|
| 16 |
+
return len(self.images)
|
| 17 |
+
|
| 18 |
+
def __getitem__(self, idx):
|
| 19 |
+
image = self.images[idx]
|
| 20 |
+
label = self.labels[idx]
|
| 21 |
+
|
| 22 |
+
if self.transform:
|
| 23 |
+
image = self.transform(image)
|
| 24 |
+
|
| 25 |
+
label = torch.tensor(label, dtype=torch.float32).squeeze()
|
| 26 |
+
|
| 27 |
+
return image, label
|
app.py
CHANGED
|
@@ -5,19 +5,23 @@ import gradio
|
|
| 5 |
from PIL import Image
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from models.linear_predictor import Predictor
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
LABELS = [
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
]
|
| 22 |
|
| 23 |
|
|
@@ -32,19 +36,37 @@ model.load_state_dict(torch.load(model_file,map_location=device))
|
|
| 32 |
model.to(device)
|
| 33 |
model.eval()
|
| 34 |
|
| 35 |
-
|
| 36 |
tf = v2.Compose([
|
| 37 |
v2.ToImage(),
|
| 38 |
v2.Resize((64, 64)),
|
| 39 |
v2.ToDtype(torch.float32, scale=True),
|
| 40 |
])
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@spaces.GPU
|
| 43 |
def predict(image):
|
| 44 |
if image is None:
|
| 45 |
return None
|
| 46 |
|
| 47 |
-
|
| 48 |
pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 49 |
|
| 50 |
img_tensor = tf(pil_img).unsqueeze(0).to(device)
|
|
@@ -55,13 +77,29 @@ def predict(image):
|
|
| 55 |
|
| 56 |
return {LABELS[i]: float(probabilities[i]) for i in range(len(LABELS))}
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from models.linear_predictor import Predictor
|
| 8 |
+
from torch.utils.data import Dataset
|
| 9 |
+
from TestSampleDataset import TestDataset
|
| 10 |
+
import numpy
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
LABELS = [
|
| 16 |
+
"Adipose",
|
| 17 |
+
"Background",
|
| 18 |
+
"Debris",
|
| 19 |
+
"Lymphocytes",
|
| 20 |
+
"Mucus",
|
| 21 |
+
"Smooth Muscle",
|
| 22 |
+
"Normal Colon Mucosa",
|
| 23 |
+
"Cancer-associated Stroma",
|
| 24 |
+
"Colorectal Adenocarcinoma Epithelium",
|
| 25 |
]
|
| 26 |
|
| 27 |
|
|
|
|
| 36 |
model.to(device)
|
| 37 |
model.eval()
|
| 38 |
|
|
|
|
| 39 |
tf = v2.Compose([
|
| 40 |
v2.ToImage(),
|
| 41 |
v2.Resize((64, 64)),
|
| 42 |
v2.ToDtype(torch.float32, scale=True),
|
| 43 |
])
|
| 44 |
|
| 45 |
+
dataset = numpy.load("test_samples.npz")
|
| 46 |
+
images = dataset["images"]
|
| 47 |
+
labels = dataset["labels"]
|
| 48 |
+
|
| 49 |
+
example_rows = []
|
| 50 |
+
number_of_examples = len(labels)
|
| 51 |
+
|
| 52 |
+
os.makedirs("ui_examples", exist_ok=True)
|
| 53 |
+
|
| 54 |
+
for i in range(number_of_examples):
|
| 55 |
+
img_array = images[i]
|
| 56 |
+
label_index = int(labels[i].item() if hasattr(labels[i], 'item') else labels[i])
|
| 57 |
+
|
| 58 |
+
truth_label_text = LABELS[label_index] if label_index < len(LABELS) else f"Class {label_index}"
|
| 59 |
+
|
| 60 |
+
file_path = f"ui_examples/sample_{i}.jpg"
|
| 61 |
+
Image.fromarray(img_array.astype("uint8"), "RGB").save(file_path)
|
| 62 |
+
|
| 63 |
+
example_rows.append([file_path, truth_label_text])
|
| 64 |
+
|
| 65 |
@spaces.GPU
|
| 66 |
def predict(image):
|
| 67 |
if image is None:
|
| 68 |
return None
|
| 69 |
|
|
|
|
| 70 |
pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 71 |
|
| 72 |
img_tensor = tf(pil_img).unsqueeze(0).to(device)
|
|
|
|
| 77 |
|
| 78 |
return {LABELS[i]: float(probabilities[i]) for i in range(len(LABELS))}
|
| 79 |
|
| 80 |
+
with gradio.Blocks() as demo:
|
| 81 |
+
gradio.Markdown("# PathMNIST Image Classification")
|
| 82 |
+
|
| 83 |
+
with gradio.Tab("Predict"):
|
| 84 |
+
gradio.Markdown("Upload a tissue patch image for classification.")
|
| 85 |
+
input_img = gradio.Image()
|
| 86 |
+
output_lbl = gradio.Label(num_top_classes=3)
|
| 87 |
+
btn = gradio.Button("Predict")
|
| 88 |
+
btn.click(fn=predict, inputs=input_img, outputs=output_lbl)
|
| 89 |
+
|
| 90 |
+
with gradio.Tab("Examples"):
|
| 91 |
+
gradio.Markdown("Click an example below to test the model against the PathMNIST test dataset.")
|
| 92 |
+
|
| 93 |
+
# Create a read-only text box to display the column for Truth Labels
|
| 94 |
+
truth_box = gradio.Textbox(label="Ground Truth Label", interactive=False)
|
| 95 |
+
|
| 96 |
+
gradio.Examples(
|
| 97 |
+
examples=example_rows, # Passes both the image file path and the text label
|
| 98 |
+
inputs=[input_img, truth_box], # Maps the data columns to both UI components
|
| 99 |
+
outputs=output_lbl,
|
| 100 |
+
fn=predict,
|
| 101 |
+
cache_examples=True,
|
| 102 |
+
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
gradio==6.24.0
|
| 2 |
huggingface_hub
|
| 3 |
torch
|
|
|
|
| 4 |
torchvision
|
| 5 |
pillow
|
| 6 |
git+https://github.com/HasanAli5/MAE-Model-MedMNIST-Predictor.git#egg=mae_model
|
|
|
|
| 1 |
gradio==6.24.0
|
| 2 |
huggingface_hub
|
| 3 |
torch
|
| 4 |
+
numpy
|
| 5 |
torchvision
|
| 6 |
pillow
|
| 7 |
git+https://github.com/HasanAli5/MAE-Model-MedMNIST-Predictor.git#egg=mae_model
|
test_samples.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:36bc98ea602b8a622548839906cbb2aaf67ab8cc17e78e94a1a08b0e342ede4a
|
| 3 |
+
size 2548100
|