From c6853d5d9d73404bf1b32d076ca0bec210982ec6 Mon Sep 17 00:00:00 2001 From: Tiago Freitas Pereira <tiagofrepereira@gmail.com> Date: Thu, 3 Jun 2021 13:35:40 +0200 Subject: [PATCH] Adapted Hatef example --- notebooks/Extract_ArcFace_from_MOBIO.ipynb | 265 +++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 notebooks/Extract_ArcFace_from_MOBIO.ipynb diff --git a/notebooks/Extract_ArcFace_from_MOBIO.ipynb b/notebooks/Extract_ArcFace_from_MOBIO.ipynb new file mode 100644 index 00000000..417e9dd5 --- /dev/null +++ b/notebooks/Extract_ArcFace_from_MOBIO.ipynb @@ -0,0 +1,265 @@ +{ + "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=<function save at 0x7f345a1224d0>))),\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=<function load at 0x7f345a122320>,\n", + " save_func=<function save at 0x7f345a1224d0>)))])\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(<function TCPServer._handle_connection.<locals>.<lambda> at 0x7f3470e7c8c0>, <Task finished coro=<BaseTCPListener._handle_stream() done, defined at /idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py:445> 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 <lambda>\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(<function TCPServer._handle_connection.<locals>.<lambda> at 0x7f3470e7ce60>, <Task finished coro=<BaseTCPListener._handle_stream() done, defined at /idiap/user/tpereira/conda/envs/bob.nightlies/lib/python3.7/site-packages/distributed/comm/tcp.py:445> 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 <lambda>\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" + ] + } + ], + "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 +} -- GitLab