{"id":13435653,"url":"https://github.com/peteryuX/arcface-tf2","last_synced_at":"2025-03-18T11:31:48.756Z","repository":{"id":39739674,"uuid":"204101634","full_name":"peteryuX/arcface-tf2","owner":"peteryuX","description":"ArcFace unofficial Implemented in Tensorflow 2.0+ (ResNet50, MobileNetV2). \"ArcFace: Additive Angular Margin Loss for Deep Face Recognition\" Published in CVPR 2019. With Colab.","archived":false,"fork":false,"pushed_at":"2022-11-21T21:17:05.000Z","size":115,"stargazers_count":261,"open_issues_count":22,"forks_count":60,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-27T18:58:23.774Z","etag":null,"topics":["arcface","arcface-tf2","colab","colab-notebook","deep-face-recognition","face-recognition","tensorflow","tf2"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peteryuX.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-24T03:11:22.000Z","updated_at":"2024-10-25T09:19:56.000Z","dependencies_parsed_at":"2022-08-24T10:20:53.445Z","dependency_job_id":null,"html_url":"https://github.com/peteryuX/arcface-tf2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteryuX%2Farcface-tf2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteryuX%2Farcface-tf2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteryuX%2Farcface-tf2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peteryuX%2Farcface-tf2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peteryuX","download_url":"https://codeload.github.com/peteryuX/arcface-tf2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244211083,"owners_count":20416580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arcface","arcface-tf2","colab","colab-notebook","deep-face-recognition","face-recognition","tensorflow","tf2"],"created_at":"2024-07-31T03:00:37.785Z","updated_at":"2025-03-18T11:31:43.748Z","avatar_url":"https://github.com/peteryuX.png","language":"Python","funding_links":[],"categories":["Sample Codes / Projects \u003ca name=\"sample\" /\u003e ⛏️📐📁"],"sub_categories":["Reinforcement Learning \u003ca name=\"RL\" /\u003e🔮"],"readme":"# [arcface-tf2](https://github.com/peteryuX/arcface-tf2)\n\n[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/peteryuX/arcface-tf2.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/peteryuX/arcface-tf2/context:python)\n![Star](https://img.shields.io/github/stars/peteryuX/arcface-tf2)\n![Fork](https://img.shields.io/github/forks/peteryuX/arcface-tf2)\n![License](https://img.shields.io/github/license/peteryuX/arcface-tf2)\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/peteryuX/arcface-tf2/blob/master/notebooks/colab-github-demo.ipynb)\n\n:fire: ArcFace (Additive Angular Margin Loss for Deep Face Recognition, published in CVPR 2019) implemented in Tensorflow 2.0+. This is an unofficial implementation. :fire:\n\n\u003e  Additive Angular Margin Loss(ArcFace) has a clear geometric interpretation due to the exact correspondence to the geodesic distance on the hypersphere, and consistently outperforms the state-of-the-art and can be easily implemented with negligible computational overhead.\n\nOriginal Paper: \u0026nbsp; [Arxiv](https://arxiv.org/abs/1801.07698) \u0026nbsp; [CVPR2019](http://openaccess.thecvf.com/content_CVPR_2019/html/Deng_ArcFace_Additive_Angular_Margin_Loss_for_Deep_Face_Recognition_CVPR_2019_paper.html)\n\nOffical Implementation: \u0026nbsp; [MXNet](https://github.com/deepinsight/insightface)\n\n\u003cimg src=\"photo/architecture.JPG\"\u003e\n\n****\n\n## Contents\n:bookmark_tabs:\n\n* [Installation](#Installation)\n* [Data Preparing](#Data-Preparing)\n* [Training and Testing](#Training-and-Testing)\n* [Benchmark and Models](#Benchmark-and-Models)\n* [References](#References)\n\n## Installation\n:pizza:\n\nCreate a new python virtual environment by [Anaconda](https://www.anaconda.com/) or just use pip in your python environment and then clone this repository as following.\n\n### Clone this repo\n```bash\ngit clone https://github.com/peteryuX/arcface-tf2.git\ncd arcface-tf2\n```\n\n### Conda\n```bash\nconda env create -f environment.yml\nconda activate arcface-tf2\n```\n\n### Pip\n\n```bash\npip install -r requirements.txt\n```\n\n****\n\n## Data Preparing\n:beer:\n\nAll datasets used in this repository can be found from [face.evoLVe.PyTorch's Data-Zoo](https://github.com/ZhaoJ9014/face.evoLVe.PyTorch#Data-Zoo).\n\nNote:\n\n- Both training and testing dataset are \"Align_112x112\" version.\n\n### Training Dataset\n\nDownload [MS-Celeb-1M](https://drive.google.com/file/d/1X202mvYe5tiXFhOx82z4rPiPogXD435i/view?usp=sharing) datasets, then extract and convert them to tfrecord as traning data as following.\n\n```bash\n# Binary Image: convert really slow, but loading faster when traning.\npython data/convert_train_binary_tfrecord.py --dataset_path=\"/path/to/ms1m_align_112/imgs\" --output_path=\"./data/ms1m_bin.tfrecord\"\n\n# Online Image Loading: convert really fast, but loading slower when training.\npython data/convert_train_tfrecord.py --dataset_path=\"/path/to/ms1m_align_112/imgs\" --output_path=\"./data/ms1m.tfrecord\"\n```\n\nNote:\n- You can run `python ./dataset_checker.py` to check if the dataloader work.\n\n### Testing Dataset\n\nDownload [LFW](https://drive.google.com/file/d/1WO5Meh_yAau00Gm2Rz2Pc0SRldLQYigT/view?usp=sharing), [Aged30](https://drive.google.com/file/d/1AoZrZfym5ZhdTyKSxD0qxa7Xrp2Q1ftp/view?usp=sharing) and [CFP-FP](https://drive.google.com/file/d/1-sDn79lTegXRNhFuRnIRsgdU88cBfW6V/view?usp=sharing) datasets, then extract them to `/your/path/to/test_dataset`. These testing data are already binary files, so it's not necessary to do any preprocessing. The directory structure should be like bellow.\n```\n/your/path/to/test_dataset/\n    -\u003e lfw_align_112/lfw\n        -\u003e data/\n        -\u003e meta/\n        -\u003e ...\n    -\u003e agedb_align_112/agedb_30\n        -\u003e ...\n    -\u003e cfp_align_112/cfp_fp\n        -\u003e ...\n```\n\n****\n\n## Training and Testing\n:lollipop:\n\nYou can modify your own dataset path or other settings of model in [./configs/*.yaml](https://github.com/peteryuX/arcface-tf2/tree/master/configs) for training and testing, which like below.\n\n```python\n# general (shared both in training and testing)\nbatch_size: 128\ninput_size: 112\nembd_shape: 512\nsub_name: 'arc_res50'\nbackbone_type: 'ResNet50' # or 'MobileNetV2'\nhead_type: ArcHead # or 'NormHead': FC to targets.\nis_ccrop: False # central-cropping or not\n\n# train\ntrain_dataset: './data/ms1m_bin.tfrecord' # or './data/ms1m.tfrecord'\nbinary_img: True # False if dataset is online decoding\nnum_classes: 85742\nnum_samples: 5822653\nepochs: 5\nbase_lr: 0.01\nw_decay: !!float 5e-4\nsave_steps: 1000\n\n# test\ntest_dataset: '/your/path/to/test_dataset'\n```\n\nNote:\n- The `sub_name` is the name of outputs directory used in checkpoints and logs folder. (make sure of setting it unique to other models)\n- The `head_type` is used to choose [ArcFace](https://arxiv.org/abs/1801.07698) head or normal fully connected layer head for classification in training. (see more detail in [./modules/models.py](https://github.com/peteryuX/arcface-tf2/blob/master/modules/models.py#L90-L94))\n- The `is_ccrop` means doing central-cropping on both trainging and testing data or not.\n- The `binary_img` is used to choose the type of training data, which should be according to the data type you created in the [Data-Preparing](#Data-Preparing).\n\n\n### Training\n\nHere have two modes for training your model, which should be perform the same results at the end.\n```bash\n# traning with tf.GradientTape(), great for debugging.\npython train.py --mode=\"eager_tf\" --cfg_path=\"./configs/arc_res50.yaml\"\n\n# training with model.fit().\npython train.py --mode=\"fit\" --cfg_path=\"./configs/arc_res50.yaml\"\n```\n\n### Testing\n\nYou can download my trained models for testing from [Benchmark and Models](#Benchmark-and-Models) without training it yourself. And, evaluate the models you got with the corresponding cfg file on the testing dataset. The testing code in [./modules/evaluations.py](https://github.com/peteryuX/arcface-tf2/blob/master/modules/evaluations.py) were modified from [face.evoLVe](https://github.com/ZhaoJ9014/face.evoLVe.PyTorch).\n\n```bash\npython test.py --cfg_path=\"./configs/arc_res50.yaml\"\n```\n\n### Encode Input Image\n\nYou can also encode image into latent vector by model. For example, encode the image from [./data/BruceLee.jpg](https://github.com/peteryuX/arcface-tf2/blob/master/data/BruceLee.jpg) and save to `./output_embeds.npy` as following.\n\n```bash\npython test.py --cfg_path=\"./configs/arc_res50.yaml\" --img_path=\"./data/BruceLee.jpg\"\n```\n\n****\n\n## Benchmark and Models\n:coffee:\n\nVerification results (%) of different backbone, head tpye, data augmentation and loss function.\n\n| Backbone | Head | Loss | CCrop | LFW | AgeDB-30 | CFP-FP | Download Link |\n|----------|------|------|-------|-----|--------|----------|---------------|\n| [ResNet50](https://arxiv.org/abs/1512.03385) | [ArcFace](https://arxiv.org/abs/1801.07698) | Softmax | False | 99.42 | 95.32  |  92.56   | [GoogleDrive](https://drive.google.com/file/d/1HasWQb86s4xSYy36YbmhRELg9LBmvhvt/view?usp=sharing) |\n| [MobileNetV2](https://arxiv.org/abs/1801.04381) | [ArcFace](https://arxiv.org/abs/1801.07698) | Softmax | False | 99.13 | 91.62  |  91.50   | [GoogleDrive](https://drive.google.com/file/d/1qG8BChcPHzKuGwjJhrpeIxBqQmhpLvTX/view?usp=sharing) |\n| [ResNet50](https://arxiv.org/abs/1512.03385) | [ArcFace](https://arxiv.org/abs/1801.07698) | Softmax | True | 99.38 | 95.13 | 94.87 | [GoogleDrive](https://drive.google.com/file/d/1zUulC-4hSY_kPqZpcoIHO96OmjMivuKB/view?usp=sharing) |\n| [MobileNetV2](https://arxiv.org/abs/1801.04381) | [ArcFace](https://arxiv.org/abs/1801.07698) | Softmax | True | 98.88 | 91.58 | 93.19 | [GoogleDrive](https://drive.google.com/file/d/1nSnIc0eV0MkSjg48x29PJwTt3fGXKDU4/view?usp=sharing) |\n\nNote:\n- The 'CCrop' tag above means doing central-cropping on both trainging and testing data, which could eliminate the redundant boundary of intput face data (especially for AgeDB-30).\n- All training settings of the models can be found in the corresponding [./configs/*.yaml](https://github.com/peteryuX/arcface-tf2/tree/master/configs) files.\n- **Based on the property of the training dataset, all the pre-trained models can only be used for non-commercial applications.**\n\n****\n\n## References\n:hamburger:\n\nThanks for these source codes porviding me with knowledges to complete this repository.\n\n- https://github.com/deepinsight/insightface/tree/master/recognition (Official)\n    - Face Analysis Project on MXNet http://insightface.ai\n- https://github.com/zzh8829/yolov3-tf2\n    - YoloV3 Implemented in TensorFlow 2.0\n- https://github.com/ZhaoJ9014/face.evoLVe.PyTorch\n    - face.evoLVe: High-Performance Face Recognition Library based on PyTorch\n- https://github.com/luckycallor/InsightFace-tensorflow\n    - Tensoflow implementation of InsightFace (ArcFace: Additive Angular Margin Loss for Deep Face Recognition).\n- https://github.com/dmonterom/face_recognition_TF2\n    - Training a face Recognizer using ResNet50 + ArcFace in TensorFlow 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FpeteryuX%2Farcface-tf2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FpeteryuX%2Farcface-tf2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FpeteryuX%2Farcface-tf2/lists"}