Models.face_detector_retinaface package

Submodules

Models.face_detector_retinaface.FaceDetector module

class Models.face_detector_retinaface.FaceDetector.FaceDetector(weights_path: Path, model_name: Literal['mobile0.25', 'resnet50'], confidence_threshold: float = 0.7, landmarks: bool = False, nms_threshold: float | None = 0.4, max_input_size: int | None = None, use_cuda: bool = False)[source]

Bases: object

RetinaFace face detector.

Create the face detector.

Parameters:
  • weights_path (Path) – Path to the model weights file.

  • model_name (Literal["mobile0.25", "resnet50"]) – The model backbone name. One of “mobile0.25” or “resnet50”.

  • confidence_threshold (float, optional) – The minimum detection confidence. Defaults to 0.7.

  • landmarks (bool, optional) – Process the face landmarks. Defaults to False.

  • nms_threshold (Optional[float], optional) – Non-maximum-suppression threshold. None to disable. Defaults to 0.4.

  • max_input_size (Optional[int], optional) – Maximum size of the image larger side. If None it is ignored. Defaults to None.

  • use_cuda (bool, optional) – Run the model on a CUDA device. Defaults to False.

Raises:

ValueError – If model_name is not one of “mobile0.25” or “resnet50”.

predict(image: ndarray)[source]

Detect faces on an image and return its bounding boxes and landmarks.

Parameters:

image (np.ndarray) – A BGR uint8 image of shape (H, W, 3).

Returns:

List of Instances with the following fields:
  • bounding_box (BoundingBox): A BoundingBox object with the

    position of the detected face.

  • confidence (float): The detection confidence.

  • landmarks (np.ndarray): Predicted face landmarks if

    self.landmarks is set to True.

Return type:

List[Instance]

Models.face_detector_retinaface.box_utils module

Models.face_detector_retinaface.box_utils.center_size(boxes)[source]

Convert prior_boxes to (cx, cy, w, h) representation for comparison to center-size form ground truth data.

Parameters:

boxes – (tensor) point_form boxes

Returns:

(tensor) Converted xmin, ymin, xmax, ymax form of boxes.

Return type:

boxes

Models.face_detector_retinaface.box_utils.decode(loc, priors, variances)[source]

Decode locations from predictions using priors to undo the encoding we did for offset regression at train time.

Parameters:
  • loc (tensor) – location predictions for loc layers, Shape: [num_priors,4]

  • priors (tensor) – Prior boxes in center-offset form. Shape: [num_priors,4].

  • variances – (list[float]) Variances of priorboxes

Returns:

decoded bounding box predictions

Models.face_detector_retinaface.box_utils.decode_landm(pre, priors, variances)[source]

Decode landm from predictions using priors to undo the encoding we did for offset regression at train time.

Parameters:
  • pre (tensor) – landm predictions for loc layers, Shape: [num_priors,10]

  • priors (tensor) – Prior boxes in center-offset form. Shape: [num_priors,4].

  • variances – (list[float]) Variances of priorboxes

Returns:

decoded landm predictions

Models.face_detector_retinaface.box_utils.encode(matched, priors, variances)[source]

Encode the variances from the priorbox layers into the ground truth boxes we have matched (based on jaccard overlap) with the prior boxes.

Parameters:
  • matched – (tensor) Coords of ground truth for each prior in point-form Shape: [num_priors, 4].

  • priors – (tensor) Prior boxes in center-offset form Shape: [num_priors,4].

  • variances – (list[float]) Variances of priorboxes

Returns:

[num_priors, 4]

Return type:

encoded boxes (tensor), Shape

Models.face_detector_retinaface.box_utils.encode_landm(matched, priors, variances)[source]

Encode the variances from the priorbox layers into the ground truth boxes we have matched (based on jaccard overlap) with the prior boxes.

Parameters:
  • matched – (tensor) Coords of ground truth for each prior in point-form Shape: [num_priors, 10].

  • priors – (tensor) Prior boxes in center-offset form Shape: [num_priors,4].

  • variances – (list[float]) Variances of priorboxes

Returns:

[num_priors, 10]

Return type:

encoded landm (tensor), Shape

Models.face_detector_retinaface.box_utils.intersect(box_a, box_b)[source]

We resize both tensors to [A,B,2] without new malloc: [A,2] -> [A,1,2] -> [A,B,2] [B,2] -> [1,B,2] -> [A,B,2] Then we compute the area of intersect between box_a and box_b.

Parameters:
  • box_a – (tensor) bounding boxes, Shape: [A,4].

  • box_b – (tensor) bounding boxes, Shape: [B,4].

Returns:

[A,B].

Return type:

(tensor) intersection area, Shape

Models.face_detector_retinaface.box_utils.jaccard(box_a, box_b)[source]

