Spaces:
Sleeping
Sleeping
Commit
·
62a4c73
1
Parent(s):
af3faed
Initial commit with app.py, .gitignore, and requirements.txt
Browse files- .gitignore +29 -0
- app.py +71 -0
- requirements.txt +8 -0
.gitignore
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Python cache and bytecode
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*.so
|
5 |
+
|
6 |
+
# Virtual environments
|
7 |
+
env/
|
8 |
+
venv/
|
9 |
+
.venv/
|
10 |
+
|
11 |
+
# Model weights and large files
|
12 |
+
*.pth
|
13 |
+
*.pt
|
14 |
+
|
15 |
+
# Output images and plots
|
16 |
+
*.png
|
17 |
+
*.jpg
|
18 |
+
*.jpeg
|
19 |
+
|
20 |
+
# Gradio cached files
|
21 |
+
gradio_cached_examples/
|
22 |
+
|
23 |
+
# System/OS files
|
24 |
+
.DS_Store
|
25 |
+
Thumbs.db
|
26 |
+
|
27 |
+
# VS Code settings
|
28 |
+
.vscode/
|
29 |
+
.idea/
|
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchvision import transforms
|
4 |
+
from PIL import Image
|
5 |
+
from torchvision.transforms import InterpolationMode
|
6 |
+
from torchvision.models import efficientnet_b3
|
7 |
+
|
8 |
+
# Model setup
|
9 |
+
class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
10 |
+
model = efficientnet_b3(weights=None)
|
11 |
+
model.classifier[1] = torch.nn.Linear(in_features=1536, out_features=len(class_names))
|
12 |
+
|
13 |
+
model.load_state_dict(torch.load(
|
14 |
+
"Eff_net_b3_01_brain_tumor.pth",
|
15 |
+
map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
+
))
|
17 |
+
model.eval()
|
18 |
+
|
19 |
+
# Image transform
|
20 |
+
img_transform = transforms.Compose([
|
21 |
+
transforms.Resize(320, interpolation=InterpolationMode.BICUBIC),
|
22 |
+
transforms.CenterCrop(300),
|
23 |
+
transforms.ToTensor(),
|
24 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
25 |
+
std=[0.229, 0.224, 0.225])
|
26 |
+
])
|
27 |
+
|
28 |
+
# Prediction function
|
29 |
+
def predict(image):
|
30 |
+
transformed_image = img_transform(image).unsqueeze(0)
|
31 |
+
with torch.inference_mode():
|
32 |
+
preds = model(transformed_image)
|
33 |
+
probs = torch.softmax(preds, dim=1)
|
34 |
+
label_idx = torch.argmax(probs, dim=1).item()
|
35 |
+
class_label = class_names[label_idx]
|
36 |
+
confidence = probs[0, label_idx].item()
|
37 |
+
return class_label, confidence
|
38 |
+
|
39 |
+
# Gradio Blocks UI
|
40 |
+
with gr.Blocks(title="🧠 Brain Tumor MRI Classifier") as demo:
|
41 |
+
gr.Markdown("## 🧠 Brain Tumor Classifier (EfficientNet-B3)")
|
42 |
+
gr.Markdown("""
|
43 |
+
Upload an MRI scan of the brain, and this model will classify it as one of:
|
44 |
+
- **Glioma Tumor**
|
45 |
+
- **Meningioma Tumor**
|
46 |
+
- **Pituitary Tumor**
|
47 |
+
- **No Tumor**
|
48 |
+
|
49 |
+
Uses EfficientNet-B3 trained on labeled brain MRI dataset.
|
50 |
+
""")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
image_input = gr.Image(type="pil", label="Upload MRI Image")
|
55 |
+
predict_button = gr.Button("🔍 Predict")
|
56 |
+
clear_button = gr.Button("🧹 Clear")
|
57 |
+
|
58 |
+
with gr.Column():
|
59 |
+
output_label = gr.Label(label="Predicted Class")
|
60 |
+
confidence_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Confidence Score")
|
61 |
+
|
62 |
+
predict_button.click(fn=predict, inputs=image_input, outputs=[output_label, confidence_slider])
|
63 |
+
clear_button.click(lambda: (None, None), inputs=[], outputs=[image_input, output_label, confidence_slider])
|
64 |
+
|
65 |
+
gr.Markdown("---")
|
66 |
+
gr.Markdown(
|
67 |
+
"<center>👤 Developed by [Sagar Bisht](https://www.linkedin.com/in/sagarbisht123)</center>",
|
68 |
+
elem_id="footer"
|
69 |
+
)
|
70 |
+
|
71 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.1+cu113
|
2 |
+
torchvision==0.13.1+cu113
|
3 |
+
Pillow==10.4.0
|
4 |
+
gradio==3.4.0
|
5 |
+
numpy==1.24.3
|
6 |
+
python==3.9.21
|
7 |
+
tqdm==4.67.1
|
8 |
+
matplotlib==3.9.4
|