Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,41 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from huggingface_hub import from_pretrained_keras
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def predict_image(img1, img2):
|
| 8 |
assert img1.shape == (28, 28)
|
|
@@ -14,4 +48,4 @@ def predict_image(img1, img2):
|
|
| 14 |
return lab
|
| 15 |
|
| 16 |
iface = gr.Interface(predict_image, inputs=["sketchpad", "sketchpad"], outputs="label")
|
| 17 |
-
iface.launch(debug='True')
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from huggingface_hub import from_pretrained_keras
|
| 4 |
|
| 5 |
+
|
| 6 |
+
def loss(margin=1):
|
| 7 |
+
"""Provides 'constrastive_loss' an enclosing scope with variable 'margin'.
|
| 8 |
+
|
| 9 |
+
Arguments:
|
| 10 |
+
margin: Integer, defines the baseline for distance for which pairs
|
| 11 |
+
should be classified as dissimilar. - (default is 1).
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
'constrastive_loss' function with data ('margin') attached.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# Contrastive loss = mean( (1-true_value) * square(prediction) +
|
| 18 |
+
# true_value * square( max(margin-prediction, 0) ))
|
| 19 |
+
def contrastive_loss(y_true, y_pred):
|
| 20 |
+
"""Calculates the constrastive loss.
|
| 21 |
+
|
| 22 |
+
Arguments:
|
| 23 |
+
y_true: List of labels, each label is of type float32.
|
| 24 |
+
y_pred: List of predictions of same length as of y_true,
|
| 25 |
+
each label is of type float32.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
A tensor containing constrastive loss as floating point value.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
square_pred = tf.math.square(y_pred)
|
| 32 |
+
margin_square = tf.math.square(tf.math.maximum(margin - (y_pred), 0))
|
| 33 |
+
return tf.math.reduce_mean(
|
| 34 |
+
(1 - y_true) * square_pred + (y_true) * margin_square
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
return contrastive_loss
|
| 38 |
+
|
| 39 |
+
siamese = from_pretrained_keras("rushic24/keras-siamese-contrastive", custom_objects={"contrastive_loss": loss})
|
| 40 |
|
| 41 |
def predict_image(img1, img2):
|
| 42 |
assert img1.shape == (28, 28)
|
|
|
|
| 48 |
return lab
|
| 49 |
|
| 50 |
iface = gr.Interface(predict_image, inputs=["sketchpad", "sketchpad"], outputs="label")
|
| 51 |
+
iface.launch(debug='True')
|