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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Contents
  2. - [SimCLR Description](#simclr-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 Process](#training-process)
  11. - [Training](#training)
  12. - [Evaluation Process](#evaluation-process)
  13. - [Evaluation](#evaluation)
  14. - [Model Description](#model-description)
  15. - [Performance](#performance)
  16. - [Evaluation Performance](#evaluation-performance)
  17. - [ModelZoo Homepage](#modelzoo-homepage)
  18. ## [SimCLR Description](#contents)
  19. SimCLR: a simple framework for contrastive learning of visual representations.
  20. [Paper](https://arxiv.org/pdf/2002.05709.pdf): Ting Chen and Simon Kornblith and Mohammad Norouzi and Geoffrey Hinton. A Simple Framework for Contrastive Learning of Visual Representations. *arXiv preprint arXiv:2002.05709*. 2020.
  21. ## [Model Architecture](#contents)
  22. SimCLR learns representations by maximizing agreement between differently augmented views of the same data example via a contrastive loss in the latent space. This framework comprises the following four major components: a stochastic data augmentation module, a neural network base encoder, a small neural network projection head and a contrastive loss function.
  23. ## [Dataset](#contents)
  24. In the following sections, we will introduce how to run the scripts using the related dataset below.
  25. Dataset used: [CIFAR-10](<http://www.cs.toronto.edu/~kriz/cifar.html>)
  26. - Dataset size:175M,60,000 32*32 colorful images in 10 classes
  27. - Train:146M,50,000 images
  28. - Test:29.3M,10,000 images
  29. - Data format:binary files
  30. - Note:Data will be processed in dataset.py
  31. - Download the dataset, the directory structure is as follows:
  32. ```bash
  33. ├─cifar-10-batches-bin
  34. └─cifar-10-verify-bin
  35. ```
  36. ## [Environment Requirements](#contents)
  37. - Hardware(Ascend)
  38. - Prepare hardware environment with Ascend processor.
  39. - Framework
  40. - [MindSpore](https://www.mindspore.cn/install/en)
  41. - For more information, please check the resources below:
  42. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  43. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  44. ## [Quick Start](#contents)
  45. After installing MindSpore via the official website, you can start training and evaluation as follows:
  46. ```python
  47. # enter script dir, train SimCLR
  48. sh run_standalone_train_ascend.sh [cifar10] [TRAIN_DATASET_PATH] [DEVICE_ID]
  49. or
  50. sh run_distribution_ascend.sh [DEVICENUM] [RANK_TABLE_FILE] [cifar10] [TRAIN_DATASET_PATH]
  51. # enter script dir, evaluate SimCLR
  52. sh run_standalone_eval_ascend.sh [cifar10] [DEVICE_ID] [SIMCLR_MODEL_PATH] [TRAIN_DATASET_PATH] [EVAL_DATASET_PATH]
  53. ```
  54. ## [Script Description](#contents)
  55. ### [Script and Sample Code](#contents)
  56. ```bash
  57. ├── cv
  58. ├── SimCLR
  59. ├── README.md // descriptions about SimCLR
  60. ├── requirements.txt // package needed
  61. ├── scripts
  62. │ ├──run_distribution_train_ascend.sh // train in ascend
  63. │ ├──run_standalone_train_ascend.sh // train in ascend
  64. │ ├──run_standalone_eval_ascend.sh // evaluate in ascend
  65. ├── src
  66. │ ├──dataset.py // creating dataset
  67. │ ├──lr_generator.py // generating learning rate
  68. │ ├──nt_xent.py // contrastive cross entropy loss
  69. │ ├──optimizer.py // generating optimizer
  70. │ ├──resnet.py // base encoder network
  71. │ ├──simclr_model.py // simclr architecture
  72. ├── train.py // training script
  73. ├── linear_eval.py // linear evaluation script
  74. ├── export.py // export model for inference
  75. ```
  76. ### [Script Parameters](#contents)
  77. ```python
  78. Major parameters in train.py as follows:
  79. --device_target: Device target, Currently only Ascend is supported.
  80. --run_cloudbrain: Whether it is running on CloudBrain platform.
  81. --run_distribute: Run distributed training.
  82. --device_num: Device num.
  83. --device_id: Device id, default is 0.
  84. --dataset_name: Dataset, Currently only cifar10 is supported.
  85. --train_url: Cloudbrain Location of training outputs.This parameter needs to be set when running on the cloud brain platform.
  86. --data_url: Cloudbrain Location of data. This parameter needs to be set when running on the cloud brain platform.
  87. --train_dataset_path: Dataset path for training classifier. This parameter needs to be set when running on the host.
  88. --train_output_path: Location of ckpt and log. This parameter needs to be set when running on the host.
  89. --batch_size: Batch size, default is 128.
  90. --epoch_size: Epoch size for training, default is 100.
  91. --projection_dimension: Projection output dimensionality, default is 128.
  92. --width_multiplier: Width multiplier for ResNet50, default is 1.
  93. --temperature: Temperature for contrastive cross entropy loss.
  94. --pre_trained_path: Pretrained checkpoint path.
  95. --pretrain_epoch_size: real_epoch_size = epoch_size - pretrain_epoch_size.
  96. save_checkpoint_epochs: Save checkpoint epochs, default is 1.
  97. --save_graphs: Whether save graphs, default is False.
  98. --optimizer: Optimizer, Currently only Adam is supported.
  99. --weight_decay: Weight decay.
  100. --warmup_epochs: Warmup epochs.
  101. Major parameters in linear_eval.py as follows:
  102. --device_target: Device target, Currently only Ascend is supported.
  103. --run_cloudbrain: Whether it is running on CloudBrain platform.
  104. --run_distribute: Run distributed training.
  105. --device_num: Device num.
  106. --device_id: Device id, default is 0.
  107. --dataset_name: Dataset, Currently only cifar10 is supported.
  108. --train_url: Cloudbrain Location of training outputs.This parameter needs to be set when running on the cloud brain platform.
  109. --data_url: Cloudbrain Location of data. This parameter needs to be set when running on the cloud brain platform.
  110. --train_dataset_path: Dataset path for training classifier. This parameter needs to be set when running on the host.
  111. --eval_dataset_path: Dataset path for evaluating classifier.This parameter needs to be set when running on the host.
  112. --train_output_path: Location of ckpt and log. This parameter needs to be set when running on the host.
  113. --class_num: dataset classification number, default is 10 for cifar10.
  114. --batch_size: Batch size, default is 128.
  115. --epoch_size: Epoch size for training, default is 100.
  116. --projection_dimension: Projection output dimensionality, default is 128.
  117. --width_multiplier: Width multiplier for ResNet50, default is 1.
  118. --pre_classifier_checkpoint_path: Classifier Checkpoint file path.
  119. --encoder_checkpoint_path: Encoder Checkpoint file path.
  120. --save_checkpoint_epochs: Save checkpoint epochs, default is 10.
  121. --print_iter: Log print iter, default is 100.
  122. --save_graphs: whether save graphs, default is False.
  123. ```
  124. ### [Training Process](#contents)
  125. #### Training
  126. - running on Ascend
  127. ```bash
  128. sh run_distribution_ascend.sh [DEVICENUM] [RANK_TABLE_FILE] [cifar10] [TRAIN_DATASET_PATH]
  129. ```
  130. After training, the loss value will be achieved as follows:
  131. ```bash
  132. # grep "loss is " log
  133. epoch: 1 step: 48, loss is 9.5758915
  134. epoch time: 253236.075 ms, per step time: 5275.752 ms
  135. epoch: 1 step: 48, loss is 9.363186
  136. epoch time: 253739.376 ms, per step time: 5286.237 ms
  137. epoch: 1 step: 48, loss is 9.36029
  138. epoch time: 253711.625 ms, per step time: 5285.659 ms
  139. ...
  140. epoch: 100 step: 48, loss is 7.453776
  141. epoch time: 12341.851 ms, per step time: 257.122 ms
  142. epoch: 100 step: 48, loss is 7.499168
  143. epoch time: 12420.060 ms, per step time: 258.751 ms
  144. epoch: 100 step: 48, loss is 7.442362
  145. epoch time: 12725.863 ms, per step time: 265.122 ms
  146. ...
  147. ```
  148. The model checkpoint will be saved in the outputs directory.
  149. ### [Evaluation Process](#contents)
  150. #### Evaluation
  151. Before running the command below, please check the checkpoint path used for evaluation.
  152. - running on Ascend
  153. ```bash
  154. sh run_standalone_eval_ascend.sh [cifar10] [DEVICE_ID] [SIMCLR_MODEL_PATH] [TRAIN_DATASET_PATH] [EVAL_DATASET_PATH]
  155. ```
  156. You can view the results through the file "eval_log". The accuracy of the test dataset will be as follows:
  157. ```bash
  158. # grep "Average accuracy: " eval_log
  159. 'Accuracy': 0.84505
  160. ```
  161. ## [Model Description](#contents)
  162. ### [Performance](#contents)
  163. #### Evaluation Performance
  164. | Parameters | Ascend |
  165. | -------------------------- | ------------------------------------------------------------|
  166. | Resource | Ascend 910; CPU 2.60GHz, 192cores; Memory, 755G |
  167. | uploaded Date | 30/03/2021 (month/day/year) |
  168. | MindSpore Version | 1.1.1 |
  169. | Dataset | CIFAR-10 |
  170. | Training Parameters | epoch=100, batch_size=128, device_num=8 |
  171. | Optimizer | Adam |
  172. | Loss Function | NT-Xent Loss |
  173. | linear eval | 84.505% |
  174. | Total time | 25m04s |
  175. | Scripts | [SimCLR Script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/simclr) | [SimCLR Script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/simclr) |
  176. ## [Description of Random Situation](#contents)
  177. We set the seed inside dataset.py. We also use random seed in train.py.
  178. ## [ModelZoo Homepage](#contents)
  179. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).