import streamlit as st import tensorflow as tf import numpy as np from PIL import Image from tensorflow.keras import layers, Input, Model, Sequential from tensorflow.keras.applications.resnet_v2 import ResNet50V2, preprocess_input st.set_page_config(page_title="CIFAR-10 Final", layout="centered") @st.cache_resource def load_fixed_model(): try: # 1. Define the Input img_input = Input(shape=(32, 32, 3), name="input_1") # 2. UpSampling (Layer 0) x = layers.UpSampling2D(size=(3, 3), name="up_sampling2d")(img_input) # 3. Augmentation (Layer 1) - We must use the same name as the notebook aug_layer = Sequential([ layers.RandomFlip("horizontal"), layers.RandomRotation(0.1), layers.RandomZoom(0.1), ], name="sequential") x = aug_layer(x) # 4. ResNet50V2 Base (Layer 2) # We set weights=None so it doesn't download anything base_model = ResNet50V2(weights=None, include_top=False, input_shape=(96, 96, 3)) x = base_model(x) # 5. The Head (Layers 3-7) x = layers.GlobalAveragePooling2D(name="global_average_pooling2d")(x) x = layers.BatchNormalization(name="batch_normalization")(x) x = layers.Dense(256, activation='relu', name="dense")(x) x = layers.Dropout(0.4, name="dropout")(x) predictions = layers.Dense(10, activation='softmax', name="dense_1")(x) # Create the model model = Model(inputs=img_input, outputs=predictions) # 6. THE FIX: Load by name # This ignores the 'layer index' and looks for 'resnet50v2', 'dense', etc. model.load_weights('cifar10_augtlmodel.h5', by_name=True, skip_mismatch=True) return model except Exception as e: st.error(f"Surgical Load Error: {e}") return None # --- UI LOGIC --- st.title("🚀 CIFAR-10 Classifier") st.write("Mode: Surgical Weight Mapping (Functional API)") model = load_fixed_model() uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"]) if uploaded_file and model: image = Image.open(uploaded_file).convert('RGB') st.image(image, width=250) img_32 = image.resize((32, 32)) x = np.array(img_32) x = np.expand_dims(x, axis=0) x = preprocess_input(x) if st.button('Predict'): preds = model.predict(x) labels = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck'] idx = np.argmax(preds) st.success(f"Result: **{labels[idx]}** ({np.max(preds)*100:.2f}%)")