text stringlengths 0 4.99k |
|---|
margin_square = tf.math.square(tf.math.maximum(margin - (y_pred), 0)) |
return tf.math.reduce_mean( |
(1 - y_true) * square_pred + (y_true) * margin_square |
) |
return contrastive_loss |
Compile the model with the contrastive loss |
siamese.compile(loss=loss(margin=margin), optimizer=\"RMSprop\", metrics=[\"accuracy\"]) |
siamese.summary() |
Model: \"model_1\" |
__________________________________________________________________________________________________ |
Layer (type) Output Shape Param # Connected to |
================================================================================================== |
input_2 (InputLayer) [(None, 28, 28, 1)] 0 |
__________________________________________________________________________________________________ |
input_3 (InputLayer) [(None, 28, 28, 1)] 0 |
__________________________________________________________________________________________________ |
model (Functional) (None, 10) 5318 input_2[0][0] |
input_3[0][0] |
__________________________________________________________________________________________________ |
lambda (Lambda) (None, 1) 0 model[0][0] |
model[1][0] |
__________________________________________________________________________________________________ |
batch_normalization_2 (BatchNor (None, 1) 4 lambda[0][0] |
__________________________________________________________________________________________________ |
dense_1 (Dense) (None, 1) 2 batch_normalization_2[0][0] |
================================================================================================== |
Total params: 5,324 |
Trainable params: 4,808 |
Non-trainable params: 516 |
__________________________________________________________________________________________________ |
Train the model |
history = siamese.fit( |
[x_train_1, x_train_2], |
labels_train, |
validation_data=([x_val_1, x_val_2], labels_val), |
batch_size=batch_size, |
epochs=epochs, |
) |
Epoch 1/10 |
3750/3750 [==============================] - 25s 6ms/step - loss: 0.1993 - accuracy: 0.6626 - val_loss: 0.0525 - val_accuracy: 0.9331 |
Epoch 2/10 |
3750/3750 [==============================] - 23s 6ms/step - loss: 0.0611 - accuracy: 0.9187 - val_loss: 0.0277 - val_accuracy: 0.9644 |
Epoch 3/10 |
3750/3750 [==============================] - 24s 6ms/step - loss: 0.0455 - accuracy: 0.9409 - val_loss: 0.0214 - val_accuracy: 0.9719 |
Epoch 4/10 |
3750/3750 [==============================] - 27s 7ms/step - loss: 0.0386 - accuracy: 0.9506 - val_loss: 0.0198 - val_accuracy: 0.9743 |
Epoch 5/10 |
3750/3750 [==============================] - 45s 12ms/step - loss: 0.0362 - accuracy: 0.9529 - val_loss: 0.0169 - val_accuracy: 0.9783 |
Epoch 6/10 |
2497/3750 [==================>...........] - ETA: 10s - loss: 0.0343 - accuracy: 0.9552 |
Visualize results |
def plt_metric(history, metric, title, has_valid=True): |
\"\"\"Plots the given 'metric' from 'history'. |
Arguments: |
history: history attribute of History object returned from Model.fit. |
metric: Metric to plot, a string value present as key in 'history'. |
title: A string to be used as title of plot. |
has_valid: Boolean, true if valid data was passed to Model.fit else false. |
Returns: |
None. |
\"\"\" |
plt.plot(history[metric]) |
if has_valid: |
plt.plot(history[\"val_\" + metric]) |
plt.legend([\"train\", \"validation\"], loc=\"upper left\") |
plt.title(title) |
plt.ylabel(metric) |
plt.xlabel(\"epoch\") |
plt.show() |
# Plot the accuracy |
plt_metric(history=history.history, metric=\"accuracy\", title=\"Model accuracy\") |
# Plot the constrastive loss |
plt_metric(history=history.history, metric=\"loss\", title=\"Constrastive Loss\") |
png |
png |
Evaluate the model |
results = siamese.evaluate([x_test_1, x_test_2], labels_test) |
print(\"test loss, test acc:\", results) |
625/625 [==============================] - 3s 4ms/step - loss: 0.0150 - accuracy: 0.9810 |
test loss, test acc: [0.015001337975263596, 0.9810000061988831] |
Visualize the predictions |
predictions = siamese.predict([x_test_1, x_test_2]) |
visualize(pairs_test, labels_test, to_show=3, predictions=predictions, test=True) |
png |
Training a Siamese Network to compare the similarity of images using a triplet loss function. |
Introduction |
A Siamese Network is a type of network architecture that contains two or more identical subnetworks used to generate feature vectors for each input and compare them. |
Siamese Networks can be applied to different use cases, like detecting duplicates, finding anomalies, and face recognition. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.