Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +40 -16
src/streamlit_app.py
CHANGED
|
@@ -3,27 +3,59 @@ import numpy as np
|
|
| 3 |
from PIL import Image
|
| 4 |
import tensorflow as tf
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# -----------------------------------
|
| 8 |
# CONFIG
|
| 9 |
# -----------------------------------
|
| 10 |
IMG_SIZE = 300
|
| 11 |
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
|
|
|
| 12 |
|
| 13 |
# -----------------------------------
|
| 14 |
-
# MODEL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# -----------------------------------
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
st.error(f"
|
| 24 |
return None
|
| 25 |
-
|
| 26 |
-
model =
|
| 27 |
|
| 28 |
# -----------------------------------
|
| 29 |
# IMAGE PREPROCESS
|
|
@@ -32,18 +64,12 @@ def process_image(img):
|
|
| 32 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
| 33 |
img = np.array(img)
|
| 34 |
|
| 35 |
-
# Boyut kontrolleri
|
| 36 |
if img.ndim == 2:
|
| 37 |
img = np.stack([img]*3, axis=-1)
|
| 38 |
elif img.shape[-1] == 4:
|
| 39 |
img = img[..., :3]
|
| 40 |
|
| 41 |
-
# Batch boyutu ekle: (1, 300, 300, 3)
|
| 42 |
img = np.expand_dims(img, 0)
|
| 43 |
-
|
| 44 |
-
# EfficientNet B3 Preprocess
|
| 45 |
-
# Keras 3'te bu fonksiyon bazen farklı path'te olabilir,
|
| 46 |
-
# ama tf.keras.applications genelde güvenlidir.
|
| 47 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 48 |
return img
|
| 49 |
|
|
@@ -58,7 +84,6 @@ uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
| 58 |
if uploaded and model is not None:
|
| 59 |
try:
|
| 60 |
img = Image.open(uploaded).convert("RGB")
|
| 61 |
-
|
| 62 |
col1, col2 = st.columns(2)
|
| 63 |
|
| 64 |
with col1:
|
|
@@ -66,7 +91,6 @@ if uploaded and model is not None:
|
|
| 66 |
|
| 67 |
with col2:
|
| 68 |
st.write("Classifying...")
|
| 69 |
-
|
| 70 |
image_tensor = process_image(img)
|
| 71 |
preds = model.predict(image_tensor)
|
| 72 |
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import tensorflow as tf
|
| 5 |
import os
|
| 6 |
+
from tensorflow.keras import layers, models
|
| 7 |
+
from tensorflow.keras.applications import EfficientNetB3
|
| 8 |
|
| 9 |
# -----------------------------------
|
| 10 |
# CONFIG
|
| 11 |
# -----------------------------------
|
| 12 |
IMG_SIZE = 300
|
| 13 |
CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
|
| 14 |
+
NUM_CLASSES = len(CLASS_NAMES)
|
| 15 |
|
| 16 |
# -----------------------------------
|
| 17 |
+
# MODEL DEFINITION
|
| 18 |
+
# -----------------------------------
|
| 19 |
+
def create_model():
|
| 20 |
+
base_model = EfficientNetB3(
|
| 21 |
+
weights=None,
|
| 22 |
+
include_top=False,
|
| 23 |
+
input_shape=(IMG_SIZE, IMG_SIZE, 3)
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
base_model.trainable = True
|
| 27 |
+
|
| 28 |
+
model = models.Sequential([
|
| 29 |
+
base_model,
|
| 30 |
+
layers.GlobalAveragePooling2D(),
|
| 31 |
+
layers.BatchNormalization(),
|
| 32 |
+
layers.Dropout(0.3),
|
| 33 |
+
layers.Dense(256, activation='relu'),
|
| 34 |
+
layers.Dropout(0.2),
|
| 35 |
+
layers.Dense(NUM_CLASSES, activation='softmax')
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
+
return model
|
| 39 |
+
|
| 40 |
+
# -----------------------------------
|
| 41 |
+
# MODEL LOADING (WEIGHTS ONLY)
|
| 42 |
# -----------------------------------
|
| 43 |
@st.cache_resource
|
| 44 |
+
def load_model_from_weights():
|
| 45 |
+
model = create_model()
|
| 46 |
+
|
| 47 |
+
weights_path = os.path.join(os.path.dirname(__file__), "kitchen_weights.weights.h5")
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
model.build((None, IMG_SIZE, IMG_SIZE, 3))
|
| 51 |
+
|
| 52 |
+
model.load_weights(weights_path)
|
| 53 |
+
return model
|
| 54 |
except Exception as e:
|
| 55 |
+
st.error(f"Ağırlıklar yüklenemedi: {e}")
|
| 56 |
return None
|
| 57 |
+
|
| 58 |
+
model = load_model_from_weights()
|
| 59 |
|
| 60 |
# -----------------------------------
|
| 61 |
# IMAGE PREPROCESS
|
|
|
|
| 64 |
img = img.resize((IMG_SIZE, IMG_SIZE))
|
| 65 |
img = np.array(img)
|
| 66 |
|
|
|
|
| 67 |
if img.ndim == 2:
|
| 68 |
img = np.stack([img]*3, axis=-1)
|
| 69 |
elif img.shape[-1] == 4:
|
| 70 |
img = img[..., :3]
|
| 71 |
|
|
|
|
| 72 |
img = np.expand_dims(img, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
img = tf.keras.applications.efficientnet.preprocess_input(img)
|
| 74 |
return img
|
| 75 |
|
|
|
|
| 84 |
if uploaded and model is not None:
|
| 85 |
try:
|
| 86 |
img = Image.open(uploaded).convert("RGB")
|
|
|
|
| 87 |
col1, col2 = st.columns(2)
|
| 88 |
|
| 89 |
with col1:
|
|
|
|
| 91 |
|
| 92 |
with col2:
|
| 93 |
st.write("Classifying...")
|
|
|
|
| 94 |
image_tensor = process_image(img)
|
| 95 |
preds = model.predict(image_tensor)
|
| 96 |
|