Upload 3 files
Browse files- BTD_model.pth +3 -0
- Inference.py +32 -0
- TumorModel.py +34 -0
BTD_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:269506d46e3854e3561379a7d4eb977194e66430b781b36bc56fab481e224f17
|
3 |
+
size 178115794
|
Inference.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# inference.py
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from io import BytesIO
|
5 |
+
from PIL import Image
|
6 |
+
from torchvision import transforms
|
7 |
+
from TumorModel import TumorClassification
|
8 |
+
|
9 |
+
# 1) Preprocessing pipeline
|
10 |
+
_transform = transforms.Compose([
|
11 |
+
transforms.Grayscale(),
|
12 |
+
transforms.Resize((224, 224)),
|
13 |
+
transforms.ToTensor(),
|
14 |
+
transforms.Normalize([0.5], [0.5]),
|
15 |
+
])
|
16 |
+
|
17 |
+
# 2) Load model once
|
18 |
+
_model = TumorClassification()
|
19 |
+
_model.load_state_dict(torch.load("BTD_model.pth", map_location="cpu"))
|
20 |
+
_model.eval()
|
21 |
+
|
22 |
+
def inference(image_bytes):
|
23 |
+
"""
|
24 |
+
Hugging Face will pass the raw image bytes here.
|
25 |
+
Return {"label": <one of glioma, meningioma, notumor, pituitary>}.
|
26 |
+
"""
|
27 |
+
img = Image.open(BytesIO(image_bytes)).convert("RGB")
|
28 |
+
x = _transform(img).unsqueeze(0) # batch dimension
|
29 |
+
with torch.no_grad():
|
30 |
+
idx = torch.argmax(_model(x), dim=1).item()
|
31 |
+
labels = ["glioma", "meningioma", "notumor", "pituitary"]
|
32 |
+
return {"label": labels[idx]}
|
TumorModel.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
|
3 |
+
class TumorClassification(nn.Module):
|
4 |
+
def __init__(self):
|
5 |
+
super().__init__()
|
6 |
+
self.model = nn.Sequential(
|
7 |
+
nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1),
|
8 |
+
nn.ReLU(),
|
9 |
+
nn.MaxPool2d(2),
|
10 |
+
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
|
11 |
+
nn.ReLU(),
|
12 |
+
nn.MaxPool2d(2),
|
13 |
+
nn.Flatten(),
|
14 |
+
nn.Linear(32 * 56 * 56, 128),
|
15 |
+
nn.ReLU(),
|
16 |
+
nn.Linear(128, 4) # 4 classes: glioma, meningioma, notumor, pituitary
|
17 |
+
)
|
18 |
+
|
19 |
+
def forward(self, x):
|
20 |
+
return self.model(x)
|
21 |
+
|
22 |
+
class GliomaStageModel(nn.Module):
|
23 |
+
def __init__(self):
|
24 |
+
super().__init__()
|
25 |
+
self.model = nn.Sequential(
|
26 |
+
nn.Linear(9, 128),
|
27 |
+
nn.ReLU(),
|
28 |
+
nn.Linear(128, 64),
|
29 |
+
nn.ReLU(),
|
30 |
+
nn.Linear(64, 4) # 4 glioma stages
|
31 |
+
)
|
32 |
+
|
33 |
+
def forward(self, x):
|
34 |
+
return self.model(x)
|