Extract_ArcFace_from_MOBIO.ipynb 11.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{
 "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",
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
227
228
229
230
231
      "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"
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
     ]
    }
   ],
   "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
}