Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,54 +7,36 @@ Original file is located at
|
|
7 |
https://colab.research.google.com/drive/1sjyLFLqBccpUzaUi4eyyP3NYE3gDtHfs
|
8 |
"""
|
9 |
|
10 |
-
import
|
11 |
-
from
|
12 |
-
|
|
|
|
|
|
|
13 |
import torch
|
14 |
import torch.nn as nn
|
15 |
import torch.nn.functional as F
|
16 |
from torchvision import transforms
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
def __init__(self):
|
21 |
-
super(MelanomaModel, self).__init__()
|
22 |
-
# Define your model layers here (example)
|
23 |
-
self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
|
24 |
-
self.fc1 = nn.Linear(64*224*224, 10)
|
25 |
-
|
26 |
-
def forward(self, x):
|
27 |
-
x = self.conv1(x)
|
28 |
-
x = x.view(x.size(0), -1)
|
29 |
-
x = self.fc1(x)
|
30 |
-
return x
|
31 |
-
|
32 |
-
# Model paths for all disease types
|
33 |
-
model_path_skin_disease = 'multi_weight.pth' # Skin Disease Model
|
34 |
-
|
35 |
-
model_path_alzheimers = 'alzheimers_model.pkl'
|
36 |
-
model_path_eye_disease = 'eye_disease_model.pkl'
|
37 |
-
|
38 |
-
# Load models with CPU mapping
|
39 |
-
skin_disease_model = torch.load(model_path_skin_disease, map_location=torch.device('cpu')) # For Skin Disease model
|
40 |
-
|
41 |
-
alzheimers_model = load_learner(model_path_alzheimers, cpu=True)
|
42 |
-
eye_disease_model = load_learner(model_path_eye_disease, cpu=True)
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
7: 'Squamous cell carcinoma',
|
54 |
-
8: 'Unknown'
|
55 |
-
}
|
56 |
|
57 |
-
#
|
58 |
transform = transforms.Compose([
|
59 |
transforms.Resize(256),
|
60 |
transforms.CenterCrop(224),
|
@@ -62,82 +44,141 @@ transform = transforms.Compose([
|
|
62 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
63 |
])
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
probs = F.softmax(outputs, dim=1)
|
71 |
-
top_probs, top_idxs = torch.topk(probs, 3, dim=1) # top 3 predictions
|
72 |
-
|
73 |
-
predictions = []
|
74 |
-
for prob, idx in zip(top_probs[0], top_idxs[0]):
|
75 |
-
label = DIAGNOSIS_MAP.get(idx.item(), "Unknown")
|
76 |
-
confidence = prob.item() * 100
|
77 |
-
predictions.append(f"{label}: {confidence:.2f}%")
|
78 |
-
|
79 |
-
return "\n".join(predictions)
|
80 |
-
|
81 |
-
# Brain Tumor Prediction Function
|
82 |
-
def predict_brain_tumor(image):
|
83 |
-
pred, _, prob = brain_tumor_model.predict(image)
|
84 |
-
return f"Prediction: {pred}, Probability: {prob.max():.2f}"
|
85 |
|
86 |
-
#
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
def main():
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
gr.Markdown("Upload a skin lesion image for diagnosis prediction.")
|
113 |
-
image_input_skin = gr.Image(type="pil", label="Upload Skin Lesion Image")
|
114 |
-
output_skin = gr.Textbox(label="Prediction Results")
|
115 |
-
image_input_skin.change(predict_skin_disease, inputs=image_input_skin, outputs=output_skin)
|
116 |
-
|
117 |
-
with gr.Tab("Brain Tumor Prediction"):
|
118 |
-
with gr.Column():
|
119 |
-
gr.Markdown("Upload a brain scan image for tumor classification.")
|
120 |
-
image_input_brain = gr.Image(type="pil", label="Upload Brain Scan Image")
|
121 |
-
output_brain = gr.Textbox(label="Prediction Results")
|
122 |
-
image_input_brain.change(predict_brain_tumor, inputs=image_input_brain, outputs=output_brain)
|
123 |
-
|
124 |
-
with gr.Tab("Alzheimer's Prediction"):
|
125 |
-
with gr.Column():
|
126 |
-
gr.Markdown("Upload a brain image for Alzheimer's detection.")
|
127 |
-
image_input_alz = gr.Image(type="pil", label="Upload Alzheimer's Image")
|
128 |
-
output_alz = gr.Textbox(label="Prediction Results")
|
129 |
-
image_input_alz.change(predict_alzheimers, inputs=image_input_alz, outputs=output_alz)
|
130 |
-
|
131 |
-
with gr.Tab("Eye Disease Prediction"):
|
132 |
-
with gr.Column():
|
133 |
-
gr.Markdown("Upload an image for eye disease classification.")
|
134 |
-
image_input_eye = gr.Image(type="pil", label="Upload Eye Disease Image")
|
135 |
-
output_eye = gr.Textbox(label="Prediction Results")
|
136 |
-
image_input_eye.change(predict_eye_disease, inputs=image_input_eye, outputs=output_eye)
|
137 |
-
|
138 |
-
demo.launch()
|
139 |
-
|
140 |
-
# Run the Gradio app
|
141 |
-
if __name__ == "__main__":
|
142 |
main()
|
143 |
|
|
|
|
|
|
7 |
https://colab.research.google.com/drive/1sjyLFLqBccpUzaUi4eyyP3NYE3gDtHfs
|
8 |
"""
|
9 |
|
10 |
+
import streamlit as st
|
11 |
+
from streamlit_option_menu import option_menu
|
12 |
+
import tensorflow as tf
|
13 |
+
from tensorflow import keras
|
14 |
+
import numpy as np
|
15 |
+
import pandas as pd
|
16 |
import torch
|
17 |
import torch.nn as nn
|
18 |
import torch.nn.functional as F
|
19 |
from torchvision import transforms
|
20 |
+
from PIL import Image
|
21 |
+
import os
|
22 |
+
import time
|
23 |
+
from efficientnet_pytorch import EfficientNet
|
24 |
+
from fastai.vision.all import load_learner
|
25 |
|
26 |
+
# Set up environment variables for GPU handling
|
27 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Cache to load models efficiently
|
30 |
+
@st.cache_resource
|
31 |
+
def load_skin_model():
|
32 |
+
model = MelanomaModel(out_size=9)
|
33 |
+
model_path = "multi_weight.pth"
|
34 |
+
checkpoint = torch.load(model_path, map_location=torch.device('cpu'), weights_only=False)
|
35 |
+
model.load_state_dict(checkpoint["model_state_dict"])
|
36 |
+
model.eval()
|
37 |
+
return model
|
|
|
|
|
|
|
38 |
|
39 |
+
# Transform for skin lesion images
|
40 |
transform = transforms.Compose([
|
41 |
transforms.Resize(256),
|
42 |
transforms.CenterCrop(224),
|
|
|
44 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
45 |
])
|
46 |
|
47 |
+
# Diagnosis map for skin disease model
|
48 |
+
DIAGNOSIS_MAP = {
|
49 |
+
0: 'Melanoma', 1: 'Melanocytic nevus', 2: 'Basal cell carcinoma', 3: 'Actinic keratosis',
|
50 |
+
4: 'Benign keratosis', 5: 'Dermatofibroma', 6: 'Vascular lesion', 7: 'Squamous cell carcinoma', 8: 'Unknown'
|
51 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Model for skin lesion classification
|
54 |
+
class MelanomaModel(nn.Module):
|
55 |
+
def __init__(self, out_size, dropout_prob=0.5):
|
56 |
+
super(MelanomaModel, self).__init__()
|
57 |
+
self.efficient_net = EfficientNet.from_pretrained('efficientnet-b0')
|
58 |
+
self.efficient_net._fc = nn.Identity()
|
59 |
+
self.fc1 = nn.Linear(1280, 512)
|
60 |
+
self.fc2 = nn.Linear(512, 256)
|
61 |
+
self.fc3 = nn.Linear(256, out_size)
|
62 |
+
self.dropout = nn.Dropout(dropout_prob)
|
63 |
|
64 |
+
def forward(self, x):
|
65 |
+
x = self.efficient_net(x)
|
66 |
+
x = x.view(x.size(0), -1)
|
67 |
+
x = F.relu(self.fc1(x))
|
68 |
+
x = self.dropout(x)
|
69 |
+
x = F.relu(self.fc2(x))
|
70 |
+
x = self.dropout(x)
|
71 |
+
x = self.fc3(x)
|
72 |
+
return x
|
73 |
|
74 |
+
# Alzheimer's Prediction
|
75 |
+
@st.cache_resource
|
76 |
+
def load_alzheimer_model():
|
77 |
+
return keras.models.load_model('alzheimer_99.5.h5')
|
78 |
+
|
79 |
+
# Brain Tumor Prediction
|
80 |
+
@st.cache_resource
|
81 |
+
def load_brain_tumor_model(classes):
|
82 |
+
if classes == '44 Classes':
|
83 |
+
return keras.models.load_model('44class_96.5.h5')
|
84 |
+
elif classes == '17 Classes':
|
85 |
+
return keras.models.load_model('17class_98.1.h5')
|
86 |
+
elif classes == '15 Classes':
|
87 |
+
return keras.models.load_model('15class_99.8.h5')
|
88 |
+
else: # Default 2 classes
|
89 |
+
return keras.models.load_model('2calss_lagre_dataset_99.1.h5')
|
90 |
+
|
91 |
+
# Prediction for Skin Disease
|
92 |
+
def predict_skin_lesion(img: Image.Image, model: nn.Module):
|
93 |
+
img_tensor = transform(img).unsqueeze(0)
|
94 |
+
with torch.no_grad():
|
95 |
+
outputs = model(img_tensor)
|
96 |
+
probs = F.softmax(outputs, dim=1)
|
97 |
+
top_probs, top_idxs = torch.topk(probs, 3, dim=1)
|
98 |
+
predictions = [(DIAGNOSIS_MAP.get(idx.item(), "Unknown"), prob.item() * 100) for prob, idx in zip(top_probs[0], top_idxs[0])]
|
99 |
+
return predictions
|
100 |
+
|
101 |
+
# Prediction for Brain Tumor and Alzheimer
|
102 |
+
def predict(img_path, model, result_classes):
|
103 |
+
img = tf.keras.utils.load_img(img_path, target_size=(224, 224))
|
104 |
+
img_array = np.array(img).reshape(-1, 224, 224, 3)
|
105 |
+
pred = model.predict(img_array)
|
106 |
+
return result_classes[np.argmax(pred, axis=1)[0]]
|
107 |
+
|
108 |
+
# Sidebar for Disease Categories
|
109 |
+
def spr_sidebar():
|
110 |
+
menu = option_menu(
|
111 |
+
menu_title="Navigation",
|
112 |
+
options=["Brain Tumor", "Alzheimer", "Skin Disease", "Eye Disease", "About"],
|
113 |
+
icons=["house", "brain", "microscope", "eye", "info-square"],
|
114 |
+
menu_icon="cast",
|
115 |
+
default_index=0,
|
116 |
+
orientation="horizontal"
|
117 |
+
)
|
118 |
+
return menu
|
119 |
+
|
120 |
+
# Home Page Content
|
121 |
+
def home_page():
|
122 |
+
st.title("Disease Detection Web App")
|
123 |
+
|
124 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
125 |
+
|
126 |
+
if uploaded_file is not None:
|
127 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
128 |
+
|
129 |
+
if st.button("Classify"):
|
130 |
+
if selected_category == "Brain Tumor":
|
131 |
+
classes = st.selectbox("Select Number of Classes", ['44 Classes', '17 Classes', '15 Classes', '2 Classes'])
|
132 |
+
model = load_brain_tumor_model(classes)
|
133 |
+
result_classes = ['Astrocitoma', 'Carcinoma', 'Ependimoma', '_NORMAL', 'etc...'] # Define all the classes
|
134 |
+
result = predict(uploaded_file, model, result_classes)
|
135 |
+
st.success(f"Prediction: {result}")
|
136 |
+
|
137 |
+
elif selected_category == "Alzheimer":
|
138 |
+
model = load_alzheimer_model()
|
139 |
+
result_classes = ['Mild_Demented', 'Moderate_Demented', 'Non_Demented']
|
140 |
+
result = predict(uploaded_file, model, result_classes)
|
141 |
+
st.success(f"Prediction: {result}")
|
142 |
+
|
143 |
+
elif selected_category == "Skin Disease":
|
144 |
+
model = load_skin_model()
|
145 |
+
img = Image.open(uploaded_file)
|
146 |
+
predictions = predict_skin_lesion(img, model)
|
147 |
+
for idx, (label, confidence) in enumerate(predictions, 1):
|
148 |
+
st.write(f"{idx}. {label}: {confidence:.2f}%")
|
149 |
+
|
150 |
+
elif selected_category == "Eye Disease":
|
151 |
+
# Implement Eye Disease prediction (similar to others)
|
152 |
+
pass
|
153 |
+
|
154 |
+
# About Page Content
|
155 |
+
def about_page():
|
156 |
+
st.header('About the Project')
|
157 |
+
st.write("""
|
158 |
+
This web app detects different diseases using machine learning models.
|
159 |
+
- **Brain Tumor Detection**: Classifies different types of brain tumors.
|
160 |
+
- **Alzheimer Detection**: Classifies stages of Alzheimer.
|
161 |
+
- **Skin Disease Classification**: Classifies types of skin lesions.
|
162 |
+
- **Eye Disease Classification**: TBD.
|
163 |
+
""")
|
164 |
+
|
165 |
+
# Main Function to Run the App
|
166 |
def main():
|
167 |
+
selected_category = spr_sidebar()
|
168 |
+
|
169 |
+
if selected_category == "Brain Tumor":
|
170 |
+
home_page()
|
171 |
+
elif selected_category == "Alzheimer":
|
172 |
+
home_page()
|
173 |
+
elif selected_category == "Skin Disease":
|
174 |
+
home_page()
|
175 |
+
elif selected_category == "Eye Disease":
|
176 |
+
home_page() # Eye Disease detection page
|
177 |
+
elif selected_category == "About":
|
178 |
+
about_page()
|
179 |
+
|
180 |
+
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
main()
|
182 |
|
183 |
+
|
184 |
+
|