Spaces:
Runtime error
Runtime error
Upload 8 files
Browse files- mri_classifier/app.py +64 -0
- mri_classifier/efficientnet_mri_model.pth +3 -0
- mri_classifier/examples/Copy of Te-me_0223.jpg +0 -0
- mri_classifier/examples/Copy of Te-no_0040.jpg +0 -0
- mri_classifier/examples/Copy of Te-pi_0054.jpg +0 -0
- mri_classifier/examples/Copy of Tr-gl_1277.jpg +0 -0
- mri_classifier/model.py +23 -0
- mri_classifier/requirements.txt +4 -0
mri_classifier/app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from model import create_efficientb2_model
|
| 7 |
+
from timeit import default_timer as timer
|
| 8 |
+
|
| 9 |
+
class_names = [
|
| 10 |
+
"glioma",
|
| 11 |
+
"meningioma",
|
| 12 |
+
"notumor",
|
| 13 |
+
"pituitary"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
efficientb2, transforms = create_efficientb2_model(num_classes=4)
|
| 17 |
+
|
| 18 |
+
efficientb2.load_state_dict(
|
| 19 |
+
torch.load(
|
| 20 |
+
f="efficientnet_mri_model.pth",
|
| 21 |
+
map_location=torch.device("cpu")
|
| 22 |
+
)
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def predict_img(img):
|
| 26 |
+
|
| 27 |
+
start_time = timer()
|
| 28 |
+
|
| 29 |
+
img = transforms(img).unsqueeze(0)
|
| 30 |
+
|
| 31 |
+
efficientb2.eval()
|
| 32 |
+
|
| 33 |
+
with torch.inference_mode():
|
| 34 |
+
|
| 35 |
+
pred_probs = torch.softmax(efficientb2(img), dim=1)
|
| 36 |
+
|
| 37 |
+
pred_labels_and_probs = {
|
| 38 |
+
class_names[i] : float(pred_probs[0][i]) for i in range(len(class_names))
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
pred_time = round(timer() - start_time(),5)
|
| 42 |
+
|
| 43 |
+
return pred_labels_and_probs, pred_time
|
| 44 |
+
|
| 45 |
+
title = "MRI Result Finder"
|
| 46 |
+
description = "Efficientnet b2 model to classify MRI images"
|
| 47 |
+
article = "Created at 2026"
|
| 48 |
+
|
| 49 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=predict_img,
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Label(num_top_classes=4,label="Predictions"),
|
| 55 |
+
gr.Number(label="Prediction Time")
|
| 56 |
+
],
|
| 57 |
+
examples = example_list,
|
| 58 |
+
title = title,
|
| 59 |
+
description = description,
|
| 60 |
+
article = article
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo.lunch()
|
mri_classifier/efficientnet_mri_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc2dfe58e56a2b31937368faa189edbe42305f03760359665151389141c78380
|
| 3 |
+
size 31365477
|
mri_classifier/examples/Copy of Te-me_0223.jpg
ADDED
|
mri_classifier/examples/Copy of Te-no_0040.jpg
ADDED
|
mri_classifier/examples/Copy of Te-pi_0054.jpg
ADDED
|
mri_classifier/examples/Copy of Tr-gl_1277.jpg
ADDED
|
mri_classifier/model.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision
|
| 4 |
+
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
def create_efficientb2_model(
|
| 8 |
+
num_classes: int=4,
|
| 9 |
+
seed: int=42):
|
| 10 |
+
|
| 11 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 12 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
| 13 |
+
auto_transform = weights.transforms()
|
| 14 |
+
|
| 15 |
+
for params in model.parameters():
|
| 16 |
+
params.requires_grad = False
|
| 17 |
+
|
| 18 |
+
model.classifier = nn.Sequential(
|
| 19 |
+
nn.Dropout(p=0.3,inplace=True),
|
| 20 |
+
nn.Linear(1408,num_classes)
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
return model, auto_transform
|
mri_classifier/requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
torch==1.12.0
|
| 3 |
+
torchvision==0.13.0
|
| 4 |
+
gradio==3.1.4
|