Convert Figma logo to code with AI

biubug6 logoPytorch_Retinaface

Retinaface get 80.99% in widerface hard val using mobilenet0.25.

2,586
760
2,586
141

Top Related Projects

The pytorch re-implement of the official efficientdet with SOTA performance in real time and pretrained weights.

State-of-the-art 2D and 3D Face Analysis Project

💎1MB lightweight face detection model (1MB轻量级人脸检测模型)

Quick Overview

Pytorch_Retinaface is a GitHub repository that implements the RetinaFace face detection algorithm using PyTorch. It provides a robust and efficient face detection solution that can handle various face scales, poses, and occlusions in images and videos.

Pros

  • High accuracy in face detection, even for challenging scenarios
  • Supports both CPU and GPU inference
  • Includes pre-trained models for easy deployment
  • Provides options for different backbones (ResNet50, MobileNet) to balance speed and accuracy

Cons

  • Limited documentation and examples for advanced usage
  • Requires some understanding of PyTorch and deep learning concepts
  • May be computationally intensive for real-time applications on low-end devices
  • Lacks built-in face recognition capabilities (only performs detection)

Code Examples

  1. Loading a pre-trained model:
from models.retinaface import RetinaFace
from data import cfg_mnet

# Load the model
net = RetinaFace(cfg=cfg_mnet, phase='test')
net.load_state_dict(torch.load('weights/mobilenet0.25_Final.pth', map_location=torch.device('cpu')))
net.eval()
  1. Performing face detection on an image:
from utils.box_utils import decode, decode_landm
from utils.nms.py_cpu_nms import py_cpu_nms

# Preprocess the image
img = cv2.imread('path/to/image.jpg')
img = np.float32(img)
im_height, im_width = img.shape[:2]
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)

# Perform detection
loc, conf, landms = net(img)
priorbox = PriorBox(cfg_mnet, image_size=(im_height, im_width))
priors = priorbox.forward()
boxes = decode(loc.data.squeeze(0), priors.data, cfg_mnet['variance'])
boxes = boxes * scale
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]

# Apply NMS
keep = py_cpu_nms(boxes, scores, 0.4)
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
dets = dets[keep, :]
  1. Visualizing the results:
for b in dets:
    if b[4] < 0.6:
        continue
    text = "{:.4f}".format(b[4])
    b = list(map(int, b))
    cv2.rectangle(img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2)
    cx = b[0]
    cy = b[1] + 12
    cv2.putText(img, text, (cx, cy),
                cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))

cv2.imshow("image", img)
cv2.waitKey(0)

Getting Started

  1. Clone the repository:

    git clone https://github.com/biubug6/Pytorch_Retinaface.git
    cd Pytorch_Retinaface
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Download pre-trained weights:

    mkdir weights
    wget -P weights/ https://github.com/biubug6/Pytorch_Retinaface/releases/download/v1.0/mobilenet0.25_Final.pth
    
  4. Run the demo:

    python detect.py
    

Competitor Comparisons

The pytorch re-implement of the official efficientdet with SOTA performance in real time and pretrained weights.

Pros of Yet-Another-EfficientDet-Pytorch

  • Implements EfficientDet, a more recent and efficient object detection architecture
  • Provides pre-trained weights for various EfficientDet models (D0-D7)
  • Includes tools for easy model conversion and ONNX export

Cons of Yet-Another-EfficientDet-Pytorch

  • Less focused on face detection specifically
  • May require more computational resources for training and inference
  • Documentation is less comprehensive compared to Pytorch_Retinaface

Code Comparison

Pytorch_Retinaface:

from models.retinaface import RetinaFace
model = RetinaFace(cfg=cfg, phase='test')
model = load_model(model, args.trained_model, args.cpu)

Yet-Another-EfficientDet-Pytorch:

from backbone import EfficientDetBackbone
model = EfficientDetBackbone(compound_coef=0, num_classes=len(obj_list))
model.load_state_dict(torch.load(weights_path, map_location=device))

Both repositories provide PyTorch implementations of object detection models, but they focus on different architectures. Pytorch_Retinaface is specifically designed for face detection using the RetinaFace model, while Yet-Another-EfficientDet-Pytorch implements the more general-purpose EfficientDet architecture. The choice between them depends on the specific use case and requirements of the project.

State-of-the-art 2D and 3D Face Analysis Project

Pros of insightface

  • More comprehensive face analysis toolkit, including recognition, detection, and alignment
  • Supports multiple deep learning frameworks (MXNet, PyTorch, TensorFlow)
  • Provides pre-trained models for various tasks and datasets

Cons of insightface

  • Steeper learning curve due to its broader scope and multiple framework support
  • May be overkill for projects focused solely on face detection

Code Comparison

insightface:

from insightface.app import FaceAnalysis
app = FaceAnalysis()
app.prepare(ctx_id=0, det_size=(640, 640))
img = cv2.imread('sample.jpg')
faces = app.get(img)

Pytorch_Retinaface:

from retinaface import RetinaFace
img = cv2.imread('sample.jpg')
faces = RetinaFace.detect_faces(img)

insightface offers a more comprehensive solution with additional features, while Pytorch_Retinaface provides a simpler, more focused approach to face detection. The choice between the two depends on the specific requirements of your project, such as the need for additional face analysis tasks or the preference for a particular deep learning framework.

