Update app.py
Browse files
app.py
CHANGED
@@ -1,189 +1,113 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pickle
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
import
|
17 |
-
import
|
18 |
-
|
19 |
-
from keras.
|
20 |
-
from keras.
|
21 |
-
from keras.
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
x =
|
39 |
-
|
40 |
-
|
41 |
-
x =
|
42 |
-
|
43 |
-
|
44 |
-
x =
|
45 |
-
|
46 |
-
|
47 |
-
x =
|
48 |
-
|
49 |
-
|
50 |
-
x =
|
51 |
-
|
52 |
-
|
53 |
-
x =
|
54 |
-
x =
|
55 |
-
|
56 |
-
|
57 |
-
x =
|
58 |
-
|
59 |
-
|
60 |
-
x =
|
61 |
-
x =
|
62 |
-
|
63 |
-
|
64 |
-
x =
|
65 |
-
|
66 |
-
|
67 |
-
x =
|
68 |
-
x =
|
69 |
-
x =
|
70 |
-
x =
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
model.
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
image =
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
st.
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
st.write("The image is **not infected**.")
|
115 |
-
else:
|
116 |
-
st.write("The image is **infected**.")
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
# import streamlit as st
|
148 |
-
# import numpy as np
|
149 |
-
# from PIL import Image
|
150 |
-
# import tensorflow as tf
|
151 |
-
# from tensorflow.keras.models import load_model
|
152 |
-
|
153 |
-
# # Load the model
|
154 |
-
# @st.cache(allow_output_mutation=True)
|
155 |
-
# def load_model():
|
156 |
-
# model = tf.keras.models.load_model('best_weights.hdf5')
|
157 |
-
# return model
|
158 |
-
|
159 |
-
# model = load_model()
|
160 |
-
|
161 |
-
# # Function to preprocess the image
|
162 |
-
# def preprocess_image(image):
|
163 |
-
# image = image.resize((150, 150)) # Resize the image
|
164 |
-
# image = np.array(image) / 255.0 # Normalize the image
|
165 |
-
# image = np.expand_dims(image, axis=0) # Add batch dimension
|
166 |
-
# return image
|
167 |
-
|
168 |
-
# # Streamlit app
|
169 |
-
# st.title('Pneumonia Detection App')
|
170 |
-
|
171 |
-
# uploaded_file = st.file_uploader("Choose an image...", type=['jpg', 'jpeg', 'png'])
|
172 |
-
|
173 |
-
# if uploaded_file is not None:
|
174 |
-
# # Display the uploaded image
|
175 |
-
# image = Image.open(uploaded_file)
|
176 |
-
# st.image(image, caption='Uploaded Image.', use_column_width=True)
|
177 |
-
|
178 |
-
# # Preprocess the image
|
179 |
-
# processed_image = preprocess_image(image)
|
180 |
-
|
181 |
-
# # Make predictions
|
182 |
-
# prediction = model.predict(processed_image)
|
183 |
-
# result = np.round(prediction).astype(int)[0][0]
|
184 |
-
|
185 |
-
# # Interpret the result
|
186 |
-
# if result == 0:
|
187 |
-
# st.write("Prediction: The image is not infected.")
|
188 |
-
# else:
|
189 |
-
# st.write("Prediction: The image is infected.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import numpy as np
|
7 |
+
import pandas as pd
|
8 |
+
import random
|
9 |
+
import logging
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
# Deep learning libraries
|
13 |
+
import tensorflow as tf
|
14 |
+
import keras
|
15 |
+
import keras.backend as K
|
16 |
+
from keras.models import Model, Sequential
|
17 |
+
from keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization
|
18 |
+
from keras.layers import Conv2D, SeparableConv2D, MaxPool2D, LeakyReLU, Activation
|
19 |
+
from keras.optimizers import Adam
|
20 |
+
from keras.preprocessing.image import ImageDataGenerator
|
21 |
+
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
22 |
+
|
23 |
+
# Setting seeds for reproducibility
|
24 |
+
seed = 232
|
25 |
+
np.random.seed(seed)
|
26 |
+
tf.random.set_seed(seed)
|
27 |
+
|
28 |
+
# Define the model architecture
|
29 |
+
def build_model(input_shape):
|
30 |
+
inputs = Input(shape=input_shape)
|
31 |
+
|
32 |
+
# First conv block
|
33 |
+
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(inputs)
|
34 |
+
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
35 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
36 |
+
|
37 |
+
# Second conv block
|
38 |
+
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
39 |
+
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
40 |
+
x = BatchNormalization()(x)
|
41 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
42 |
+
|
43 |
+
# Third conv block
|
44 |
+
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
45 |
+
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
46 |
+
x = BatchNormalization()(x)
|
47 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
48 |
+
|
49 |
+
# Fourth conv block
|
50 |
+
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
51 |
+
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
52 |
+
x = BatchNormalization()(x)
|
53 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
54 |
+
x = Dropout(rate=0.2)(x)
|
55 |
+
|
56 |
+
# Fifth conv block
|
57 |
+
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
58 |
+
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
59 |
+
x = BatchNormalization()(x)
|
60 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
61 |
+
x = Dropout(rate=0.2)(x)
|
62 |
+
|
63 |
+
# FC layer
|
64 |
+
x = Flatten()(x)
|
65 |
+
x = Dense(units=1024, activation='relu')(x)
|
66 |
+
x = Dropout(rate=0.7)(x)
|
67 |
+
x = Dense(units=512, activation='relu')(x)
|
68 |
+
x = Dropout(rate=0.5)(x)
|
69 |
+
x = Dense(units=256, activation='relu')(x)
|
70 |
+
x = Dropout(rate=0.3)(x)
|
71 |
+
|
72 |
+
# Output layer
|
73 |
+
output = Dense(units=1, activation='sigmoid')(x)
|
74 |
+
|
75 |
+
model = Model(inputs=inputs, outputs=output)
|
76 |
+
return model
|
77 |
+
|
78 |
+
# Initialize and compile the model
|
79 |
+
input_shape = (150, 150, 3)
|
80 |
+
model = build_model(input_shape)
|
81 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
82 |
+
|
83 |
+
# Load the weights
|
84 |
+
model.load_weights('best_weights.hdf5')
|
85 |
+
|
86 |
+
|
87 |
+
# Function to preprocess the image
|
88 |
+
def preprocess_image(image):
|
89 |
+
image = image.convert('RGB')
|
90 |
+
image = image.resize((150, 150))
|
91 |
+
image = np.array(image) / 255.0
|
92 |
+
image = np.expand_dims(image, axis=0)
|
93 |
+
return image
|
94 |
+
|
95 |
+
st.title("Pneumonia Detection from Chest X-ray")
|
96 |
+
|
97 |
+
# Upload image
|
98 |
+
uploaded_file = st.file_uploader("Choose a chest X-ray image...", type="jpeg")
|
99 |
+
|
100 |
+
if uploaded_file is not None:
|
101 |
+
image = Image.open(uploaded_file)
|
102 |
+
st.image(image, caption='Uploaded X-ray.', use_column_width=True)
|
103 |
+
st.write("")
|
104 |
+
st.write("Classifying...")
|
105 |
+
|
106 |
+
processed_image = preprocess_image(image)
|
107 |
+
prediction = model.predict(processed_image)
|
108 |
+
result = np.round(prediction).astype(int)[0][0]
|
109 |
+
|
110 |
+
if result == 0:
|
111 |
+
st.write("The image is **not infected**.")
|
112 |
+
else:
|
113 |
+
st.write("The image is **infected**.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|