Spaces:
Sleeping
Sleeping
Commit
·
96e506d
1
Parent(s):
f5ede97
Fix Keras model loading compatibility (compile=False)
Browse files- app.py +22 -5
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -13,8 +13,10 @@ from PIL import Image
|
|
| 13 |
|
| 14 |
# Import TensorFlow
|
| 15 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
|
|
|
|
|
| 16 |
import tensorflow as tf
|
| 17 |
-
from tensorflow
|
| 18 |
from tensorflow.keras.applications.inception_v3 import preprocess_input
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
|
@@ -32,10 +34,20 @@ def load_models():
|
|
| 32 |
model_dir = "models"
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
# Load Keras model
|
| 36 |
model_path = os.path.join(model_dir, "batik_model.keras")
|
| 37 |
-
model = load_model(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
print(f" Loaded Keras model from {model_path}")
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Load class names
|
| 41 |
classes_path = os.path.join(model_dir, "batik_classes.json")
|
|
@@ -74,9 +86,11 @@ def index():
|
|
| 74 |
"""API info endpoint"""
|
| 75 |
return jsonify({
|
| 76 |
"name": "Batik Classifier API",
|
| 77 |
-
"model": "Fine-tuned InceptionV3",
|
| 78 |
"classes": len(class_names) if class_names else 0,
|
| 79 |
"accuracy": config.get('val_accuracy', 0) if config else 0,
|
|
|
|
|
|
|
| 80 |
"endpoints": {
|
| 81 |
"/": "API info",
|
| 82 |
"/predict": "POST - Classify batik image",
|
|
@@ -161,7 +175,10 @@ def predict():
|
|
| 161 |
}), 500
|
| 162 |
|
| 163 |
# Load models on startup
|
| 164 |
-
print("
|
|
|
|
|
|
|
|
|
|
| 165 |
if load_models():
|
| 166 |
print(" All models loaded successfully!")
|
| 167 |
else:
|
|
|
|
| 13 |
|
| 14 |
# Import TensorFlow
|
| 15 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
| 16 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 17 |
+
|
| 18 |
import tensorflow as tf
|
| 19 |
+
from tensorflow import keras
|
| 20 |
from tensorflow.keras.applications.inception_v3 import preprocess_input
|
| 21 |
|
| 22 |
app = Flask(__name__)
|
|
|
|
| 34 |
model_dir = "models"
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Load Keras model with compile=False to avoid custom layer issues
|
| 38 |
model_path = os.path.join(model_dir, "batik_model.keras")
|
| 39 |
+
model = keras.models.load_model(model_path, compile=False)
|
| 40 |
+
|
| 41 |
+
# Compile manually with simple config
|
| 42 |
+
model.compile(
|
| 43 |
+
optimizer='adam',
|
| 44 |
+
loss='categorical_crossentropy',
|
| 45 |
+
metrics=['accuracy']
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
print(f" Loaded Keras model from {model_path}")
|
| 49 |
+
print(f" Model input shape: {model.input_shape}")
|
| 50 |
+
print(f" Model output shape: {model.output_shape}")
|
| 51 |
|
| 52 |
# Load class names
|
| 53 |
classes_path = os.path.join(model_dir, "batik_classes.json")
|
|
|
|
| 86 |
"""API info endpoint"""
|
| 87 |
return jsonify({
|
| 88 |
"name": "Batik Classifier API",
|
| 89 |
+
"model": "Fine-tuned InceptionV3 (Keras)",
|
| 90 |
"classes": len(class_names) if class_names else 0,
|
| 91 |
"accuracy": config.get('val_accuracy', 0) if config else 0,
|
| 92 |
+
"train_accuracy": config.get('train_accuracy', 0) if config else 0,
|
| 93 |
+
"epochs": config.get('epochs', 0) if config else 0,
|
| 94 |
"endpoints": {
|
| 95 |
"/": "API info",
|
| 96 |
"/predict": "POST - Classify batik image",
|
|
|
|
| 175 |
}), 500
|
| 176 |
|
| 177 |
# Load models on startup
|
| 178 |
+
print("=" * 60)
|
| 179 |
+
print("Loading Batik Classifier Models...")
|
| 180 |
+
print("=" * 60)
|
| 181 |
+
|
| 182 |
if load_models():
|
| 183 |
print(" All models loaded successfully!")
|
| 184 |
else:
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
Flask==3.0.3
|
| 2 |
flask-cors==5.0.0
|
| 3 |
-
tensorflow==2.
|
| 4 |
scikit-learn==1.4.2
|
| 5 |
numpy>=1.23.5,<2.0.0
|
| 6 |
pillow==10.2.0
|
|
|
|
| 1 |
Flask==3.0.3
|
| 2 |
flask-cors==5.0.0
|
| 3 |
+
tensorflow==2.18.0
|
| 4 |
scikit-learn==1.4.2
|
| 5 |
numpy>=1.23.5,<2.0.0
|
| 6 |
pillow==10.2.0
|