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
Revolutionize your weighing needs with BWER, Iraq’s top provider of weighbridge systems, featuring unparalleled accuracy, durability, and expert installation services.
Blue Techker 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!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
BWER is Iraq’s go-to provider for weighbridges, ensuring durability, accuracy, and cost-efficiency in all weighing solutions, backed by exceptional customer support and maintenance services.
Strands Hint This is my first time pay a quick visit at here and i am really happy to read everthing at one place
Thinker Pedia Pretty! This has been a really wonderful post. Many thanks for providing these details.
Blue Techker Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
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
Fantastic site A lot of helpful info here Im sending it to some buddies ans additionally sharing in delicious And naturally thanks on your sweat
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!
Pink Withney I really like reading through a post that can make men and women think. Also, thank you for allowing me to comment!
Family Dollar I just like the helpful information you provide in your articles
My brother recommended I might like this web site He was totally right This post actually made my day You cannt imagine just how much time I had spent for this information Thanks
Nutra Gears Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
Bài viết này thật tuyệt vời! 👍 Tôi sẽ chia sẻ nó với bạn bè của mình.
ข้อมูลเชิงลึกของคุณเกี่ยวกับหัวข้อนี้ช่างน่าสนใจ บทความนี้เป็นความสุขที่แท้จริงในการอ่าน
Techno rozen For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.
Real Estate I like the efforts you have put in this, regards for all the great content.
Business dicker Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated
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
Hi i think that i saw you visited my web site thus i came to Return the favore I am attempting to find things to improve my web siteI suppose its ok to use some of your ideas.
My brother recommended I might like this web site He was totally right This post actually made my day You cannt imagine just how much time I had spent for this information Thanks
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
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
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
Temp mail I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
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
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
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
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
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
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
For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
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?
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
Fantastic site A lot of helpful info here Im sending it to some buddies ans additionally sharing in delicious And naturally thanks on your sweat
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
I don’t usually read blog entries, but this one forced me to take a closer look. You have such a beautiful writing style. I appreciate your wonderful post.
helloI like your writing very so much proportion we keep up a correspondence extra approximately your post on AOL I need an expert in this space to unravel my problem May be that is you Taking a look forward to see you