A PyTorch Implementation of SphereFace.

Overview

SphereFace

A PyTorch Implementation of SphereFace. The code can be trained on CASIA-Webface and the best accuracy on LFW is 99.22%.

SphereFace: Deep Hypersphere Embedding for Face Recognition

Train

python train.py

Test

# lfw.tgz to lfw.zip
tar zxf lfw.tgz; cd lfw; zip -r ../lfw.zip *; cd ..

# lfw evaluation
python lfw_eval.py --model model/sphere20a_20171020.pth

Pre-trained models

Model name LFW accuracy Training dataset
20171020 0.9922 CASIA-WebFace

φ

equation

equation

phi

References

sphereface

Comments
  • predicts[train] Error

    predicts[train] Error

    hey there, when i tried to run lfw_eval.py, i set my path to lfw dataset. then i got an error like this:

    Traceback (most recent call last): File "lfw_eval.py", line 128, in best_thresh = find_best_threshold(thresholds, predicts[train]) IndexError: too many indices for array

    is there any idea for this issue? may be sth wrong in channels which cause many arrays cant be matched well when inputing ? i got raw dataset of LFW(downloaded from websites) with ur landmark txt.

    opened by lucaskyle 6
  • errors

    errors

    parser.add_argument('--dataset', default='../../dataset/face/casia/casia.zip', type=str) Place the original casia.zip in the above place,and the rest of the region has not changed. There are still some errors during run time:

    File "/home/***/PycharmProjects/sphereface/dataset.py", line 39, in dataset_handle data = callback(name,filename,pindex,cacheobj,zfile) File "/home/***/PycharmProjects/sphereface/train.py", line 56, in dataset_load data = np.frombuffer(zfile.read(nameinzip),np.uint8) File "/home/***/anaconda3/lib/python3.5/zipfile.py", line 1232, in read with self.open(name, "r", pwd) as fp: File "/home/***/anaconda3/lib/python3.5/zipfile.py", line 1255, in open zinfo = self.getinfo(name) File "/home/***/anaconda3/lib/python3.5/zipfile.py", line 1199, in getinfo 'There is no item named %r in the archive' % name) KeyError: "There is no item named '0348347/018.jpg' in the archive"

    opened by Hucley 4
  • About your implementation

    About your implementation

    Hi, thanks for your wonderful code! I have a little problem about your implementation. When you normalize w martrix, I notice you used ww = w.renorm(2,1,1e-5).mul(1e5). I think a natural implementation is just ww = F.normalize(w, dim=0). Do you have some special reasons for that? Thank you very much!!!

    opened by HuangZiliAndy 3
  • Accuracy problem

    Accuracy problem

    I used my own train.py to train the model which has same data processing and same parameters. But my test result is 98.38% using clcarwin's lfw_eval.py in Python2.7 and Pytorch0.4.0. I don't know why my result is lower than clcarwin's. Does anybody can solve my problem?Thanks.

    opened by wyq0227 2
  • stuck during training

    stuck during training

    Thanks for you codes.It really helpful. I use cleaned CASIA-WebFace, and have 455594 photos.After one epoch train,it stuck at 04:26:07 Te=0 Loss=8.7917 | AccT=2.0000% (12200/454469) 8.2977 16.70 888

    when i use interrupt ,the program stop at '' File "train.py", line 142, in train(epoch,args) File "train.py", line 103, in train img,label = ds.get() File "/sphereface/dataset.py", line 113, in get index,imageshape,labelshape,pathlist = self.result.get()''

    By the way,it always remain ''raise BadZipFile("Bad magic number for file header")zipfile.BadZipFile: Bad magic number for file header'' and ''zipfile.BadZipFile: File name in directory 'CASIA-WebFace/1813440/030.jpg' and header b'CASIA-WebFace/0110168/158.jpg' differ.'' is there something wrong? How to solve this issue?Thank you.

    opened by fluctlux 2
  • when CrossEntropyLoss loss=nan

    when CrossEntropyLoss loss=nan

    when I use

        loss = nn.CrossEntropyLoss()(output, target) 
    

    replace the code in AngleLoss

        logpt = F.log_softmax(output)
        logpt = logpt.gather(1,target)
        logpt = logpt.view(-1)
        pt = Variable(logpt.data.exp())
        loss = -1 * (1-pt)**self.gamma * logpt
        loss = loss.mean()
    

    I got loss=nan

    I think theoretically logpt = F.log_softmax(output) here should be logpt = F.log_softmax(output,1) which is just the same as CrossEntropyLoss, but you implement is logpt = F.log_softmax(output,0) Would you help explain it?

    opened by JonesonZheng 2
  • The annealing optimization strategy for A-Softmax loss

    The annealing optimization strategy for A-Softmax loss

    Thanks for your nice repo. I'm trying to your codes. My question is the paper said about the annealing optimization strategy for A-Softmax loss with introducing lambda. here, your implementation is

    self.lamb = max(self.LambdaMin,self.LambdaMax/(1+0.1*self.it ))
    output = cos_theta * 1.0
    output[index] -= cos_theta[index]*(1.0+0)/(1+self.lamb)
    output[index] += phi_theta[index]*(1.0+0)/(1+self.lamb)
    

    but, i think the cos term is to be scaled by a factor of lambda such that

    output = cos_theta * self.lamb
    output[index] -= cos_theta[index]*(self.lamb)/(1+self.lamb)
    output[index] += phi_theta[index]*(1.0)/(1+self.lamb)
    

    Please, give me your idea Thanks

    opened by taey16 2
  • gamma

    gamma

    Thanks for your code! but the code: loss = -1 * (1-pt)**self.gamma * logpt in the Angleloss,I really did not get it,because,the default gamma is 0,and the term: (1-pt)**self.gamma is useless,it is equal to: loss = -1 * logpt my question is , is 0 is your default gamma value?

    opened by zhly0 2
  • Can you upload your lfw

    Can you upload your lfw

    Thanks for your code,this is really nice work,but I do not have matlab,can you upload your pocessed lfw image?really appreciate it if you can upload it!

    opened by zhly0 2
  • RE: Facial landmark text file

    RE: Facial landmark text file

    I'm trying to use my own dataset but I have trouble generating facial landmarks like you created for Casia dataset. What did you use (opencv, dlib, or some other module) to get those facial landmarks?

    opened by Phillip1982 1
  • 'float' object cannot be interpreted as an integer

    'float' object cannot be interpreted as an integer

    Anyone solve same problem?

    File "train.py", line 142, in train(epoch,args) File "train.py", line 101, in train bs=args.bs,shuffle=True,nthread=6,imagesize=128) File "/DL/pytorch/sphereface_pytorch/dataset.py", line 60, in init self.nparrimage = np.frombuffer(self.arrimage.get_obj(),np.float32).reshape(10,len(self.arrimage)/10) TypeError: 'float' object cannot be interpreted as an integer

    opened by leadcain84 1
  • Improved logic for iterating over 'pairs.txt'

    Improved logic for iterating over 'pairs.txt'

    closes #62 closes #63

    Instead of loading the entire file into the memory, use the iterator object natively provided by the file object returned by the open function.

    • This saves memory.
    • Avoid __getitem__ call, improve performance.
    • More pythonic.
    opened by sayandipdutta 0
  • Unnecessarily loading file contents of `pairs.txt` into memory `lfw_eval.py`

    Unnecessarily loading file contents of `pairs.txt` into memory `lfw_eval.py`

    In lfw_eval.py, the contents of pairs.txt is being loaded into memory, even though it is not used for anything other than iterating over the lines. It will be easier and more memory efficient to iterate over the file object itself.

    with open('data/pairs.txt') as f:
        pairs_lines = f.readlines()[1:]
    

    Which will also avoid the __getitem__ call in line 91, making the code more efficient.

    for i in range(6000):
        p = pairs_lines[i].replace('\n','').split('\t')
    
    opened by sayandipdutta 0
  • Can't load the pretrained model

    Can't load the pretrained model

    Hi, I am testing the code under the environment of: pytorch == 1.9.0 torchvision == 0.10.0 python == 3.6.5

    When I try torch.load(args.model) it returns an error:

    File "/usr/local/cuda/lib/python3.6/site-packages/torch/serialization.py", line 777, in _legacy_load
        magic_number = pickle_module.load(f, **pickle_load_args)
    _pickle.UnpicklingError: invalid load key, '7'.
    

    Could you help me with this? Thanks!

    opened by lychenyoko 1
  • Is the task in lfw_eval.py exactly the face recognition?

    Is the task in lfw_eval.py exactly the face recognition?

    Thanks for your great work and kind sharing.

    I am a beginner in the face area of CV. And I get confused in the evaluation process. According to the lfw_eval.py,https://github.com/clcarwin/sphereface_pytorch/blob/e2f11f0b0f394cf75b9f65ce8d353e4390b0948c/lfw_eval.py#L102-L103 it loads 2 face images, and the model's output is to predict is these 2 images from a single identity.

    I did a lot of reading and searching, but cannot sure about is the task in lfw_eval.py exactly the face recognition?

    opened by rrryan2016 1
  • Why the classnum is 10574 instead of 10575

    Why the classnum is 10574 instead of 10575

    The CASIA has 10575 different classes. I wonder:

    1. why your model only has 10574 classes?
    2. which label did you skipped during training.

    Thank you very much.

    opened by njuaplusplus 2