💎1MB lightweight face detection model (1MB轻量级人脸检测模型)

Pros of Ultra-Light-Fast-Generic-Face-Detector-1MB

  • Extremely lightweight (1MB model size) for efficient deployment on mobile devices
  • Supports multiple deep learning frameworks (PyTorch, TensorFlow, MNN, NCNN)
  • Faster inference speed, suitable for real-time applications

Cons of Ultra-Light-Fast-Generic-Face-Detector-1MB

  • Lower accuracy compared to Pytorch_Retinaface, especially for small faces
  • Limited to face detection only, while Pytorch_Retinaface includes facial landmark detection
  • Less extensive documentation and community support

Code Comparison

Ultra-Light-Fast-Generic-Face-Detector-1MB:

from vision.ssd.config.fd_config import define_img_size
input_size = 320
define_img_size(input_size)
from vision.ssd.mb_tiny_fd import create_mb_tiny_fd
net = create_mb_tiny_fd(2, is_test=True)

Pytorch_Retinaface:

from models.retinaface import RetinaFace
cfg = cfg_mnet
net = RetinaFace(cfg=cfg, phase='test')
net = load_model(net, args.trained_model, args.cpu)
net.eval()

Both repositories provide face detection functionality, but Ultra-Light-Fast-Generic-Face-Detector-1MB focuses on lightweight models for mobile deployment, while Pytorch_Retinaface offers higher accuracy and additional features like facial landmark detection. The code snippets demonstrate the different approaches to model initialization and configuration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

RetinaFace in PyTorch

A PyTorch implementation of RetinaFace: Single-stage Dense Face Localisation in the Wild. Model size only 1.7M, when Retinaface use mobilenet0.25 as backbone net. We also provide resnet50 as backbone net to get better result. The official code in Mxnet can be found here.

Mobile or Edge device deploy

We also provide a set of Face Detector for edge device in here from python training to C++ inference.

WiderFace Val Performance in single scale When using Resnet50 as backbone net.

Styleeasymediumhard
Pytorch (same parameter with Mxnet)94.82 %93.84%89.60%
Pytorch (original image scale)95.48%94.04%84.43%
Mxnet94.86%93.87%88.33%
Mxnet(original image scale)94.97%93.89%82.27%

WiderFace Val Performance in single scale When using Mobilenet0.25 as backbone net.

Styleeasymediumhard
Pytorch (same parameter with Mxnet)88.67%87.09%80.99%
Pytorch (original image scale)90.70%88.16%73.82%
Mxnet88.72%86.97%79.19%
Mxnet(original image scale)89.58%87.11%69.12%

FDDB Performance.

FDDB(pytorch)performance
Mobilenet0.2598.64%
Resnet5099.22%

Contents

Installation

Clone and install
  1. git clone https://github.com/biubug6/Pytorch_Retinaface.git

  2. Pytorch version 1.1.0+ and torchvision 0.3.0+ are needed.

  3. Codes are based on Python 3

Data
  1. Download the WIDERFACE dataset.

  2. Download annotations (face bounding boxes & five facial landmarks) from baidu cloud or dropbox

  3. Organise the dataset directory as follows:

  ./data/widerface/
    train/
      images/
      label.txt
    val/
      images/
      wider_val.txt

ps: wider_val.txt only include val file names but not label information.

Data1

We also provide the organized dataset we used as in the above directory structure.

Link: from google cloud or baidu cloud Password: ruck

Training

We provide restnet50 and mobilenet0.25 as backbone network to train model. We trained Mobilenet0.25 on imagenet dataset and get 46.58% in top 1. If you do not wish to train the model, we also provide trained model. Pretrain model and trained model are put in google cloud and baidu cloud Password: fstq . The model could be put as follows:

  ./weights/
      mobilenet0.25_Final.pth
      mobilenetV1X0.25_pretrain.tar
      Resnet50_Final.pth
  1. Before training, you can check network configuration (e.g. batch_size, min_sizes and steps etc..) in data/config.py and train.py.

  2. Train the model using WIDER FACE:

CUDA_VISIBLE_DEVICES=0,1,2,3 python train.py --network resnet50 or
CUDA_VISIBLE_DEVICES=0 python train.py --network mobile0.25

Evaluation

Evaluation widerface val

  1. Generate txt file
python test_widerface.py --trained_model weight_file --network mobile0.25 or resnet50
  1. Evaluate txt results. Demo come from Here
cd ./widerface_evaluate
python setup.py build_ext --inplace
python evaluation.py
  1. You can also use widerface official Matlab evaluate demo in Here

Evaluation FDDB

  1. Download the images FDDB to:
./data/FDDB/images/
  1. Evaluate the trained model using:
python test_fddb.py --trained_model weight_file --network mobile0.25 or resnet50
  1. Download eval_tool to evaluate the performance.

TensorRT

-TensorRT

References

@inproceedings{deng2019retinaface,
title={RetinaFace: Single-stage Dense Face Localisation in the Wild},
author={Deng, Jiankang and Guo, Jia and Yuxiang, Zhou and Jinke Yu and Irene Kotsia and Zafeiriou, Stefanos},
booktitle={arxiv},
year={2019}