You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # Contents
  2. - [RetinaFace Description](#retinaface-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Pretrain Model](#pretrain-model)
  5. - [Dataset](#dataset)
  6. - [Environment Requirements](#environment-requirements)
  7. - [Quick Start](#quick-start)
  8. - [Script Description](#script-description)
  9. - [Script and Sample Code](#script-and-sample-code)
  10. - [Script Parameters](#script-parameters)
  11. - [Training Process](#training-process)
  12. - [Training](#training)
  13. - [Distributed Training](#distributed-training)
  14. - [Evaluation Process](#evaluation-process)
  15. - [Evaluation](#evaluation)
  16. - [Model Description](#model-description)
  17. - [Performance](#performance)
  18. - [Evaluation Performance](#evaluation-performance)
  19. - [How to use](#how-to-use)
  20. - [Continue Training on the Pretrained Model](#continue-training-on-the-pretrained-model)
  21. - [Description of Random Situation](#description-of-random-situation)
  22. - [ModelZoo Homepage](#modelzoo-homepage)
  23. # [RetinaFace Description](#contents)
  24. Retinaface is a face detection model, which was proposed in 2019 and achieved the best results on the wideface dataset at that time. Retinaface, the full name of the paper is retinaface: single stage dense face localization in the wild. Compared with s3fd and mtcnn, it has a significant improvement, and has a higher recall rate for small faces. It is not good for multi-scale face detection. In order to solve these problems, retinaface feature pyramid structure is used for feature fusion between different scales, and SSH module is added.
  25. [Paper](https://arxiv.org/abs/1905.00641v2): Jiankang Deng, Jia Guo, Yuxiang Zhou, Jinke Yu, Irene Kotsia, Stefanos Zafeiriou. "RetinaFace: Single-stage Dense Face Localisation in the Wild". 2019.
  26. # [Pretrain Model](#contents)
  27. Retinaface needs a resnet50 backbone to extract image features for detection. You could get resnet50 train script from our modelzoo and modify the pad structure of resnet50 according to resnet in ./src/network.py, Final train it on imagenet2012 to get resnet50 pretrain model.
  28. Steps:
  29. 1. Get resnet50 train script from our modelzoo.
  30. 2. Modify the resnet50 architecture according to resnet in ```./src/network.py```.(You can also leave the structure of a unchanged, but the accuracy will be 2-3 percentage points lower.)
  31. 3. Train resnet50 on imagenet2012.
  32. # [Model Architecture](#contents)
  33. Specifically, the retinaface network is based on retinanet. The feature pyramid structure of retinanet is used in the network, and SSH structure is added. Besides the traditional detection branch, the prediction branch of key points and self-monitoring branch are added in the network. The paper indicates that the two branches can improve the performance of the model. Here we do not implement the self-monitoring branch.
  34. # [Dataset](#contents)
  35. Dataset used: [WIDERFACE](http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/WiderFace_Results.html)
  36. Dataset acquisition:
  37. 1. Get the dataset and annotations from [here](https://github.com/peteryuX/retinaface-tf2).
  38. 2. Get the eval ground truth label from [here](https://github.com/peteryuX/retinaface-tf2/tree/master/widerface_evaluate/ground_truth).
  39. - Dataset size:3.42G,32,203 colorful images
  40. - Train:1.36G,12,800 images
  41. - Val:345.95M,3,226 images
  42. - Test:1.72G,16,177 images
  43. # [Environment Requirements](#contents)
  44. - Hardware(GPU)
  45. - Prepare hardware environment with GPU processor.
  46. - Framework
  47. - [MindSpore](https://www.mindspore.cn/install/en)
  48. - For more information, please check the resources below:
  49. - [MindSpore tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  50. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  51. # [Quick Start](#contents)
  52. After installing MindSpore via the official website and download the dataset, you can start training and evaluation as follows:
  53. - running on GPU
  54. ```python
  55. # run training example
  56. export CUDA_VISIBLE_DEVICES=0
  57. python train.py > train.log 2>&1 &
  58. # run distributed training example
  59. bash scripts/run_distribute_gpu_train.sh 4 0,1,2,3
  60. # run evaluation example
  61. export CUDA_VISIBLE_DEVICES=0
  62. python eval.py > eval.log 2>&1 &
  63. OR
  64. bash run_standalone_gpu_eval.sh 0
  65. ```
  66. # [Script Description](#contents)
  67. ## [Script and Sample Code](#contents)
  68. ```text
  69. ├── model_zoo
  70. ├── README.md // descriptions about all the models
  71. ├── retinaface
  72. ├── README.md // descriptions about googlenet
  73. ├── scripts
  74. │ ├──run_distribute_gpu_train.sh // shell script for distributed on GPU
  75. │ ├──run_standalone_gpu_eval.sh // shell script for evaluation on GPU
  76. ├── src
  77. │ ├──dataset.py // creating dataset
  78. │ ├──network.py // retinaface architecture
  79. │ ├──config.py // parameter configuration
  80. │ ├──augmentation.py // data augment method
  81. │ ├──loss.py // loss function
  82. │ ├──utils.py // data preprocessing
  83. │ ├──lr_schedule.py // learning rate schedule
  84. ├── data
  85. │ ├──widerface // dataset data
  86. │ ├──resnet50_pretrain.ckpt // resnet50 imagenet pretrain model
  87. │ ├──ground_truth // eval label
  88. ├── train.py // training script
  89. ├── eval.py // evaluation script
  90. ```
  91. ## [Script Parameters](#contents)
  92. Parameters for both training and evaluation can be set in config.py
  93. - config for RetinaFace, WIDERFACE dataset
  94. ```python
  95. 'variance': [0.1, 0.2], # Variance
  96. 'clip': False, # Clip
  97. 'loc_weight': 2.0, # Bbox regression loss weight
  98. 'class_weight': 1.0, # Confidence/Class regression loss weight
  99. 'landm_weight': 1.0, # Landmark regression loss weight
  100. 'batch_size': 8, # Batch size of train
  101. 'num_workers': 8, # Num worker of dataset load data
  102. 'num_anchor': 29126, # Num of anchor boxes, it depends on the image size
  103. 'ngpu': 4, # Num gpu of train
  104. 'epoch': 100, # Training epoch number
  105. 'decay1': 70, # Epoch number of the first weight attenuation
  106. 'decay2': 90, # Epoch number of the second weight attenuation
  107. 'image_size': 840, # Training image size
  108. 'match_thresh': 0.35, # Threshold for match box
  109. 'optim': 'sgd', # Optimizer type
  110. 'warmup_epoch': 5, # Warmup size, 0 means no warm-up
  111. 'initial_lr': 0.01, # Learning rate
  112. 'momentum': 0.9, # Momentum for Optimizer
  113. 'weight_decay': 5e-4, # Weight decay for Optimizer
  114. 'gamma': 0.1, # Attenuation ratio of learning rate
  115. 'ckpt_path': './checkpoint/', # Model save path
  116. 'save_checkpoint_steps': 2000, # Save checkpoint steps
  117. 'keep_checkpoint_max': 1, # Number of reserved checkpoints
  118. 'resume_net': None, # Network for restart, default is None
  119. 'training_dataset': '', # Training dataset label path, like 'data/widerface/train/label.txt'
  120. 'pretrain': True, # Whether training based on the pre-trained backbone
  121. 'pretrain_path': './data/res50_pretrain.ckpt', # Pre-trained backbone checkpoint path
  122. 'seed': 1, # Setup train seed
  123. 'lr_type': 'dynamic_lr', # Learning rate decline function type, set dynamic_lr or standard_lr
  124. # val
  125. 'val_model': './checkpoint/ckpt_0/RetinaFace-100_536.ckpt', # Validation model path
  126. 'val_dataset_folder': './data/widerface/val/', # Validation dataset path
  127. 'val_origin_size': False, # Is full size verification used
  128. 'val_confidence_threshold': 0.02, # Threshold for val confidence
  129. 'val_nms_threshold': 0.4, # Threshold for val NMS
  130. 'val_iou_threshold': 0.5, # Threshold for val IOU
  131. 'val_save_result': False, # Whether save the resultss
  132. 'val_predict_save_folder': './widerface_result', # Result save path
  133. 'val_gt_dir': './data/ground_truth/', # Path of val set ground_truth
  134. ```
  135. ## [Training Process](#contents)
  136. ### Training
  137. - running on GPU
  138. ```bash
  139. export CUDA_VISIBLE_DEVICES=0
  140. python train.py > train.log 2>&1 &
  141. ```
  142. The python command above will run in the background, you can view the results through the file `train.log`.
  143. After training, you'll get some checkpoint files under the folder `./checkpoint/` by default.
  144. ### Distributed Training
  145. - running on GPU
  146. ```bash
  147. bash scripts/run_distribute_gpu_train.sh 4 0,1,2,3
  148. ```
  149. The above shell script will run distribute training in the background. You can view the results through the file `train/train.log`.
  150. After training, you'll get some checkpoint files under the folder `./checkpoint/ckpt_0/` by default.
  151. ## [Evaluation Process](#contents)
  152. ### Evaluation
  153. - evaluation on WIDERFACE dataset when running on GPU
  154. Before running the command below, please check the checkpoint path used for evaluation. Please set the checkpoint path to be the absolute full path in src/config.py, e.g., "username/retinaface/checkpoint/ckpt_0/RetinaFace-100_402.ckpt".
  155. ```bash
  156. export CUDA_VISIBLE_DEVICES=0
  157. python eval.py > eval.log 2>&1 &
  158. ```
  159. The above python command will run in the background. You can view the results through the file "eval.log". The result of the test dataset will be as follows:
  160. ```text
  161. # grep "Val AP" eval.log
  162. Easy Val AP : 0.9422
  163. Medium Val AP : 0.9325
  164. Hard Val AP : 0.8900
  165. ```
  166. OR,
  167. ```bash
  168. bash run_standalone_gpu_eval.sh 0
  169. ```
  170. The above python command will run in the background. You can view the results through the file "eval/eval.log". The result of the test dataset will be as follows:
  171. ```text
  172. # grep "Val AP" eval.log
  173. Easy Val AP : 0.9422
  174. Medium Val AP : 0.9325
  175. Hard Val AP : 0.8900
  176. ```
  177. # [Model Description](#contents)
  178. ## [Performance](#contents)
  179. ### Evaluation Performance
  180. | Parameters | GPU |
  181. | -------------------------- | -------------------------------------------------------------|
  182. | Model Version | RetinaFace + Resnet50 |
  183. | Resource | NV SMX2 V100-16G |
  184. | uploaded Date | 10/16/2020 (month/day/year) |
  185. | MindSpore Version | 1.0.0 |
  186. | Dataset | WIDERFACE |
  187. | Training Parameters | epoch=100, steps=402, batch_size=8, lr=0.01 |
  188. | Optimizer | SGD |
  189. | Loss Function | MultiBoxLoss + Softmax Cross Entropy |
  190. | outputs | bounding box + confidence + landmark |
  191. | Loss | 1.200 |
  192. | Speed | 4pcs: 560 ms/step |
  193. | Total time | 4pcs: 6.4 hours |
  194. | Parameters (M) | 27.29M |
  195. | Checkpoint for Fine tuning | 336.3M (.ckpt file) |
  196. | Scripts | [retinaface script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/retinaface_resnet50) |
  197. ## [How to use](#contents)
  198. ### Continue Training on the Pretrained Model
  199. - running on GPU
  200. ```python
  201. # Load dataset
  202. ds_train = create_dataset(training_dataset, cfg, batch_size, multiprocessing=True, num_worker=cfg['num_workers'])
  203. # Define model
  204. multibox_loss = MultiBoxLoss(num_classes, cfg['num_anchor'], negative_ratio, cfg['batch_size'])
  205. lr = adjust_learning_rate(initial_lr, gamma, stepvalues, steps_per_epoch, max_epoch, warmup_epoch=cfg['warmup_epoch'])
  206. opt = nn.SGD(params=net.trainable_params(), learning_rate=lr, momentum=momentum,
  207. weight_decay=weight_decay, loss_scale=1)
  208. backbone = resnet50(1001)
  209. net = RetinaFace(phase='train', backbone=backbone)
  210. # Continue training if resume_net is not None
  211. pretrain_model_path = cfg['resume_net']
  212. param_dict_retinaface = load_checkpoint(pretrain_model_path)
  213. load_param_into_net(net, param_dict_retinaface)
  214. net = RetinaFaceWithLossCell(net, multibox_loss, cfg)
  215. net = TrainingWrapper(net, opt)
  216. model = Model(net)
  217. # Set callbacks
  218. config_ck = CheckpointConfig(save_checkpoint_steps=cfg['save_checkpoint_steps'],
  219. keep_checkpoint_max=cfg['keep_checkpoint_max'])
  220. ckpoint_cb = ModelCheckpoint(prefix="RetinaFace", directory=cfg['ckpt_path'], config=config_ck)
  221. time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
  222. callback_list = [LossMonitor(), time_cb, ckpoint_cb]
  223. # Start training
  224. model.train(max_epoch, ds_train, callbacks=callback_list,
  225. dataset_sink_mode=False)
  226. ```
  227. # [Description of Random Situation](#contents)
  228. In train.py, we set the seed with setup_seed function.
  229. # [ModelZoo Homepage](#contents)
  230. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).