Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,38 @@ import os
|
|
| 7 |
# Suppress oneDNN warnings (optional)
|
| 8 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
-
siamese = keras.models.load_model(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
print(f"Error loading model: {e}")
|
| 15 |
raise
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Load stored images
|
| 18 |
def load_stored_images():
|
| 19 |
try:
|
|
|
|
| 7 |
# Suppress oneDNN warnings (optional)
|
| 8 |
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 9 |
|
| 10 |
+
# Define custom functions
|
| 11 |
+
def euclidean_distance(vectors):
|
| 12 |
+
x, y = vectors
|
| 13 |
+
sum_square = tf.reduce_sum(tf.square(x - y), axis=1, keepdims=True)
|
| 14 |
+
return tf.sqrt(tf.maximum(sum_square, tf.keras.backend.epsilon()))
|
| 15 |
+
|
| 16 |
+
def contrastive_loss(y_true, y_pred, margin=1.0):
|
| 17 |
+
square_pred = tf.square(y_pred)
|
| 18 |
+
margin_square = tf.square(tf.maximum(margin - y_pred, 0))
|
| 19 |
+
return tf.reduce_mean(y_true * square_pred + (1 - y_true) * margin_square)
|
| 20 |
+
|
| 21 |
+
# Load model with custom objects
|
| 22 |
try:
|
| 23 |
+
siamese = keras.models.load_model(
|
| 24 |
+
"my_siamese.keras",
|
| 25 |
+
custom_objects={
|
| 26 |
+
"euclidean_distance": euclidean_distance,
|
| 27 |
+
"contrastive_loss": contrastive_loss
|
| 28 |
+
},
|
| 29 |
+
compile=False
|
| 30 |
+
)
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Error loading model: {e}")
|
| 33 |
raise
|
| 34 |
|
| 35 |
+
# Load the model
|
| 36 |
+
# try:
|
| 37 |
+
# siamese = keras.models.load_model("my_siamese.keras", compile=False)
|
| 38 |
+
# except Exception as e:
|
| 39 |
+
# print(f"Error loading model: {e}")
|
| 40 |
+
# raise
|
| 41 |
+
|
| 42 |
# Load stored images
|
| 43 |
def load_stored_images():
|
| 44 |
try:
|