Owner
carwin
carwin
An essential implementation of BYOL in PyTorch + PyTorch Lightning

Essential BYOL A simple and complete implementation of Bootstrap your own latent: A new approach to self-supervised Learning in PyTorch + PyTorch Ligh

Enrico Fini 48 Sep 27, 2022
RealFormer-Pytorch Implementation of RealFormer using pytorch

RealFormer-Pytorch Implementation of RealFormer using pytorch. Includes comparison with classical Transformer on image classification task (ViT) wrt C

Simo Ryu 90 Dec 8, 2022
A PyTorch implementation of the paper Mixup: Beyond Empirical Risk Minimization in PyTorch

Mixup: Beyond Empirical Risk Minimization in PyTorch This is an unofficial PyTorch implementation of mixup: Beyond Empirical Risk Minimization. The co

Harry Yang 121 Dec 17, 2022
A pytorch implementation of Pytorch-Sketch-RNN

Pytorch-Sketch-RNN A pytorch implementation of https://arxiv.org/abs/1704.03477 In order to draw other things than cats, you will find more drawing da

Alexis David Jacq 172 Dec 12, 2022
PyTorch implementation of Advantage async actor-critic Algorithms (A3C) in PyTorch

Advantage async actor-critic Algorithms (A3C) in PyTorch @inproceedings{mnih2016asynchronous, title={Asynchronous methods for deep reinforcement lea

LEI TAI 111 Dec 8, 2022
Pytorch-diffusion - A basic PyTorch implementation of 'Denoising Diffusion Probabilistic Models'

PyTorch implementation of 'Denoising Diffusion Probabilistic Models' This reposi

Arthur Juliani 76 Jan 7, 2023
Fang Zhonghao 13 Nov 19, 2022
RETRO-pytorch - Implementation of RETRO, Deepmind's Retrieval based Attention net, in Pytorch

RETRO - Pytorch (wip) Implementation of RETRO, Deepmind's Retrieval based Attent

Phil Wang 556 Jan 4, 2023
HashNeRF-pytorch - Pure PyTorch Implementation of NVIDIA paper on Instant Training of Neural Graphics primitives

HashNeRF-pytorch Instant-NGP recently introduced a Multi-resolution Hash Encodin

Yash Sanjay Bhalgat 616 Jan 6, 2023
Generic template to bootstrap your PyTorch project with PyTorch Lightning, Hydra, W&B, and DVC.

NN Template Generic template to bootstrap your PyTorch project. Click on Use this Template and avoid writing boilerplate code for: PyTorch Lightning,

Luca Moschella 520 Dec 30, 2022
A PyTorch Extension: Tools for easy mixed precision and distributed training in Pytorch

This repository holds NVIDIA-maintained utilities to streamline mixed precision and distributed training in Pytorch. Some of the code here will be included in upstream Pytorch eventually. The intention of Apex is to make up-to-date utilities available to users as quickly as possible.

NVIDIA Corporation 6.9k Jan 3, 2023
Objective of the repository is to learn and build machine learning models using Pytorch. 30DaysofML Using Pytorch

30 Days Of Machine Learning Using Pytorch Objective of the repository is to learn and build machine learning models using Pytorch. List of Algorithms

Mayur 119 Nov 24, 2022
Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pytorch Lightning 1.4k Jan 1, 2023
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 360 Dec 10, 2022
The Incredible PyTorch: a curated list of tutorials, papers, projects, communities and more relating to PyTorch.

This is a curated list of tutorials, projects, libraries, videos, papers, books and anything related to the incredible PyTorch. Feel free to make a pu

Ritchie Ng 9.2k Jan 2, 2023
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 359 Jan 5, 2023
A bunch of random PyTorch models using PyTorch's C++ frontend

PyTorch Deep Learning Models using the C++ frontend Gettting started Clone the repo 1. https://github.com/mrdvince/pytorchcpp 2. cd fashionmnist or

Vince 0 Jul 13, 2021
PyTorch Autoencoders - Implementing a Variational Autoencoder (VAE) Series in Pytorch.

PyTorch Autoencoders Implementing a Variational Autoencoder (VAE) Series in Pytorch. Inspired by this repository Model List check model paper conferen

Subin An 8 Nov 21, 2022
PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices.

PyTorch-LIT PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices. With

Amin Rezaei 157 Dec 11, 2022