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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Contents
  2. - [Face Attribute Description](#face-attribute-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Environment Requirements](#environment-requirements)
  6. - [Script Description](#script-description)
  7. - [Script and Sample Code](#script-and-sample-code)
  8. - [Running Example](#running-example)
  9. - [Model Description](#model-description)
  10. - [Performance](#performance)
  11. - [ModelZoo Homepage](#modelzoo-homepage)
  12. # [Face Attribute Description](#contents)
  13. This is a Face Attributes Recognition network based on Resnet18, with support for training and evaluation on Ascend910.
  14. ResNet (residual neural network) was proposed by Kaiming He and other four Chinese of Microsoft Research Institute. Through the use of ResNet unit, it successfully trained 152 layers of neural network, and won the championship in ilsvrc2015. The error rate on top 5 was 3.57%, and the parameter quantity was lower than vggnet, so the effect was very outstanding. Traditional convolution network or full connection network will have more or less information loss. At the same time, it will lead to the disappearance or explosion of gradient, which leads to the failure of deep network training. ResNet solves this problem to a certain extent. By passing the input information to the output, the integrity of the information is protected. The whole network only needs to learn the part of the difference between input and output, which simplifies the learning objectives and difficulties.The structure of ResNet can accelerate the training of neural network very quickly, and the accuracy of the model is also greatly improved. At the same time, ResNet is very popular, even can be directly used in the concept net network.
  15. [Paper](https://arxiv.org/pdf/1512.03385.pdf): Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. "Deep Residual Learning for Image Recognition"
  16. # [Model Architecture](#contents)
  17. Face Attribute uses a modified-Resnet18 network for performing feature extraction.
  18. # [Dataset](#contents)
  19. This network can recognize the age/gender/mask from a human face. The default rule is:
  20. ```python
  21. age:
  22. 0: 0~2 years
  23. 1: 3~9 years
  24. 2: 10~19 years
  25. 3: 20~29 years
  26. 4: 30~39 years
  27. 5: 40~49 years
  28. 6: 50~59 years
  29. 7: 60~69 years
  30. 8: 70+ years
  31. gender:
  32. 0: male
  33. 1: female
  34. mask:
  35. 0: wearing mask
  36. 1: without mask
  37. ```
  38. We use about 91K face images as training dataset and 11K as evaluating dataset in this example, and you can also use your own datasets or open source datasets (e.g. FairFace and RWMFD)
  39. - step 1: The dataset should be saved in a txt file, which contain the following contents:
  40. ```python
  41. [PATH_TO_IMAGE]/1.jpg [LABEL_AGE] [LABEL_GENDER] [LABEL_MASK]
  42. [PATH_TO_IMAGE]/2.jpg [LABEL_AGE] [LABEL_GENDER] [LABEL_MASK]
  43. [PATH_TO_IMAGE]/3.jpg [LABEL_AGE] [LABEL_GENDER] [LABEL_MASK]
  44. ...
  45. ```
  46. The value range of [LABEL_AGE] is [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8], -1 means the label should be ignored.
  47. The value range of [LABEL_GENDER] is [-1, 0, 1], -1 means the label should be ignored.
  48. The value range of [LABEL_MASK] is [-1, 0, 1], -1 means the label should be ignored.
  49. - step 2: Convert the dataset to mindrecord:
  50. ```bash
  51. python src/data_to_mindrecord_train.py
  52. ```
  53. or
  54. ```bash
  55. python src/data_to_mindrecord_eval.py
  56. ```
  57. If your dataset is too big to convert at a time, you can add data to an existed mindrecord in turn:
  58. ```bash
  59. python src/data_to_mindrecord_train_append.py
  60. ```
  61. # [Environment Requirements](#contents)
  62. - Hardware(Ascend)
  63. - Prepare hardware environment with Ascend processor.
  64. - Framework
  65. - [MindSpore](https://www.mindspore.cn/install/en)
  66. - For more information, please check the resources below:
  67. - [MindSpore tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  68. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  69. # [Script Description](#contents)
  70. ## [Script and Sample Code](#contents)
  71. The entire code structure is as following:
  72. ```python
  73. .
  74. └─ Face Attribute
  75. ├─ README.md
  76. ├─ scripts
  77. ├─ run_standalone_train.sh # launch standalone training(1p) in ascend
  78. ├─ run_distribute_train.sh # launch distributed training(8p) in ascend
  79. ├─ run_eval.sh # launch evaluating in ascend
  80. └─ run_export.sh # launch exporting air model
  81. ├─ src
  82. ├─ FaceAttribute
  83. ├─ cross_entropy.py # cross entroy loss
  84. ├─ custom_net.py # network unit
  85. ├─ loss_factory.py # loss function
  86. ├─ head_factory.py # network head
  87. ├─ resnet18.py # network backbone
  88. ├─ head_factory_softmax.py # network head with softmax
  89. └─ resnet18_softmax.py # network backbone with softmax
  90. ├─ config.py # parameter configuration
  91. ├─ dataset_eval.py # dataset loading and preprocessing for evaluating
  92. ├─ dataset_train.py # dataset loading and preprocessing for training
  93. ├─ logging.py # log function
  94. ├─ lrsche_factory.py # generate learning rate
  95. ├─ data_to_mindrecord_train.py # convert dataset to mindrecord for training
  96. ├─ data_to_mindrecord_train_append.py # add dataset to an existed mindrecord for training
  97. └─ data_to_mindrecord_eval.py # convert dataset to mindrecord for evaluating
  98. ├─ train.py # training scripts
  99. ├─ eval.py # evaluation scripts
  100. └─ export.py # export air model
  101. ```
  102. ## [Running Example](#contents)
  103. ### Train
  104. - Stand alone mode
  105. ```bash
  106. cd ./scripts
  107. sh run_standalone_train.sh [MINDRECORD_FILE] [USE_DEVICE_ID]
  108. ```
  109. or (fine-tune)
  110. ```bash
  111. cd ./scripts
  112. sh run_standalone_train.sh [MINDRECORD_FILE] [USE_DEVICE_ID] [PRETRAINED_BACKBONE]
  113. ```
  114. for example:
  115. ```bash
  116. cd ./scripts
  117. sh run_standalone_train.sh /home/train.mindrecord 0 /home/a.ckpt
  118. ```
  119. - Distribute mode (recommended)
  120. ```bash
  121. cd ./scripts
  122. sh run_distribute_train.sh [MINDRECORD_FILE] [RANK_TABLE]
  123. ```
  124. or (fine-tune)
  125. ```bash
  126. cd ./scripts
  127. sh run_distribute_train.sh [MINDRECORD_FILE] [RANK_TABLE] [PRETRAINED_BACKBONE]
  128. ```
  129. for example:
  130. ```bash
  131. cd ./scripts
  132. sh run_distribute_train.sh /home/train.mindrecord ./rank_table_8p.json /home/a.ckpt
  133. ```
  134. You will get the loss value of each step as following in "./output/[TIME]/[TIME].log" or "./scripts/device0/train.log":
  135. ```python
  136. epoch[0], iter[0], loss:4.489518, 12.92 imgs/sec
  137. epoch[0], iter[10], loss:3.619693, 13792.76 imgs/sec
  138. epoch[0], iter[20], loss:3.580932, 13817.78 imgs/sec
  139. epoch[0], iter[30], loss:3.574254, 7834.65 imgs/sec
  140. epoch[0], iter[40], loss:3.557742, 7884.87 imgs/sec
  141. ...
  142. epoch[69], iter[6120], loss:1.225308, 9561.00 imgs/sec
  143. epoch[69], iter[6130], loss:1.209557, 8913.28 imgs/sec
  144. epoch[69], iter[6140], loss:1.158641, 9755.81 imgs/sec
  145. epoch[69], iter[6150], loss:1.167064, 9300.77 imgs/sec
  146. ```
  147. ### Evaluation
  148. ```bash
  149. cd ./scripts
  150. sh run_eval.sh [MINDRECORD_FILE] [USE_DEVICE_ID] [PRETRAINED_BACKBONE]
  151. ```
  152. for example:
  153. ```bash
  154. cd ./scripts
  155. sh run_eval.sh /home/eval.mindrecord 0 /home/a.ckpt
  156. ```
  157. You will get the result as following in "./scripts/device0/eval.log" or txt file in [PRETRAINED_BACKBONE]'s folder:
  158. ```python
  159. age accuracy: 0.45773233522001094
  160. gen accuracy: 0.8950155194449516
  161. mask accuracy: 0.992539346357495
  162. gen precision: 0.8869598765432098
  163. gen recall: 0.8907400232468036
  164. gen f1: 0.88884593079451
  165. mask precision: 1.0
  166. mask recall: 0.998539346357495
  167. mask f1: 0.9992691394116572
  168. ```
  169. ### Convert model
  170. If you want to infer the network on Ascend 310, you should convert the model to AIR:
  171. ```bash
  172. cd ./scripts
  173. sh run_export.sh [BATCH_SIZE] [USE_DEVICE_ID] [PRETRAINED_BACKBONE]
  174. ```
  175. # [Model Description](#contents)
  176. ## [Performance](#contents)
  177. ### Training Performance
  178. | Parameters | Face Attribute |
  179. | -------------------------- | ----------------------------------------------------------- |
  180. | Model Version | V1 |
  181. | Resource | Ascend 910; CPU 2.60GHz, 192cores; Memory 755G; OS Euler2.8 |
  182. | uploaded Date | 09/30/2020 (month/day/year) |
  183. | MindSpore Version | 1.0.0 |
  184. | Dataset | 91K images |
  185. | Training Parameters | epoch=70, batch_size=128, momentum=0.9, lr=0.001 |
  186. | Optimizer | Momentum |
  187. | Loss Function | Softmax Cross Entropy |
  188. | outputs | probability |
  189. | Speed | 1pc: 200~250 ms/step; 8pcs: 100~150 ms/step |
  190. | Total time | 1pc: 2.5 hours; 8pcs: 0.3 hours |
  191. | Checkpoint for Fine tuning | 88M (.ckpt file) |
  192. ### Evaluation Performance
  193. | Parameters | Face Attribute |
  194. | ------------------- | --------------------------- |
  195. | Model Version | V1 |
  196. | Resource | Ascend 910; OS Euler2.8 |
  197. | Uploaded Date | 09/30/2020 (month/day/year) |
  198. | MindSpore Version | 1.0.0 |
  199. | Dataset | 11K images |
  200. | batch_size | 1 |
  201. | outputs | accuracy |
  202. | Accuracy(8pcs) | age:45.7% |
  203. | | gender:89.5% |
  204. | | mask:99.2% |
  205. | Model for inference | 88M (.ckpt file) |
  206. # [ModelZoo Homepage](#contents)
  207. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).