Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +189 -0
- best_weights.hdf5 +3 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
best_weights.hdf5 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
import os
|
7 |
+
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
+
import random
|
10 |
+
# import cv2
|
11 |
+
import logging
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
# Deep learning libraries
|
16 |
+
import tensorflow as tf
|
17 |
+
import keras
|
18 |
+
import keras.backend as K
|
19 |
+
from keras.models import Model, Sequential
|
20 |
+
from keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization
|
21 |
+
from keras.layers import Conv2D, SeparableConv2D, MaxPool2D, LeakyReLU, Activation
|
22 |
+
from keras.optimizers import Adam
|
23 |
+
from keras.preprocessing.image import ImageDataGenerator
|
24 |
+
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
25 |
+
|
26 |
+
# Setting seeds for reproducibility
|
27 |
+
seed = 232
|
28 |
+
np.random.seed(seed)
|
29 |
+
tf.random.set_seed(seed)
|
30 |
+
|
31 |
+
# Define the model architecture
|
32 |
+
def build_model(input_shape):
|
33 |
+
inputs = Input(shape=input_shape)
|
34 |
+
|
35 |
+
# First conv block
|
36 |
+
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(inputs)
|
37 |
+
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
38 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
39 |
+
|
40 |
+
# Second conv block
|
41 |
+
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
42 |
+
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
43 |
+
x = BatchNormalization()(x)
|
44 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
45 |
+
|
46 |
+
# Third conv block
|
47 |
+
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
48 |
+
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
49 |
+
x = BatchNormalization()(x)
|
50 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
51 |
+
|
52 |
+
# Fourth conv block
|
53 |
+
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
54 |
+
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
55 |
+
x = BatchNormalization()(x)
|
56 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
57 |
+
x = Dropout(rate=0.2)(x)
|
58 |
+
|
59 |
+
# Fifth conv block
|
60 |
+
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
61 |
+
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x)
|
62 |
+
x = BatchNormalization()(x)
|
63 |
+
x = MaxPool2D(pool_size=(2, 2))(x)
|
64 |
+
x = Dropout(rate=0.2)(x)
|
65 |
+
|
66 |
+
# FC layer
|
67 |
+
x = Flatten()(x)
|
68 |
+
x = Dense(units=1024, activation='relu')(x)
|
69 |
+
x = Dropout(rate=0.7)(x)
|
70 |
+
x = Dense(units=512, activation='relu')(x)
|
71 |
+
x = Dropout(rate=0.5)(x)
|
72 |
+
x = Dense(units=256, activation='relu')(x)
|
73 |
+
x = Dropout(rate=0.3)(x)
|
74 |
+
|
75 |
+
# Output layer
|
76 |
+
output = Dense(units=1, activation='sigmoid')(x)
|
77 |
+
|
78 |
+
model = Model(inputs=inputs, outputs=output)
|
79 |
+
return model
|
80 |
+
|
81 |
+
# Initialize and compile the model
|
82 |
+
input_shape = (150, 150, 3)
|
83 |
+
model = build_model(input_shape)
|
84 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
85 |
+
|
86 |
+
# Load the weights
|
87 |
+
model.load_weights('best_weights.hdf5')
|
88 |
+
|
89 |
+
|
90 |
+
# Function to preprocess the image
|
91 |
+
def preprocess_image(image):
|
92 |
+
image = image.convert('RGB')
|
93 |
+
image = image.resize((150, 150))
|
94 |
+
image = np.array(image) / 255.0
|
95 |
+
image = np.expand_dims(image, axis=0)
|
96 |
+
return image
|
97 |
+
|
98 |
+
st.title("Pneumonia Detection from Chest X-ray")
|
99 |
+
|
100 |
+
# Upload image
|
101 |
+
uploaded_file = st.file_uploader("Choose a chest X-ray image...", type="jpeg")
|
102 |
+
|
103 |
+
if uploaded_file is not None:
|
104 |
+
image = Image.open(uploaded_file)
|
105 |
+
st.image(image, caption='Uploaded X-ray.', use_column_width=True)
|
106 |
+
st.write("")
|
107 |
+
st.write("Classifying...")
|
108 |
+
|
109 |
+
processed_image = preprocess_image(image)
|
110 |
+
prediction = model.predict(processed_image)
|
111 |
+
result = np.round(prediction).astype(int)[0][0]
|
112 |
+
|
113 |
+
if result == 0:
|
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.")
|
best_weights.hdf5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb57db1720a217e18b32be5f4b354c3c322f918c6ba7f12d6ade9311b60e0b71
|
3 |
+
size 78473152
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pillow
|
3 |
+
numpy
|
4 |
+
tensorflow==2.13.0
|
5 |
+
keras
|