Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
from torchvision import transforms, datasets, models
|
| 5 |
-
from PIL import Image
|
| 6 |
-
|
| 7 |
-
# Title
|
| 8 |
-
st.title("Brain Tumor Classification")
|
| 9 |
-
|
| 10 |
-
# Class names
|
| 11 |
-
class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
| 12 |
-
|
| 13 |
-
# Load pre-trained ResNet18 model
|
| 14 |
-
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
|
| 15 |
-
num_of_classes = len(class_names)
|
| 16 |
-
num_of_features = model.fc.in_features
|
| 17 |
-
model.fc = nn.Linear(num_of_features, num_of_classes)
|
| 18 |
-
|
| 19 |
-
# Load trained model weights
|
| 20 |
-
model.load_state_dict(torch.load('resnet18_model (1).pth', map_location=torch.device('cpu')))
|
| 21 |
-
model.eval()
|
| 22 |
-
|
| 23 |
-
# Image upload
|
| 24 |
-
uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 25 |
-
|
| 26 |
-
if uploaded_img is not None:
|
| 27 |
-
# Display uploaded image
|
| 28 |
-
image = Image.open(uploaded_img)
|
| 29 |
-
st.image(image, caption="Uploaded Image",
|
| 30 |
-
|
| 31 |
-
# Image transformations
|
| 32 |
-
sample_transform = transforms.Compose([
|
| 33 |
-
transforms.Resize((224, 224)),
|
| 34 |
-
transforms.ToTensor(),
|
| 35 |
-
transforms.Normalize(mean=[0.1776, 0.1776, 0.1776], std=[0.1735, 0.1735, 0.1735])
|
| 36 |
-
])
|
| 37 |
-
|
| 38 |
-
# Apply transformations
|
| 39 |
-
transformed_img = sample_transform(image).unsqueeze(0)
|
| 40 |
-
|
| 41 |
-
# Model inference
|
| 42 |
-
with torch.no_grad():
|
| 43 |
-
pred = model(transformed_img).argmax(dim=1).item()
|
| 44 |
-
|
| 45 |
-
# Display prediction
|
| 46 |
-
st.success(f"Predicted Class: {class_names[pred]}")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchvision import transforms, datasets, models
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Title
|
| 8 |
+
st.title("Brain Tumor Classification")
|
| 9 |
+
|
| 10 |
+
# Class names
|
| 11 |
+
class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
|
| 12 |
+
|
| 13 |
+
# Load pre-trained ResNet18 model
|
| 14 |
+
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
|
| 15 |
+
num_of_classes = len(class_names)
|
| 16 |
+
num_of_features = model.fc.in_features
|
| 17 |
+
model.fc = nn.Linear(num_of_features, num_of_classes)
|
| 18 |
+
|
| 19 |
+
# Load trained model weights
|
| 20 |
+
model.load_state_dict(torch.load('resnet18_model (1).pth', map_location=torch.device('cpu')))
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
# Image upload
|
| 24 |
+
uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 25 |
+
|
| 26 |
+
if uploaded_img is not None:
|
| 27 |
+
# Display uploaded image
|
| 28 |
+
image = Image.open(uploaded_img)
|
| 29 |
+
st.image(image, caption="Uploaded Image", use_container_width =True)
|
| 30 |
+
|
| 31 |
+
# Image transformations
|
| 32 |
+
sample_transform = transforms.Compose([
|
| 33 |
+
transforms.Resize((224, 224)),
|
| 34 |
+
transforms.ToTensor(),
|
| 35 |
+
transforms.Normalize(mean=[0.1776, 0.1776, 0.1776], std=[0.1735, 0.1735, 0.1735])
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
+
# Apply transformations
|
| 39 |
+
transformed_img = sample_transform(image).unsqueeze(0)
|
| 40 |
+
|
| 41 |
+
# Model inference
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
pred = model(transformed_img).argmax(dim=1).item()
|
| 44 |
+
|
| 45 |
+
# Display prediction
|
| 46 |
+
st.success(f"Predicted Class: {class_names[pred]}")
|