AdaFace
No description available
Install / Use
/learn @mk-minchul/AdaFaceREADME
AdaFace: Quality Adaptive Margin for Face Recognition
Official github repository for AdaFace: Quality Adaptive Margin for Face Recognition. The paper (https://arxiv.org/abs/2204.00964) is presented in CVPR 2022 (Oral).
Abstract: Recognition in low quality face datasets is challenging because facial attributes are obscured and degraded. Advances in margin-based loss functions have resulted in enhanced discriminability of faces in the embedding space. Further, previous studies have studied the effect of adaptive losses to assign more importance to misclassified (hard) examples. In this work, we introduce another aspect of adaptiveness in the loss function, namely the image quality. We argue that the strategy to emphasize misclassified samples should be adjusted according to their image quality. Specifically, the relative importance of easy and hard samples should be based on the sample's image quality. We propose a new loss function that emphasizes samples of different difficulty based on their image quality. Our method achieves this in the form of an adaptive margin function by approximating the image quality with feature norms. Extensive experiments show that our method, AdaFace, improves the face recognition performance over the state-of-the-art (SoTA) on four datasets (IJB-B, IJB-C, IJB-S and TinyFace).
@inproceedings{kim2022adaface,
title={AdaFace: Quality Adaptive Margin for Face Recognition},
author={Kim, Minchul and Jain, Anil K and Liu, Xiaoming},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2022}
}
<img src="assets/main_figure.png"/>
Repository Updates
- [x] CVLFace is the new official repository for AdaFace (supports various architectures such as ViT, SWIN-ViT, KP-RPE, etc).
- [x] Added PartialFC AdaFace implementation at CVLFace (and much more functionalities related to face recognition for conducting research).
- [x] Pytorch Lightning 1.8 compatibility is tested.
- [x] 5 Minute video presentation uploaded.
- [x] Added the option to directly train with InsightFace dataset (train.rec) files without extracting images.
News
- You can also check out our new paper
KP-RPE: KeyPoint Relative Position Encoding for Face RecognitionPaper Video Code for facial landmark assisted face recognition. TLDR: Face recognition with facial landmark for alignment robustness. (SoTA in TinyFace, IJB-S). - You can also check out our new paper
Cluster and Aggregate (CAFace, NeurIPS2022)Link for video based face recognition. TLDR: Face recognition with long probe videos.
5 Minute Video Presentation
- https://www.youtube.com/watch?v=NfHzn6epAHM
- The talk was given during the CVPR 2022 Conference. Thank you all who showed interest in the paper during the oral and poster session.
Demo Comparison between AdaFace and ArcFace on Low Quality Images
The demo shows a comparison between AdaFace and ArcFace on a live video.
To show how model performs with low quality images, we show original, blur+ and blur++ setting where
blur++ means it is heavily blurred.
The numbers with colorbox show the cosine similarity between the live image and the cloest matching gallery image.
The statistics on the bottom show the cumulative count of true positive match for blur++ setting.
AdaFace has high true positive rate.
It also shows it is less prone to making false positive (red) mistakes as sometimes observed in ArcFace.
Usage
import torch
from head import AdaFace
# typical inputs with 512 dimension
B = 5
embbedings = torch.randn((B, 512)).float() # latent code
norms = torch.norm(embbedings, 2, -1, keepdim=True)
normalized_embedding = embbedings / norms
labels = torch.randint(70722, (B,))
# instantiate AdaFace
adaface = AdaFace(embedding_size=512,
classnum=70722,
m=0.4,
h=0.333,
s=64.,
t_alpha=0.01,)
# calculate loss
cosine_with_margin = adaface(normalized_embedding, norms, labels)
loss = torch.nn.CrossEntropyLoss()(cosine_with_margin, labels)
Installation
conda create --name adaface pytorch==1.8.0 torchvision==0.9.0 cudatoolkit=10.2 -c pytorch
conda activate adaface
conda install scikit-image matplotlib pandas scikit-learn
pip install -r requirements.txt
Train (Preapring Dataset and Training Scripts)
- Please refer to README_TRAIN.md
- [IMPORTANT] Note that our implementation assumes that input to the model is
BGRcolor channel as incv2package. InsightFace models assumeRGBcolor channel as inPILpackage. So all our evaluation code usesBGRcolor channel withcv2package.
Pretrained Models
Note that our pretrained model takes the input in BGR color channel. This is different from the InsightFace released model which uses RGB color channel.
| Arch | Dataset | Link | |------|------------|----------------------------------------------------------------------------------------------| | R18 | CASIA-WebFace | gdrive | | R18 | VGGFace2 | gdrive | | R18 | WebFace4M | gdrive | | R50 | CASIA-WebFace | gdrive | | R50 | WebFace4M | gdrive | | R50 | MS1MV2 | gdrive | | R100 | MS1MV2 | gdrive | | R100 | MS1MV3 | gdrive | | R100 | WebFace4M | gdrive | | R100 | WebFace12M | gdrive |
Inferece
Example using provided sample images
AdaFace takes input images that are preproccsed. The preprocessing step involves
- aligned with facial landmark (using MTCNN) and
- cropped to 112x112x3 size whose color channel is BGR order.
We provide the code for performing the preprocessing step. For using pretrained AdaFace model for inference,
-
Download the pretrained adaface model and place it in
pretrained/ -
For using pretrained AdaFace on below 3 images, run
python inference.py
| img1 | img2 | img3 | |:--------------------------------------------------------------:|:--------------------------------------------------------------:|---------------------------------------------------------------:| | <img src="face_alignment/test_images/img1.jpeg" width="215" /> | <img src="face_alignment/test_images/img2.jpeg" width="130" /> | <img src="face_alignment/test_images/img3.jpeg" width="191" /> |
The similarity score result should be
tensor([[ 1.0000, 0.7334, -0.0655],
[ 0.7334, 1.0000, -0.0277],
[-0.0655, -0.0277, 1.0000]], grad_fn=<MmBackward0>)
General Inference Guideline
In a nutshell, inference code looks as below.
from face_alignment import align
from inference import load_pretrained_model, to_input
model = load_pretrained_model('ir_50')
path = 'path_to_the_image'
aligned_rgb_img = align.get_aligned_face(path)
bgr_input = to_input(aligned_rgb_img)
feature, _ = model(bgr_input)
- Note that AdaFace model is a vanilla pytorch model which takes in
bgr_inputwhich is 112x112x3 torch tensor with BGR color channel whose value is normalized withmean=0.5andstd=0.5, as in [to_input()](h
