tinyface.py 8.31 KB
Newer Older
Yu LINGHU's avatar
Yu LINGHU committed
1
2
3
4
5
6
7
8
9
from bob.ip.color import gray_to_rgb
import logging
import numpy as np
import pickle
import os, sys
from collections import namedtuple
import time
from bob.io.image import to_matplotlib
import pkg_resources
Xinyi ZHANG's avatar
Xinyi ZHANG committed
10
from bob.extension import rc
11
from bob.extension.download import get_file
Yu LINGHU's avatar
Yu LINGHU committed
12
13
14

logger = logging.getLogger(__name__)

15

Yu LINGHU's avatar
Yu LINGHU committed
16
17
18
19
20
21
22
23
24
25
26
27
class TinyFacesDetector:

    """TinyFace face detector. Original Model is ``ResNet101`` from 
    https://github.com/peiyunh/tiny. Please check for details. The 
    model used in this section is the MxNet version from 
    https://github.com/chinakook/hr101_mxnet.

    Attributes
    ----------
    prob_thresh: float
        Thresholds are a trade-off between false positives and missed detections.
    """
28

Yu LINGHU's avatar
Yu LINGHU committed
29
30
31
32
    def __init__(self, prob_thresh=0.5, **kwargs):
        super().__init__(**kwargs)

        import mxnet as mx
Xinyi ZHANG's avatar
Xinyi ZHANG committed
33

34
35
36
        urls = [
            "https://www.idiap.ch/software/bob/data/bob/bob.ip.facedetect/master/tinyface_detector.tar.gz"
        ]
Xinyi ZHANG's avatar
Xinyi ZHANG committed
37

38
39
40
41
42
43
        filename = get_file(
            "tinyface_detector.tar.gz",
            urls,
            cache_subdir="data/tinyface_detector",
            file_hash="f24e820b47a7440d7cdd7e0c43d4d455",
            extract=True,
Xinyi ZHANG's avatar
Xinyi ZHANG committed
44
45
        )

46
        self.checkpoint_path = os.path.dirname(filename)
Xinyi ZHANG's avatar
Xinyi ZHANG committed
47

48
        self.MAX_INPUT_DIM = 5000.0
Yu LINGHU's avatar
Yu LINGHU committed
49
50
        self.prob_thresh = prob_thresh
        self.nms_thresh = 0.1
51
52
53
        self.model_root = pkg_resources.resource_filename(
            __name__, self.checkpoint_path
        )
Yu LINGHU's avatar
Yu LINGHU committed
54

55
56
57
        sym, arg_params, aux_params = mx.model.load_checkpoint(
            os.path.join(self.checkpoint_path, "hr101"), 0
        )
Yu LINGHU's avatar
Yu LINGHU committed
58
59
        all_layers = sym.get_internals()

60
        meta_file = open(os.path.join(self.checkpoint_path, "meta.pkl"), "rb")
Yu LINGHU's avatar
Yu LINGHU committed
61
62
63
        self.clusters = pickle.load(meta_file)
        self.averageImage = pickle.load(meta_file)
        meta_file.close()
64
65
66
        self.clusters_h = self.clusters[:, 3] - self.clusters[:, 1] + 1
        self.clusters_w = self.clusters[:, 2] - self.clusters[:, 0] + 1
        self.normal_idx = np.where(self.clusters[:, 4] == 1)
Yu LINGHU's avatar
Yu LINGHU committed
67

68
69
70
71
72
73
74
75
76
77
78
79
        self.mod = mx.mod.Module(
            symbol=all_layers["fusex_output"], data_names=["data"], label_names=None
        )
        self.mod.bind(
            for_training=False,
            data_shapes=[("data", (1, 3, 224, 224))],
            label_shapes=None,
            force_rebind=False,
        )
        self.mod.set_params(
            arg_params=arg_params, aux_params=aux_params, force_init=False
        )
Yu LINGHU's avatar
Yu LINGHU committed
80
81
82

    @staticmethod
    def _nms(dets, prob_thresh):
83

