Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,522 +1,545 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
3 |
import tensorflow as tf
|
4 |
import pandas as pd
|
|
|
5 |
|
6 |
# 🔹 Expand the Page Layout
|
7 |
-
st.set_page_config(layout="wide") #
|
8 |
|
|
|
9 |
current_model = "Model Mini"
|
10 |
-
|
11 |
-
class_names = ['apple_pie',
|
12 |
-
'
|
13 |
-
'
|
14 |
-
'
|
15 |
-
'
|
16 |
-
'
|
17 |
-
'
|
18 |
-
'
|
19 |
-
'
|
20 |
-
'
|
21 |
-
'
|
22 |
-
'
|
23 |
-
'
|
24 |
-
'
|
25 |
-
'
|
26 |
-
'
|
27 |
-
'
|
28 |
-
'
|
29 |
-
'
|
30 |
-
'
|
31 |
-
'chicken_wings',
|
32 |
-
'chocolate_cake',
|
33 |
-
'chocolate_mousse',
|
34 |
-
'churros',
|
35 |
-
'clam_chowder',
|
36 |
-
'club_sandwich',
|
37 |
-
'crab_cakes',
|
38 |
-
'creme_brulee',
|
39 |
-
'croque_madame',
|
40 |
-
'cup_cakes',
|
41 |
-
'deviled_eggs',
|
42 |
-
'donuts',
|
43 |
-
'dumplings',
|
44 |
-
'edamame',
|
45 |
-
'eggs_benedict',
|
46 |
-
'escargots',
|
47 |
-
'falafel',
|
48 |
-
'filet_mignon',
|
49 |
-
'fish_and_chips',
|
50 |
-
'foie_gras',
|
51 |
-
'french_fries',
|
52 |
-
'french_onion_soup',
|
53 |
-
'french_toast',
|
54 |
-
'fried_calamari',
|
55 |
-
'fried_rice',
|
56 |
-
'frozen_yogurt',
|
57 |
-
'garlic_bread',
|
58 |
-
'gnocchi',
|
59 |
-
'greek_salad',
|
60 |
-
'grilled_cheese_sandwich',
|
61 |
-
'grilled_salmon',
|
62 |
-
'guacamole',
|
63 |
-
'gyoza',
|
64 |
-
'hamburger',
|
65 |
-
'hot_and_sour_soup',
|
66 |
-
'hot_dog',
|
67 |
-
'huevos_rancheros',
|
68 |
-
'hummus',
|
69 |
-
'ice_cream',
|
70 |
-
'lasagna',
|
71 |
-
'lobster_bisque',
|
72 |
-
'lobster_roll_sandwich',
|
73 |
-
'macaroni_and_cheese',
|
74 |
-
'macarons',
|
75 |
-
'miso_soup',
|
76 |
-
'mussels',
|
77 |
-
'nachos',
|
78 |
-
'omelette',
|
79 |
-
'onion_rings',
|
80 |
-
'oysters',
|
81 |
-
'pad_thai',
|
82 |
-
'paella',
|
83 |
-
'pancakes',
|
84 |
-
'panna_cotta',
|
85 |
-
'peking_duck',
|
86 |
-
'pho',
|
87 |
-
'pizza',
|
88 |
-
'pork_chop',
|
89 |
-
'poutine',
|
90 |
-
'prime_rib',
|
91 |
-
'pulled_pork_sandwich',
|
92 |
-
'ramen',
|
93 |
-
'ravioli',
|
94 |
-
'red_velvet_cake',
|
95 |
-
'risotto',
|
96 |
-
'samosa',
|
97 |
-
'sashimi',
|
98 |
-
'scallops',
|
99 |
-
'seaweed_salad',
|
100 |
-
'shrimp_and_grits',
|
101 |
-
'spaghetti_bolognese',
|
102 |
-
'spaghetti_carbonara',
|
103 |
-
'spring_rolls',
|
104 |
-
'steak',
|
105 |
-
'strawberry_shortcake',
|
106 |
-
'sushi',
|
107 |
-
'tacos',
|
108 |
-
'takoyaki',
|
109 |
-
'tiramisu',
|
110 |
-
'tuna_tartare',
|
111 |
-
'waffles']
|
112 |
|
113 |
top_ten_dict = {
|
114 |
-
"class_name": ["edamame", "macarons", "oysters", "pho",
|
115 |
-
"
|
116 |
"f1-score": [0.964427, 0.900433, 0.853119, 0.852652, 0.850622,
|
117 |
0.844794, 0.834356, 0.833006, 0.83209, 0.831967]
|
118 |
}
|
119 |
-
|
120 |
last_ten_dict = {
|
121 |
-
"class_name": ["chocolate_mousse", "tuna_tartare",
|
122 |
-
"
|
123 |
-
|
124 |
-
|
125 |
-
0.354497, 0.340426, 0.340045, 0.339785, 0.324826, 0.282407]
|
126 |
}
|
127 |
|
128 |
-
# 🔹 Custom CSS for
|
129 |
st.markdown(
|
130 |
"""
|
131 |
<style>
|
132 |
-
/*
|
133 |
-
.main-container {
|
134 |
-
max-width: 95% !important;
|
135 |
-
margin: auto;
|
136 |
-
}
|
137 |
-
|
138 |
-
/* Center all content inside containers */
|
139 |
.centered {
|
140 |
display: flex;
|
141 |
flex-direction: column;
|
142 |
align-items: center;
|
143 |
-
justify-content: center;
|
144 |
text-align: center;
|
145 |
-
width: 100%;
|
|
|
|
|
|
|
146 |
}
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
151 |
}
|
152 |
-
|
153 |
-
|
154 |
-
div[data-testid="stFileUploader"] {
|
155 |
-
width: 70% !important;
|
156 |
}
|
|
|
|
|
|
|
|
|
157 |
|
158 |
-
/* Center images */
|
159 |
-
img {
|
160 |
display: block;
|
161 |
margin-left: auto;
|
162 |
margin-right: auto;
|
163 |
-
width: 200px;
|
164 |
-
height: 200px;
|
|
|
|
|
|
|
165 |
border-radius: 20px;
|
|
|
166 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
</style>
|
168 |
""",
|
169 |
unsafe_allow_html=True
|
170 |
)
|
171 |
|
172 |
-
|
173 |
-
st.
|
174 |
-
|
175 |
-
|
176 |
st.divider()
|
177 |
-
st.subheader("What is a CNN(Convolutional Neural Network)")
|
178 |
-
st.write("A Neural network is network of nodes, consiting of input nodes, output nodes and hidden nodes.\
|
179 |
-
Each node lies in its respective layer, corresponding to its name. \
|
180 |
-
The input nodes reside in the input layer, the output nodes reside in the output layer and the hidden\
|
181 |
-
nodes reside in the hidden layer. The nodes pass information from the input layer to the output layer.\
|
182 |
-
The information consists of data(text, numbers, pictures, audio, videos) encoded as numbers\
|
183 |
-
that the network uses to learn information. It does this through complex mathematical operations\
|
184 |
-
and algorithms.")
|
185 |
-
|
186 |
-
# Display image of Neural Network here in between dividers
|
187 |
-
|
188 |
-
st.write("A Convolutional Neural Network in short is a version\
|
189 |
-
of a Neural Network that specializes on Images, video, basically anything visual.")
|
190 |
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
from tensorflow.keras import mixed_precision
|
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 |
optimizer=optimizer,
|
224 |
metrics=["accuracy"])
|
225 |
|
226 |
-
#
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
st.
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
st.
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
base_model.trainable = True
|
254 |
|
255 |
-
# Freeze
|
256 |
-
|
257 |
-
|
|
|
258 |
|
259 |
-
#
|
260 |
-
|
261 |
|
262 |
-
#
|
263 |
-
|
264 |
-
|
|
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
st.divider()
|
274 |
-
st.subheader("Model Building Details")
|
275 |
-
st.write(f'The Model was built using the :blue[Food101 kaggle dataset].\
|
276 |
-
The Dataset consist of 101 classes of Food.\
|
277 |
-
Namely: {[food.replace("_", "").title() for food in class_names]}')
|
278 |
-
|
279 |
-
st.divider()
|
280 |
-
st.write("When predicting you have to pass an image of any of the 101 classes of food.\
|
281 |
-
The Model has not yet been trained outside the 101 classes of food yet.")
|
282 |
-
|
283 |
-
st.divider()
|
284 |
-
st.subheader("Top and Least Classes Performance.")
|
285 |
-
st.write("After training, some classes evidently performed better than others.\
|
286 |
-
Below are the performance of the top classes and least classes based on the F1 score")
|
287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
st.divider()
|
289 |
-
st.subheader("F1-score")
|
290 |
-
st.write("The F1 score is a measure of a test's accuracy, which considers both the precision and the recall of the test to compute the score. The F1 score is the harmonic mean of precision and recall, where an F1 score reaches its best value at 1 (perfect precision and recall) and worst at 0. \
|
291 |
-
Precision is the number of true positive results divided by the number of all positive results, including those not correctly identified (i.e., the proportion of positive identifications that were actually correct). \
|
292 |
-
Recall (or Sensitivity) is the number of true positive results divided by the number of positives that should have been identified (i.e., the proportion of actual positives that were correctly identified).")
|
293 |
|
294 |
-
|
295 |
-
st.subheader("
|
296 |
-
|
|
|
|
|
|
|
|
|
297 |
st.divider()
|
298 |
|
299 |
-
#
|
300 |
-
st.subheader("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
with st.container():
|
302 |
-
|
|
|
303 |
|
304 |
-
|
305 |
-
|
|
|
306 |
|
307 |
-
col1, col2 = st.columns(2)
|
308 |
|
|
|
309 |
with col1:
|
310 |
-
st.write("Top 10 Classes
|
311 |
-
st.bar_chart(
|
312 |
-
horizontal=True,
|
313 |
-
|
314 |
with col2:
|
315 |
-
st.write("
|
316 |
-
st.bar_chart(
|
317 |
-
horizontal=True,
|
318 |
-
|
319 |
-
st.markdown('</div>', unsafe_allow_html=True) # CLOSE DIV BLOCK
|
320 |
-
|
321 |
-
new_model = "Food Vision"
|
322 |
st.divider()
|
323 |
-
st.divider()
|
324 |
-
st.header(f"Try out the Current Models, :blue[{current_model}] and :blue[{new_model}] your self.")
|
325 |
-
st.caption("_The Model is periodically being improved. Model might change in the future_.")
|
326 |
|
327 |
|
|
|
|
|
328 |
def load_model(filepath):
|
329 |
-
"""
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
else:
|
368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
369 |
|
|
|
370 |
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
input image.
|
375 |
-
|
376 |
-
Args:
|
377 |
-
model_path(str): The path to the Model
|
378 |
-
image(UploadedFile Object): the uploaded image.
|
379 |
-
|
380 |
-
Returns:
|
381 |
-
predicted_class_name(str): the name of the predicted class.
|
382 |
-
"""
|
383 |
-
with st.spinner("Predicting using your image..."):
|
384 |
-
# Process the image
|
385 |
-
processed_image = load_prep_image(image, scale=False) # EfficientNet has built in scaling
|
386 |
-
model = load_model(model_path)
|
387 |
-
pred_prob = model.predict(processed_image)
|
388 |
-
predicted_class = class_names[pred_prob.argmax()] # Get the predicted class name
|
389 |
|
390 |
-
|
|
|
|
|
|
|
391 |
|
|
|
|
|
|
|
|
|
392 |
|
393 |
-
|
394 |
-
|
395 |
-
if option == "upload":
|
396 |
-
st.session_state.upload = True
|
397 |
-
st.session_state.camera = False
|
398 |
-
elif option == "camera":
|
399 |
-
st.session_state.upload = False
|
400 |
-
st.session_state.camera = True
|
401 |
|
|
|
|
|
|
|
|
|
|
|
402 |
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
st.session_state.model_mini = True
|
407 |
-
st.session_state.food_vision = False
|
408 |
-
elif option == "food_vision":
|
409 |
-
st.session_state.model_mini = False
|
410 |
-
st.session_state.food_vision = True
|
411 |
|
|
|
|
|
|
|
|
|
|
|
412 |
|
413 |
-
|
414 |
-
st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
415 |
|
416 |
-
#
|
417 |
-
|
418 |
-
# Define columns inside the main container
|
419 |
-
cols = st.columns([3, 1, 2, 1, 2], gap="medium")
|
420 |
-
has_predicted = False
|
421 |
-
has_uploaded = False
|
422 |
-
|
423 |
-
# 🖼️ Image Input Container
|
424 |
-
with cols[0]:
|
425 |
-
with st.container():
|
426 |
-
st.markdown('<div class="centered">', unsafe_allow_html=True) # START DIV BLOCK
|
427 |
-
|
428 |
-
with st.spinner("Uploading image..."):
|
429 |
-
try:
|
430 |
-
upload = st.checkbox("Upload Image", key="upload",
|
431 |
-
on_change=toggle_checkbox, args=("upload",))
|
432 |
-
camera = st.checkbox("Use your camera", key="camera",
|
433 |
-
on_change=toggle_checkbox, args=("camera",))
|
434 |
-
if upload:
|
435 |
-
uploaded_image = st.file_uploader(label="Upload an image (Max 200MB)",
|
436 |
-
type=["png", "jpg", "jpeg"],
|
437 |
-
accept_multiple_files=False, key="uploaded_image")
|
438 |
-
|
439 |
-
has_uploaded = True # To check if file_uploader widget has loaded
|
440 |
-
|
441 |
-
if "uploaded_image" not in st.session_state:
|
442 |
-
st.session_state["uploaded_image"] = uploaded_image
|
443 |
-
|
444 |
-
elif camera:
|
445 |
-
uploaded_image = st.camera_input("Take a Picture",
|
446 |
-
disabled=not camera, key="uploaded_image")
|
447 |
-
|
448 |
-
has_uploaded = True # To check if camera_input widget has loaded
|
449 |
-
|
450 |
-
if "uploaded_image" not in st.session_state:
|
451 |
-
st.session_state["uploaded_image"] = uploaded_image
|
452 |
-
|
453 |
-
except Exception as e:
|
454 |
-
st.error(f"Image Upload failed: {e}")
|
455 |
-
else:
|
456 |
-
if has_uploaded: # If file_uploader/camera_input widget has loaded
|
457 |
-
if uploaded_image: # If user has uploaded an image
|
458 |
-
st.success("Image Uploaded.")
|
459 |
-
st.image(st.session_state.uploaded_image,
|
460 |
-
caption="Your uploaded image", width=200)
|
461 |
-
|
462 |
-
st.markdown('</div>', unsafe_allow_html=True) # CLOSE DIV BLOCK
|
463 |
-
|
464 |
-
# ➡️ Arrow 1 Container
|
465 |
-
with cols[1]:
|
466 |
-
with st.container():
|
467 |
-
st.markdown('<div class="centered">', unsafe_allow_html=True)
|
468 |
-
st.write("➡️") # Example arrow to be changed to image
|
469 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
470 |
-
|
471 |
-
# 🧠 Neural Network Image Container
|
472 |
-
with cols[2]:
|
473 |
-
with st.container():
|
474 |
-
st.markdown('<div class="centered">', unsafe_allow_html=True)
|
475 |
-
|
476 |
-
st.write("Pick a Model")
|
477 |
-
model_mini = st.checkbox("Model Mini", key="model_mini",
|
478 |
-
on_change=toggle_model, args=("model_mini",))
|
479 |
-
food_vision = st.checkbox("Food Vision", key="food_vision",
|
480 |
-
on_change=toggle_model, args=("food_vision",))
|
481 |
-
|
482 |
-
if model_mini:
|
483 |
-
st.image("brain.png")
|
484 |
-
elif food_vision:
|
485 |
-
st.image("content/creativity_15557951.png") # To be changed
|
486 |
-
|
487 |
-
if has_uploaded:
|
488 |
-
status = st.button(label="Predict Using Image", icon="⚛️", type="primary")
|
489 |
-
if status and model_mini:
|
490 |
-
result_class = predict_using_model(uploaded_image,
|
491 |
-
model_path="model_mini_Food101.keras")
|
492 |
-
has_predicted = True
|
493 |
-
elif status and food_vision:
|
494 |
-
result_class = predict_using_model(uploaded_image, model_path="FoodVision.keras")
|
495 |
-
has_predicted = True
|
496 |
-
|
497 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
498 |
-
|
499 |
-
# ➡️ Arrow 2 Container
|
500 |
-
with cols[3]:
|
501 |
-
with st.container():
|
502 |
-
st.markdown('<div class="centered">', unsafe_allow_html=True)
|
503 |
-
st.write("➡️") # Example arrow to be changed to image
|
504 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
505 |
-
|
506 |
-
# 🏆 Output Container
|
507 |
-
with cols[4]:
|
508 |
-
with st.container():
|
509 |
-
st.markdown('<div class="centered">', unsafe_allow_html=True)
|
510 |
-
if has_predicted:
|
511 |
-
st.image(st.session_state.uploaded_image)
|
512 |
-
if "_" in result_class:
|
513 |
-
modified_class = result_class.replace("_", "").title()
|
514 |
-
st.write(f"This is an image of :blue[{modified_class}]")
|
515 |
-
else:
|
516 |
-
st.write(f"This is an image of :blue[{result_class.title()}]")
|
517 |
-
else:
|
518 |
-
st.write("The Image and Prediction will appear here")
|
519 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
520 |
-
|
521 |
-
# Close the widened container
|
522 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
import streamlit as st
|
3 |
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
4 |
import tensorflow as tf
|
5 |
import pandas as pd
|
6 |
+
from PIL import Image # Needed for image display consistency potentially
|
7 |
|
8 |
# 🔹 Expand the Page Layout
|
9 |
+
st.set_page_config(layout="wide") # Use Streamlit's built-in wide layout
|
10 |
|
11 |
+
# --- Constants and Data ---
|
12 |
current_model = "Model Mini"
|
13 |
+
new_model = "Food Vision" # Define the second model name
|
14 |
+
class_names = ['apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare',
|
15 |
+
'beet_salad', 'beignets', 'bibimbap', 'bread_pudding', 'breakfast_burrito',
|
16 |
+
'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad', 'carrot_cake',
|
17 |
+
'ceviche', 'cheese_plate', 'cheesecake', 'chicken_curry', 'chicken_quesadilla',
|
18 |
+
'chicken_wings', 'chocolate_cake', 'chocolate_mousse', 'churros', 'clam_chowder',
|
19 |
+
'club_sandwich', 'crab_cakes', 'creme_brulee', 'croque_madame', 'cup_cakes',
|
20 |
+
'deviled_eggs', 'donuts', 'dumplings', 'edamame', 'eggs_benedict', 'escargots',
|
21 |
+
'falafel', 'filet_mignon', 'fish_and_chips', 'foie_gras', 'french_fries',
|
22 |
+
'french_onion_soup', 'french_toast', 'fried_calamari', 'fried_rice',
|
23 |
+
'frozen_yogurt', 'garlic_bread', 'gnocchi', 'greek_salad',
|
24 |
+
'grilled_cheese_sandwich', 'grilled_salmon', 'guacamole', 'gyoza', 'hamburger',
|
25 |
+
'hot_and_sour_soup', 'hot_dog', 'huevos_rancheros', 'hummus', 'ice_cream',
|
26 |
+
'lasagna', 'lobster_bisque', 'lobster_roll_sandwich', 'macaroni_and_cheese',
|
27 |
+
'macarons', 'miso_soup', 'mussels', 'nachos', 'omelette', 'onion_rings',
|
28 |
+
'oysters', 'pad_thai', 'paella', 'pancakes', 'panna_cotta', 'peking_duck',
|
29 |
+
'pho', 'pizza', 'pork_chop', 'poutine', 'prime_rib', 'pulled_pork_sandwich',
|
30 |
+
'ramen', 'ravioli', 'red_velvet_cake', 'risotto', 'samosa', 'sashimi',
|
31 |
+
'scallops', 'seaweed_salad', 'shrimp_and_grits', 'spaghetti_bolognese',
|
32 |
+
'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake',
|
33 |
+
'sushi', 'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
top_ten_dict = {
|
36 |
+
"class_name": ["edamame", "macarons", "oysters", "pho", "mussels", # Corrected 'mussles' -> 'mussels'
|
37 |
+
"sashimi", "seaweed_salad", "dumplings", "guacamole", "onion_rings"],
|
38 |
"f1-score": [0.964427, 0.900433, 0.853119, 0.852652, 0.850622,
|
39 |
0.844794, 0.834356, 0.833006, 0.83209, 0.831967]
|
40 |
}
|
|
|
41 |
last_ten_dict = {
|
42 |
+
"class_name": ["chocolate_mousse", "tuna_tartare", "scallops", "huevos_rancheros",
|
43 |
+
"foie_gras", "steak", "bread_pudding", "ravioli", "pork_chop", "apple_pie"],
|
44 |
+
"f1-score": [0.413793, 0.399254, 0.383693, 0.367698, 0.354497,
|
45 |
+
0.340426, 0.340045, 0.339785, 0.324826, 0.282407]
|
|
|
46 |
}
|
47 |
|
48 |
+
# 🔹 Custom CSS for Centered Content within elements and layout stability
|
49 |
st.markdown(
|
50 |
"""
|
51 |
<style>
|
52 |
+
/* Center content vertically and horizontally using flexbox */
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
.centered {
|
54 |
display: flex;
|
55 |
flex-direction: column;
|
56 |
align-items: center;
|
57 |
+
justify-content: center; /* Can adjust to flex-start if needed */
|
58 |
text-align: center;
|
59 |
+
width: 100%; /* Take full width of its container (e.g., column) */
|
60 |
+
min-height: 300px; /* Give containers minimum height to reduce collapse */
|
61 |
+
padding-top: 20px; /* Add some padding */
|
62 |
+
padding-bottom: 20px;
|
63 |
}
|
64 |
|
65 |
+
/* Style file uploader for better centering if needed */
|
66 |
+
/* Streamlit structure might change, this targets common patterns */
|
67 |
+
div[data-testid="stFileUploader"] > section {
|
68 |
+
padding: 0; /* Reduce default padding if it pushes content */
|
69 |
}
|
70 |
+
div[data-testid="stFileUploader"] > section > input {
|
71 |
+
/* Hide default input if necessary */
|
|
|
|
|
72 |
}
|
73 |
+
div[data-testid="stFileUploader"] label {
|
74 |
+
/* Style the label if needed */
|
75 |
+
}
|
76 |
+
|
77 |
|
78 |
+
/* Center images and standardize size */
|
79 |
+
.centered img { /* Target images specifically within centered divs */
|
80 |
display: block;
|
81 |
margin-left: auto;
|
82 |
margin-right: auto;
|
83 |
+
max-width: 200px; /* Use max-width for responsiveness */
|
84 |
+
max-height: 200px; /* Use max-height */
|
85 |
+
width: auto; /* Allow auto width */
|
86 |
+
height: auto; /* Allow auto height */
|
87 |
+
object-fit: contain; /* Contain ensures the whole image fits */
|
88 |
border-radius: 20px;
|
89 |
+
margin-bottom: 15px; /* Add space below image */
|
90 |
}
|
91 |
+
|
92 |
+
/* Ensure columns try to vertically align content */
|
93 |
+
div[data-testid="stVerticalBlock"] div[data-testid="stHorizontalBlock"] {
|
94 |
+
align-items: center;
|
95 |
+
}
|
96 |
+
|
97 |
+
/* Style the radio buttons */
|
98 |
+
div[data-testid="stRadio"] > label {
|
99 |
+
font-weight: bold; /* Make label bold */
|
100 |
+
margin-bottom: 10px;
|
101 |
+
}
|
102 |
+
div[data-testid="stRadio"] > div {
|
103 |
+
display: flex;
|
104 |
+
justify-content: center; /* Center radio options */
|
105 |
+
gap: 15px; /* Add space between radio buttons */
|
106 |
+
}
|
107 |
+
|
108 |
+
/* Style the button */
|
109 |
+
div[data-testid="stButton"] > button {
|
110 |
+
width: 80%; /* Make button wider */
|
111 |
+
margin-top: 20px; /* Add space above button */
|
112 |
+
}
|
113 |
+
|
114 |
</style>
|
115 |
""",
|
116 |
unsafe_allow_html=True
|
117 |
)
|
118 |
|
119 |
+
# --- Page Title and Intro ---
|
120 |
+
st.title("Food Vision Demo App 🍔🧠")
|
121 |
+
st.header("A food vision app using a CNN model fine-tuned on EfficientNet.")
|
|
|
122 |
st.divider()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
# --- Explanations (Collapsible) ---
|
125 |
+
with st.expander("Learn More: What is a CNN?"):
|
126 |
+
st.write("""
|
127 |
+
A Neural Network is a system inspired by the human brain, composed of interconnected nodes (neurons) organized in layers: an input layer, one or more hidden layers, and an output layer.
|
128 |
+
Data (like text, numbers, images) is fed into the input layer, encoded as numbers. This information flows through the network, undergoing mathematical transformations at each node based on learned 'weights'.
|
129 |
+
The network 'learns' by adjusting these weights during training to minimize the difference between its predictions and the actual outcomes.
|
130 |
+
""")
|
131 |
+
# Consider adding a simple diagram URL if available
|
132 |
+
# st.image("url_to_neural_network_diagram.png")
|
133 |
+
st.write("""
|
134 |
+
A **Convolutional Neural Network (CNN)** is a specialized type of neural network particularly effective for processing grid-like data, such as images and videos.
|
135 |
+
CNNs use special layers called 'convolutional layers' that apply filters to input images to automatically learn hierarchical patterns, like edges, textures, and shapes. This makes them excellent for visual recognition tasks.
|
136 |
+
""")
|
137 |
+
|
138 |
+
with st.expander("Learn More: Sample CNN Code Snippet (TensorFlow/Keras)"):
|
139 |
+
st.write("This is a simplified example showing key components like using a pre-trained base model (EfficientNet), adding custom layers, enabling mixed precision (for faster training), and compiling the model.")
|
140 |
+
code = """
|
141 |
+
import tensorflow as tf
|
142 |
+
from tensorflow.keras import layers, models, applications
|
143 |
from tensorflow.keras import mixed_precision
|
144 |
|
145 |
+
# --- Configuration ---
|
146 |
+
IMAGE_SHAPE = (224, 224, 3)
|
147 |
+
NUM_CLASSES = 101 # Example number of food classes
|
148 |
+
|
149 |
+
# --- Enable Mixed Precision (Optional but recommended for speed) ---
|
150 |
+
# mixed_precision.set_global_policy("mixed_float16")
|
151 |
+
|
152 |
+
# --- Data Augmentation Layer ---
|
153 |
+
# Define data augmentation transformations here
|
154 |
+
data_augmentation = tf.keras.Sequential([
|
155 |
+
layers.RandomFlip("horizontal"),
|
156 |
+
layers.RandomRotation(0.2),
|
157 |
+
layers.RandomZoom(0.2),
|
158 |
+
layers.RandomHeight(0.2),
|
159 |
+
layers.RandomWidth(0.2),
|
160 |
+
# Rescaling might be part of EfficientNet preprocessing, check docs
|
161 |
+
], name="data_augmentation")
|
162 |
+
|
163 |
+
# --- Build the Model using Functional API ---
|
164 |
+
# 1. Input Layer
|
165 |
+
inputs = layers.Input(shape=IMAGE_SHAPE, name="input_layer")
|
166 |
+
|
167 |
+
# 2. Data Augmentation (applied during training)
|
168 |
+
# x = data_augmentation(inputs) # Apply augmentation first
|
169 |
+
|
170 |
+
# 3. Base Model (EfficientNetB0) - Transfer Learning
|
171 |
+
base_model = applications.EfficientNetB0(include_top=False, # Don't include the final classification layer
|
172 |
+
weights='imagenet', # Load pre-trained weights
|
173 |
+
input_shape=IMAGE_SHAPE)
|
174 |
+
base_model.trainable = False # Freeze the base model initially
|
175 |
+
|
176 |
+
# Pass input through base model (ensure correct preprocessing if not done before)
|
177 |
+
# EfficientNet often has a preprocessing function or handles rescaling internally
|
178 |
+
x = base_model(inputs, training=False) # Use inputs directly if augmentation is after base_model
|
179 |
+
|
180 |
+
# 4. Pooling Layer
|
181 |
+
x = layers.GlobalAveragePooling2D(name="global_average_pooling")(x)
|
182 |
+
|
183 |
+
# 5. Output Layer (Dense)
|
184 |
+
# The number of units must match the number of classes
|
185 |
+
# Use float32 for the final layer for numerical stability with mixed precision
|
186 |
+
logits = layers.Dense(NUM_CLASSES, name="dense_logits")(x)
|
187 |
+
outputs = layers.Activation("softmax", dtype=tf.float32, name="softmax_output")(logits)
|
188 |
+
|
189 |
+
# 6. Create the Model
|
190 |
+
model = models.Model(inputs=inputs, outputs=outputs)
|
191 |
+
|
192 |
+
# --- Compile the Model ---
|
193 |
+
# Use Adam optimizer (common choice)
|
194 |
+
optimizer = tf.keras.optimizers.Adam()
|
195 |
+
# If using mixed precision, wrap the optimizer
|
196 |
+
# optimizer = mixed_precision.LossScaleOptimizer(optimizer)
|
197 |
+
|
198 |
+
model.compile(loss="categorical_crossentropy", # Use if labels are one-hot encoded
|
199 |
optimizer=optimizer,
|
200 |
metrics=["accuracy"])
|
201 |
|
202 |
+
# --- Model Summary ---
|
203 |
+
# model.summary()
|
204 |
+
|
205 |
+
# --- Train the Model (Example) ---
|
206 |
+
# history = model.fit(train_data,
|
207 |
+
# epochs=5,
|
208 |
+
# validation_data=test_data,
|
209 |
+
# ...)
|
210 |
+
"""
|
211 |
+
st.code(code, language="python")
|
212 |
+
|
213 |
+
with st.expander("Learn More: What is EfficientNet?"):
|
214 |
+
st.write("""
|
215 |
+
EfficientNet is a family of Convolutional Neural Networks (CNNs) developed by Google Brain.
|
216 |
+
Its key innovation is a method called **compound scaling**. Instead of arbitrarily increasing just the depth (number of layers), width (number of channels), or input image resolution, EfficientNet scales all three dimensions simultaneously using a fixed set of scaling coefficients.
|
217 |
+
This balanced scaling approach allows EfficientNet models (like EfficientNetB0, B1, ..., B7) to achieve state-of-the-art accuracy on image classification tasks while being significantly smaller and faster (more computationally efficient) than previous models with similar accuracy.
|
218 |
+
""")
|
219 |
+
|
220 |
+
with st.expander("Learn More: What is Fine-Tuning?"):
|
221 |
+
st.write("""
|
222 |
+
**Fine-tuning** is a transfer learning technique where you take a model pre-trained on a large dataset (like ImageNet, which contains millions of general images) and train it further on a smaller, specific dataset (like our Food-101 dataset).
|
223 |
+
|
224 |
+
**Why Fine-Tune?**
|
225 |
+
1. **Leverage Existing Knowledge:** The pre-trained model has already learned general visual features (edges, textures, shapes) from the large dataset.
|
226 |
+
2. **Faster Training:** You don't need to train the entire network from scratch, saving significant time and computational resources.
|
227 |
+
3. **Better Performance on Small Datasets:** It often leads to better results than training from scratch, especially when your specific dataset is relatively small.
|
228 |
+
|
229 |
+
**Process:**
|
230 |
+
1. **Load Pre-trained Model:** Load a model (like EfficientNet) with its pre-trained weights, typically excluding its final classification layer.
|
231 |
+
2. **Freeze Base Layers:** Initially, keep the weights of the pre-trained layers frozen (`trainable = False`).
|
232 |
+
3. **Add New Layers:** Add new layers on top (e.g., Pooling, Dense layers) suitable for your specific task (e.g., classifying 101 food types).
|
233 |
+
4. **Train Top Layers:** Train *only* the new layers on your dataset for a few epochs.
|
234 |
+
5. **(Optional but common) Unfreeze Some Layers:** Unfreeze some of the later layers of the base model (`trainable = True`).
|
235 |
+
6. **Train with Low Learning Rate:** Continue training the entire network (or the unfrozen parts) with a very low learning rate. This allows the pre-trained weights to adapt slightly to the nuances of your specific dataset without drastically changing the learned general features.
|
236 |
+
""")
|
237 |
+
|
238 |
+
with st.expander("Learn More: Fine-Tuning Code Snippet (TensorFlow/Keras)"):
|
239 |
+
st.write("This snippet shows how to unfreeze layers and re-compile the model for fine-tuning, typically done *after* initial feature extraction training.")
|
240 |
+
tune_code = """
|
241 |
+
# --- Load weights from initial training phase (where base_model was frozen) ---
|
242 |
+
# model.load_weights(checkpoint_path_feature_extraction)
|
243 |
+
|
244 |
+
# --- Unfreeze some or all layers of the base model ---
|
245 |
base_model.trainable = True
|
246 |
|
247 |
+
# --- Optional: Freeze earlier layers again (fine-tune only later layers) ---
|
248 |
+
# print(f"Number of layers in base model: {len(base_model.layers)}")
|
249 |
+
# Fine-tune from this layer onwards
|
250 |
+
# fine_tune_at = 100 # Example: Unfreeze layers from index 100 onwards
|
251 |
|
252 |
+
# for layer in base_model.layers[:fine_tune_at]:
|
253 |
+
# layer.trainable = False
|
254 |
|
255 |
+
# --- Re-compile the Model with a Lower Learning Rate ---
|
256 |
+
# Lowering the learning rate is crucial for fine-tuning to avoid
|
257 |
+
# destroying the pre-trained weights.
|
258 |
+
LOW_LEARNING_RATE = 0.0001 # Example: 10x smaller than initial LR
|
259 |
|
260 |
+
optimizer = tf.keras.optimizers.Adam(learning_rate=LOW_LEARNING_RATE)
|
261 |
+
# If using mixed precision:
|
262 |
+
# optimizer = mixed_precision.LossScaleOptimizer(tf.keras.optimizers.Adam(learning_rate=LOW_LEARNING_RATE))
|
263 |
|
264 |
+
model.compile(loss="categorical_crossentropy",
|
265 |
+
optimizer=optimizer, # Use the optimizer with the low learning rate
|
266 |
+
metrics=["accuracy"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
+
# --- Continue Training (Fine-tuning) ---
|
269 |
+
# history_fine_tune = model.fit(train_data,
|
270 |
+
# epochs=initial_epochs + 5, # Train for a few more epochs
|
271 |
+
# initial_epoch=history.epoch[-1], # Start where previous training left off
|
272 |
+
# validation_data=test_data,
|
273 |
+
# ...)
|
274 |
+
"""
|
275 |
+
st.code(tune_code, language="python")
|
276 |
st.divider()
|
|
|
|
|
|
|
|
|
277 |
|
278 |
+
# --- Model Build Details ---
|
279 |
+
st.subheader("Model Building Details")
|
280 |
+
formatted_class_names = [food.replace("_", " ").title() for food in class_names]
|
281 |
+
st.write(f"The model was built using the **Food-101 dataset**.")
|
282 |
+
with st.expander("View All 101 Food Classes"):
|
283 |
+
st.write(f"The dataset consists of 101 classes of food: {', '.join(formatted_class_names)}")
|
284 |
+
st.info("When predicting, please provide an image belonging to one of these 101 classes. The model has not been trained on other types of food or objects.")
|
285 |
st.divider()
|
286 |
|
287 |
+
# --- Model Performance ---
|
288 |
+
st.subheader("Model Performance Insights")
|
289 |
+
st.write("""
|
290 |
+
After training, some food classes are predicted more accurately than others.
|
291 |
+
This can be due to factors like the number of training images available for each class, visual similarity between classes, and image quality.
|
292 |
+
We use the **F1-score** to evaluate performance per class, as it balances precision and recall.
|
293 |
+
""")
|
294 |
+
|
295 |
+
with st.expander("What is the F1-Score?"):
|
296 |
+
st.write("""
|
297 |
+
The **F1-score** is a metric used to evaluate a model's accuracy on classification tasks, especially when dealing with imbalanced datasets (where some classes have many more samples than others). It's the harmonic mean of **Precision** and **Recall**.
|
298 |
+
|
299 |
+
* **Precision:** Out of all the times the model predicted a specific class (e.g., "Pizza"), what proportion were actually correct?
|
300 |
+
$$ \text{Precision} = \\frac{\\text{True Positives}}{\\text{True Positives} + \\text{False Positives}} $$
|
301 |
+
* **Recall (Sensitivity):** Out of all the actual instances of a specific class (e.g., all the real Pizza images), what proportion did the model correctly identify?
|
302 |
+
$$ \text{Recall} = \\frac{\\text{True Positives}}{\\text{True Positives} + \\text{False Negatives}} $$
|
303 |
+
|
304 |
+
The F1-score combines these two:
|
305 |
+
""")
|
306 |
+
st.latex(r"F_1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}")
|
307 |
+
st.write("An F1-score ranges from 0 (worst) to 1 (best - perfect precision and recall).")
|
308 |
+
|
309 |
+
# --- Top/Last 10 Charts ---
|
310 |
+
st.subheader("Top and Least Performing Classes (by F1-Score)")
|
311 |
with st.container():
|
312 |
+
top_ten_df = pd.DataFrame(top_ten_dict).sort_values("f1-score", ascending=False)
|
313 |
+
last_ten_df = pd.DataFrame(last_ten_dict).sort_values("f1-score", ascending=True) # Already sorted ascendingly in dict creation usually
|
314 |
|
315 |
+
# Format class names for display
|
316 |
+
top_ten_df['class_name_display'] = top_ten_df['class_name'].str.replace('_', ' ').str.title()
|
317 |
+
last_ten_df['class_name_display'] = last_ten_df['class_name'].str.replace('_', ' ').str.title()
|
318 |
|
|
|
319 |
|
320 |
+
col1, col2 = st.columns(2)
|
321 |
with col1:
|
322 |
+
st.write("**Top 10 Classes**")
|
323 |
+
st.bar_chart(top_ten_df.set_index('class_name_display')['f1-score'],
|
324 |
+
# horizontal=True, # Bar chart auto-detects horizontal best here
|
325 |
+
use_container_width=True)
|
326 |
with col2:
|
327 |
+
st.write("**Bottom 10 Classes**")
|
328 |
+
st.bar_chart(last_ten_df.set_index('class_name_display')['f1-score'],
|
329 |
+
# horizontal=True,
|
330 |
+
use_container_width=True, color="#ff748c") # Red color for low scores
|
|
|
|
|
|
|
331 |
st.divider()
|
|
|
|
|
|
|
332 |
|
333 |
|
334 |
+
# --- Helper Functions ---
|
335 |
+
@st.cache_resource # Cache the loaded model
|
336 |
def load_model(filepath):
|
337 |
+
"""Loads a Tensorflow Keras Model."""
|
338 |
+
st.write(f"Cache miss: Loading model from {filepath}") # Debug message
|
339 |
+
try:
|
340 |
+
model = tf.keras.models.load_model(filepath)
|
341 |
+
# You might need a warm-up prediction for GPU memory allocation
|
342 |
+
# For example: model.predict(tf.zeros([1, 224, 224, 3]))
|
343 |
+
return model
|
344 |
+
except Exception as e:
|
345 |
+
st.error(f"Error loading model from {filepath}: {e}")
|
346 |
+
return None
|
347 |
+
|
348 |
+
def load_prep_image(image_input: UploadedFile, img_shape=224):
|
349 |
+
"""Reads and preprocesses an image for EfficientNet prediction."""
|
350 |
+
try:
|
351 |
+
# Read image file buffer
|
352 |
+
bytes_data = image_input.getvalue()
|
353 |
+
# Decode image
|
354 |
+
image_tensor = tf.io.decode_image(bytes_data, channels=3)
|
355 |
+
# Resize image
|
356 |
+
# Use tf.image.resize with method='nearest' or 'bilinear' (default)
|
357 |
+
image_tensor_resized = tf.image.resize(image_tensor, [img_shape, img_shape])
|
358 |
+
# Expand dimensions to create batch_size 1 -> (1, H, W, C)
|
359 |
+
image_tensor_expanded = tf.expand_dims(image_tensor_resized, axis=0)
|
360 |
+
# EfficientNet models usually have their own preprocessing layer/function
|
361 |
+
# or expect inputs scaled 0-255. Check the specific model's requirement.
|
362 |
+
# If it expects 0-1 scaling and doesn't do it internally:
|
363 |
+
# image_tensor_scaled = image_tensor_expanded / 255.0
|
364 |
+
# return image_tensor_scaled
|
365 |
+
# Assuming EfficientNet B0 handles scaling or expects 0-255:
|
366 |
+
return image_tensor_expanded
|
367 |
+
except Exception as e:
|
368 |
+
st.error(f"Error processing image: {e}")
|
369 |
+
return None
|
370 |
+
|
371 |
+
def predict_using_model(image_input: UploadedFile, model_path: str) -> tuple[str | None, float | None]:
|
372 |
+
"""Predicts the class name and probability for an image."""
|
373 |
+
if image_input is None:
|
374 |
+
st.warning("No image provided for prediction.")
|
375 |
+
return None, None
|
376 |
+
|
377 |
+
processed_image = load_prep_image(image_input)
|
378 |
+
if processed_image is None:
|
379 |
+
return None, None
|
380 |
+
|
381 |
+
model = load_model(model_path)
|
382 |
+
if model is None:
|
383 |
+
return None, None
|
384 |
+
|
385 |
+
try:
|
386 |
+
with st.spinner("🤖 Model is predicting..."):
|
387 |
+
pred_prob = model.predict(processed_image)
|
388 |
+
predicted_index = tf.argmax(pred_prob, axis=1).numpy()[0] # Get index of highest probability
|
389 |
+
predicted_class_name = class_names[predicted_index]
|
390 |
+
predicted_probability = float(tf.reduce_max(pred_prob).numpy()) # Get the highest probability
|
391 |
+
return predicted_class_name, predicted_probability
|
392 |
+
except Exception as e:
|
393 |
+
st.error(f"Prediction failed: {e}")
|
394 |
+
return None, None
|
395 |
+
|
396 |
+
# --- Interactive Demo Section ---
|
397 |
+
st.divider()
|
398 |
+
st.header(f"Try the Models: :blue[{current_model}] & :blue[{new_model}]")
|
399 |
+
st.caption("_Model performance may vary. Models are periodically updated._")
|
400 |
+
|
401 |
+
# Initialize session state keys if they don't exist
|
402 |
+
if "prediction_result" not in st.session_state:
|
403 |
+
st.session_state.prediction_result = None
|
404 |
+
if "predicted_image_bytes" not in st.session_state:
|
405 |
+
st.session_state.predicted_image_bytes = None
|
406 |
+
if "predicted_prob" not in st.session_state:
|
407 |
+
st.session_state.predicted_prob = None
|
408 |
+
|
409 |
+
|
410 |
+
# Use columns for layout
|
411 |
+
cols = st.columns([3, 0.5, 2, 0.5, 3], gap="medium") # Adjusted column ratios and gaps
|
412 |
+
|
413 |
+
# --- Column 1: Image Input ---
|
414 |
+
with cols[0]:
|
415 |
+
st.markdown('<div class="centered">', unsafe_allow_html=True) # Apply centering
|
416 |
+
st.subheader("1. Provide an Image")
|
417 |
+
image_source = st.radio(
|
418 |
+
"Choose image source:",
|
419 |
+
("Upload Image", "Use Camera"),
|
420 |
+
key="image_source",
|
421 |
+
horizontal=True,
|
422 |
+
label_visibility="collapsed" # Hide the radio label itself
|
423 |
+
)
|
424 |
+
|
425 |
+
uploaded_image = None
|
426 |
+
image_bytes_for_state = None
|
427 |
+
|
428 |
+
if image_source == "Upload Image":
|
429 |
+
uploaded_image = st.file_uploader(
|
430 |
+
"Upload (.png, .jpg, .jpeg)",
|
431 |
+
type=["png", "jpg", "jpeg"],
|
432 |
+
accept_multiple_files=False,
|
433 |
+
key="uploader",
|
434 |
+
label_visibility="collapsed"
|
435 |
+
)
|
436 |
+
elif image_source == "Use Camera":
|
437 |
+
uploaded_image = st.camera_input(
|
438 |
+
"Take a picture",
|
439 |
+
key="camera_input",
|
440 |
+
label_visibility="collapsed"
|
441 |
+
)
|
442 |
+
|
443 |
+
# Display uploaded image preview
|
444 |
+
if uploaded_image:
|
445 |
+
image_bytes_for_state = uploaded_image.getvalue() # Store bytes for state
|
446 |
+
st.image(image_bytes_for_state, caption="Your image", use_column_width='auto') # Auto width fits container
|
447 |
+
st.success("Image ready!")
|
448 |
else:
|
449 |
+
st.info("Upload or take a picture.")
|
450 |
+
|
451 |
+
st.markdown('</div>', unsafe_allow_html=True) # Close centered div
|
452 |
+
|
453 |
+
# --- Column 2: Arrow 1 ---
|
454 |
+
with cols[1]:
|
455 |
+
st.markdown('<div class="centered" style="justify-content: center; min-height: 300px;">➡️</div>', unsafe_allow_html=True)
|
456 |
+
|
457 |
+
# --- Column 3: Model Selection & Prediction ---
|
458 |
+
with cols[2]:
|
459 |
+
st.markdown('<div class="centered">', unsafe_allow_html=True) # Apply centering
|
460 |
+
st.subheader("2. Select Model")
|
461 |
+
|
462 |
+
chosen_model = st.radio(
|
463 |
+
"Pick a Model:",
|
464 |
+
(current_model, new_model),
|
465 |
+
key="model_choice",
|
466 |
+
horizontal=True,
|
467 |
+
label_visibility="collapsed"
|
468 |
+
)
|
469 |
+
|
470 |
+
model_path_to_use = ""
|
471 |
+
model_image_path = ""
|
472 |
+
|
473 |
+
if chosen_model == current_model: # Model Mini
|
474 |
+
model_image_path = "brain.png" # Make sure this file exists
|
475 |
+
model_path_to_use = "model_mini_Food101.keras" # Make sure this path is correct
|
476 |
+
elif chosen_model == new_model: # Food Vision
|
477 |
+
model_image_path = "content/creativity_15557951.png" # Make sure this file exists
|
478 |
+
model_path_to_use = "FoodVision.keras" # Make sure this path is correct
|
479 |
+
|
480 |
+
# Display model icon/image if path is valid
|
481 |
+
try:
|
482 |
+
if model_image_path:
|
483 |
+
st.image(model_image_path, width=150) # Control model image size
|
484 |
+
except Exception as e:
|
485 |
+
st.warning(f"Could not load model image: {model_image_path}")
|
486 |
+
|
487 |
+
# Prediction Button
|
488 |
+
predict_button = st.button(
|
489 |
+
label="Predict Food!",
|
490 |
+
icon="⚛️",
|
491 |
+
type="primary",
|
492 |
+
use_container_width=True, # Make button fill column width
|
493 |
+
disabled=not uploaded_image or not model_path_to_use # Disable if no image or path
|
494 |
+
)
|
495 |
+
|
496 |
+
if predict_button:
|
497 |
+
if uploaded_image and model_path_to_use:
|
498 |
+
# Perform prediction
|
499 |
+
result_class, result_prob = predict_using_model(uploaded_image, model_path=model_path_to_use)
|
500 |
+
# Store results in session state
|
501 |
+
st.session_state.prediction_result = result_class
|
502 |
+
st.session_state.predicted_prob = result_prob
|
503 |
+
st.session_state.predicted_image_bytes = image_bytes_for_state # Store the bytes of the image used
|
504 |
+
else:
|
505 |
+
st.warning("Please provide an image and select a valid model.")
|
506 |
|
507 |
+
st.markdown('</div>', unsafe_allow_html=True) # Close centered div
|
508 |
|
509 |
+
# --- Column 4: Arrow 2 ---
|
510 |
+
with cols[3]:
|
511 |
+
st.markdown('<div class="centered" style="justify-content: center; min-height: 300px;">➡️</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
|
513 |
+
# --- Column 5: Output ---
|
514 |
+
with cols[4]:
|
515 |
+
st.markdown('<div class="centered">', unsafe_allow_html=True) # Apply centering
|
516 |
+
st.subheader("3. Prediction Result")
|
517 |
|
518 |
+
# Display result from session state
|
519 |
+
if st.session_state.prediction_result and st.session_state.predicted_image_bytes:
|
520 |
+
# Display the image associated with the prediction
|
521 |
+
st.image(st.session_state.predicted_image_bytes, caption="Image Analyzed", use_column_width='auto')
|
522 |
|
523 |
+
result_class = st.session_state.prediction_result
|
524 |
+
probability = st.session_state.predicted_prob
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
|
526 |
+
# Format class name nicely
|
527 |
+
if "_" in result_class:
|
528 |
+
modified_class = result_class.replace("_", " ").title()
|
529 |
+
else:
|
530 |
+
modified_class = result_class.title()
|
531 |
|
532 |
+
st.success(f"Prediction: **:blue[{modified_class}]**")
|
533 |
+
if probability:
|
534 |
+
st.write(f"Confidence: {probability:.2%}") # Display confidence
|
|
|
|
|
|
|
|
|
|
|
535 |
|
536 |
+
elif predict_button:
|
537 |
+
# If button was clicked but prediction failed or had no result
|
538 |
+
st.error("Prediction could not be completed. Check logs or try again.")
|
539 |
+
else:
|
540 |
+
st.info("Result will appear here after prediction.")
|
541 |
|
542 |
+
st.markdown('</div>', unsafe_allow_html=True) # Close centered div
|
|
|
543 |
|
544 |
+
# --- Footer or Final Divider ---
|
545 |
+
st.divider()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|