import streamlit as st from streamlit.runtime.uploaded_file_manager import UploadedFile import tensorflow as tf import pandas as pd # 🔹 Expand the Page Layout st.set_page_config(layout="wide") # Forces full-width mode current_model = "Model Mini" class_names = ['apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare', 'beet_salad', 'beignets', 'bibimbap', 'bread_pudding', 'breakfast_burrito', 'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad', 'carrot_cake', 'ceviche', 'cheese_plate', 'cheesecake', 'chicken_curry', 'chicken_quesadilla', 'chicken_wings', 'chocolate_cake', 'chocolate_mousse', 'churros', 'clam_chowder', 'club_sandwich', 'crab_cakes', 'creme_brulee', 'croque_madame', 'cup_cakes', 'deviled_eggs', 'donuts', 'dumplings', 'edamame', 'eggs_benedict', 'escargots', 'falafel', 'filet_mignon', 'fish_and_chips', 'foie_gras', 'french_fries', 'french_onion_soup', 'french_toast', 'fried_calamari', 'fried_rice', 'frozen_yogurt', 'garlic_bread', 'gnocchi', 'greek_salad', 'grilled_cheese_sandwich', 'grilled_salmon', 'guacamole', 'gyoza', 'hamburger', 'hot_and_sour_soup', 'hot_dog', 'huevos_rancheros', 'hummus', 'ice_cream', 'lasagna', 'lobster_bisque', 'lobster_roll_sandwich', 'macaroni_and_cheese', 'macarons', 'miso_soup', 'mussels', 'nachos', 'omelette', 'onion_rings', 'oysters', 'pad_thai', 'paella', 'pancakes', 'panna_cotta', 'peking_duck', 'pho', 'pizza', 'pork_chop', 'poutine', 'prime_rib', 'pulled_pork_sandwich', 'ramen', 'ravioli', 'red_velvet_cake', 'risotto', 'samosa', 'sashimi', 'scallops', 'seaweed_salad', 'shrimp_and_grits', 'spaghetti_bolognese', 'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake', 'sushi', 'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles'] top_ten_dict = { "class_name": ["edamame", "macarons", "oysters", "pho", "mussles", "sashimi", "seaweed_salad", "dumplings", "guacamole", "onion_rings"], "f1-score": [0.964427, 0.900433, 0.853119, 0.852652, 0.850622, 0.844794, 0.834356, 0.833006, 0.83209, 0.831967] } last_ten_dict = { "class_name": ["chocolate_mousse", "tuna_tartare", "scallops", "huevos_rancheros", "foie_gras", "steak", "bread_pudding", "ravioli", "pork_chop", "apple_pie"], "f1-score": [0.413793, 0.399254, 0.383693, 0.367698, 0.354497, 0.340426, 0.340045, 0.339785, 0.324826, 0.282407] } # 🔹 Custom CSS for Full Width & Centered Content st.markdown( """ """, unsafe_allow_html=True ) st.title("Food vision demo App 🍔🧠") st.header( "A food vision app, using a Machine Learning Model(CNN), fine tuned on EfficientNet.") st.divider() st.subheader("What is a CNN(Convolutional Neural Network)") st.write("A Neural network is network of nodes, consiting of input nodes, output nodes and hidden nodes.\ Each node lies in its respective layer, corresponding to its name. \ The input nodes reside in the input layer, the output nodes reside in the output layer and the hidden\ nodes reside in the hidden layer. The nodes pass information from the input layer to the output layer.\ The information consists of data(text, numbers, pictures, audio, videos) encoded as numbers\ that the network uses to learn information. It does this through complex mathematical operations\ and algorithms.") # Display image of Neural Network here in between dividers st.write("A Convolutional Neural Network in short is a version\ of a Neural Network that specializes on Images, video, basically anything visual.") st.divider() code = """import tensorflow as tf from tensorflow.keras import mixed_precision # Enable mixed precision mixed_precision.set_global_policy("mixed_float16") image_shape = (224, 224, 3) # Load EfficientNet with mixed precision base_model = tf.keras.applications.EfficientNetB0(include_top=False) base_model.trainable = False inputs = tf.keras.layers.Input(shape=image_shape, name="input_layer") # Apply data augmentation x = data_augmentation(inputs) x = base_model(x, training=False) x = tf.keras.layers.GlobalAveragePooling2D(name="global_average_pooling_layer")(x) x = tf.keras.layers.Dense(len(train_data.class_names), name="dense_logits")(x) # Ensure output layer remains in FP32 outputs = tf.keras.layers.Activation(activation="softmax", dtype=tf.float32, name="predictions")(x) model = tf.keras.Model(inputs, outputs) # Use a LossScaleOptimizer to prevent numerical issues optimizer = mixed_precision.LossScaleOptimizer(tf.keras.optimizers.Adam()) model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=["accuracy"]) # Train the model history = model.fit(train_data, epochs=5, validation_data=test_data, validation_steps=int(0.15 * len(test_data)), callbacks=[create_tensorboard_callback("model_mini", "model"), checkpoint_callback])""" st.subheader("Sample Code for the CNN using TensorFlow Functional API using Transfer Learning (NOT FULL CODE)") st.code(code, language="python") st.divider() st.divider() st.subheader("What is Efficient Net") st.write("EfficientNet is a family of convolutional neural networks that are designed to be more efficient and accurate. \ It scales up the model's width, depth, and resolution in a balanced way, which helps to achieve better performance \ with fewer resources. In simple terms, EfficientNet can achieve high accuracy on image classification tasks while \ using less computational power and memory compared to other models.") st.divider() st.subheader("What is Fine Tuning") st.write("Fine-tuning is a process in machine learning where a pre-trained model is further trained on a new, but related, dataset. \ This helps the model to adapt to the new data and improve its performance on specific tasks. \ Essentially, it takes advantage of the knowledge the model has already gained and refines it for better accuracy.") st.divider() tune_code = """# Load feature extraction weights model.load_weights(checkpoint_path) # Unfreeze all layers in the base model base_model.trainable = True # Freeze all layers except the last 5 for layer in base_model.layers[:-5]: layer.trainable = False # Use a LossScaleOptimizer to prevent numerical issues optimizer = mixed_precision.LossScaleOptimizer(tf.keras.optimizers.Adam()) # Recompile the Model with Lower Learning Rate to reduce overfitting model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=["accuracy"]) # Learning rate lowered by 10x model_tuned_history = model.fit(train_data, epochs=10, initial_epoch=history.epoch[-1], validation_data=test_data, validation_steps=int(0.15 * len(test_data)), callbacks=[create_tensorboard_callback("model_mini", "model_tuned")])""" st.subheader("Example of Fine Tuning Using TensorFlow (NOT FULL CODE)") st.code(tune_code, language="python") st.divider() st.subheader("Model Building Details") st.write(f'The Model was built using the :blue[Food101 kaggle dataset].\ The Dataset consist of 101 classes of Food.\ Namely: {[food.replace("_", "").title() for food in class_names]}') st.divider() st.write("When predicting you have to pass an image of any of the 101 classes of food.\ The Model has not yet been trained outside the 101 classes of food yet.") st.divider() st.subheader("Top and Least Classes Performance.") st.write("After training, some classes evidently performed better than others.\ Below are the performance of the top classes and least classes based on the F1 score") st.divider() st.subheader("F1-score") 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. \ 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). \ 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).") st.divider() st.subheader("The formula for F1-score is") st.latex(r"F_1 = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}") st.divider() # Top 10 last 10 Bar charts st.subheader("Top and Least Classes") with st.container(): st.markdown('