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

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. # Contents
  2. - [CNNCTC Description](#CNNCTC-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Features](#features)
  6. - [Mixed Precision](#mixed-precision)
  7. - [Environment Requirements](#environment-requirements)
  8. - [Quick Start](#quick-start)
  9. - [Script Description](#script-description)
  10. - [Script and Sample Code](#script-and-sample-code)
  11. - [Script Parameters](#script-parameters)
  12. - [Training Process](#training-process)
  13. - [Training](#training)
  14. - [Distributed Training](#distributed-training)
  15. - [Evaluation Process](#evaluation-process)
  16. - [Evaluation](#evaluation)
  17. - [Inference Process](#inference-process)
  18. - [Export MindIR](#export-mindir)
  19. - [Infer on Ascend310](#infer-on-ascend310)
  20. - [result](#result)
  21. - [Model Description](#model-description)
  22. - [Performance](#performance)
  23. - [Training Performance](#training-performance)
  24. - [Evaluation Performance](#evaluation-performance)
  25. - [Inference Performance](#inference-performance)
  26. - [How to use](#how-to-use)
  27. - [Inference](#inference)
  28. - [Continue Training on the Pretrained Model](#continue-training-on-the-pretrained-model)
  29. - [Transfer Learning](#transfer-learning)
  30. - [Description of Random Situation](#description-of-random-situation)
  31. - [ModelZoo Homepage](#modelzoo-homepage)
  32. # [CNNCTC Description](#contents)
  33. This paper proposes three major contributions to addresses scene text recognition (STR).
  34. First, we examine the inconsistencies of training and evaluation datasets, and the performance gap results from inconsistencies.
  35. Second, we introduce a unified four-stage STR framework that most existing STR models fit into.
  36. Using this framework allows for the extensive evaluation of previously proposed STR modules and the discovery of previously
  37. unexplored module combinations. Third, we analyze the module-wise contributions to performance in terms of accuracy, speed,
  38. and memory demand, under one consistent set of training and evaluation datasets. Such analyses clean up the hindrance on the current
  39. comparisons to understand the performance gain of the existing modules.
  40. [Paper](https://arxiv.org/abs/1904.01906): J. Baek, G. Kim, J. Lee, S. Park, D. Han, S. Yun, S. J. Oh, and H. Lee, “What is wrong with scene text recognition model comparisons? dataset and model analysis,” ArXiv, vol. abs/1904.01906, 2019.
  41. # [Model Architecture](#contents)
  42. This is an example of training CNN+CTC model for text recognition on MJSynth and SynthText dataset with MindSpore.
  43. # [Dataset](#contents)
  44. Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
  45. The [MJSynth](https://www.robots.ox.ac.uk/~vgg/data/text/) and [SynthText](https://github.com/ankush-me/SynthText) dataset are used for model training. The [The IIIT 5K-word dataset](https://cvit.iiit.ac.in/research/projects/cvit-projects/the-iiit-5k-word-dataset) dataset is used for evaluation.
  46. - step 1:
  47. All the datasets have been preprocessed and stored in .lmdb format and can be downloaded [**HERE**](https://drive.google.com/drive/folders/192UfE9agQUMNq6AgU3_E05_FcPZK4hyt).
  48. - step 2:
  49. Uncompress the downloaded file, rename the MJSynth dataset as MJ, the SynthText dataset as ST and the IIIT dataset as IIIT.
  50. - step 3:
  51. Move above mentioned three datasets into `cnnctc_data` folder, and the structure should be as below:
  52. ```text
  53. |--- CNNCTC/
  54. |--- cnnctc_data/
  55. |--- ST/
  56. data.mdb
  57. lock.mdb
  58. |--- MJ/
  59. data.mdb
  60. lock.mdb
  61. |--- IIIT/
  62. data.mdb
  63. lock.mdb
  64. ......
  65. ```
  66. - step 4:
  67. Preprocess the dataset by running:
  68. ```bash
  69. python src/preprocess_dataset.py
  70. ```
  71. This takes around 75 minutes.
  72. # [Features](#contents)
  73. ## Mixed Precision
  74. The [mixed precision](https://www.mindspore.cn/tutorials/experts/en/r1.7/others/mixed_precision.html) training method accelerates the deep learning neural network training process by using both the single-precision and half-precision data formats, and maintains the network precision achieved by the single-precision training at the same time. Mixed precision training can accelerate the computation process, reduce memory usage, and enable a larger model or batch size to be trained on specific hardware.
  75. For FP16 operators, if the input data type is FP32, the backend of MindSpore will automatically handle it with reduced precision. Users could check the reduced-precision operators by enabling INFO log and then searching ‘reduce precision’.
  76. # [Environment Requirements](#contents)
  77. - Hardware(Ascend/GPU)
  78. - Prepare hardware environment with Ascend or GPU processor.
  79. - Framework
  80. - [MindSpore](https://www.mindspore.cn/install/en)
  81. - For more information, please check the resources below:
  82. - [MindSpore tutorials](https://www.mindspore.cn/tutorials/en/master/index.html)
  83. - [MindSpore Python API](https://www.mindspore.cn/docs/en/r1.7/index.html)
  84. # [Quick Start](#contents)
  85. - Install dependencies:
  86. ```bash
  87. pip install lmdb
  88. pip install Pillow
  89. pip install tqdm
  90. pip install six
  91. ```
  92. ```default_config.yaml
  93. TRAIN_DATASET_PATH: /home/DataSet/MJ-ST-IIIT/ST-MJ/
  94. TRAIN_DATASET_INDEX_PATH: /home/DataSet/MJ-ST-IIIT/st_mj_fixed_length_index_list.pkl
  95. TEST_DATASET_PATH: /home/DataSet/MJ-ST-IIIT/IIIT5K_3000
  96. Modify the parameters according to the actual path
  97. ```
  98. - Standalone Ascend Training:
  99. ```bash
  100. bash scripts/run_standalone_train_ascend.sh $DEVICE_ID $PRETRAINED_CKPT(options)
  101. # example: bash scripts/run_standalone_train_ascend.sh 0
  102. ```
  103. - Standalone GPU Training:
  104. ```bash
  105. bash scripts/run_standalone_train_gpu.sh $PRETRAINED_CKPT(options)
  106. ```
  107. - Distributed Ascend Training:
  108. ```bash
  109. bash scripts/run_distribute_train_ascend.sh $RANK_TABLE_FILE $PRETRAINED_CKPT(options)
  110. # example: bash scripts/run_distribute_train_ascend.sh ~/hccl_8p.json
  111. ```
  112. - Distributed GPU Training:
  113. ```bash
  114. bash scripts/run_distribute_train_gpu.sh $PRETRAINED_CKPT(options)
  115. ```
  116. - Ascend Evaluation:
  117. ```bash
  118. bash scripts/run_eval_ascend.sh $DEVICE_ID $TRAINED_CKPT
  119. # example: scripts/run_eval_ascend.sh 0 /home/model/cnnctc/ckpt/CNNCTC-1_8000.ckpt
  120. ```
  121. - GPU Evaluation:
  122. ```bash
  123. bash scripts/run_eval_gpu.sh $TRAINED_CKPT
  124. ```
  125. # [Script Description](#contents)
  126. ## [Script and Sample Code](#contents)
  127. The entire code structure is as following:
  128. ```text
  129. |--- CNNCTC/
  130. |---README.md // descriptions about cnnctc
  131. |---README_cn.md // descriptions about cnnctc
  132. |---default_config.yaml // config file
  133. |---train.py // train scripts
  134. |---eval.py // eval scripts
  135. |---export.py // export scripts
  136. |---preprocess.py // preprocess scripts
  137. |---postprocess.py // postprocess scripts
  138. |---ascend310_infer // application for 310 inference
  139. |---scripts
  140. |---run_infer_310.sh // shell script for infer on ascend310
  141. |---run_standalone_train_ascend.sh // shell script for standalone on ascend
  142. |---run_standalone_train_gpu.sh // shell script for standalone on gpu
  143. |---run_distribute_train_ascend.sh // shell script for distributed on ascend
  144. |---run_distribute_train_gpu.sh // shell script for distributed on gpu
  145. |---run_eval_ascend.sh // shell script for eval on ascend
  146. |---src
  147. |---__init__.py // init file
  148. |---cnn_ctc.py // cnn_ctc network
  149. |---callback.py // loss callback file
  150. |---dataset.py // process dataset
  151. |---util.py // routine operation
  152. |---preprocess_dataset.py // preprocess dataset
  153. |--- model_utils
  154. |---config.py // Parameter config
  155. |---moxing_adapter.py // modelarts device configuration
  156. |---device_adapter.py // Device Config
  157. |---local_adapter.py // local device config
  158. ```
  159. ## [Script Parameters](#contents)
  160. Parameters for both training and evaluation can be set in `default_config.yaml`.
  161. Arguments:
  162. - `--CHARACTER`: Character labels.
  163. - `--NUM_CLASS`: The number of classes including all character labels and the <blank> label for CTCLoss.
  164. - `--HIDDEN_SIZE`: Model hidden size.
  165. - `--FINAL_FEATURE_WIDTH`: The number of features.
  166. - `--IMG_H`: The height of input image.
  167. - `--IMG_W`: The width of input image.
  168. - `--TRAIN_DATASET_PATH`: The path to training dataset.
  169. - `--TRAIN_DATASET_INDEX_PATH`: The path to training dataset index file which determines the order .
  170. - `--TRAIN_BATCH_SIZE`: Training batch size. The batch size and index file must ensure input data is in fixed shape.
  171. - `--TRAIN_DATASET_SIZE`: Training dataset size.
  172. - `--TEST_DATASET_PATH`: The path to test dataset.
  173. - `--TEST_BATCH_SIZE`: Test batch size.
  174. - `--TRAIN_EPOCHS`:Total training epochs.
  175. - `--CKPT_PATH`:The path to model checkpoint file, can be used to resume training and evaluation.
  176. - `--SAVE_PATH`:The path to save model checkpoint file.
  177. - `--LR`:Learning rate for standalone training.
  178. - `--LR_PARA`:Learning rate for distributed training.
  179. - `--MOMENTUM`:Momentum.
  180. - `--LOSS_SCALE`:Loss scale to prevent gradient underflow.
  181. - `--SAVE_CKPT_PER_N_STEP`:Save model checkpoint file per N steps.
  182. - `--KEEP_CKPT_MAX_NUM`:The maximum number of saved model checkpoint file.
  183. ## [Training Process](#contents)
  184. ### Training
  185. - Standalone Ascend Training:
  186. ```bash
  187. bash scripts/run_standalone_train_ascend.sh [DEVICE_ID] [PRETRAINED_CKPT(options)]
  188. # example: bash scripts/run_standalone_train_ascend.sh 0
  189. ```
  190. Results and checkpoints are written to `./train` folder. Log can be found in `./train/log` and loss values are recorded in `./train/loss.log`.
  191. `$PRETRAINED_CKPT` is the path to model checkpoint and it is **optional**. If none is given the model will be trained from scratch.
  192. - Distributed Ascend Training:
  193. ```bash
  194. bash scripts/run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_CKPT(options)]
  195. # example: bash scripts/run_distribute_train_ascend.sh ~/hccl_8p.json
  196. ```
  197. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  198. Please follow the instructions in the link below:
  199. <https://gitee.com/mindspore/models/tree/master/utils/hccl_tools>.
  200. Results and checkpoints are written to `./train_parallel_{i}` folder for device `i` respectively.
  201. Log can be found in `./train_parallel_{i}/log_{i}.log` and loss values are recorded in `./train_parallel_{i}/loss.log`.
  202. `$RANK_TABLE_FILE` is needed when you are running a distribute task on ascend.
  203. `$PATH_TO_CHECKPOINT` is the path to model checkpoint and it is **optional**. If none is given the model will be trained from scratch.
  204. ### Training Result
  205. Training result will be stored in the example path, whose folder name begins with "train" or "train_parallel". You can find checkpoint file together with result like the following in loss.log.
  206. ```text
  207. # distribute training result(8p)
  208. epoch: 1 step: 1 , loss is 76.25, average time per step is 0.235177839748392712
  209. epoch: 1 step: 2 , loss is 73.46875, average time per step is 0.25798572540283203
  210. epoch: 1 step: 3 , loss is 69.46875, average time per step is 0.229678678512573
  211. epoch: 1 step: 4 , loss is 64.3125, average time per step is 0.23512671788533527
  212. epoch: 1 step: 5 , loss is 58.375, average time per step is 0.23149147033691406
  213. epoch: 1 step: 6 , loss is 52.7265625, average time per step is 0.2292975425720215
  214. ...
  215. epoch: 1 step: 8689 , loss is 9.706798802612482, average time per step is 0.2184656601312549
  216. epoch: 1 step: 8690 , loss is 9.70612545289855, average time per step is 0.2184725407765116
  217. epoch: 1 step: 8691 , loss is 9.70695776049204, average time per step is 0.21847309686135555
  218. epoch: 1 step: 8692 , loss is 9.707279624277456, average time per step is 0.21847339290613375
  219. epoch: 1 step: 8693 , loss is 9.70763437950938, average time per step is 0.2184720295013031
  220. epoch: 1 step: 8694 , loss is 9.707695425072046, average time per step is 0.21847410284595573
  221. epoch: 1 step: 8695 , loss is 9.708408273381295, average time per step is 0.21847338271072345
  222. epoch: 1 step: 8696 , loss is 9.708703753591953, average time per step is 0.2184726025560777
  223. epoch: 1 step: 8697 , loss is 9.709536406025824, average time per step is 0.21847212061114694
  224. epoch: 1 step: 8698 , loss is 9.708542263610315, average time per step is 0.2184715309307257
  225. ```
  226. - running on ModelArts
  227. - If you want to train the model on modelarts, you can refer to the [official guidance document] of modelarts (https://support.huaweicloud.com/modelarts/)
  228. ```python
  229. # Example of using distributed training dpn on modelarts :
  230. # Data set storage method
  231. # ├── CNNCTC_Data # dataset dir
  232. # ├──train # train dir
  233. # ├── ST_MJ # train dataset dir
  234. # ├── data.mdb # data file
  235. # ├── lock.mdb
  236. # ├── st_mj_fixed_length_index_list.pkl
  237. # ├── eval # eval dir
  238. # ├── IIIT5K_3000 # eval dataset dir
  239. # ├── checkpoint # checkpoint dir
  240. # (1) Choose either a (modify yaml file parameters) or b (modelArts create training job to modify parameters) 。
  241. # a. set "enable_modelarts=True"
  242. # set "run_distribute=True"
  243. # set "TRAIN_DATASET_PATH=/cache/data/ST_MJ/"
  244. # set "TRAIN_DATASET_INDEX_PATH=/cache/data/st_mj_fixed_length_index_list.pkl"
  245. # set "SAVE_PATH=/cache/train/checkpoint"
  246. #
  247. # b. add "enable_modelarts=True" Parameters are on the interface of modearts。
  248. # Set the parameters required by method a on the modelarts interface
  249. # Note: The path parameter does not need to be quoted
  250. # (2) Set the path of the network configuration file "_config_path=/The path of config in default_config.yaml/"
  251. # (3) Set the code path on the modelarts interface "/path/cnnctc"。
  252. # (4) Set the model's startup file on the modelarts interface "train.py" 。
  253. # (5) Set the data path of the model on the modelarts interface ".../CNNCTC_Data/train"(choices CNNCTC_Data/train Folder path) ,
  254. # The output path of the model "Output file path" and the log path of the model "Job log path" 。
  255. # (6) start trainning the model。
  256. # Example of using model inference on modelarts
  257. # (1) Place the trained model to the corresponding position of the bucket。
  258. # (2) chocie a or b。
  259. # a.set "enable_modelarts=True"
  260. # set "TEST_DATASET_PATH=/cache/data/IIIT5K_3000/"
  261. # set "CHECKPOINT_PATH=/cache/data/checkpoint/checkpoint file name"
  262. # b. Add "enable_modelarts=True" parameter on the interface of modearts。
  263. # Set the parameters required by method a on the modelarts interface
  264. # Note: The path parameter does not need to be quoted
  265. # (3) Set the path of the network configuration file "_config_path=/The path of config in default_config.yaml/"
  266. # (4) Set the code path on the modelarts interface "/path/cnnctc"。
  267. # (5) Set the model's startup file on the modelarts interface "train.py" 。
  268. # (6) Set the data path of the model on the modelarts interface ".../CNNCTC_Data/train"(choices CNNCTC_Data/train Folder path) ,
  269. # The output path of the model "Output file path" and the log path of the model "Job log path" 。
  270. # (7) Start model inference。
  271. ```
  272. - Standalone GPU Training:
  273. ```bash
  274. bash scripts/run_standalone_train_gpu.sh [PRETRAINED_CKPT(options)]
  275. ```
  276. Results and checkpoints are written to `./train` folder. Log can be found in `./train/log` and loss values are recorded in `./train/loss.log`.
  277. `$PRETRAINED_CKPT` is the path to model checkpoint and it is **optional**. If none is given the model will be trained from scratch.
  278. - Distributed GPU Training:
  279. ```bash
  280. bash scripts/run_distribute_train_gpu.sh [PRETRAINED_CKPT(options)]
  281. ```
  282. Results and checkpoints are written to `./train_parallel` folder with model checkpoints in ckpt_{i} directories.
  283. Log can be found in `./train_parallel/log` and loss values are recorded in `./train_parallel/loss.log`.
  284. ## [Evaluation Process](#contents)
  285. ### Evaluation
  286. - Ascend Evaluation:
  287. ```bash
  288. bash scripts/run_eval_ascend.sh [DEVICE_ID] [TRAINED_CKPT]
  289. # example: scripts/run_eval_ascend.sh 0 /home/model/cnnctc/ckpt/CNNCTC-1_8000.ckpt
  290. ```
  291. The model will be evaluated on the IIIT dataset, sample results and overall accuracy will be printed.
  292. - GPU Evaluation:
  293. ```bash
  294. bash scripts/run_eval_gpu.sh [TRAINED_CKPT]
  295. ```
  296. ## [Inference process](#contents)
  297. ### Export MindIR
  298. ```shell
  299. python export.py --ckpt_file [CKPT_PATH] --file_format [EXPORT_FORMAT] --TEST_BATCH_SIZE [BATCH_SIZE]
  300. ```
  301. The ckpt_file parameter is required,
  302. `EXPORT_FORMAT` should be in ["AIR", "MINDIR"].
  303. `BATCH_SIZE` current batch_size can only be set to 1.
  304. - Export MindIR on Modelarts
  305. ```Modelarts
  306. Export MindIR example on ModelArts
  307. Data storage method is the same as training
  308. # (1) Choose either a (modify yaml file parameters) or b (modelArts create training job to modify parameters)。
  309. # a. set "enable_modelarts=True"
  310. # set "file_name=cnnctc"
  311. # set "file_format=MINDIR"
  312. # set "ckpt_file=/cache/data/checkpoint file name"
  313. # b. Add "enable_modelarts=True" parameter on the interface of modearts。
  314. # Set the parameters required by method a on the modelarts interface
  315. # Note: The path parameter does not need to be quoted
  316. # (2)Set the path of the network configuration file "_config_path=/The path of config in default_config.yaml/"
  317. # (3) Set the code path on the modelarts interface "/path/cnnctc"。
  318. # (4) Set the model's startup file on the modelarts interface "export.py" 。
  319. # (5) Set the data path of the model on the modelarts interface ".../CNNCTC_Data/eval/checkpoint"(choices CNNCTC_Data/eval/checkpoint Folder path) ,
  320. # The output path of the model "Output file path" and the log path of the model "Job log path" 。
  321. ```
  322. ### Infer on Ascend310
  323. Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model.
  324. ```shell
  325. # Ascend310 inference
  326. bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID]
  327. ```
  328. - `DVPP` is mandatory, and must choose from ["DVPP", "CPU"], it's case-insensitive. CNNCTC only support CPU mode .
  329. - `DEVICE_ID` is optional, default value is 0.
  330. ### Result
  331. - Ascend Result
  332. Inference result is saved in current path, you can find result like this in acc.log file.
  333. ```bash
  334. 'Accuracy': 0.8642
  335. ```
  336. - GPU result
  337. Inference result is saved in ./eval/log, you can find result like this.
  338. ```bash
  339. accuracy: 0.8533
  340. ```
  341. # [Model Description](#contents)
  342. ## [Performance](#contents)
  343. ### Training Performance
  344. | Parameters | CNNCTC |
  345. | -------------------------- | ----------------------------------------------------------- |
  346. | Model Version | V1 |
  347. | Resource | Ascend 910; CPU 2.60GHz, 192cores; Memory 755G; OS Euler2.8 |
  348. | uploaded Date | 09/28/2020 (month/day/year) |
  349. | MindSpore Version | 1.0.0 |
  350. | Dataset | MJSynth,SynthText |
  351. | Training Parameters | epoch=3, batch_size=192 |
  352. | Optimizer | RMSProp |
  353. | Loss Function | CTCLoss |
  354. | Speed | 1pc: 250 ms/step; 8pcs: 260 ms/step |
  355. | Total time | 1pc: 15 hours; 8pcs: 1.92 hours |
  356. | Parameters (M) | 177 |
  357. | Scripts | <https://gitee.com/mindspore/models/tree/master/official/cv/cnnctc> |
  358. | Parameters | CNNCTC |
  359. | -------------------------- | ----------------------------------------------------------- |
  360. | Model Version | V1 |
  361. | Resource | GPU(Tesla V100-PCIE); CPU 2.60 GHz, 26 cores; Memory 790G; OS linux-gnu |
  362. | uploaded Date | 07/06/2021 (month/day/year) |
  363. | MindSpore Version | 1.0.0 |
  364. | Dataset | MJSynth,SynthText |
  365. | Training Parameters | epoch=3, batch_size=192 |
  366. | Optimizer | RMSProp |
  367. | Loss Function | CTCLoss |
  368. | Speed | 1pc: 1180 ms/step; 8pcs: 1180 ms/step |
  369. | Total time | 1pc: 62.9 hours; 8pcs: 8.67 hours |
  370. | Parameters (M) | 177 |
  371. | Scripts | <https://gitee.com/mindspore/models/tree/master/official/cv/cnnctc> |
  372. ### Evaluation Performance
  373. | Parameters | CNNCTC |
  374. | ------------------- | --------------------------- |
  375. | Model Version | V1 |
  376. | Resource | Ascend 910; OS Euler2.8 |
  377. | Uploaded Date | 09/28/2020 (month/day/year) |
  378. | MindSpore Version | 1.0.0 |
  379. | Dataset | IIIT5K |
  380. | batch_size | 192 |
  381. | outputs | Accuracy |
  382. | Accuracy | 85% |
  383. | Model for inference | 675M (.ckpt file) |
  384. ### Inference Performance
  385. | Parameters | Ascend |
  386. | ------------------- | --------------------------- |
  387. | Model Version | CNNCTC |
  388. | Resource | Ascend 310; CentOS 3.10 |
  389. | Uploaded Date | 19/05/2021 (month/day/year) |
  390. | MindSpore Version | 1.2.0 |
  391. | Dataset | IIIT5K |
  392. | batch_size | 1 |
  393. | outputs | Accuracy |
  394. | Accuracy | Accuracy=0.8642 |
  395. | Model for inference | 675M(.ckpt file) |
  396. ## [How to use](#contents)
  397. ### Inference
  398. If you need to use the trained model to perform inference on multiple hardware platforms, such as GPU, Ascend 910 or Ascend 310, you can refer to this [Link](https://www.mindspore.cn/tutorials/experts/en/r1.7/infer/inference.html). Following the steps below, this is a simple example:
  399. - Running on Ascend
  400. ```python
  401. # Set context
  402. context.set_context(mode=context.GRAPH_HOME, device_target=cfg.device_target)
  403. context.set_context(device_id=cfg.device_id)
  404. # Load unseen dataset for inference
  405. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  406. # Define model
  407. net = CNNCTC(cfg.NUM_CLASS, cfg.HIDDEN_SIZE, cfg.FINAL_FEATURE_WIDTH)
  408. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  409. cfg.momentum, weight_decay=cfg.weight_decay)
  410. loss = P.CTCLoss(preprocess_collapse_repeated=False,
  411. ctc_merge_repeated=True,
  412. ignore_longer_outputs_than_inputs=False)
  413. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  414. # Load pre-trained model
  415. param_dict = load_checkpoint(cfg.checkpoint_path)
  416. load_param_into_net(net, param_dict)
  417. net.set_train(False)
  418. # Make predictions on the unseen dataset
  419. acc = model.eval(dataset)
  420. print("accuracy: ", acc)
  421. ```
  422. ### Continue Training on the Pretrained Model
  423. - running on Ascend
  424. ```python
  425. # Load dataset
  426. dataset = create_dataset(cfg.data_path, 1)
  427. batch_num = dataset.get_dataset_size()
  428. # Define model
  429. net = CNNCTC(cfg.NUM_CLASS, cfg.HIDDEN_SIZE, cfg.FINAL_FEATURE_WIDTH)
  430. # Continue training if set pre_trained to be True
  431. if cfg.pre_trained:
  432. param_dict = load_checkpoint(cfg.checkpoint_path)
  433. load_param_into_net(net, param_dict)
  434. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  435. steps_per_epoch=batch_num)
  436. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  437. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  438. loss = P.CTCLoss(preprocess_collapse_repeated=False,
  439. ctc_merge_repeated=True,
  440. ignore_longer_outputs_than_inputs=False)
  441. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  442. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  443. # Set callbacks
  444. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  445. keep_checkpoint_max=cfg.keep_checkpoint_max)
  446. time_cb = TimeMonitor(data_size=batch_num)
  447. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./",
  448. config=config_ck)
  449. loss_cb = LossMonitor()
  450. # Start training
  451. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  452. print("train success")
  453. ```
  454. # [ModelZoo Homepage](#contents)
  455. Please check the official [homepage](https://gitee.com/mindspore/models).

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。