Compute the jaccard overlap of two sets of boxes. The jaccard overlap is simply the intersection over union of two boxes. Here we operate on ground truth boxes and default boxes.

E.g.:

A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)

Parameters:
  • box_a – (tensor) Ground truth bounding boxes, Shape: [num_objects,4]

  • box_b – (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]

Returns:

(tensor) Shape: [box_a.size(0), box_b.size(0)]

Return type:

jaccard overlap

Models.face_detector_retinaface.box_utils.log_sum_exp(x)[source]

Utility function for computing log_sum_exp while determining This will be used to determine unaveraged confidence loss across all examples in a batch.

Parameters:

x (Variable(tensor)) – conf_preds from conf layers

Models.face_detector_retinaface.box_utils.match(threshold, truths, priors, variances, labels, landms, loc_t, conf_t, landm_t, idx)[source]

Match each prior box with the ground truth box of the highest jaccard overlap, encode the bounding boxes, then return the matched indices corresponding to both confidence and location preds.

Parameters:
  • threshold – (float) The overlap threshold used when mathing boxes.

  • truths – (tensor) Ground truth boxes, Shape: [num_obj, 4].

  • priors – (tensor) Prior boxes from priorbox layers, Shape: [n_priors,4].

  • variances – (tensor) Variances corresponding to each prior coord, Shape: [num_priors, 4].

  • labels – (tensor) All the class labels for the image, Shape: [num_obj].

  • landms – (tensor) Ground truth landms, Shape [num_obj, 10].

  • loc_t – (tensor) Tensor to be filled w/ endcoded location targets.

  • conf_t – (tensor) Tensor to be filled w/ matched indices for conf preds.

  • landm_t – (tensor) Tensor to be filled w/ endcoded landm targets.

  • idx – (int) current batch index

Returns:

The matched indices corresponding to 1)location 2)confidence 3)landm preds.

Models.face_detector_retinaface.box_utils.matrix_iof(a, b)[source]

return iof of a and b, numpy version for data augmentation

Models.face_detector_retinaface.box_utils.matrix_iou(a, b)[source]

return iou of a and b, numpy version for data augmentation

Models.face_detector_retinaface.box_utils.nms(boxes, scores, overlap=0.5, top_k=200)[source]

Apply non-maximum suppression at test time to avoid detecting too many overlapping bounding boxes for a given object.

Parameters:
  • boxes – (tensor) The location preds for the img, Shape: [num_priors,4].

  • scores – (tensor) The class predscores for the img, Shape:[num_priors].

  • overlap – (float) The overlap thresh for suppressing unnecessary boxes.

  • top_k – (int) The Maximum number of box preds to consider.

Returns:

The indices of the kept boxes with respect to num_priors.

Models.face_detector_retinaface.box_utils.point_form(boxes)[source]

Convert prior_boxes to (xmin, ymin, xmax, ymax) representation for comparison to point form ground truth data.

Parameters:

boxes – (tensor) center-size default boxes from priorbox layers.

Returns:

(tensor) Converted xmin, ymin, xmax, ymax form of boxes.

Return type:

boxes

Models.face_detector_retinaface.config module

Models.face_detector_retinaface.net module

class Models.face_detector_retinaface.net.FPN(in_channels_list, out_channels)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class Models.face_detector_retinaface.net.MobileNetV1[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class Models.face_detector_retinaface.net.SSH(in_channel, out_channel)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
Models.face_detector_retinaface.net.conv_bn(inp, oup, stride=1, leaky=0)[source]
Models.face_detector_retinaface.net.conv_bn1X1(inp, oup, stride, leaky=0)[source]
Models.face_detector_retinaface.net.conv_bn_no_relu(inp, oup, stride)[source]
Models.face_detector_retinaface.net.conv_dw(inp, oup, stride, leaky=0.1)[source]

Models.face_detector_retinaface.prior_box module

class Models.face_detector_retinaface.prior_box.PriorBox(cfg, image_size=None, phase='train')[source]

Bases: object

forward()[source]

Models.face_detector_retinaface.py_cpu_nms module

Models.face_detector_retinaface.py_cpu_nms.py_cpu_nms(dets, thresh)[source]

Pure Python NMS baseline.

Models.face_detector_retinaface.retinaface module

class Models.face_detector_retinaface.retinaface.BboxHead(inchannels=512, num_anchors=3)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class Models.face_detector_retinaface.retinaface.ClassHead(inchannels=512, num_anchors=3)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class Models.face_detector_retinaface.retinaface.LandmarkHead(inchannels=512, num_anchors=3)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class Models.face_detector_retinaface.retinaface.RetinaFace(cfg=None, phase='train')[source]

Bases: Module

Parameters:
  • cfg – Network related settings.

  • phase – train or test.

forward(inputs)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

Module contents