@@ -23,13 +23,13 @@ two of them will be similar (_anchor_ and _positive_ samples), and the third wil
23
23
Our goal is for the model to learn to estimate the similarity between images.
24
24
25
25
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
27
27
loss function as follows:
28
28
29
29
` L(A, P, N) = max(‖f(A) - f(P)‖² - ‖f(A) - f(N)‖² + margin, 0) `
30
30
31
31
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 ) .
33
33
34
34
---
35
35
## Setup
@@ -83,10 +83,10 @@ positive_images_path = cache_dir / "right"
83
83
84
84
<div class =" k-default-codeblock " >
85
85
```
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]
90
90
91
91
```
92
92
</div >
@@ -171,10 +171,10 @@ train_dataset = dataset.take(round(image_count * 0.8))
171
171
val_dataset = dataset.skip(round (image_count * 0.8 ))
172
172
173
173
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 )
175
175
176
176
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 )
178
178
179
179
```
180
180
@@ -205,7 +205,9 @@ visualize(*list(train_dataset.take(1).as_numpy_iterator())[0])
205
205
```
206
206
207
207
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
+
209
211
210
212
211
213
---
@@ -243,6 +245,14 @@ for layer in base_cnn.layers:
243
245
layer.trainable = trainable
244
246
```
245
247
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 >
246
256
---
247
257
## Setting up the Siamese Network model
248
258
@@ -381,27 +391,27 @@ siamese_model.fit(train_dataset, epochs=10, validation_data=val_dataset)
381
391
<div class =" k-default-codeblock " >
382
392
```
383
393
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
385
395
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
387
397
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
389
399
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
391
401
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
393
403
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
395
405
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
397
407
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
399
409
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
401
411
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
403
413
404
- <tensorflow.python. keras.callbacks.History at 0x7fc69064bd10 >
414
+ <keras.callbacks.History at 0x7f04dff32e80 >
405
415
406
416
```
407
417
</div >
@@ -431,7 +441,9 @@ anchor_embedding, positive_embedding, negative_embedding = (
431
441
```
432
442
433
443
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
+
435
447
436
448
437
449
Finally, we can compute the cosine similarity between the anchor and positive
@@ -455,8 +467,8 @@ print("Negative similarity", negative_similarity.numpy())
455
467
456
468
<div class =" k-default-codeblock " >
457
469
```
458
- Positive similarity: 0.9940324
459
- Negative similarity 0.9918252
470
+ Positive similarity: 0.991258
471
+ Negative similarity 0.9886112
460
472
461
473
```
462
474
</div >
@@ -486,3 +498,9 @@ which records every operation that you perform inside it. In this example, we us
486
498
gradients passed to the optimizer to update the model weights at every step. For more details, check out the
487
499
[ Intro to Keras for researchers] ( https://keras.io/getting_started/intro_to_keras_for_researchers/ )
488
500
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 ) |
0 commit comments