diff --git a/notebooks/compare_two_samples.ipynb b/notebooks/compare_two_samples.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..299ceffecdb441ea5ee819125f186cdcad2a21fc --- /dev/null +++ b/notebooks/compare_two_samples.ipynb @@ -0,0 +1,294 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Comparing 2 samples\n", + "\n", + "In this example we show how to take a baseline and compare two samples with it" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:SavedModel saved prior to TF 2.4 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named \"keras_metadata.pb\" in the SavedModel directory.\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-0._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-1._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-2._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-3._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-3._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-4._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-6._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-5._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-6._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-7._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-8._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-9._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-10._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-11._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-12._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-13._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-14._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-15._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-16._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-17._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-18._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-19._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-20._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-21._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-22._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-23._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-24._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-25._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-26._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-27._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-28._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-29._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-30._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-31._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-32._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-33._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-34._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-35._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-36._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-37._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-38._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-39._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-40._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-41._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-42._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-43._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-44._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-45._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-46._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-47._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-48._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-51._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-52._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-49._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-50._inbound_nodes\n", + "WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.\n", + "WARNING:tensorflow:No training configuration found in save file, so the model was *not* compiled. Compile it manually.\n", + "-0.40941279115199114\n" + ] + } + ], + "source": [ + "# Add the path of sample A with sample B\n", + "SAMPLE_A = \"\"\n", + "SAMPLE_B = \"\"\n", + "\n", + "from bob.pipelines import SampleSet, DelayedSample\n", + "import bob.io.base\n", + "import functools\n", + "\n", + "# Picking a baseline\n", + "from bob.bio.face.embeddings.tensorflow import inception_resnet_v2_casia_centerloss_2018\n", + "\n", + "# Setting where the eyes are for each sample (here the two samples have the same eyes position)\n", + "fixed_positions = {\"leye\":(60,123), \"reye\":(60,63)}\n", + "\n", + "pipeline = inception_resnet_v2_casia_centerloss_2018(\n", + " annotation_type=\"eyes-center\", fixed_positions=fixed_positions\n", + ")\n", + "\n", + "def create_sample(path, key):\n", + " return SampleSet(\n", + " [DelayedSample(functools.partial(bob.io.base.load, path), key=str(key))],\n", + " key=str(key),\n", + " biometric_id=str(key),)\n", + "\n", + "sample_A = create_sample(SAMPLE_A, 0)\n", + "sample_B = create_sample(SAMPLE_B, 1)\n", + "\n", + "\n", + "biometric_references = pipeline.create_biometric_reference([sample_A])\n", + "scores, _ = pipeline.compute_scores([sample_B], biometric_references)\n", + "\n", + "# Printing the score from the first sample\n", + "print(scores[0].samples[0].data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Extracting embeddings from two samples" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:SavedModel saved prior to TF 2.4 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named \"keras_metadata.pb\" in the SavedModel directory.\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-0._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-1._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-2._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-3._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-3._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-4._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-6._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-5._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-6._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-7._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-8._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-9._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-10._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-11._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-12._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-13._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-14._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-15._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-16._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-17._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-18._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-19._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-20._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-21._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-22._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-23._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-24._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-25._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-26._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-27._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-28._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-29._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-30._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-31._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-32._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-33._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-34._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-35._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-36._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-37._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-38._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-39._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-40._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-41._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-42._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-43._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-44._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-45._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-46._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-47._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-48._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-51._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-52._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-49._inbound_nodes\n", + "WARNING:tensorflow:Unresolved object in checkpoint: (root).layer_with_weights-50._inbound_nodes\n", + "WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.\n", + "WARNING:tensorflow:No training configuration found in save file, so the model was *not* compiled. Compile it manually.\n", + "WARNING:tensorflow:5 out of the last 8 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fe46e4b54d0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", + "[array([ 0.09172679, 0.0324957 , 0.03359955, -0.00483591, 0.02353592,\n", + " 0.04886897, -0.00094087, 0.06978928, 0.10372686, -0.10392421,\n", + " 0.13330463, 0.07076985, 0.08029327, -0.04671128, -0.02518994,\n", + " 0.12051351, 0.06940517, -0.038332 , -0.02331832, 0.02664019,\n", + " 0.04434336, -0.02471577, -0.0102796 , -0.085636 , 0.09365616,\n", + " 0.01478034, -0.01528178, 0.03954509, 0.02043953, 0.0704874 ,\n", + " -0.03089085, 0.0312874 , 0.10803461, -0.05972857, -0.16184652,\n", + " -0.03643044, 0.09576611, -0.08911359, -0.13417085, -0.03623924,\n", + " 0.16680372, 0.06437656, -0.12195335, 0.15070474, -0.00109789,\n", + " -0.01936167, 0.12604736, -0.02172692, -0.16884778, -0.03880082,\n", + " 0.03399109, -0.05475918, 0.0058937 , -0.24445663, -0.0814684 ,\n", + " 0.06544494, 0.09598684, 0.18717638, 0.02309373, -0.1937956 ,\n", + " -0.11580025, -0.03947522, 0.02225816, -0.17796317, 0.0286997 ,\n", + " 0.05637549, 0.13862395, 0.07888459, -0.0746187 , -0.06245924,\n", + " -0.05905129, -0.06469788, -0.13217713, -0.00836692, -0.03132226,\n", + " 0.02515431, 0.10684554, 0.03239268, -0.08999073, 0.09488151,\n", + " 0.07205841, -0.05697955, -0.01520361, -0.10826666, -0.11681797,\n", + " -0.06068925, 0.08056987, 0.11805228, -0.01560696, -0.05713017,\n", + " 0.00941482, -0.08339076, 0.10243417, 0.1385035 , -0.08926324,\n", + " -0.08198499, 0.09009691, 0.01847403, -0.04287886, 0.01292743,\n", + " -0.02277992, -0.14719044, 0.10625618, 0.00111244, 0.19167267,\n", + " -0.01272508, 0.08188605, -0.12089421, -0.05653883, -0.05532645,\n", + " -0.05844725, -0.11874794, 0.12547068, -0.06742927, -0.07416619,\n", + " -0.01912123, 0.04917778, -0.10811004, 0.04180698, 0.18301588,\n", + " -0.00808303, 0.00328449, -0.12431399, -0.0101751 , -0.10478832,\n", + " 0.12978972, 0.09860662, 0.12227818], dtype=float32)]\n" + ] + } + ], + "source": [ + "# Add the path of sample A with sample B\n", + "SAMPLE_A = \"\"\n", + "SAMPLE_B = \"\"\n", + "\n", + "from bob.pipelines import SampleSet, DelayedSample\n", + "import bob.io.base\n", + "import functools\n", + "\n", + "# Picking a baseline\n", + "from bob.bio.face.embeddings.tensorflow import inception_resnet_v2_casia_centerloss_2018\n", + "\n", + "# Setting where the eyes are for each sample (here the two samples have the same eyes position)\n", + "fixed_positions = {\"leye\":(60,123), \"reye\":(60,63)}\n", + "\n", + "pipeline = inception_resnet_v2_casia_centerloss_2018(\n", + " annotation_type=\"eyes-center\", fixed_positions=fixed_positions\n", + ")\n", + "transformer = pipeline.transformer\n", + "\n", + "def create_sample(path, key):\n", + " return SampleSet(\n", + " [DelayedSample(functools.partial(bob.io.base.load, path), key=str(key))],\n", + " key=str(key),\n", + " biometric_id=str(key),)\n", + "\n", + "sample_A = create_sample(SAMPLE_A, 0)\n", + "sample_B = create_sample(SAMPLE_B, 1)\n", + "\n", + "\n", + "transformed_samples = transformer.transform([sample_A, sample_B])\n", + "\n", + "print([x.data for x in transformed_samples[0].samples])\n", + "\n", + "\n", + "#biometric_references = pipeline.create_biometric_reference([sample_A])\n", + "#scores, _ = pipeline.compute_scores([sample_B], biometric_references)\n", + "\n", + "# Printing the score from the first sample\n", + "#print(scores[0].samples[0].data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}