Yu LINGHU's avatar
Yu LINGHU committed
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
        x1 = dets[:, 0]
        y1 = dets[:, 1]
        x2 = dets[:, 2]
        y2 = dets[:, 3]
        scores = dets[:, 4]

        areas = (x2 - x1 + 1) * (y2 - y1 + 1)

        order = scores.argsort()[::-1]

        keep = []
        while order.size > 0:
            i = order[0]
            keep.append(i)
            xx1 = np.maximum(x1[i], x1[order[1:]])
            yy1 = np.maximum(y1[i], y1[order[1:]])
            xx2 = np.minimum(x2[i], x2[order[1:]])
            yy2 = np.minimum(y2[i], y2[order[1:]])
            w = np.maximum(0.0, xx2 - xx1 + 1)
            h = np.maximum(0.0, yy2 - yy1 + 1)
            inter = w * h

            ovr = inter / (areas[i] + areas[order[1:]] - inter)
            inds = np.where(ovr <= prob_thresh)[0]

            order = order[inds + 1]
        return keep

    def detect(self, img):
        """Detects and annotates all faces in the image.

        Parameters
        ----------
        image : numpy.ndarray
            An RGB image in Bob format.

        Returns
        -------
        list
            A list of annotations. Annotations are dictionaries that contain the
            following keys: ``topleft``, ``bottomright``, ``reye``, ``leye``. 
            (``reye`` and ``leye`` are the estimated results, not captured by the 
            model.)
        """
Yu LINGHU's avatar
Yu LINGHU committed
128
129
        import cv2 as cv
        import mxnet as mx
130
131

        Batch = namedtuple("Batch", ["data"])
Yu LINGHU's avatar
Yu LINGHU committed
132

Yu LINGHU's avatar
Yu LINGHU committed
133
134
135
136
137
138
139
        raw_img = img
        if len(raw_img.shape) == 2:
            raw_img = gray_to_rgb(raw_img)
        assert img.shape[0] == 3, img.shape

        raw_img = to_matplotlib(raw_img)
        raw_img = raw_img[..., ::-1]
140

Yu LINGHU's avatar
Yu LINGHU committed
141
142
143
144
145
146
        raw_h = raw_img.shape[0]
        raw_w = raw_img.shape[1]

        raw_img = cv.cvtColor(raw_img, cv.COLOR_BGR2RGB)
        raw_img_f = raw_img.astype(np.float32)

147
148
149
150
151
152
153
154
        min_scale = min(
            np.floor(np.log2(np.max(self.clusters_w[self.normal_idx] / raw_w))),
            np.floor(np.log2(np.max(self.clusters_h[self.normal_idx] / raw_h))),
        )
        max_scale = min(1.0, -np.log2(max(raw_h, raw_w) / self.MAX_INPUT_DIM))

        scales_down = np.arange(min_scale, 0 + 0.0001, 1.0)
        scales_up = np.arange(0.5, max_scale + 0.0001, 0.5)
Yu LINGHU's avatar
Yu LINGHU committed
155
156
157
158
        scales_pow = np.hstack((scales_down, scales_up))
        scales = np.power(2.0, scales_pow)

        start = time.time()
159
        bboxes = np.empty(shape=(0, 5))
Yu LINGHU's avatar
Yu LINGHU committed
160
        for s in scales[::-1]:
161
162
            img = cv.resize(raw_img_f, (0, 0), fx=s, fy=s)
            img = np.transpose(img, (2, 0, 1))
Yu LINGHU's avatar
Yu LINGHU committed
163
164
165
            img = img - self.averageImage

            tids = []
166
            if s <= 1.0:
Yu LINGHU's avatar
Yu LINGHU committed
167
                tids = list(range(4, 12))
168
            else:
Yu LINGHU's avatar
Yu LINGHU committed
169
                tids = list(range(4, 12)) + list(range(18, 25))
170
            ignoredTids = list(set(range(0, self.clusters.shape[0])) - set(tids))
Yu LINGHU's avatar
Yu LINGHU committed
171
172
173
174
            img_h = img.shape[1]
            img_w = img.shape[2]
            img = img[np.newaxis, :]

175
            self.mod.reshape(data_shapes=[("data", (1, 3, img_h, img_w))])
