{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extracting embedding features from face data\n", "In this notebook, we aim to extract embedding features from images using face recogntion extractors.\n", "As an example, we use MOBIO dataset, and extract Arcface features from the face images:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "##### CHANGE YOUR DATABASE HERE\n", "from bob.bio.face.config.database.mobio_male import database\n", "annotation_type = database.annotation_type\n", "fixed_positions = database.fixed_positions\n", "memory_demanding = True\n", "dask_client = None" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from bob.bio.face.embeddings.mxnet import arcface_insightFace_lresnet100\n", "pipeline = arcface_insightFace_lresnet100(annotation_type=annotation_type,\n", " fixed_positions=fixed_positions,\n", " memory_demanding=memory_demanding)\n", "transformer = pipeline.transformer" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pipeline(steps=[('ToDaskBag', ToDaskBag()),\n", " ('samplewrapper-1',\n", " DaskWrapper(estimator=CheckpointWrapper(estimator=SampleWrapper(estimator=FaceCrop(annotator=BobIpMTCNN(),\n", " cropped_image_size=(112,\n", " 112),\n", " cropped_positions={'leye': (55,\n", " 81),\n", " 'reye': (55,\n", " 42)}),\n", " fit_extra_arguments=(),\n", " transform_extra_arguments=(('annotations',\n", " 'annotations'),)),\n", " features_dir='featur...\n", " save_func=))),\n", " ('samplewrapper-2',\n", " DaskWrapper(estimator=CheckpointWrapper(estimator=SampleWrapper(estimator=ArcFaceInsightFace_LResNet100(memory_demanding=True),\n", " fit_extra_arguments=(),\n", " transform_extra_arguments=()),\n", " features_dir='features/samplewrapper-2',\n", " load_func=,\n", " save_func=)))])\n" ] } ], "source": [ "from bob.pipelines import wrap\n", "\n", "\n", "features_dir = \"features\" #Path to store extracted features\n", "# Wrapping with CHECKPOINT and DASK\n", "transformer = wrap([\"checkpoint\",\"dask\"],\n", " transformer,\n", " features_dir=features_dir)\n", "\n", "# Printing the setup of the transformer\n", "print(transformer)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an example, we consider 10 samples from this database and extract features for these samples:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# get 10 samples from database\n", "samples = database.all_samples()[:10]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Setting the DASK client\n", "# HERE MAKE ABSOLUTELLY SURE THAT YOU DO `SETSHELL grid` \n", "# BEFORE STARTING THE NOTEBOOK\n", "\n", "from dask.distributed import Client\n", "from bob.pipelines.distributed.sge import SGEMultipleQueuesCluster\n", "\n", "cluster = SGEMultipleQueuesCluster(min_jobs=1)\n", "dask_client = Client(cluster)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "\n", "features = transformer.transform(samples)\n", "if dask_client is not None:\n", " features = features.compute(scheduler=dask_client)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following cells, we convert the extracted features to `numpy.array` and check the size of features." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from bob.pipelines import SampleBatch\n", "\n", "np_features = np.array(SampleBatch(features))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0.5345935 , -1.0668839 , -0.62798595, ..., -0.78859204,\n", " -0.5147211 , 2.1415784 ]],\n", "\n", " [[ 0.24587776, -1.1436105 , -0.21513344, ..., -0.4950465 ,\n", " -0.7586405 , 1.9262394 ]],\n", "\n", " [[-0.01235329, -1.0903177 , -0.7307515 , ..., -1.5341333 ,\n", " -0.9396954 , 1.8103021 ]],\n", "\n", " ...,\n", "\n", " [[ 0.46007535, -0.9715014 , -0.52703196, ..., -0.29170716,\n", " -0.74297565, 1.8094344 ]],\n", "\n", " [[ 0.6113469 , -1.1828535 , -0.19491309, ..., -0.22889124,\n", " -0.58382076, 2.185493 ]],\n", "\n", " [[ 0.71980965, -0.4669612 , -0.49327967, ..., 0.0910981 ,\n", " -0.65268064, 0.93472594]]], dtype=float32)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_features" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "tornado.application - ERROR - Exception in callback functools.partial(. at 0x7f3470e7c8c0>, exception=ValueError('invalid operation on non-started TCPListener')>)\n", "Traceback (most recent call last):\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/tornado/ioloop.py\", line 743, in _run_callback\n", " ret = callback()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/tornado/tcpserver.py\", line 327, in \n", " gen.convert_yielded(future), lambda f: f.result()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 451, in _handle_stream\n", " logger.debug(\"Incoming connection from %r to %r\", address, self.contact_address)\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 486, in contact_address\n", " host, port = self.get_host_port()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 467, in get_host_port\n", " self._check_started()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 443, in _check_started\n", " raise ValueError(\"invalid operation on non-started TCPListener\")\n", "ValueError: invalid operation on non-started TCPListener\n", "tornado.application - ERROR - Exception in callback functools.partial(. at 0x7f3470e7ce60>, exception=ValueError('invalid operation on non-started TCPListener')>)\n", "Traceback (most recent call last):\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/tornado/ioloop.py\", line 743, in _run_callback\n", " ret = callback()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/tornado/tcpserver.py\", line 327, in \n", " gen.convert_yielded(future), lambda f: f.result()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 451, in _handle_stream\n", " logger.debug(\"Incoming connection from %r to %r\", address, self.contact_address)\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 486, in contact_address\n", " host, port = self.get_host_port()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 467, in get_host_port\n", " self._check_started()\n", " File \"/idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py\", line 443, in _check_started\n", " raise ValueError(\"invalid operation on non-started TCPListener\")\n", "ValueError: invalid operation on non-started TCPListener\n", "distributed.client - ERROR - Failed to reconnect to scheduler after 10.00 seconds, closing client\n", "_GatheringFuture exception was never retrieved\n", "future: <_GatheringFuture finished exception=CancelledError()>\n", "concurrent.futures._base.CancelledError\n" ] } ], "source": [ "# KILL THE SGE WORKERS\n", "dask_client.shutdown()" ] }, { "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": 5 }