-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
347 lines (274 loc) · 12.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Imporiting Necessary Libraries
import base64
import streamlit as st
from PIL import Image
import io
import numpy as np
import pandas as pd
import cv2
import fpdf
import json
import tensorflow as tf
from utils import clean_image, get_prediction, make_results
# Loading the Model and saving to cache
def classification1_page():
@st.cache(allow_output_mutation=True)
def load_model(path):
# Xception Model
xception_model = tf.keras.models.Sequential([
tf.keras.applications.xception.Xception(
include_top=False, weights='imagenet', input_shape=(512, 512, 3)),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(4, activation='softmax')
])
# DenseNet Model
densenet_model = tf.keras.models.Sequential([
tf.keras.applications.densenet.DenseNet121(
include_top=False, weights='imagenet', input_shape=(512, 512, 3)),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(4, activation='softmax')
])
# Ensembling the Models
inputs = tf.keras.Input(shape=(512, 512, 3))
xception_output = xception_model(inputs)
densenet_output = densenet_model(inputs)
outputs = tf.keras.layers.average([densenet_output, xception_output])
model = tf.keras.Model(inputs=inputs, outputs=outputs)
# Loading the Weights of Model
model.load_weights(path)
return model
# Loading the Model
model = load_model('model.h5')
# Title and Description
st.title('Apple Leaf Disease Classification')
st.write("Upload the apple leaf image and get the classifications!")
# Setting the files that can be uploaded
uploaded_file = st.file_uploader(
"Choose a Image file", type=["png", "jpg"])
# If there is a uploaded file, start making prediction
if uploaded_file != None:
# Display progress and text
progress = st.text("Processing Image")
my_bar = st.progress(0)
i = 0
# Reading the uploaded image
image = Image.open(io.BytesIO(uploaded_file.read()))
data = uploaded_file.getvalue()
image_cv = cv2.imdecode(np.frombuffer(data, np.uint8), -1)
im3 = cv2.applyColorMap(image_cv, cv2.COLORMAP_JET)
im4 = cv2.applyColorMap(image_cv, cv2.COLORMAP_BONE)
im5 = cv2.applyColorMap(image_cv, cv2.COLORMAP_INFERNO)
im6 = cv2.applyColorMap(image_cv, cv2.COLORMAP_OCEAN)
im7 = cv2.applyColorMap(image_cv, cv2.COLORMAP_RAINBOW)
im8 = cv2.applyColorMap(image_cv, cv2.COLORMAP_HSV)
st.subheader('Original Image')
st.image(np.array(Image.fromarray(np.array(image)).resize(
(700, 400), Image.ANTIALIAS)), width=None)
st.subheader('Jet Colormap Image')
st.image(im3)
st.subheader('Bone Colormap Image')
st.image(im4)
st.subheader('Inferno Colormap Image')
st.image(im5)
st.subheader('Ocean Colormap Image')
st.image(im6)
st.subheader('Rainbow Colormap Image')
st.image(im7)
st.subheader('HSV Colormap Image')
st.image(im8)
my_bar.progress(i + 40)
# Cleaning the image
image = clean_image(image)
# Making the predictions
predictions, predictions_arr = get_prediction(model, image)
my_bar.progress(i + 30)
# Making the results
result = make_results(predictions, predictions_arr)
# Removing progress bar and text after prediction done
my_bar.progress(i + 30)
progress.empty()
i = 0
my_bar.empty()
scab_file = open('scab.json')
rust_file = open('rust.json')
scab_content = json.load(scab_file)
rust_content = json.load(rust_file)
# Show the results
st.title(
f"The leaf {result['status']} with {result['prediction']} classification probability.")
if result['status'] == ' has Scab ':
st.subheader("Apple Trees Affected: ")
st.write(scab_content['Affected'])
st.subheader("Symptoms: ")
st.write(scab_content['Symptoms'])
st.subheader("Causes: ")
st.write(scab_content['Causes'])
st.subheader("Treatment: ")
st.write(scab_content['Treatment'])
st.subheader("Risk: ")
st.write(scab_content['Risk'])
elif result['status'] == ' has Rust ':
st.subheader("Apple Trees Affected: ")
st.write(rust_content['Affected'])
st.subheader("Symptoms: ")
st.write(rust_content['Symptoms'])
st.subheader("Causes: ")
st.write(rust_content['Causes'])
st.subheader("Treatment: ")
st.write(rust_content['Treatment'])
st.subheader("Risk: ")
st.write(rust_content['Risk'])
elif result['status'] == ' has Multiple Diseases ':
st.subheader(
"The given leaf image has multiple diseases - fire blight, black rot and many more.")
else:
result = "Healthy"
st.subheader("The given image is healthy!")
def classification2_page():
@st.cache(allow_output_mutation=True)
def load_model(path):
inputs = tf.keras.Input(shape=(256, 256, 3))
x = tf.keras.applications.MobileNetV2(include_top=False)(inputs)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
outputs = tf.keras.layers.Dense(6, activation='sigmoid')(x)
model = tf.keras.models.Model(inputs, outputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
# Loading the Weights of Model
model.load_weights(path)
return model
# Loading the Model
model = load_model('model_2021_version2.h5')
# Title and Description
st.title('Apple Leaf Disease Classification')
st.write("Upload the apple leaf image and get the classifications!")
# Setting the files that can be uploaded
uploaded_file = st.file_uploader(
"Choose a Image file", type=["png", "jpg"])
# If there is a uploaded file, start making prediction
if uploaded_file != None:
# Display progress and text
progress = st.text("Processing Image")
my_bar = st.progress(0)
i = 0
# Reading the uploaded image
image = Image.open(io.BytesIO(uploaded_file.read()))
data = uploaded_file.getvalue()
image_cv = cv2.imdecode(np.frombuffer(data, np.uint8), -1)
im3 = cv2.applyColorMap(image_cv, cv2.COLORMAP_JET)
im4 = cv2.applyColorMap(image_cv, cv2.COLORMAP_BONE)
im5 = cv2.applyColorMap(image_cv, cv2.COLORMAP_INFERNO)
im6 = cv2.applyColorMap(image_cv, cv2.COLORMAP_OCEAN)
im7 = cv2.applyColorMap(image_cv, cv2.COLORMAP_RAINBOW)
im8 = cv2.applyColorMap(image_cv, cv2.COLORMAP_HSV)
st.subheader('Original Image')
st.image(np.array(Image.fromarray(np.array(image)).resize(
(700, 400), Image.ANTIALIAS)), width=None)
st.subheader('Jet Colormap Image')
st.image(im3)
st.subheader('Bone Colormap Image')
st.image(im4)
st.subheader('Inferno Colormap Image')
st.image(im5)
st.subheader('Ocean Colormap Image')
st.image(im6)
st.subheader('Rainbow Colormap Image')
st.image(im7)
st.subheader('HSV Colormap Image')
st.image(im8)
my_bar.progress(i + 40)
# Cleaning the image
image = clean_image(image)
# Making the predictions
predictions, predictions_arr = get_prediction(model, image)
my_bar.progress(i + 30)
df = pd.read_csv('Train.csv')
result = df.columns[predictions_arr+1]
# Removing progress bar and text after prediction done
my_bar.progress(i + 30)
progress.empty()
i = 0
my_bar.empty()
st.title(
f"The leaf has {result} classification with probability distribution of {round(predictions.max(), 2)}.")
def about_us():
col1, col2 = st.columns(2)
with col1:
st.header("Nimesh Mohanakrishnan")
st.image("./images/Nimesh.jpg")
st.header("Mohammed Saqlain")
st.image("./images/Saqlain.jpeg")
with col2:
st.header("Mohammed Hussam Khatib")
st.image("./images/Hussam.jpeg")
st.header("Nashra Tanseer")
st.image("./images/Nashra1.jpeg")
def about_project():
st.title("Classification of Foliar Diseases in Apple Leaves")
st.write("Diseases in apple leaves are a major threat to the quality growth of apple fruit. While many machine learning and deep learning approaches that use single model base, exist to detect, and classify plant leaf diseases, hybrid modelling and ensemble approaches are least explored and examined. In this project, we focus on developing an ensemble CNN model to accuractely classify the diseases present in apple tree leaves.")
st.write(
"Appropriate apple leaf images are collected from a kaggle repository. The images present in the kaggle repository were mined from [khanlab](https://blogs.cornell.edu/applevarietydatabase/machine-learning-for-disease-detection/) - a research lab at cornell university.")
st.header("Image Classes")
st.write("**Classes:** Healthy, Cedar Rust, Apple Scab, and Multiple Diseases")
st.caption("Apple Scab")
st.image("Train_0.jpg")
st.caption("Multiple Disease")
st.image("Train_1.jpg")
st.caption("Healthy")
st.image("Train_2.jpg")
st.caption("Cedar Rust")
st.image("Train_3.jpg")
st.header("Tech Stack")
dataframe = {"Technology": ['ML Library', 'Colormap Module', 'Statistical Module', 'Front-End'],
"Items": ['Tensorflow, Keras, and Sklearn', 'OpenCV', 'Numpy and Pandas', 'Streamlit']}
df = pd.DataFrame(data=dataframe)
st.table(df)
st.header("Parameters for Model Learning")
dataframe3 = {'Parameters': ['Starting Learning Rate', 'Maximum Learning Rate', 'Exponential Decay', 'Epochs', 'Activation Function', 'Optimizer', 'Loss Function', 'Metrics',
'Input Shape', 'Monitoring', 'Colormap', 'No. of Outputs'], 'Values': ['0.00001', '0.0001', '0.8', '25', 'Softmax', 'Adam', 'Categorical_Crossentropy', 'Accuracy', '(512, 512, 3)', 'Validation Accuracy', 'Jet', '4']}
df3 = pd.DataFrame(data=dataframe3)
st.table(df3)
st.header("Models")
st.write("We have experimented with different sets of ensemble models. Of all, **DenseNet121 and Xception** ensemble models gave the best results.")
dataframe2 = {'Model 1': ['Xception', 'InceptionResNetV2', 'DenseNet201'], 'Model 2': ['DenseNet121', 'ResNet152V2', 'DenseNet169'], 'Accuracy': [
99.86, 99.81, 99.89], 'Validation Accuracy': [100, 99.91, 99.91], 'Loss': [0.9, 2.7, 1.1], 'Validation Loss': [0.24, 2.9, 2.3]}
df2 = pd.DataFrame(data=dataframe2)
st.table(df2)
st.write("Best Ensemble Model Performance- Xception & DenseNet121")
st.subheader("Xception Model")
st.write("Layers:\n1. Input Layer\n2. Xception Model Layers\n3. GlobalPoolingAverage2d Layer\n4. Dense Layer(Output Layer)")
xceptipn_code = '''xception_model = tf.keras.models.Sequential([
tf.keras.applications.DenseNet169(include_top=False, weights='imagenet', input_shape=(512, 512, 3)),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(4,activation='softmax')
])
xception_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
xception_model.summary()'''
st.code(xceptipn_code, language='python')
st.subheader("DenseNet121 Model")
st.write("Layers:\n1. Input Layer\n2. DenseNet121 Model Layers\n3. GlobalPoolingAverage2d Layer\n4. Dense Layer(Output Layer)")
densenet121_code = '''densenet_model = tf.keras.models.Sequential([
tf.keras.applications.DenseNet201(include_top = False, weights='imagenet',input_shape=(512, 512, 3)),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(4,activation='softmax')
])
densenet_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
densenet_model.summary()'''
st.code(densenet121_code, language='python')
st.subheader("Ensemble Xception & DenseNet121")
ensemble_code = '''inputs = tf.keras.Input(shape=(512, 512, 3))
xception_output = xception_model(inputs)
densenet_output = densenet_model(inputs)
outputs = tf.keras.layers.average([densenet_output, xception_output])
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()'''
st.code(ensemble_code, language='python')
page_names_to_funcs = {
"Project Details": about_project,
"Ensemble Model Classification": classification1_page,
"MobileNetV2 Model Classification": classification2_page,
"Team": about_us,
}
selected_page = st.sidebar.selectbox(
"Select a page", page_names_to_funcs.keys())
page_names_to_funcs[selected_page]()