Spaces:
Runtime error
Runtime error
File size: 2,650 Bytes
662acbd | 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 | 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}%)") |