Yu LINGHU's avatar
Yu LINGHU committed
176
177
178
179
            self.mod.forward(Batch([mx.nd.array(img)]))
            self.mod.get_outputs()[0].wait_to_read()
            fusex_res = self.mod.get_outputs()[0]

180
181
182
183
184
185
            score_cls = mx.nd.slice_axis(
                fusex_res, axis=1, begin=0, end=25, name="score_cls"
            )
            score_reg = mx.nd.slice_axis(
                fusex_res, axis=1, begin=25, end=None, name="score_reg"
            )
Yu LINGHU's avatar
Yu LINGHU committed
186
187
188
            prob_cls = mx.nd.sigmoid(score_cls)

            prob_cls_np = prob_cls.asnumpy()
189
            prob_cls_np[0, ignoredTids, :, :] = 0.0
Yu LINGHU's avatar
Yu LINGHU committed
190
191
192
193
194

            _, fc, fy, fx = np.where(prob_cls_np > self.prob_thresh)

            cy = fy * 8 - 1
            cx = fx * 8 - 1
195
            ch = self.clusters[fc, 3] - self.clusters[fc, 1] + 1
Yu LINGHU's avatar
Yu LINGHU committed
196
197
198
199
200
201
            cw = self.clusters[fc, 2] - self.clusters[fc, 0] + 1

            Nt = self.clusters.shape[0]

            score_reg_np = score_reg.asnumpy()
            tx = score_reg_np[0, 0:Nt, :, :]
202
203
204
            ty = score_reg_np[0, Nt : 2 * Nt, :, :]
            tw = score_reg_np[0, 2 * Nt : 3 * Nt, :, :]
            th = score_reg_np[0, 3 * Nt : 4 * Nt, :, :]
Yu LINGHU's avatar
Yu LINGHU committed
205
206
207
208
209
210
211
212
213
214
215

            dcx = cw * tx[fc, fy, fx]
            dcy = ch * ty[fc, fy, fx]
            rcx = cx + dcx
            rcy = cy + dcy
            rcw = cw * np.exp(tw[fc, fy, fx])
            rch = ch * np.exp(th[fc, fy, fx])

            score_cls_np = score_cls.asnumpy()
            scores = score_cls_np[0, fc, fy, fx]

216
217
218
219
            tmp_bboxes = np.vstack(
                (rcx - rcw / 2, rcy - rch / 2, rcx + rcw / 2, rcy + rch / 2)
            )
            tmp_bboxes = np.vstack((tmp_bboxes / s, scores))
Yu LINGHU's avatar
Yu LINGHU committed
220
221
222
223
224
225
226
227
228
229
            tmp_bboxes = tmp_bboxes.transpose()
            bboxes = np.vstack((bboxes, tmp_bboxes))

        refind_idx = self._nms(bboxes, self.nms_thresh)
        refind_bboxes = bboxes[refind_idx]
        refind_bboxes = refind_bboxes.astype(np.int32)

        annotations = refind_bboxes
        annots = []
        for i in range(len(refind_bboxes)):
230
231
232
233
234
            topleft = round(float(annotations[i][1])), round(float(annotations[i][0]))
            bottomright = (
                round(float(annotations[i][3])),
                round(float(annotations[i][2])),
            )
Yu LINGHU's avatar
Yu LINGHU committed
235
236
            width = float(annotations[i][2]) - float(annotations[i][0])
            length = float(annotations[i][3]) - float(annotations[i][1])
237
238
239
240
241
242
243
244
            right_eye = (
                round((0.37) * length + float(annotations[i][1])),
                round((0.3) * width + float(annotations[i][0])),
            )
            left_eye = (
                round((0.37) * length + float(annotations[i][1])),
                round((0.7) * width + float(annotations[i][0])),
            )
Yu LINGHU's avatar
Yu LINGHU committed
245
246
247
248
249
250
251
252
253
            annots.append(
                {
                    "topleft": topleft,
                    "bottomright": bottomright,
                    "reye": right_eye,
                    "leye": left_eye,
                }
            )

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
254
        return annots