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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # ResNet Example
  2. ## Description
  3. These are examples of training ResNet-50/ResNet-101 with CIFAR-10/ImageNet2012 dataset in MindSpore.
  4. (Training ResNet-101 with dataset CIFAR-10 is unsupported now.)
  5. ## Requirements
  6. - Install [MindSpore](https://www.mindspore.cn/install/en).
  7. - Download the dataset CIFAR-10 or ImageNet2012
  8. CIFAR-10
  9. > Unzip the CIFAR-10 dataset to any path you want and the folder structure should include train and eval dataset as follows:
  10. > ```
  11. > .
  12. > └─dataset
  13. > ├─ cifar-10-batches-bin # train dataset
  14. > └─ cifar-10-verify-bin # evaluate dataset
  15. > ```
  16. ImageNet2012
  17. > Unzip the ImageNet2012 dataset to any path you want and the folder should include train and eval dataset as follows:
  18. >
  19. > ```
  20. > .
  21. > └─dataset
  22. > ├─ilsvrc # train dataset
  23. > └─validation_preprocess # evaluate dataset
  24. > ```
  25. ## Structure
  26. ```shell
  27. .
  28. └──resnet
  29. ├── README.md
  30. ├── script
  31. ├── run_distribute_train.sh # launch distributed training(8 pcs)
  32. ├── run_eval.sh # launch evaluation
  33. └── run_standalone_train.sh # launch standalone training(1 pcs)
  34. ├── src
  35. ├── config.py # parameter configuration
  36. ├── dataset.py # data preprocessing
  37. ├── crossentropy.py # loss definition for ImageNet2012 dataset
  38. ├── lr_generator.py # generate learning rate for each step
  39. └── resnet.py # resnet backbone, including resnet50 and resnet101
  40. ├── eval.py # eval net
  41. └── train.py # train net
  42. ```
  43. ## Parameter configuration
  44. Parameters for both training and evaluation can be set in config.py.
  45. - config for ResNet-50, CIFAR-10 dataset
  46. ```
  47. "class_num": 10, # dataset class num
  48. "batch_size": 32, # batch size of input tensor
  49. "loss_scale": 1024, # loss scale
  50. "momentum": 0.9, # momentum
  51. "weight_decay": 1e-4, # weight decay
  52. "epoch_size": 90, # only valid for taining, which is always 1 for inference
  53. "save_checkpoint": True, # whether save checkpoint or not
  54. "save_checkpoint_steps": 195, # the step interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  55. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  56. "save_checkpoint_path": "./", # path to save checkpoint
  57. "warmup_epochs": 5, # number of warmup epoch
  58. "lr_decay_mode": "poly" # decay mode can be selected in steps, ploy and default
  59. "lr_init": 0.01, # initial learning rate
  60. "lr_end": 0.00001, # final learning rate
  61. "lr_max": 0.1, # maximum learning rate
  62. ```
  63. - config for ResNet-50, ImageNet2012 dataset
  64. ```
  65. "class_num": 1001, # dataset class number
  66. "batch_size": 32, # batch size of input tensor
  67. "loss_scale": 1024, # loss scale
  68. "momentum": 0.9, # momentum optimizer
  69. "weight_decay": 1e-4, # weight decay
  70. "epoch_size": 90, # only valid for taining, which is always 1 for inference
  71. "pretrained_epoch_size": 1, # epoch size that model has been trained before load pretrained checkpoint
  72. "save_checkpoint": True, # whether save checkpoint or not
  73. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last epoch
  74. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  75. "save_checkpoint_path": "./", # path to save checkpoint relative to the executed path
  76. "warmup_epochs": 0, # number of warmup epoch
  77. "lr_decay_mode": "cosine", # decay mode for generating learning rate
  78. "label_smooth": True, # label smooth
  79. "label_smooth_factor": 0.1, # label smooth factor
  80. "lr_init": 0, # initial learning rate
  81. "lr_max": 0.1, # maximum learning rate
  82. ```
  83. - config for ResNet-101, ImageNet2012 dataset
  84. ```
  85. "class_num": 1001, # dataset class number
  86. "batch_size": 32, # batch size of input tensor
  87. "loss_scale": 1024, # loss scale
  88. "momentum": 0.9, # momentum optimizer
  89. "weight_decay": 1e-4, # weight decay
  90. "epoch_size": 120, # epoch sizes for training
  91. "pretrain_epoch_size": 0, # epoch size of pretrain checkpoint
  92. "save_checkpoint": True, # whether save checkpoint or not
  93. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last epoch
  94. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  95. "save_checkpoint_path": "./", # path to save checkpoint relative to the executed path
  96. "warmup_epochs": 0, # number of warmup epoch
  97. "lr_decay_mode": "cosine" # decay mode for generating learning rate
  98. "label_smooth": 1, # label_smooth
  99. "label_smooth_factor": 0.1, # label_smooth_factor
  100. "lr": 0.1 # base learning rate
  101. ```
  102. ## Running the example
  103. ### Train
  104. #### Usage
  105. ```
  106. # distributed training
  107. Usage: sh run_distribute_train.sh [resnet50|resnet101] [cifar10|imagenet2012] [MINDSPORE_HCCL_CONFIG_PATH] [DATASET_PATH]
  108. [PRETRAINED_CKPT_PATH](optional)
  109. # standalone training
  110. Usage: sh run_standalone_train.sh [resnet50|resnet101] [cifar10|imagenet2012] [DATASET_PATH]
  111. [PRETRAINED_CKPT_PATH](optional)
  112. ```
  113. #### Launch
  114. ```
  115. # distribute training example
  116. sh run_distribute_train.sh resnet50 cifar10 rank_table.json ~/cifar-10-batches-bin
  117. # standalone training example
  118. sh run_standalone_train.sh resnet50 cifar10 ~/cifar-10-batches-bin
  119. ```
  120. > About rank_table.json, you can refer to the [distributed training tutorial](https://www.mindspore.cn/tutorial/en/master/advanced_use/distributed_training.html).
  121. #### Result
  122. Training result will be stored in the example path, whose folder name begins with "train" or "train_parallel". Under this, you can find checkpoint file together with result like the followings in log.
  123. - training ResNet-50 with CIFAR-10 dataset
  124. ```
  125. # distribute training result(8 pcs)
  126. epoch: 1 step: 195, loss is 1.9601055
  127. epoch: 2 step: 195, loss is 1.8555021
  128. epoch: 3 step: 195, loss is 1.6707983
  129. epoch: 4 step: 195, loss is 1.8162166
  130. epoch: 5 step: 195, loss is 1.393667
  131. ...
  132. ```
  133. - training ResNet-50 with ImageNet2012 dataset
  134. ```
  135. # distribute training result(8 pcs)
  136. epoch: 1 step: 5004, loss is 4.8995576
  137. epoch: 2 step: 5004, loss is 3.9235563
  138. epoch: 3 step: 5004, loss is 3.833077
  139. epoch: 4 step: 5004, loss is 3.2795618
  140. epoch: 5 step: 5004, loss is 3.1978393
  141. ...
  142. ```
  143. - training ResNet-101 with ImageNet2012 dataset
  144. ```
  145. # distribute training result(8p)
  146. epoch: 1 step: 5004, loss is 4.805483
  147. epoch: 2 step: 5004, loss is 3.2121816
  148. epoch: 3 step: 5004, loss is 3.429647
  149. epoch: 4 step: 5004, loss is 3.3667371
  150. epoch: 5 step: 5004, loss is 3.1718972
  151. ...
  152. epoch: 67 step: 5004, loss is 2.2768745
  153. epoch: 68 step: 5004, loss is 1.7223864
  154. epoch: 69 step: 5004, loss is 2.0665488
  155. epoch: 70 step: 5004, loss is 1.8717369
  156. ...
  157. ```
  158. ### Evaluation
  159. #### Usage
  160. ```
  161. # evaluation
  162. Usage: sh run_eval.sh [resnet50|resnet101] [cifar10|imagenet2012] [DATASET_PATH] [CHECKPOINT_PATH]
  163. ```
  164. #### Launch
  165. ```
  166. # evaluation example
  167. sh run_eval.sh resnet50 cifar10 ~/cifar10-10-verify-bin ~/resnet50_cifar10/train_parallel0/resnet-90_195.ckpt
  168. ```
  169. > checkpoint can be produced in training process.
  170. #### Result
  171. Evaluation result will be stored in the example path, whose folder name is "eval". Under this, you can find result like the followings in log.
  172. - evaluating ResNet-50 with CIFAR-10 dataset
  173. ```
  174. result: {'acc': 0.91446314102564111} ckpt=~/resnet50_cifar10/train_parallel0/resnet-90_195.ckpt
  175. ```
  176. - evaluating ResNet-50 with ImageNet2012 dataset
  177. ```
  178. result: {'acc': 0.7671054737516005} ckpt=train_parallel0/resnet-90_5004.ckpt
  179. ```
  180. - evaluating ResNet-101 with ImageNet2012 dataset
  181. ```
  182. result: {'top_5_accuracy': 0.9429417413572343, 'top_1_accuracy': 0.7853513124199744} ckpt=train_parallel0/resnet-120_5004.ckpt
  183. ```
  184. ### Running on GPU
  185. ```
  186. # distributed training example
  187. mpirun -n 8 python train.py ---net=resnet50 --dataset=cifar10 -dataset_path=~/cifar-10-batches-bin --device_target="GPU" --run_distribute=True
  188. # standalone training example
  189. python train.py --net=resnet50 --dataset=cifar10 --dataset_path=~/cifar-10-batches-bin --device_target="GPU"
  190. # infer example
  191. python eval.py --net=resnet50 --dataset=cifar10 --dataset_path=~/cifar10-10-verify-bin --device_target="GPU" --checkpoint_path=resnet-90_195.ckpt
  192. ```