Update inference.py
Browse files- inference.py +76 -0
inference.py
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# Define the model class (must match training architecture)
|
| 8 |
+
class custom_brain_tumor_model(nn.Module):
|
| 9 |
+
def __init__(self, num_classes=4):
|
| 10 |
+
super(custom_brain_tumor_model, self).__init__()
|
| 11 |
+
self.conv_layers = nn.Sequential(
|
| 12 |
+
nn.Conv2d(1, 64, kernel_size=3, padding=1),
|
| 13 |
+
nn.BatchNorm2d(64),
|
| 14 |
+
nn.ReLU(),
|
| 15 |
+
nn.MaxPool2d(2, 2),
|
| 16 |
+
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
| 17 |
+
nn.BatchNorm2d(128),
|
| 18 |
+
nn.ReLU(),
|
| 19 |
+
nn.MaxPool2d(2, 2),
|
| 20 |
+
nn.Conv2d(128, 256, kernel_size=3, padding=1),
|
| 21 |
+
nn.BatchNorm2d(256),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.MaxPool2d(2, 2),
|
| 24 |
+
nn.Conv2d(256, 512, kernel_size=3, padding=1),
|
| 25 |
+
nn.BatchNorm2d(512),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.MaxPool2d(2, 2),
|
| 28 |
+
nn.Dropout(0.25)
|
| 29 |
+
)
|
| 30 |
+
self.fc_layers = nn.Sequential(
|
| 31 |
+
nn.Linear(512 * 8 * 8, 1024),
|
| 32 |
+
nn.ReLU(),
|
| 33 |
+
nn.Linear(1024, 512),
|
| 34 |
+
nn.ReLU(),
|
| 35 |
+
nn.Linear(512, 256),
|
| 36 |
+
nn.ReLU(),
|
| 37 |
+
nn.Linear(256, 128),
|
| 38 |
+
nn.ReLU(),
|
| 39 |
+
nn.Dropout(0.5),
|
| 40 |
+
nn.Linear(128, num_classes)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def forward(self, x):
|
| 44 |
+
x = self.conv_layers(x)
|
| 45 |
+
x = x.view(x.size(0), -1)
|
| 46 |
+
x = self.fc_layers(x)
|
| 47 |
+
return x
|
| 48 |
+
|
| 49 |
+
# Load the model
|
| 50 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 51 |
+
model = custom_brain_tumor_model(num_classes=4)
|
| 52 |
+
model.load_state_dict(torch.load("brain_tumor_custom_model.pth", map_location=device))
|
| 53 |
+
model.to(device)
|
| 54 |
+
model.eval()
|
| 55 |
+
|
| 56 |
+
# Transform for images
|
| 57 |
+
transform = transforms.Compose([
|
| 58 |
+
transforms.Resize((128, 128)),
|
| 59 |
+
transforms.ToTensor(),
|
| 60 |
+
transforms.Normalize([0.5], [0.5])
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
class_names = ['glioma', 'meningioma', 'no_tumor', 'pituitary']
|
| 64 |
+
|
| 65 |
+
# HF API inference function
|
| 66 |
+
def predict(image_bytes):
|
| 67 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("L")
|
| 68 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
outputs = model(image)
|
| 71 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)[0]
|
| 72 |
+
predicted_idx = torch.argmax(probabilities).item()
|
| 73 |
+
return {"class": class_names[predicted_idx], "confidence": float(probabilities[predicted_idx])}
|
| 74 |
+
|
| 75 |
+
def __call__(self, inputs):
|
| 76 |
+
return predict(inputs)
|