gradio app
Browse files- .vscode/settings.json +3 -0
- app.py +55 -0
- requirements.txt +5 -0
.vscode/settings.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"python-envs.defaultEnvManager": "ms-python.python:system"
|
| 3 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision.transforms import v2
|
| 3 |
+
import gradio
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from mae_model.models.linear_predictor import Predictor
|
| 6 |
+
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
LABELS = [
|
| 10 |
+
"adipose",
|
| 11 |
+
"background",
|
| 12 |
+
"debris",
|
| 13 |
+
"lymphocytes",
|
| 14 |
+
"mucus",
|
| 15 |
+
"smooth muscle",
|
| 16 |
+
"normal colon mucosa",
|
| 17 |
+
"cancer-associated stroma",
|
| 18 |
+
"colorectal adenocarcinoma epithelium",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
model = Predictor(n_labels=len(LABELS))
|
| 23 |
+
model.load_state_dict(torch.load("checkpoints/model_linear_v2_epoch_100.pt"))
|
| 24 |
+
model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
tf = v2.Compose([
|
| 29 |
+
v2.ToImage(),
|
| 30 |
+
v2.ToDtype(torch.float32, scale=True),
|
| 31 |
+
])
|
| 32 |
+
|
| 33 |
+
def predict(image):
|
| 34 |
+
if image is None:
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 38 |
+
img_tensor = tf(pil_img).unsqueeze(0).to(device)
|
| 39 |
+
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model(img_tensor)
|
| 42 |
+
probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 43 |
+
|
| 44 |
+
return {LABELS[i]: float(probabilities[i]) for i in range(len(LABELS))}
|
| 45 |
+
|
| 46 |
+
demo = gradio.Interface(
|
| 47 |
+
fn=predict,
|
| 48 |
+
inputs=gradio.Image(),
|
| 49 |
+
outputs=gradio.Label(num_top_classes=3),
|
| 50 |
+
title="OrganMNIST Image Classification",
|
| 51 |
+
description="Upload a tissue patch image for classification"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
pillow
|
| 5 |
+
git+https://github.com/HasanAli5/MAE-Model-MedMNIST-Predictor.git#egg=mae_model
|