TensorFlow / Python: Comparación de Imágenes con Redes Neuronales Siamesas

A continuación, trabajaremos en la implementación de una Red Neuronal Siamesa en TensorFlow. Una red neuronal siamesa se utiliza para calcular la similitud o distancia entre dos entradas. En este ejemplo, crearemos una red siamesa para comparar imágenes y determinar si pertenecen a la misma clase o no. Usaremos TensorFlow para construir esta red.

import tensorflow as tf
from tensorflow.keras.layers import Input, Flatten, Dense, Lambda
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
import numpy as np

# Cargar y preparar el conjunto de datos MNIST
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# Función para crear una capa de distancia euclidiana
def euclidean_distance(vectors):
    vector1, vector2 = vectors
    sum_square = tf.reduce_sum(tf.square(vector1 - vector2), axis=1, keepdims=True)
    return tf.sqrt(sum_square)

# Crear dos entradas para las imágenes
input_shape = (28, 28)
input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)

# Definir una base convolucional compartida (siamesa)
shared_base = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=input_shape),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(32, activation='relu')
])

# Obtener la salida de la base compartida para ambas entradas
output_a = shared_base(input_a)
output_b = shared_base(input_b)

# Usar la capa de distancia para calcular la similitud
distance = Lambda(euclidean_distance)([output_a, output_b])

# Crear el modelo Siamese
siamese_model = Model(inputs=[input_a, input_b], outputs=distance)

# Compilar el modelo
siamese_model.compile(loss='mean_squared_error', optimizer='adam')

# Crear pares de entrenamiento para la verificación de escritura a mano
def create_pairs(x, digit_indices):
    pairs = []
    labels = []
    n = min([len(digit_indices[d]) for d in range(10)]) - 1
    for d in range(10):
        for i in range(n):
            z1, z2 = digit_indices[d][i], digit_indices[d][i + 1]
            pairs += [[x[z1], x[z2]]]
            inc = np.random.randint(1, 10)
            dn = (d + inc) % 10
            z1, z2 = digit_indices[d][i], digit_indices[dn][i]
            pairs += [[x[z1], x[z2]]]
            labels += [1, 0]
    return np.array(pairs), np.array(labels)

digit_indices = [np.where(y_train == i)[0] for i in range(10)]
tr_pairs, tr_y = create_pairs(x_train, digit_indices)

# Entrenar la red siamesa
siamese_model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y, batch_size=128, epochs=20)

# Para hacer predicciones, proporciona dos imágenes y obtén la distancia como resultado
image1 = x_test[0]
image2 = x_test[1]
distance = siamese_model.predict([image1[np.newaxis], image2[np.newaxis]])
print("Distancia entre las imágenes:", distance)
import matplotlib.pyplot as plt

# Supongamos que tienes dos índices que deseas comparar
index1 = 4  # Índice de la primera imagen
index2 = 6  # Índice de la segunda imagen

# Recupera las imágenes
image1 = x_test[index1]
image2 = x_test[index2]

# Calcula la distancia entre las imágenes
distance = siamese_model.predict([image1[np.newaxis], image2[np.newaxis]])

# Muestra las imágenes junto con la distancia
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.imshow(image1, cmap='gray')
plt.title('Imagen 1')

plt.subplot(1, 2, 2)
plt.imshow(image2, cmap='gray')
plt.title('Imagen 2')

plt.suptitle(f'Distancia: {distance[0][0]:.4f}')
plt.show()

La línea print("Distancia entre las imágenes:", distance) muestra la distancia calculada entre dos imágenes por el modelo Siamese. En este contexto, «distancia» se refiere a la diferencia o similitud entre las características extraídas de las dos imágenes.

La idea detrás de una red neuronal Siamesa es aprender una representación de las imágenes de tal manera que dos imágenes similares tengan una distancia pequeña entre sus representaciones, mientras que dos imágenes diferentes tengan una distancia grande.

Cuando ejecutas siamese_model.predict([image1[np.newaxis], image2[np.newaxis]), el modelo calcula la distancia entre las representaciones de image1 y image2. Esta distancia puede considerarse como una medida de cuán similares o diferentes son las dos imágenes. Cuanto menor sea la distancia, más similares serán las imágenes, y cuanto mayor sea la distancia, más diferentes serán

35 thoughts on “TensorFlow / Python: Comparación de Imágenes con Redes Neuronales Siamesas

  1. I have been surfing online more than 3 hours today yet I never found any interesting article like yours It is pretty worth enough for me In my opinion if all web owners and bloggers made good content as you did the web will be much more useful than ever before

  2. dodb buzz You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

  3. I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts

  4. obviously like your website but you need to test the spelling on quite a few of your posts Several of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand Ill certainly come back again

  5. I simply could not go away your web site prior to suggesting that I really enjoyed the standard info a person supply on your guests Is going to be back incessantly to investigate crosscheck new posts

  6. I am not sure where youre getting your info but good topic I needs to spend some time learning much more or understanding more Thanks for magnificent info I was looking for this information for my mission

  7. of course like your website but you have to check the spelling on several of your posts A number of them are rife with spelling issues and I in finding it very troublesome to inform the reality on the other hand I will certainly come back again

  8. you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

  9. I do not even know how I ended up here but I thought this post was great I do not know who you are but certainly youre going to a famous blogger if you are not already Cheers

  10. I loved as much as youll receive carried out right here The sketch is tasteful your authored material stylish nonetheless you command get bought an nervousness over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

  11. What i dont understood is in reality how youre now not really a lot more smartlyfavored than you might be now Youre very intelligent You understand therefore significantly in terms of this topic produced me personally believe it from a lot of numerous angles Its like women and men are not interested except it is one thing to accomplish with Woman gaga Your own stuffs outstanding Always care for it up

  12. you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

  13. I’m grateful. I have been looking for information on this subject for a time, and this is the best resource I have discovered thus far. What about the bottom line, though? Do you know for sure what the supply is?

  14. Its like you read my mind You appear to know a lot about this like you wrote the book in it or something I think that you could do with some pics to drive the message home a little bit but instead of that this is fantastic blog An excellent read I will certainly be back

Deja un comentario