MoAshraf commited on
Commit
4473ad1
1 Parent(s): 9b6035b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -189
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 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.")
 
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**.")