modelinhos 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modelinhos/__init__.py ADDED
File without changes
File without changes
@@ -0,0 +1,278 @@
1
+ import numpy as np
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+
6
+
7
+ class BlazeBlock(nn.Module):
8
+ def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
9
+ super(BlazeBlock, self).__init__()
10
+
11
+ self.stride = stride
12
+ self.channel_pad = out_channels - in_channels
13
+
14
+ # TFLite uses slightly different padding than PyTorch
15
+ # on the depthwise conv layer when the stride is 2.
16
+ if stride == 2:
17
+ self.max_pool = nn.MaxPool2d(kernel_size=stride, stride=stride)
18
+ padding = 0
19
+ else:
20
+ padding = (kernel_size - 1) // 2
21
+
22
+ self.convs = nn.Sequential(
23
+ nn.Conv2d(
24
+ in_channels=in_channels,
25
+ out_channels=in_channels,
26
+ kernel_size=kernel_size,
27
+ stride=stride,
28
+ padding=padding,
29
+ groups=in_channels,
30
+ bias=True,
31
+ ),
32
+ nn.Conv2d(
33
+ in_channels=in_channels,
34
+ out_channels=out_channels,
35
+ kernel_size=1,
36
+ stride=1,
37
+ padding=0,
38
+ bias=True,
39
+ ),
40
+ )
41
+
42
+ self.act = nn.ReLU(inplace=True)
43
+
44
+ def forward(self, x):
45
+ if self.stride == 2:
46
+ h = F.pad(x, (0, 2, 0, 2), "constant", 0)
47
+ x = self.max_pool(x)
48
+ else:
49
+ h = x
50
+
51
+ if self.channel_pad > 0:
52
+ x = F.pad(x, (0, 0, 0, 0, 0, self.channel_pad), "constant", 0)
53
+
54
+ return self.act(self.convs(h) + x)
55
+
56
+
57
+ class FinalBlazeBlock(nn.Module):
58
+ def __init__(self, channels, kernel_size=3):
59
+ super(FinalBlazeBlock, self).__init__()
60
+ # TFLite uses slightly different padding than PyTorch
61
+ # on the depthwise conv layer when the stride is 2.
62
+ self.convs = nn.Sequential(
63
+ nn.Conv2d(
64
+ in_channels=channels,
65
+ out_channels=channels,
66
+ kernel_size=kernel_size,
67
+ stride=2,
68
+ padding=0,
69
+ groups=channels,
70
+ bias=True,
71
+ ),
72
+ nn.Conv2d(
73
+ in_channels=channels,
74
+ out_channels=channels,
75
+ kernel_size=1,
76
+ stride=1,
77
+ padding=0,
78
+ bias=True,
79
+ ),
80
+ )
81
+
82
+ self.act = nn.ReLU(inplace=True)
83
+
84
+ def forward(self, x):
85
+ h = F.pad(x, (0, 2, 0, 2), "constant", 0)
86
+
87
+ return self.act(self.convs(h))
88
+
89
+
90
+ class BlazeNet(nn.Module):
91
+ """The BlazeFace face detection model from MediaPipe.
92
+
93
+ The version from MediaPipe is simpler than the one in the paper;
94
+ it does not use the "double" BlazeBlocks.
95
+
96
+ Because we won't be training this model, it doesn't need to have
97
+ batchnorm layers. These have already been "folded" into the conv
98
+ weights by TFLite.
99
+
100
+ The conversion to PyTorch is fairly straightforward, but there are
101
+ some small differences between TFLite and PyTorch in how they handle
102
+ padding on conv layers with stride 2.
103
+
104
+ This version works on batches, while the MediaPipe version can only
105
+ handle a single image at a time.
106
+
107
+ Based on code from https://github.com/tkat0/PyTorch_BlazeFace/ and
108
+ https://github.com/google/mediapipe/
109
+ """
110
+
111
+ def __init__(self, back_model=False):
112
+ super(BlazeNet, self).__init__()
113
+
114
+ # These are the settings from the MediaPipe example graphs
115
+ # mediapipe/graphs/face_detection/face_detection_mobile_gpu.pbtxt
116
+ # and
117
+ # mediapipe/graphs/face_detection/face_detection_back_mobile_gpu.pbtxt
118
+ self.num_classes = 1
119
+ self.num_anchors = 896
120
+ self.num_coords = 16
121
+ self.score_clipping_thresh = 100.0
122
+ self.back_model = back_model
123
+ if back_model:
124
+ self.x_scale = 256.0
125
+ self.y_scale = 256.0
126
+ self.h_scale = 256.0
127
+ self.w_scale = 256.0
128
+ self.min_score_thresh = 0.65
129
+ else:
130
+ self.x_scale = 128.0
131
+ self.y_scale = 128.0
132
+ self.h_scale = 128.0
133
+ self.w_scale = 128.0
134
+ self.min_score_thresh = 0.75
135
+ self.min_suppression_threshold = 0.3
136
+
137
+ self._define_layers()
138
+
139
+ def _define_layers(self):
140
+ if self.back_model:
141
+ self.backbone = nn.Sequential(
142
+ nn.Conv2d(
143
+ in_channels=3,
144
+ out_channels=24,
145
+ kernel_size=5,
146
+ stride=2,
147
+ padding=0,
148
+ bias=True,
149
+ ),
150
+ nn.ReLU(inplace=True),
151
+ BlazeBlock(24, 24),
152
+ BlazeBlock(24, 24),
153
+ BlazeBlock(24, 24),
154
+ BlazeBlock(24, 24),
155
+ BlazeBlock(24, 24),
156
+ BlazeBlock(24, 24),
157
+ BlazeBlock(24, 24),
158
+ BlazeBlock(24, 24, stride=2),
159
+ BlazeBlock(24, 24),
160
+ BlazeBlock(24, 24),
161
+ BlazeBlock(24, 24),
162
+ BlazeBlock(24, 24),
163
+ BlazeBlock(24, 24),
164
+ BlazeBlock(24, 24),
165
+ BlazeBlock(24, 24),
166
+ BlazeBlock(24, 48, stride=2),
167
+ BlazeBlock(48, 48),
168
+ BlazeBlock(48, 48),
169
+ BlazeBlock(48, 48),
170
+ BlazeBlock(48, 48),
171
+ BlazeBlock(48, 48),
172
+ BlazeBlock(48, 48),
173
+ BlazeBlock(48, 48),
174
+ BlazeBlock(48, 96, stride=2),
175
+ BlazeBlock(96, 96),
176
+ BlazeBlock(96, 96),
177
+ BlazeBlock(96, 96),
178
+ BlazeBlock(96, 96),
179
+ BlazeBlock(96, 96),
180
+ BlazeBlock(96, 96),
181
+ BlazeBlock(96, 96),
182
+ )
183
+ self.final = FinalBlazeBlock(96)
184
+ self.classifier_8 = nn.Conv2d(96, 2, 1, bias=True)
185
+ self.classifier_16 = nn.Conv2d(96, 6, 1, bias=True)
186
+
187
+ self.regressor_8 = nn.Conv2d(96, 32, 1, bias=True)
188
+ self.regressor_16 = nn.Conv2d(96, 96, 1, bias=True)
189
+ else:
190
+ self.backbone1 = nn.Sequential(
191
+ nn.Conv2d(
192
+ in_channels=3,
193
+ out_channels=24,
194
+ kernel_size=5,
195
+ stride=2,
196
+ padding=0,
197
+ bias=True,
198
+ ),
199
+ nn.ReLU(inplace=True),
200
+ BlazeBlock(24, 24),
201
+ BlazeBlock(24, 28),
202
+ BlazeBlock(28, 32, stride=2),
203
+ BlazeBlock(32, 36),
204
+ BlazeBlock(36, 42),
205
+ BlazeBlock(42, 48, stride=2),
206
+ BlazeBlock(48, 56),
207
+ BlazeBlock(56, 64),
208
+ BlazeBlock(64, 72),
209
+ BlazeBlock(72, 80),
210
+ BlazeBlock(80, 88),
211
+ )
212
+
213
+ self.backbone2 = nn.Sequential(
214
+ BlazeBlock(88, 96, stride=2),
215
+ BlazeBlock(96, 96),
216
+ BlazeBlock(96, 96),
217
+ BlazeBlock(96, 96),
218
+ BlazeBlock(96, 96),
219
+ )
220
+ self.classifier_8 = nn.Conv2d(88, 2, 1, bias=True)
221
+ self.classifier_16 = nn.Conv2d(96, 6, 1, bias=True)
222
+
223
+ self.regressor_8 = nn.Conv2d(88, 32, 1, bias=True)
224
+ self.regressor_16 = nn.Conv2d(96, 96, 1, bias=True)
225
+
226
+ def forward(self, image):
227
+ # TFLite uses slightly different padding on the first conv layer
228
+ # than PyTorch, so do it manually.
229
+ x = F.pad(image, (1, 2, 1, 2), "constant", 0)
230
+
231
+ b = x.shape[0] # batch size, needed for reshaping later
232
+
233
+ if self.back_model:
234
+ x = self.backbone(x) # (b, 16, 16, 96)
235
+ h = self.final(x) # (b, 8, 8, 96)
236
+ else:
237
+ x = self.backbone1(x) # (b, 88, 16, 16)
238
+ h = self.backbone2(x) # (b, 96, 8, 8)
239
+
240
+ # Note: Because PyTorch is NCHW but TFLite is NHWC, we need to
241
+ # permute the output from the conv layers before reshaping it.
242
+
243
+ c1 = self.classifier_8(x) # (b, 2, 16, 16)
244
+ c1 = c1.permute(0, 2, 3, 1) # (b, 16, 16, 2)
245
+ c1 = c1.reshape(b, -1, 1) # (b, 512, 1)
246
+
247
+ c2 = self.classifier_16(h) # (b, 6, 8, 8)
248
+ c2 = c2.permute(0, 2, 3, 1) # (b, 8, 8, 6)
249
+ c2 = c2.reshape(b, -1, 1) # (b, 384, 1)
250
+
251
+ c = torch.cat((c1, c2), dim=1) # (b, 896, 1)
252
+
253
+ r1 = self.regressor_8(x) # (b, 32, 16, 16)
254
+ r1 = r1.permute(0, 2, 3, 1) # (b, 16, 16, 32)
255
+ r1 = r1.reshape(b, -1, 16) # (b, 512, 16)
256
+
257
+ r2 = self.regressor_16(h) # (b, 96, 8, 8)
258
+ r2 = r2.permute(0, 2, 3, 1) # (b, 8, 8, 96)
259
+ r2 = r2.reshape(b, -1, 16) # (b, 384, 16)
260
+
261
+ r = torch.cat((r1, r2), dim=1) # (b, 896, 16)
262
+ return [r, c]
263
+
264
+
265
+ def load_weights(model: BlazeNet, path):
266
+ model.load_state_dict(torch.load(path))
267
+ model.eval()
268
+
269
+
270
+ def load_anchors(model: BlazeNet, path):
271
+ model.anchors = torch.tensor(
272
+ np.load(path),
273
+ dtype=torch.float32,
274
+ device=model.classifier_8.weight.device,
275
+ )
276
+ assert model.anchors.ndimension() == 2
277
+ assert model.anchors.shape[0] == model.num_anchors
278
+ assert model.anchors.shape[1] == 4
@@ -0,0 +1,93 @@
1
+ import cv2
2
+ import numpy as np
3
+ import torch
4
+
5
+ from modelinhos.blazenet import BlazeNet
6
+
7
+ EXPECTED = np.array(
8
+ [
9
+ [
10
+ 0.2763,
11
+ 0.3182,
12
+ 0.4465,
13
+ 0.4884,
14
+ 0.3830,
15
+ 0.3150,
16
+ 0.4561,
17
+ 0.3202,
18
+ 0.4309,
19
+ 0.3526,
20
+ 0.4229,
21
+ 0.3913,
22
+ 0.3182,
23
+ 0.3373,
24
+ 0.4769,
25
+ 0.3464,
26
+ 0.9308,
27
+ ]
28
+ ],
29
+ )
30
+
31
+
32
+ def plot(image, detections, with_keypoints=True):
33
+ visualized = image.copy()
34
+
35
+ if isinstance(detections, torch.Tensor):
36
+ detections = detections.cpu().numpy()
37
+
38
+ if detections.ndim == 1:
39
+ detections = np.expand_dims(detections, axis=0)
40
+
41
+ print("Found %d faces" % detections.shape[0])
42
+
43
+ for i in range(detections.shape[0]):
44
+ ymin = int(detections[i, 0] * image.shape[0])
45
+ xmin = int(detections[i, 1] * image.shape[1])
46
+ ymax = int(detections[i, 2] * image.shape[0])
47
+ xmax = int(detections[i, 3] * image.shape[1])
48
+
49
+ cv2.rectangle(
50
+ visualized,
51
+ (xmin, ymin),
52
+ (xmax, ymax),
53
+ color=(0, 0, 255), # red in BGR
54
+ thickness=1,
55
+ )
56
+
57
+ if with_keypoints:
58
+ for k in range(6):
59
+ kp_x = int(detections[i, 4 + k * 2] * image.shape[1])
60
+ kp_y = int(detections[i, 4 + k * 2 + 1] * image.shape[0])
61
+
62
+ cv2.circle(
63
+ visualized,
64
+ (kp_x, kp_y),
65
+ radius=2,
66
+ color=(255, 200, 100), # light-sky-blue-ish in BGR
67
+ thickness=1,
68
+ )
69
+
70
+ return visualized
71
+
72
+
73
+ def main():
74
+ front_net = BlazeNet()
75
+ front_net.load_weights("blazeface.pth")
76
+ front_net.load_anchors("anchors.npy")
77
+ front_net.min_score_thresh = 0.75
78
+ front_net.min_suppression_threshold = 0.3
79
+ image = cv2.imread("1face.png")
80
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
81
+ predictions = front_net.predict_on_image(image)
82
+ np.testing.assert_almost_equal(
83
+ predictions.cpu().numpy(),
84
+ EXPECTED,
85
+ )
86
+ visualized = plot(image, predictions)
87
+ cv2.imshow("Detections", visualized)
88
+ cv2.waitKey(0)
89
+ cv2.destroyAllWindows()
90
+
91
+
92
+ if __name__ == "__main__":
93
+ main()
@@ -0,0 +1,302 @@
1
+ import numpy as np
2
+ import torch
3
+
4
+ from modelinhos.blaze.blazenet import BlazeNet
5
+
6
+
7
+ def intersect(box_a, box_b):
8
+ """We resize both tensors to [A,B,2] without new malloc:
9
+ [A,2] -> [A,1,2] -> [A,B,2]
10
+ [B,2] -> [1,B,2] -> [A,B,2]
11
+ Then we compute the area of intersect between box_a and box_b.
12
+ Args:
13
+ box_a: (tensor) bounding boxes, Shape: [A,4].
14
+ box_b: (tensor) bounding boxes, Shape: [B,4].
15
+ Return:
16
+ (tensor) intersection area, Shape: [A,B].
17
+ """
18
+ A = box_a.size(0)
19
+ B = box_b.size(0)
20
+ max_xy = torch.min(
21
+ box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
22
+ box_b[:, 2:].unsqueeze(0).expand(A, B, 2),
23
+ )
24
+ min_xy = torch.max(
25
+ box_a[:, :2].unsqueeze(1).expand(A, B, 2),
26
+ box_b[:, :2].unsqueeze(0).expand(A, B, 2),
27
+ )
28
+ inter = torch.clamp((max_xy - min_xy), min=0)
29
+ return inter[:, :, 0] * inter[:, :, 1]
30
+
31
+
32
+ def jaccard(box_a, box_b):
33
+ """Compute the jaccard overlap of two sets of boxes. The jaccard overlap
34
+ is simply the intersection over union of two boxes. Here we operate on
35
+ ground truth boxes and default boxes.
36
+ E.g.:
37
+ A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
38
+ Args:
39
+ box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4]
40
+ box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]
41
+ Return:
42
+ jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]
43
+ """
44
+ inter = intersect(box_a, box_b)
45
+ area_a = (
46
+ ((box_a[:, 2] - box_a[:, 0]) * (box_a[:, 3] - box_a[:, 1]))
47
+ .unsqueeze(1)
48
+ .expand_as(inter)
49
+ ) # [A,B]
50
+ area_b = (
51
+ ((box_b[:, 2] - box_b[:, 0]) * (box_b[:, 3] - box_b[:, 1]))
52
+ .unsqueeze(0)
53
+ .expand_as(inter)
54
+ ) # [A,B]
55
+ union = area_a + area_b - inter
56
+ return inter / union # [A,B]
57
+
58
+
59
+ def overlap_similarity(box, other_boxes):
60
+ """Computes the IOU between a bounding box and set of other boxes."""
61
+ return jaccard(box.unsqueeze(0), other_boxes).squeeze(0)
62
+
63
+
64
+ def _weighted_non_max_suppression(
65
+ model: BlazeNet,
66
+ detections,
67
+ min_suppression_threshold: int,
68
+ ):
69
+ """The alternative NMS method as mentioned in the BlazeFace paper:
70
+
71
+ "We replace the suppression algorithm with a blending strategy that
72
+ estimates the regression parameters of a bounding box as a weighted
73
+ mean between the overlapping predictions."
74
+
75
+ The original MediaPipe code assigns the score of the most confident
76
+ detection to the weighted detection, but we take the average score
77
+ of the overlapping detections.
78
+
79
+ The input detections should be a Tensor of shape (count, 17).
80
+
81
+ Returns a list of PyTorch tensors, one for each detected face.
82
+
83
+ This is based on the source code from:
84
+ mediapipe/calculators/util/non_max_suppression_calculator.cc
85
+ mediapipe/calculators/util/non_max_suppression_calculator.proto
86
+ """
87
+ if len(detections) == 0:
88
+ return []
89
+
90
+ output_detections = []
91
+
92
+ # Sort the detections from highest to lowest score.
93
+ remaining = torch.argsort(detections[:, 16], descending=True)
94
+
95
+ while len(remaining) > 0:
96
+ detection = detections[remaining[0]]
97
+
98
+ # Compute the overlap between the first box and the other
99
+ # remaining boxes. (Note that the other_boxes also include
100
+ # the first_box.)
101
+ first_box = detection[:4]
102
+ other_boxes = detections[remaining, :4]
103
+ ious = overlap_similarity(first_box, other_boxes)
104
+
105
+ # If two detections don't overlap enough, they are considered
106
+ # to be from different faces.
107
+ mask = ious > min_suppression_threshold
108
+ overlapping = remaining[mask]
109
+ remaining = remaining[~mask]
110
+
111
+ # Take an average of the coordinates from the overlapping
112
+ # detections, weighted by their confidence scores.
113
+ weighted_detection = detection.clone()
114
+ if len(overlapping) > 1:
115
+ coordinates = detections[overlapping, :16]
116
+ scores = detections[overlapping, 16:17]
117
+ total_score = scores.sum()
118
+ weighted = (coordinates * scores).sum(dim=0) / total_score
119
+ weighted_detection[:16] = weighted
120
+ weighted_detection[16] = total_score / len(overlapping)
121
+
122
+ output_detections.append(weighted_detection)
123
+
124
+ return output_detections
125
+
126
+
127
+ def _decode_boxes(model: BlazeNet, raw, anchors):
128
+ """Converts the predictions into actual coordinates using
129
+ the anchor boxes. Processes the entire batch at once.
130
+ """
131
+ boxes = torch.zeros_like(raw)
132
+
133
+ x_center = raw[..., 0] / model.x_scale * anchors[:, 2] + anchors[:, 0]
134
+ y_center = raw[..., 1] / model.y_scale * anchors[:, 3] + anchors[:, 1]
135
+
136
+ w = raw[..., 2] / model.w_scale * anchors[:, 2]
137
+ h = raw[..., 3] / model.h_scale * anchors[:, 3]
138
+
139
+ boxes[..., 0] = y_center - h / 2.0 # ymin
140
+ boxes[..., 1] = x_center - w / 2.0 # xmin
141
+ boxes[..., 2] = y_center + h / 2.0 # ymax
142
+ boxes[..., 3] = x_center + w / 2.0 # xmax
143
+
144
+ for k in range(6):
145
+ offset = 4 + k * 2
146
+ keypoint_x = (
147
+ raw[..., offset] / model.x_scale * anchors[:, 2] + anchors[:, 0]
148
+ ) # noqa
149
+ keypoint_y = (
150
+ raw[..., offset + 1] / model.y_scale * anchors[:, 3]
151
+ + anchors[:, 1] # noqa
152
+ )
153
+ boxes[..., offset] = keypoint_x
154
+ boxes[..., offset + 1] = keypoint_y
155
+
156
+ return boxes
157
+
158
+
159
+ def predict_on_batch(
160
+ model: BlazeNet,
161
+ x,
162
+ back_model,
163
+ min_suppression_threshold: int,
164
+ min_score_thresh: float,
165
+ ):
166
+ """Makes a prediction on a batch of images.
167
+
168
+ Arguments:
169
+ x: a NumPy array of shape (b, H, W, 3) or a PyTorch tensor of
170
+ shape (b, 3, H, W). The height and width should be 128 pixels.
171
+
172
+ Returns:
173
+ A list containing a tensor of face detections for each image in
174
+ the batch. If no faces are found for an image, returns a tensor
175
+ of shape (0, 17).
176
+
177
+ Each face detection is a PyTorch tensor consisting of 17 numbers:
178
+ - ymin, xmin, ymax, xmax
179
+ - x,y-coordinates for the 6 keypoints
180
+ - confidence score
181
+ """
182
+ if isinstance(x, np.ndarray):
183
+ x = torch.from_numpy(x).permute((0, 3, 1, 2))
184
+
185
+ assert x.shape[1] == 3
186
+ if back_model:
187
+ assert x.shape[2] == 256
188
+ assert x.shape[3] == 256
189
+ else:
190
+ assert x.shape[2] == 128
191
+ assert x.shape[3] == 128
192
+
193
+ # 1. Preprocess the images into tensors:
194
+ x = x.to(model.classifier_8.weight.device)
195
+ x = _preprocess(x)
196
+
197
+ # 2. Run the neural network:
198
+ with torch.no_grad():
199
+ out = model(x)
200
+
201
+ # 3. Postprocess the raw predictions:
202
+ detections = _tensors_to_detections(
203
+ model,
204
+ out[0],
205
+ out[1],
206
+ model.anchors,
207
+ min_score_thresh,
208
+ )
209
+
210
+ for i in range(len(detections)):
211
+ faces = _weighted_non_max_suppression(
212
+ model,
213
+ detections[i],
214
+ min_suppression_threshold=min_suppression_threshold,
215
+ )
216
+ faces = torch.stack(faces) if len(faces) > 0 else torch.zeros((0, 17))
217
+ return [faces]
218
+
219
+
220
+ def _tensors_to_detections(
221
+ model: BlazeNet,
222
+ raw_box_tensor,
223
+ raw_score_tensor,
224
+ anchors,
225
+ min_score_thresh,
226
+ ):
227
+ """The output of the neural network is a tensor of shape (b, 896, 16)
228
+ containing the bounding box regressor predictions, as well as a tensor
229
+ of shape (b, 896, 1) with the classification confidences.
230
+
231
+ This function converts these two "raw" tensors into proper detections.
232
+ Returns a list of (num_detections, 17) tensors, one for each image in
233
+ the batch.
234
+
235
+ This is based on the source code from:
236
+ mediapipe/calculators/tflite/tflite_tensors_to_detections_calculator.cc
237
+ mediapipe/calculators/tflite/tflite_tensors_to_detections_calculator.proto
238
+ """
239
+ assert raw_box_tensor.ndimension() == 3
240
+ assert raw_box_tensor.shape[1] == model.num_anchors
241
+ assert raw_box_tensor.shape[2] == model.num_coords
242
+
243
+ assert raw_score_tensor.ndimension() == 3
244
+ assert raw_score_tensor.shape[1] == model.num_anchors
245
+ assert raw_score_tensor.shape[2] == model.num_classes
246
+
247
+ assert raw_box_tensor.shape[0] == raw_score_tensor.shape[0]
248
+
249
+ detection_boxes = _decode_boxes(model, raw_box_tensor, anchors)
250
+
251
+ thresh = model.score_clipping_thresh
252
+ raw_score_tensor = raw_score_tensor.clamp(-thresh, thresh)
253
+ detection_scores = raw_score_tensor.sigmoid().squeeze(dim=-1)
254
+
255
+ # Note: we stripped off the last dimension from the scores tensor
256
+ # because there is only has one class. Now we can simply use a mask
257
+ # to filter out the boxes with too low confidence.
258
+ mask = detection_scores >= min_score_thresh
259
+
260
+ # Because each image from the batch can have a different number of
261
+ # detections, process them one at a time using a loop.
262
+ output_detections = []
263
+ for i in range(raw_box_tensor.shape[0]):
264
+ boxes = detection_boxes[i, mask[i]]
265
+ scores = detection_scores[i, mask[i]].unsqueeze(dim=-1)
266
+ output_detections.append(torch.cat((boxes, scores), dim=-1))
267
+
268
+ return output_detections
269
+
270
+
271
+ def predict_on_image(
272
+ model: BlazeNet,
273
+ image,
274
+ back_model,
275
+ min_suppression_threshold: int,
276
+ min_score_thresh: float,
277
+ ):
278
+ """Makes a prediction on a single image.
279
+
280
+ Arguments:
281
+ img: a NumPy array of shape (H, W, 3) or a PyTorch tensor of
282
+ shape (3, H, W). The image's height and width should be
283
+ 128 pixels.
284
+
285
+ Returns:
286
+ A tensor with face detections.
287
+ """
288
+ if isinstance(image, np.ndarray):
289
+ image = torch.from_numpy(image).permute((2, 0, 1))
290
+
291
+ return predict_on_batch(
292
+ model,
293
+ image.unsqueeze(0),
294
+ back_model=back_model,
295
+ min_suppression_threshold=min_suppression_threshold,
296
+ min_score_thresh=min_score_thresh,
297
+ )[0]
298
+
299
+
300
+ def _preprocess(x):
301
+ """Converts the image pixels to the range [-1, 1]."""
302
+ return x.float() / 127.5 - 1.0