import streamlit as st import tensorflow as tf import numpy as np from PIL import Image st.title('🍅 Simple Tomato Leaf Disease Classifier') @st.cache_resource def load_model(): # Define the same model architecture as in training model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), input_shape=(128, 128, 3), activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Conv2D(16, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Conv2D(8, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10, activation='softmax') ]) # Load only the weights (not the full saved model) model.load_weights('100-epoch with regularization.h5') return model model = load_model() # Class labels class_names = [ 'Tomato___Bacterial_spot', 'Tomato___Early_blight', 'Tomato___Late_blight', 'Tomato___Leaf_Mold', 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite', 'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', 'Tomato___healthy' ] uploaded_file = st.file_uploader('Upload a tomato leaf image', type=['jpg', 'jpeg', 'png']) if uploaded_file is not None: image = Image.open(uploaded_file).convert('RGB') st.image(image, caption='Uploaded Image', use_column_width=True) img = image.resize((128, 128)) img_array = np.array(img) / 255.0 img_array = np.expand_dims(img_array, axis=0) preds = model.predict(img_array) pred_class = np.argmax(preds, axis=1)[0] st.success(f'Predicted Class: {class_names[pred_class]}')