Spaces:
Sleeping
Sleeping
File size: 2,034 Bytes
cb47230 74e93e1 cb47230 3727736 cb47230 833e301 cb47230 833e301 cb47230 833e301 cb47230 833e301 cb47230 dc4575f |
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 |
# ----------------------------------------------------------
# Enhanced Embedding Network
# ----------------------------------------------------------
import keras
import tensorflow as tf
from keras_facenet import FaceNet
# from app import eucledian_distance
def euclidean_distance(vectors):
x, y = vectors
return tf.sqrt(tf.reduce_sum(tf.square(x - y), axis=1, keepdims=True))
def build_embedding_network(input_shape):
# Load the FaceNet model
facenet = FaceNet()
base_model = facenet.model
# Freeze all layers except the last 10 for fine-tuning
for layer in base_model.layers[:-10]:
layer.trainable = False
# Define input and pass it through the base model
inputs = keras.Input(input_shape)
x = base_model(inputs) # Pass input through FaceNet
# Final Embedding Layer
x = keras.layers.Dense(256, activation=None)(x) # No activation before L2 norm
x = keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))(x) # L2-normalized embeddings
return keras.Model(inputs, x)
# ----------------------------------------------------------
# Siamese Network
# ----------------------------------------------------------
def build_siamese():
input_shape = (256, 256, 3) # FaceNet expects 160x160 input
embedding_network = build_embedding_network(input_shape)
# Define inputs for the Siamese network
input_1 = keras.layers.Input(input_shape)
input_2 = keras.layers.Input(input_shape)
# Pass inputs through the embedding network
tower_1 = embedding_network(input_1)
tower_2 = embedding_network(input_2)
# Compute Euclidean distance between embeddings
distance = keras.layers.Lambda(euclidean_distance, name='distance_layer')([tower_1, tower_2])
# Custom Contrastive Output Layer
output = keras.layers.Dense(1, activation='sigmoid',
kernel_regularizer=keras.regularizers.l2(1e-4))(distance)
# Build the Siamese model
siamese = keras.Model(inputs=[input_1, input_2], outputs=output)
return siamese |