Skip to content

Commit cb11b92

Browse files
authored
Added huggingface for keras siamese contrastive (keras-team#923)
* added huggingface in py * generated_notebook_and_md * removed lines 86_93
1 parent 597523d commit cb11b92

File tree

5 files changed

+60
-31
lines changed

5 files changed

+60
-31
lines changed
Loading
Loading

examples/vision/ipynb/siamese_network.ipynb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"Our goal is for the model to learn to estimate the similarity between images.\n",
3333
"\n",
3434
"For the network to learn, we use a triplet loss function. You can find an introduction to triplet loss in the\n",
35-
"[FaceNet paper](https://arxiv.org/pdf/1503.03832.pdf) by Schroff et al,. 2015. In this example, we define the triplet\n",
35+
"[FaceNet paper](https://arxiv.org/abs/1503.03832) by Schroff et al,. 2015. In this example, we define the triplet\n",
3636
"loss function as follows:\n",
3737
"\n",
3838
"`L(A, P, N) = max(\u2016f(A) - f(P)\u2016\u00b2 - \u2016f(A) - f(N)\u2016\u00b2 + margin, 0)`\n",
3939
"\n",
4040
"This example uses the [Totally Looks Like dataset](https://sites.google.com/view/totally-looks-like-dataset)\n",
41-
"by [Rosenfeld et al., 2018](https://arxiv.org/pdf/1803.01485v3.pdf)."
41+
"by [Rosenfeld et al., 2018](https://arxiv.org/abs/1803.01485)."
4242
]
4343
},
4444
{
@@ -227,10 +227,10 @@
227227
"val_dataset = dataset.skip(round(image_count * 0.8))\n",
228228
"\n",
229229
"train_dataset = train_dataset.batch(32, drop_remainder=False)\n",
230-
"train_dataset = train_dataset.prefetch(8)\n",
230+
"train_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)\n",
231231
"\n",
232232
"val_dataset = val_dataset.batch(32, drop_remainder=False)\n",
233-
"val_dataset = val_dataset.prefetch(8)\n",
233+
"val_dataset = val_dataset.prefetch(tf.data.AUTOTUNE)\n",
234234
""
235235
]
236236
},
@@ -590,14 +590,20 @@
590590
"which records every operation that you perform inside it. In this example, we use it to access the\n",
591591
"gradients passed to the optimizer to update the model weights at every step. For more details, check out the\n",
592592
"[Intro to Keras for researchers](https://keras.io/getting_started/intro_to_keras_for_researchers/)\n",
593-
"and [Writing a training loop from scratch](https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch?hl=en)."
593+
"and [Writing a training loop from scratch](https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch?hl=en).\n",
594+
"\n",
595+
"\n",
596+
"**Example available on HuggingFace**\n",
597+
"| Trained Model | Demo |\n",
598+
"| :--: | :--: |\n",
599+
"| [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Model-Siamese%20Network-black.svg)](https://huggingface.co/keras-io/siamese-contrastive) | [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Spaces-Siamese%20Network-black.svg)](https://huggingface.co/spaces/keras-io/siamese-contrastive) |"
594600
]
595601
}
596602
],
597603
"metadata": {
598604
"colab": {
599605
"collapsed_sections": [],
600-
"name": "siamesenetwork",
606+
"name": "siamese_network",
601607
"private_outputs": false,
602608
"provenance": [],
603609
"toc_visible": true

examples/vision/md/siamese_network.md

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ two of them will be similar (_anchor_ and _positive_ samples), and the third wil
2323
Our goal is for the model to learn to estimate the similarity between images.
2424

2525
For the network to learn, we use a triplet loss function. You can find an introduction to triplet loss in the
26-
[FaceNet paper](https://arxiv.org/pdf/1503.03832.pdf) by Schroff et al,. 2015. In this example, we define the triplet
26+
[FaceNet paper](https://arxiv.org/abs/1503.03832) by Schroff et al,. 2015. In this example, we define the triplet
2727
loss function as follows:
2828

2929
`L(A, P, N) = max(‖f(A) - f(P)‖² - ‖f(A) - f(N)‖² + margin, 0)`
3030

3131
This example uses the [Totally Looks Like dataset](https://sites.google.com/view/totally-looks-like-dataset)
32-
by [Rosenfeld et al., 2018](https://arxiv.org/pdf/1803.01485v3.pdf).
32+
by [Rosenfeld et al., 2018](https://arxiv.org/abs/1803.01485).
3333

3434
---
3535
## Setup
@@ -83,10 +83,10 @@ positive_images_path = cache_dir / "right"
8383

8484
<div class="k-default-codeblock">
8585
```
86-
zsh:1: command not found: gdown
87-
zsh:1: command not found: gdown
88-
unzip: cannot find or open left.zip, left.zip.zip or left.zip.ZIP.
89-
unzip: cannot find or open right.zip, right.zip.zip or right.zip.ZIP.
86+
Downloading...
87+
From: https://drive.google.com/uc?id=1EzBZUb_mh_Dp_FKD0P4XiYYSd0QBH5zW
88+
To: /mnt/hdd2/keras-io/scripts/tmp_8979114/right.zip
89+
100%|████████████████████████████████████████| 104M/104M [00:01<00:00, 66.8MB/s]
9090
9191
```
9292
</div>
@@ -171,10 +171,10 @@ train_dataset = dataset.take(round(image_count * 0.8))
171171
val_dataset = dataset.skip(round(image_count * 0.8))
172172

173173
train_dataset = train_dataset.batch(32, drop_remainder=False)
174-
train_dataset = train_dataset.prefetch(8)
174+
train_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)
175175

176176
val_dataset = val_dataset.batch(32, drop_remainder=False)
177-
val_dataset = val_dataset.prefetch(8)
177+
val_dataset = val_dataset.prefetch(tf.data.AUTOTUNE)
178178

179179
```
180180

@@ -205,7 +205,9 @@ visualize(*list(train_dataset.take(1).as_numpy_iterator())[0])
205205
```
206206

207207

208-
![png](/img/examples/vision/siamese_network/siamesenetwork_12_0.png)
208+
209+
![png](/img/examples/vision/siamese_network/siamese_network_12_0.png)
210+
209211

210212

211213
---
@@ -243,6 +245,14 @@ for layer in base_cnn.layers:
243245
layer.trainable = trainable
244246
```
245247

248+
<div class="k-default-codeblock">
249+
```
250+
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
251+
94773248/94765736 [==============================] - 2s 0us/step
252+
94781440/94765736 [==============================] - 2s 0us/step
253+
254+
```
255+
</div>
246256
---
247257
## Setting up the Siamese Network model
248258

@@ -381,27 +391,27 @@ siamese_model.fit(train_dataset, epochs=10, validation_data=val_dataset)
381391
<div class="k-default-codeblock">
382392
```
383393
Epoch 1/10
384-
151/151 [==============================] - 277s 2s/step - loss: 0.5014 - val_loss: 0.3719
394+
151/151 [==============================] - 35s 185ms/step - loss: 0.5050 - val_loss: 0.3854
385395
Epoch 2/10
386-
151/151 [==============================] - 276s 2s/step - loss: 0.3884 - val_loss: 0.3632
396+
151/151 [==============================] - 26s 170ms/step - loss: 0.3936 - val_loss: 0.3630
387397
Epoch 3/10
388-
151/151 [==============================] - 287s 2s/step - loss: 0.3711 - val_loss: 0.3509
398+
151/151 [==============================] - 26s 170ms/step - loss: 0.3722 - val_loss: 0.3382
389399
Epoch 4/10
390-
151/151 [==============================] - 295s 2s/step - loss: 0.3585 - val_loss: 0.3287
400+
151/151 [==============================] - 26s 173ms/step - loss: 0.3491 - val_loss: 0.3475
391401
Epoch 5/10
392-
151/151 [==============================] - 299s 2s/step - loss: 0.3420 - val_loss: 0.3301
402+
151/151 [==============================] - 26s 170ms/step - loss: 0.3451 - val_loss: 0.3252
393403
Epoch 6/10
394-
151/151 [==============================] - 297s 2s/step - loss: 0.3181 - val_loss: 0.3419
404+
151/151 [==============================] - 26s 171ms/step - loss: 0.3153 - val_loss: 0.3185
395405
Epoch 7/10
396-
151/151 [==============================] - 290s 2s/step - loss: 0.3131 - val_loss: 0.3201
406+
151/151 [==============================] - 26s 171ms/step - loss: 0.3118 - val_loss: 0.3255
397407
Epoch 8/10
398-
151/151 [==============================] - 295s 2s/step - loss: 0.3102 - val_loss: 0.3152
408+
151/151 [==============================] - 26s 171ms/step - loss: 0.3079 - val_loss: 0.3087
399409
Epoch 9/10
400-
151/151 [==============================] - 286s 2s/step - loss: 0.2905 - val_loss: 0.2937
410+
151/151 [==============================] - 26s 171ms/step - loss: 0.2974 - val_loss: 0.3082
401411
Epoch 10/10
402-
151/151 [==============================] - 270s 2s/step - loss: 0.2921 - val_loss: 0.2952
412+
151/151 [==============================] - 26s 172ms/step - loss: 0.2793 - val_loss: 0.3075
403413
404-
<tensorflow.python.keras.callbacks.History at 0x7fc69064bd10>
414+
<keras.callbacks.History at 0x7f04dff32e80>
405415
406416
```
407417
</div>
@@ -431,7 +441,9 @@ anchor_embedding, positive_embedding, negative_embedding = (
431441
```
432442

433443

434-
![png](/img/examples/vision/siamese_network/siamesenetwork_22_0.png)
444+
445+
![png](/img/examples/vision/siamese_network/siamese_network_22_0.png)
446+
435447

436448

437449
Finally, we can compute the cosine similarity between the anchor and positive
@@ -455,8 +467,8 @@ print("Negative similarity", negative_similarity.numpy())
455467

456468
<div class="k-default-codeblock">
457469
```
458-
Positive similarity: 0.9940324
459-
Negative similarity 0.9918252
470+
Positive similarity: 0.991258
471+
Negative similarity 0.9886112
460472
461473
```
462474
</div>
@@ -486,3 +498,9 @@ which records every operation that you perform inside it. In this example, we us
486498
gradients passed to the optimizer to update the model weights at every step. For more details, check out the
487499
[Intro to Keras for researchers](https://keras.io/getting_started/intro_to_keras_for_researchers/)
488500
and [Writing a training loop from scratch](https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch?hl=en).
501+
502+
503+
**Example available on HuggingFace**
504+
| Trained Model | Demo |
505+
| :--: | :--: |
506+
| [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Model-Siamese%20Network-black.svg)](https://huggingface.co/keras-io/siamese-contrastive) | [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Spaces-Siamese%20Network-black.svg)](https://huggingface.co/spaces/keras-io/siamese-contrastive) |

examples/vision/siamese_network.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
Our goal is for the model to learn to estimate the similarity between images.
2020
2121
For the network to learn, we use a triplet loss function. You can find an introduction to triplet loss in the
22-
[FaceNet paper](https://arxiv.org/pdf/1503.03832.pdf) by Schroff et al,. 2015. In this example, we define the triplet
22+
[FaceNet paper](https://arxiv.org/abs/1503.03832) by Schroff et al,. 2015. In this example, we define the triplet
2323
loss function as follows:
2424
2525
`L(A, P, N) = max(‖f(A) - f(P)‖² - ‖f(A) - f(N)‖² + margin, 0)`
2626
2727
This example uses the [Totally Looks Like dataset](https://sites.google.com/view/totally-looks-like-dataset)
28-
by [Rosenfeld et al., 2018](https://arxiv.org/pdf/1803.01485v3.pdf).
28+
by [Rosenfeld et al., 2018](https://arxiv.org/abs/1803.01485).
2929
"""
3030

3131
"""
@@ -412,4 +412,9 @@ def metrics(self):
412412
[Intro to Keras for researchers](https://keras.io/getting_started/intro_to_keras_for_researchers/)
413413
and [Writing a training loop from scratch](https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch?hl=en).
414414
415+
416+
**Example available on HuggingFace**
417+
| Trained Model | Demo |
418+
| :--: | :--: |
419+
| [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Model-Siamese%20Network-black.svg)](https://huggingface.co/keras-io/siamese-contrastive) | [![Generic badge](https://img.shields.io/badge/%F0%9F%A4%97%20Spaces-Siamese%20Network-black.svg)](https://huggingface.co/spaces/keras-io/siamese-contrastive) |
415420
"""

0 commit comments

Comments
 (0)