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

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