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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # Contents
  2. - [WarpCTC Description](#warpctc-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Environment Requirements](#environment-requirements)
  6. - [Quick Start](#quick-start)
  7. - [Script Description](#script-description)
  8. - [Script and Sample Code](#script-and-sample-code)
  9. - [Script Parameters](#script-parameters)
  10. - [Training Script Parameters](#training-script-parameters)
  11. - [Parameters Configuration](#parameters-configuration)
  12. - [Dataset Preparation](#dataset-preparation)
  13. - [Training Process](#training-process)
  14. - [Training](#training)
  15. - [Distributed Training](#distributed-training)
  16. - [Evaluation Process](#evaluation-process)
  17. - [Evaluation](#evaluation)
  18. - [Model Description](#model-description)
  19. - [Performance](#performance)
  20. - [Training Performance](#training-performance)
  21. - [Evaluation Performance](#evaluation-performance)
  22. - [Description of Random Situation](#description-of-random-situation)
  23. - [ModelZoo Homepage](#modelzoo-homepage)
  24. # [WarpCTC Description](#contents)
  25. This is an example of training WarpCTC with self-generated captcha image dataset in MindSpore.
  26. # [Model Architecture](#content)
  27. WarpCTC is a two-layer stacked LSTM appending with one-layer FC neural network. See src/warpctc.py for details.
  28. # [Dataset](#content)
  29. The dataset is self-generated using a third-party library called [captcha](https://github.com/lepture/captcha), which can randomly generate digits from 0 to 9 in image. In this network, we set the length of digits varying from 1 to 4.
  30. # [Environment Requirements](#contents)
  31. - Hardware(Ascend/GPU)
  32. - Prepare hardware environment with Ascend or GPU processor. If you want to try Ascend, please send the [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) to ascend@huawei.com. You will be able to have access to related resources once approved.
  33. - Framework
  34. - [MindSpore](https://gitee.com/mindspore/mindspore)
  35. - For more information, please check the resources below:
  36. - [MindSpore tutorials](https://www.mindspore.cn/tutorial/en/master/index.html)
  37. - [MindSpore API](https://www.mindspore.cn/api/en/master/index.html)
  38. # [Quick Start](#contents)
  39. - Generate dataset.
  40. Run the script `scripts/run_process_data.sh` to generate a dataset. By default, the shell script will generate 10000 test images and 50000 train images separately.
  41. ```
  42. $ cd scripts
  43. $ sh run_process_data.sh
  44. # after execution, you will find the dataset like the follows:
  45. .
  46. └─warpctc
  47. └─data
  48. ├─ train # train dataset
  49. └─ test # evaluate dataset
  50. ```
  51. - After the dataset is prepared, you may start running the training or the evaluation scripts as follows:
  52. - Running on Ascend
  53. ```
  54. # distribute training example in Ascend
  55. $ bash run_distribute_train.sh rank_table.json ../data/train
  56. # evaluation example in Ascend
  57. $ bash run_eval.sh ../data/test warpctc-30-97.ckpt Ascend
  58. # standalone training example in Ascend
  59. $ bash run_standalone_train.sh ../data/train Ascend
  60. ```
  61. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  62. Please follow the instructions in the link below:
  63. https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools.
  64. - Running on GPU
  65. ```
  66. # distribute training example in GPU
  67. $ bash run_distribute_train_for_gpu.sh 8 ../data/train
  68. # standalone training example in GPU
  69. $ bash run_standalone_train.sh ../data/train GPU
  70. # evaluation example in GPU
  71. $ bash run_eval.sh ../data/test warpctc-30-97.ckpt GPU
  72. ```
  73. # [Script Description](#contents)
  74. ## [Script and Sample Code](#contents)
  75. ```shell
  76. .
  77. └──warpctc
  78. ├── README.md
  79. ├── script
  80. ├── run_distribute_train.sh # launch distributed training in Ascend(8 pcs)
  81. ├── run_distribute_train_for_gpu.sh # launch distributed training in GPU
  82. ├── run_eval.sh # launch evaluation
  83. ├── run_process_data.sh # launch dataset generation
  84. └── run_standalone_train.sh # launch standalone training(1 pcs)
  85. ├── src
  86. ├── config.py # parameter configuration
  87. ├── dataset.py # data preprocessing
  88. ├── loss.py # ctcloss definition
  89. ├── lr_generator.py # generate learning rate for each step
  90. ├── metric.py # accuracy metric for warpctc network
  91. ├── warpctc.py # warpctc network definition
  92. └── warpctc_for_train.py # warpctc network with grad, loss and gradient clip
  93. ├── eval.py # eval net
  94. ├── process_data.py # dataset generation script
  95. └── train.py # train net
  96. ```
  97. ## [Script Parameters](#contents)
  98. ### Training Script Parameters
  99. ```
  100. # distributed training in Ascend
  101. Usage: bash run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH]
  102. # distributed training in GPU
  103. Usage: bash run_distribute_train_for_gpu.sh [RANK_SIZE] [DATASET_PATH]
  104. # standalone training
  105. Usage: bash run_standalone_train.sh [DATASET_PATH] [PLATFORM]
  106. ```
  107. ### Parameters Configuration
  108. Parameters for both training and evaluation can be set in config.py.
  109. ```
  110. "max_captcha_digits": 4, # max number of digits in each
  111. "captcha_width": 160, # width of captcha images
  112. "captcha_height": 64, # height of capthca images
  113. "batch_size": 64, # batch size of input tensor
  114. "epoch_size": 30, # only valid for taining, which is always 1 for inference
  115. "hidden_size": 512, # hidden size in LSTM layers
  116. "learning_rate": 0.01, # initial learning rate
  117. "momentum": 0.9 # momentum of SGD optimizer
  118. "save_checkpoint": True, # whether save checkpoint or not
  119. "save_checkpoint_steps": 97, # the step interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  120. "keep_checkpoint_max": 30, # only keep the last keep_checkpoint_max checkpoint
  121. "save_checkpoint_path": "./checkpoint", # path to save checkpoint
  122. ```
  123. ## [Dataset Preparation](#contents)
  124. - You may refer to "Generate dataset" in [Quick Start](#quick-start) to automatically generate a dataset, or you may choose to generate a captcha dataset by yourself.
  125. ## [Training Process](#contents)
  126. - Set options in `config.py`, including learning rate and other network hyperparameters. Click [MindSpore dataset preparation tutorial](https://www.mindspore.cn/tutorial/zh-CN/master/use/data_preparation/loading_the_datasets.html#mindspore) for more information about dataset.
  127. ### [Training](#contents)
  128. - Run `run_standalone_train.sh` for non-distributed training of WarpCTC model, either on Ascend or on GPU.
  129. ``` bash
  130. bash run_standalone_train.sh [DATASET_PATH] [PLATFORM]
  131. ```
  132. ### [Distributed Training](#contents)
  133. - Run `run_distribute_train.sh` for distributed training of WarpCTC model on Ascend.
  134. ``` bash
  135. bash run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH]
  136. ```
  137. - Run `run_distribute_train_gpu.sh` for distributed training of WarpCTC model on GPU.
  138. ``` bash
  139. bash run_distribute_train_gpu.sh [RANK_SIZE] [DATASET_PATH]
  140. ```
  141. ## [Evaluation Process](#contents)
  142. ### [Evaluation](#contents)
  143. - Run `run_eval.sh` for evaluation.
  144. ``` bash
  145. bash run_eval.sh [DATASET_PATH] [CHECKPOINT_PATH] [PLATFORM]
  146. ```
  147. # [Model Description](#contents)
  148. ## [Performance](#contents)
  149. ### [Training Performance](#contents)
  150. | Parameters | Ascend 910 | GPU |
  151. | -------------------------- | --------------------------------------------- |---------------------------------- |
  152. | Model Version | v1.0 | v1.0 |
  153. | Resource | Ascend 910,CPU 2.60GHz 56cores,Memory 314G | GPU(Tesla V100 SXM2),CPU 2.1GHz 24cores,Memory 128G /
  154. | uploaded Date | 07/01/2020 (month/day/year) | 08/01/2020 (month/day/year) |
  155. | MindSpore Version | 0.5.0-alpha | 0.6.0-alpha |
  156. | Dataset | Captcha | Captcha |
  157. | Training Parameters | epoch=30, steps per epoch=98, batch_size = 64 | epoch=30, steps per epoch=98, batch_size = 64 |
  158. | Optimizer | SGD | SGD |
  159. | Loss Function | CTCLoss | CTCLoss |
  160. | outputs | probability | probability |
  161. | Loss | 0.0000157 | 0.0000246 |
  162. | Speed | 980ms/step(8pcs) | 150ms/step(8pcs)|
  163. | Total time | 30 mins | 5 mins|
  164. | Parameters (M) | 2.75 | 2.75 |
  165. | Checkpoint for Fine tuning | 20.3M (.ckpt file) | 20.3M (.ckpt file) |
  166. | Scripts | [Link](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/warpctc) | [Link](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/warpctc) |
  167. ### [Evaluation Performance](#contents)
  168. | Parameters | WarpCTC |
  169. | ------------------- | --------------------------- |
  170. | Model Version | V1.0 |
  171. | Resource | Ascend 910 |
  172. | Uploaded Date | 08/01/2020 (month/day/year) |
  173. | MindSpore Version | 0.6.0-alpha |
  174. | Dataset | Captcha |
  175. | batch_size | 64 |
  176. | outputs | ACC |
  177. | Accuracy | 99.0% |
  178. | Model for inference | 20.3M (.ckpt file) |
  179. # [Description of Random Situation](#contents)
  180. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py for weight initialization.
  181. # [ModelZoo Homepage](#contents)
  182. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).