Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
zero = torch.Tensor([0]).cuda()
|
| 6 |
-
print(zero.device) # <-- 'cpu' 🤔
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
demo.launch()
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def conv_block(c_in, c_out):
|
| 11 |
+
return nn.Sequential(
|
| 12 |
+
nn.Conv2d(c_in, c_out, 3, padding=1),
|
| 13 |
+
nn.BatchNorm2d(c_out),
|
| 14 |
+
nn.ReLU(inplace=True),
|
| 15 |
+
nn.MaxPool2d(2),
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class SimpleCNN(nn.Module):
|
| 20 |
+
def __init__(self, n_classes):
|
| 21 |
+
super().__init__()
|
| 22 |
+
self.features = nn.Sequential(
|
| 23 |
+
conv_block(3, 32),
|
| 24 |
+
conv_block(32, 64),
|
| 25 |
+
conv_block(64, 128),
|
| 26 |
+
conv_block(128, 256),
|
| 27 |
+
)
|
| 28 |
+
self.pool = nn.AdaptiveAvgPool2d(1)
|
| 29 |
+
self.head = nn.Sequential(nn.Flatten(), nn.Dropout(0.4), nn.Linear(256, n_classes))
|
| 30 |
+
|
| 31 |
+
def forward(self, x):
|
| 32 |
+
return self.head(self.pool(self.features(x)))
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
MODEL_PATH = Path(__file__).parent / "cv01_model_full.pt"
|
| 36 |
+
ckpt = torch.load(MODEL_PATH, map_location="cpu")
|
| 37 |
+
CLASSES = ckpt["classes"]
|
| 38 |
+
IMG_SIZE = ckpt["img_size"]
|
| 39 |
+
model = SimpleCNN(len(CLASSES))
|
| 40 |
+
model.load_state_dict(ckpt["model_state"])
|
| 41 |
+
model.eval()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def classify(img):
|
| 45 |
+
if img is None:
|
| 46 |
+
return None
|
| 47 |
+
img = Image.fromarray(img) if isinstance(img, np.ndarray) else img
|
| 48 |
+
img = img.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
|
| 49 |
+
x = torch.from_numpy(np.array(img)).permute(2, 0, 1).float() / 255.0
|
| 50 |
+
x = ((x - 0.5) / 0.5).unsqueeze(0)
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
probs = torch.softmax(model(x), dim=1)[0].numpy()
|
| 53 |
+
return {c: float(p) for c, p in zip(CLASSES, probs)}
|
| 54 |
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=classify,
|
| 58 |
+
inputs=gr.Image(type="pil", label="Single blood cell image"),
|
| 59 |
+
outputs=gr.Label(num_top_classes=8, label="Prediction"),
|
| 60 |
+
title="Blood Cell Classifier",
|
| 61 |
+
description=(
|
| 62 |
+
"A CNN trained on the PBC dataset (8 normal peripheral blood cell types). "
|
| 63 |
+
"Works on single, centred cell images like the dataset; whole-field images or "
|
| 64 |
+
"other stains give unreliable results. Research prototype, not a diagnostic tool."
|
| 65 |
+
),
|
| 66 |
+
flagging_mode="never",
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|