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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. # Contents
  2. - [SqueezeNet Description](#squeezenet-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. - [Evaluation Process](#evaluation-process)
  14. - [Model Description](#model-description)
  15. - [Performance](#performance)
  16. - [Evaluation Performance](#evaluation-performance)
  17. - [Inference Performance](#inference-performance)
  18. - [How to use](#how-to-use)
  19. - [Inference](#inference)
  20. - [Continue Training on the Pretrained Model](#continue-training-on-the-pretrained-model)
  21. - [Transfer Learning](#transfer-learning)
  22. - [Description of Random Situation](#description-of-random-situation)
  23. - [ModelZoo Homepage](#modelzoo-homepage)
  24. # [SqueezeNet Description](#contents)
  25. SqueezeNet is a lightweight and efficient CNN model proposed by Han et al., published in ICLR-2017. SqueezeNet has 50x fewer parameters than AlexNet, but the model performance (accuracy) is close to AlexNet.
  26. These are examples of training SqueezeNet/SqueezeNet_Residual with CIFAR-10/ImageNet dataset in MindSpore. SqueezeNet_Residual adds residual operation on the basis of SqueezeNet, which can improve the accuracy of the model without increasing the amount of parameters.
  27. [Paper](https://arxiv.org/abs/1602.07360): Forrest N. Iandola and Song Han and Matthew W. Moskewicz and Khalid Ashraf and William J. Dally and Kurt Keutzer. "SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size"
  28. # [Model Architecture](#contents)
  29. SqueezeNet is composed of fire modules. A fire module mainly includes two layers of convolution operations: one is the squeeze layer using a **1x1 convolution** kernel; the other is an expand layer using a mixture of **1x1** and **3x3 convolution** kernels.
  30. # [Dataset](#contents)
  31. Dataset used: [CIFAR-10](<http://www.cs.toronto.edu/~kriz/cifar.html>)
  32. - Dataset size:175M,60,000 32*32 colorful images in 10 classes
  33. - Train:146M,50,000 images
  34. - Test:29M,10,000 images
  35. - Data format:binary files
  36. - Note:Data will be processed in src/dataset.py
  37. Dataset used: [ImageNet2012](http://www.image-net.org/)
  38. - Dataset size: 125G, 1250k colorful images in 1000 classes
  39. - Train: 120G, 1200k images
  40. - Test: 5G, 50k images
  41. - Data format: RGB images.
  42. - Note: Data will be processed in src/dataset.py
  43. # [Features](#contents)
  44. ## Mixed Precision
  45. The [mixed precision](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/enable_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.
  46. 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’.
  47. # [Environment Requirements](#contents)
  48. - Hardware(Ascend/GPU)
  49. - 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. Once approved, you can get the resources.
  50. - Framework
  51. - [MindSpore](https://www.mindspore.cn/install/en)
  52. - For more information, please check the resources below:
  53. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  54. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  55. # [Quick Start](#contents)
  56. After installing MindSpore via the official website, you can start training and evaluation as follows:
  57. - runing on Ascend
  58. ```
  59. # distributed training
  60. Usage: sh scripts/run_distribute_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [RANK_TABLE_FILE] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  61. # standalone training
  62. Usage: sh scripts/run_standalone_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  63. # run evaluation example
  64. Usage: sh scripts/run_eval.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  65. ```
  66. - running on GPU
  67. ```
  68. # distributed training example
  69. sh scripts/run_distribute_train_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  70. # standalone training example
  71. sh scripts/run_standalone_train_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  72. # run evaluation example
  73. sh scripts/run_eval_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  74. ```
  75. # [Script Description](#contents)
  76. ## [Script and Sample Code](#contents)
  77. ```
  78. .
  79. └── squeezenet
  80. ├── README.md
  81. ├── scripts
  82. ├── run_distribute_train.sh # launch ascend distributed training(8 pcs)
  83. ├── run_standalone_train.sh # launch ascend standalone training(1 pcs)
  84. ├── run_distribute_train_gpu.sh # launch gpu distributed training(8 pcs)
  85. ├── run_standalone_train_gpu.sh # launch gpu standalone training(1 pcs)
  86. ├── run_eval.sh # launch ascend evaluation
  87. └── run_eval_gpu.sh # launch gpu evaluation
  88. ├── src
  89. ├── config.py # parameter configuration
  90. ├── dataset.py # data preprocessing
  91. ├── CrossEntropySmooth.py # loss definition for ImageNet dataset
  92. ├── lr_generator.py # generate learning rate for each step
  93. └── squeezenet.py # squeezenet architecture, including squeezenet and squeezenet_residual
  94. ├── train.py # train net
  95. ├── eval.py # eval net
  96. └── export.py # export checkpoint files into geir/onnx
  97. ```
  98. ## [Script Parameters](#contents)
  99. Parameters for both training and evaluation can be set in config.py
  100. - config for SqueezeNet, CIFAR-10 dataset
  101. ```py
  102. "class_num": 10, # dataset class num
  103. "batch_size": 32, # batch size of input tensor
  104. "loss_scale": 1024, # loss scale
  105. "momentum": 0.9, # momentum
  106. "weight_decay": 1e-4, # weight decay
  107. "epoch_size": 120, # only valid for taining, which is always 1 for inference
  108. "pretrain_epoch_size": 0, # epoch size that model has been trained before loading pretrained checkpoint, actual training epoch size is equal to epoch_size minus pretrain_epoch_size
  109. "save_checkpoint": True, # whether save checkpoint or not
  110. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  111. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  112. "save_checkpoint_path": "./", # path to save checkpoint
  113. "warmup_epochs": 5, # number of warmup epoch
  114. "lr_decay_mode": "poly" # decay mode for generating learning rate
  115. "lr_init": 0, # initial learning rate
  116. "lr_end": 0, # final learning rate
  117. "lr_max": 0.01, # maximum learning rate
  118. ```
  119. - config for SqueezeNet, ImageNet dataset
  120. ```py
  121. "class_num": 1000, # dataset class num
  122. "batch_size": 32, # batch size of input tensor
  123. "loss_scale": 1024, # loss scale
  124. "momentum": 0.9, # momentum
  125. "weight_decay": 7e-5, # weight decay
  126. "epoch_size": 200, # only valid for taining, which is always 1 for inference
  127. "pretrain_epoch_size": 0, # epoch size that model has been trained before loading pretrained checkpoint, actual training epoch size is equal to epoch_size minus pretrain_epoch_size
  128. "save_checkpoint": True, # whether save checkpoint or not
  129. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  130. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  131. "save_checkpoint_path": "./", # path to save checkpoint
  132. "warmup_epochs": 0, # number of warmup epoch
  133. "lr_decay_mode": "poly" # decay mode for generating learning rate
  134. "use_label_smooth": True, # label smooth
  135. "label_smooth_factor": 0.1, # label smooth factor
  136. "lr_init": 0, # initial learning rate
  137. "lr_end": 0, # final learning rate
  138. "lr_max": 0.01, # maximum learning rate
  139. ```
  140. - config for SqueezeNet_Residual, CIFAR-10 dataset
  141. ```py
  142. "class_num": 10, # dataset class num
  143. "batch_size": 32, # batch size of input tensor
  144. "loss_scale": 1024, # loss scale
  145. "momentum": 0.9, # momentum
  146. "weight_decay": 1e-4, # weight decay
  147. "epoch_size": 150, # only valid for taining, which is always 1 for inference
  148. "pretrain_epoch_size": 0, # epoch size that model has been trained before loading pretrained checkpoint, actual training epoch size is equal to epoch_size minus pretrain_epoch_size
  149. "save_checkpoint": True, # whether save checkpoint or not
  150. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  151. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  152. "save_checkpoint_path": "./", # path to save checkpoint
  153. "warmup_epochs": 5, # number of warmup epoch
  154. "lr_decay_mode": "linear" # decay mode for generating learning rate
  155. "lr_init": 0, # initial learning rate
  156. "lr_end": 0, # final learning rate
  157. "lr_max": 0.01, # maximum learning rate
  158. ```
  159. - config for SqueezeNet_Residual, ImageNet dataset
  160. ```py
  161. "class_num": 1000, # dataset class num
  162. "batch_size": 32, # batch size of input tensor
  163. "loss_scale": 1024, # loss scale
  164. "momentum": 0.9, # momentum
  165. "weight_decay": 7e-5, # weight decay
  166. "epoch_size": 300, # only valid for taining, which is always 1 for inference
  167. "pretrain_epoch_size": 0, # epoch size that model has been trained before loading pretrained checkpoint, actual training epoch size is equal to epoch_size minus pretrain_epoch_size
  168. "save_checkpoint": True, # whether save checkpoint or not
  169. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  170. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  171. "save_checkpoint_path": "./", # path to save checkpoint
  172. "warmup_epochs": 0, # number of warmup epoch
  173. "lr_decay_mode": "cosine" # decay mode for generating learning rate
  174. "use_label_smooth": True, # label smooth
  175. "label_smooth_factor": 0.1, # label smooth factor
  176. "lr_init": 0, # initial learning rate
  177. "lr_end": 0, # final learning rate
  178. "lr_max": 0.01, # maximum learning rate
  179. ```
  180. For more configuration details, please refer the script `config.py`.
  181. ## [Training Process](#contents)
  182. ### Usage
  183. #### Running on Ascend
  184. ```
  185. # distributed training
  186. Usage: sh scripts/run_distribute_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [RANK_TABLE_FILE] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  187. # standalone training
  188. Usage: sh scripts/run_standalone_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  189. ```
  190. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  191. Please follow the instructions in the link [hccl_tools](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools).
  192. 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.
  193. #### Running on GPU
  194. ```
  195. # distributed training example
  196. sh scripts/run_distribute_train_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  197. # standalone training example
  198. sh scripts/run_standalone_train_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  199. ```
  200. ### Result
  201. - Training SqueezeNet with CIFAR-10 dataset
  202. ```
  203. # standalone training result
  204. epoch: 1 step 1562, loss is 1.7103254795074463
  205. epoch: 2 step 1562, loss is 2.06101131439209
  206. epoch: 3 step 1562, loss is 1.5594401359558105
  207. epoch: 4 step 1562, loss is 1.4127278327941895
  208. epoch: 5 step 1562, loss is 1.2140142917633057
  209. ...
  210. ```
  211. - Training SqueezeNet with ImageNet dataset
  212. ```
  213. # distribute training result(8 pcs)
  214. epoch: 1 step 5004, loss is 5.716324329376221
  215. epoch: 2 step 5004, loss is 5.350603103637695
  216. epoch: 3 step 5004, loss is 4.580031394958496
  217. epoch: 4 step 5004, loss is 4.784664154052734
  218. epoch: 5 step 5004, loss is 4.136358261108398
  219. ...
  220. ```
  221. - Training SqueezeNet_Residual with CIFAR-10 dataset
  222. ```
  223. # standalone training result
  224. epoch: 1 step 1562, loss is 2.298271656036377
  225. epoch: 2 step 1562, loss is 2.2728664875030518
  226. epoch: 3 step 1562, loss is 1.9493038654327393
  227. epoch: 4 step 1562, loss is 1.7553865909576416
  228. epoch: 5 step 1562, loss is 1.3370063304901123
  229. ...
  230. ```
  231. - Training SqueezeNet_Residual with ImageNet dataset
  232. ```
  233. # distribute training result(8 pcs)
  234. epoch: 1 step 5004, loss is 6.802495002746582
  235. epoch: 2 step 5004, loss is 6.386072158813477
  236. epoch: 3 step 5004, loss is 5.513605117797852
  237. epoch: 4 step 5004, loss is 5.312961101531982
  238. epoch: 5 step 5004, loss is 4.888848304748535
  239. ...
  240. ```
  241. ## [Evaluation Process](#contents)
  242. ### Usage
  243. #### Running on Ascend
  244. ```
  245. # evaluation
  246. Usage: sh scripts/run_eval.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  247. ```
  248. ```
  249. # evaluation example
  250. sh scripts/run_eval.sh squeezenet cifar10 0 ~/cifar-10-verify-bin train/squeezenet_cifar10-120_1562.ckpt
  251. ```
  252. checkpoint can be produced in training process.
  253. #### Running on GPU
  254. ```
  255. sh scripts/run_eval_gpu.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  256. ```
  257. ### Result
  258. 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.
  259. - Evaluating SqueezeNet with CIFAR-10 dataset
  260. ```
  261. result: {'top_1_accuracy': 0.8896233974358975, 'top_5_accuracy': 0.9965945512820513}
  262. ```
  263. - Evaluating SqueezeNet with ImageNet dataset
  264. ```
  265. result: {'top_1_accuracy': 0.5851472471190781, 'top_5_accuracy': 0.8105393725992317}
  266. ```
  267. - Evaluating SqueezeNet_Residual with CIFAR-10 dataset
  268. ```
  269. result: {'top_1_accuracy': 0.9077524038461539, 'top_5_accuracy': 0.9969951923076923}
  270. ```
  271. - Evaluating SqueezeNet_Residual with ImageNet dataset
  272. ```
  273. result: {'top_1_accuracy': 0.6094950384122919, 'top_5_accuracy': 0.826324423815621}
  274. ```
  275. # [Model Description](#contents)
  276. ## [Performance](#contents)
  277. ### Evaluation Performance
  278. #### SqueezeNet on CIFAR-10
  279. | Parameters | Ascend |
  280. | -------------------------- | ----------------------------------------------------------- |
  281. | Model Version | SqueezeNet |
  282. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  283. | uploaded Date | 11/06/2020 (month/day/year) |
  284. | MindSpore Version | 1.0.0 |
  285. | Dataset | CIFAR-10 |
  286. | Training Parameters | epoch=120, steps=195, batch_size=32, lr=0.01 |
  287. | Optimizer | Momentum |
  288. | Loss Function | Softmax Cross Entropy |
  289. | outputs | probability |
  290. | Loss | 0.0496 |
  291. | Speed | 1pc: 16.7 ms/step; 8pcs: 17.0 ms/step |
  292. | Total time | 1pc: 55.5 mins; 8pcs: 15.0 mins |
  293. | Parameters (M) | 4.8 |
  294. | Checkpoint for Fine tuning | 6.4M (.ckpt file) |
  295. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  296. #### SqueezeNet on ImageNet
  297. | Parameters | Ascend |
  298. | -------------------------- | ----------------------------------------------------------- |
  299. | Model Version | SqueezeNet |
  300. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  301. | uploaded Date | 11/06/2020 (month/day/year) |
  302. | MindSpore Version | 1.0.0 |
  303. | Dataset | ImageNet |
  304. | Training Parameters | epoch=200, steps=5004, batch_size=32, lr=0.01 |
  305. | Optimizer | Momentum |
  306. | Loss Function | Softmax Cross Entropy |
  307. | outputs | probability |
  308. | Loss | 2.9150 |
  309. | Speed | 8pcs: 19.9 ms/step |
  310. | Total time | 8pcs: 5.2 hours |
  311. | Parameters (M) | 4.8 |
  312. | Checkpoint for Fine tuning | 13.3M (.ckpt file) |
  313. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  314. #### SqueezeNet_Residual on CIFAR-10
  315. | Parameters | Ascend |
  316. | -------------------------- | ----------------------------------------------------------- |
  317. | Model Version | SqueezeNet_Residual |
  318. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  319. | uploaded Date | 11/06/2020 (month/day/year) |
  320. | MindSpore Version | 1.0.0 |
  321. | Dataset | CIFAR-10 |
  322. | Training Parameters | epoch=150, steps=195, batch_size=32, lr=0.01 |
  323. | Optimizer | Momentum |
  324. | Loss Function | Softmax Cross Entropy |
  325. | outputs | probability |
  326. | Loss | 0.0641 |
  327. | Speed | 1pc: 16.9 ms/step; 8pcs: 17.3 ms/step |
  328. | Total time | 1pc: 68.6 mins; 8pcs: 20.9 mins |
  329. | Parameters (M) | 4.8 |
  330. | Checkpoint for Fine tuning | 6.5M (.ckpt file) |
  331. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  332. #### SqueezeNet_Residual on ImageNet
  333. | Parameters | Ascend |
  334. | -------------------------- | ----------------------------------------------------------- |
  335. | Model Version | SqueezeNet_Residual |
  336. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  337. | uploaded Date | 11/06/2020 (month/day/year) |
  338. | MindSpore Version | 1.0.0 |
  339. | Dataset | ImageNet |
  340. | Training Parameters | epoch=300, steps=5004, batch_size=32, lr=0.01 |
  341. | Optimizer | Momentum |
  342. | Loss Function | Softmax Cross Entropy |
  343. | outputs | probability |
  344. | Loss | 2.9040 |
  345. | Speed | 8pcs: 20.2 ms/step |
  346. | Total time | 8pcs: 8.0 hours |
  347. | Parameters (M) | 4.8 |
  348. | Checkpoint for Fine tuning | 15.3M (.ckpt file) |
  349. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  350. ### Inference Performance
  351. #### SqueezeNet on CIFAR-10
  352. | Parameters | Ascend |
  353. | ------------------- | --------------------------- |
  354. | Model Version | SqueezeNet |
  355. | Resource | Ascend 910 |
  356. | Uploaded Date | 11/06/2020 (month/day/year) |
  357. | MindSpore Version | 1.0.0 |
  358. | Dataset | CIFAR-10 |
  359. | batch_size | 32 |
  360. | outputs | probability |
  361. | Accuracy | 1pc: 89.0%; 8pcs: 84.4% |
  362. #### SqueezeNet on ImageNet
  363. | Parameters | Ascend |
  364. | ------------------- | --------------------------- |
  365. | Model Version | SqueezeNet |
  366. | Resource | Ascend 910 |
  367. | Uploaded Date | 11/06/2020 (month/day/year) |
  368. | MindSpore Version | 1.0.0 |
  369. | Dataset | ImageNet |
  370. | batch_size | 32 |
  371. | outputs | probability |
  372. | Accuracy | 8pcs: 58.5%(TOP1), 81.1%(TOP5) |
  373. #### SqueezeNet_Residual on CIFAR-10
  374. | Parameters | Ascend |
  375. | ------------------- | --------------------------- |
  376. | Model Version | SqueezeNet_Residual |
  377. | Resource | Ascend 910 |
  378. | Uploaded Date | 11/06/2020 (month/day/year) |
  379. | MindSpore Version | 1.0.0 |
  380. | Dataset | CIFAR-10 |
  381. | batch_size | 32 |
  382. | outputs | probability |
  383. | Accuracy | 1pc: 90.8%; 8pcs: 87.4% |
  384. #### SqueezeNet_Residual on ImageNet
  385. | Parameters | Ascend |
  386. | ------------------- | --------------------------- |
  387. | Model Version | SqueezeNet_Residual |
  388. | Resource | Ascend 910 |
  389. | Uploaded Date | 11/06/2020 (month/day/year) |
  390. | MindSpore Version | 1.0.0 |
  391. | Dataset | ImageNet |
  392. | batch_size | 32 |
  393. | outputs | probability |
  394. | Accuracy | 8pcs: 60.9%(TOP1), 82.6%(TOP5) |
  395. ## [How to use](#contents)
  396. ### Inference
  397. 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/tutorial/training/en/master/advanced_use/migrate_3rd_scripts.html). Following the steps below, this is a simple example:
  398. - Running on Ascend
  399. ```
  400. # Set context
  401. device_id = int(os.getenv('DEVICE_ID'))
  402. context.set_context(mode=context.GRAPH_MODE,
  403. device_target='Ascend',
  404. device_id=device_id)
  405. # Load unseen dataset for inference
  406. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  407. do_train=False,
  408. batch_size=config.batch_size,
  409. target='Ascend')
  410. # Define model
  411. net = squeezenet(num_classes=config.class_num)
  412. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  413. model = Model(net,
  414. loss_fn=loss,
  415. metrics={'top_1_accuracy', 'top_5_accuracy'})
  416. # Load pre-trained model
  417. param_dict = load_checkpoint(args_opt.checkpoint_path)
  418. load_param_into_net(net, param_dict)
  419. net.set_train(False)
  420. # Make predictions on the unseen dataset
  421. acc = model.eval(dataset)
  422. print("accuracy: ", acc)
  423. ```
  424. - Running on GPU:
  425. ```
  426. # Set context
  427. device_id = int(os.getenv('DEVICE_ID'))
  428. context.set_context(mode=context.GRAPH_MODE,
  429. device_target='GPU',
  430. device_id=device_id)
  431. # Load unseen dataset for inference
  432. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  433. do_train=False,
  434. batch_size=config.batch_size,
  435. target='GPU')
  436. # Define model
  437. net = squeezenet(num_classes=config.class_num)
  438. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  439. model = Model(net,
  440. loss_fn=loss,
  441. metrics={'top_1_accuracy', 'top_5_accuracy'})
  442. # Load pre-trained model
  443. param_dict = load_checkpoint(args_opt.checkpoint_path)
  444. load_param_into_net(net, param_dict)
  445. net.set_train(False)
  446. # Make predictions on the unseen dataset
  447. acc = model.eval(dataset)
  448. print("accuracy: ", acc)
  449. ```
  450. ### Continue Training on the Pretrained Model
  451. - running on Ascend
  452. ```
  453. # Load dataset
  454. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  455. do_train=True,
  456. repeat_num=1,
  457. batch_size=config.batch_size,
  458. target='Ascend')
  459. step_size = dataset.get_dataset_size()
  460. # define net
  461. net = squeezenet(num_classes=config.class_num)
  462. # load checkpoint
  463. if args_opt.pre_trained:
  464. param_dict = load_checkpoint(args_opt.pre_trained)
  465. load_param_into_net(net, param_dict)
  466. # init lr
  467. lr = get_lr(lr_init=config.lr_init,
  468. lr_end=config.lr_end,
  469. lr_max=config.lr_max,
  470. total_epochs=config.epoch_size,
  471. warmup_epochs=config.warmup_epochs,
  472. pretrain_epochs=config.pretrain_epoch_size,
  473. steps_per_epoch=step_size,
  474. lr_decay_mode=config.lr_decay_mode)
  475. lr = Tensor(lr)
  476. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  477. loss_scale = FixedLossScaleManager(config.loss_scale,
  478. drop_overflow_update=False)
  479. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  480. lr,
  481. config.momentum,
  482. config.weight_decay,
  483. config.loss_scale,
  484. use_nesterov=True)
  485. model = Model(net,
  486. loss_fn=loss,
  487. optimizer=opt,
  488. loss_scale_manager=loss_scale,
  489. metrics={'acc'},
  490. amp_level="O2",
  491. keep_batchnorm_fp32=False)
  492. # Set callbacks
  493. config_ck = CheckpointConfig(
  494. save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
  495. keep_checkpoint_max=config.keep_checkpoint_max)
  496. time_cb = TimeMonitor(data_size=step_size)
  497. ckpt_cb = ModelCheckpoint(prefix=args_opt.net + '_' + args_opt.dataset,
  498. directory=ckpt_save_dir,
  499. config=config_ck)
  500. loss_cb = LossMonitor()
  501. # Start training
  502. model.train(config.epoch_size - config.pretrain_epoch_size, dataset,
  503. callbacks=[time_cb, ckpt_cb, loss_cb])
  504. print("train success")
  505. ```
  506. - running on GPU
  507. ```
  508. # Load dataset
  509. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  510. do_train=True,
  511. repeat_num=1,
  512. batch_size=config.batch_size,
  513. target='Ascend')
  514. step_size = dataset.get_dataset_size()
  515. # define net
  516. net = squeezenet(num_classes=config.class_num)
  517. # load checkpoint
  518. if args_opt.pre_trained:
  519. param_dict = load_checkpoint(args_opt.pre_trained)
  520. load_param_into_net(net, param_dict)
  521. # init lr
  522. lr = get_lr(lr_init=config.lr_init,
  523. lr_end=config.lr_end,
  524. lr_max=config.lr_max,
  525. total_epochs=config.epoch_size,
  526. warmup_epochs=config.warmup_epochs,
  527. pretrain_epochs=config.pretrain_epoch_size,
  528. steps_per_epoch=step_size,
  529. lr_decay_mode=config.lr_decay_mode)
  530. lr = Tensor(lr)
  531. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  532. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  533. lr,
  534. config.momentum,
  535. config.weight_decay,
  536. use_nesterov=True)
  537. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  538. # Set callbacks
  539. config_ck = CheckpointConfig(
  540. save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
  541. keep_checkpoint_max=config.keep_checkpoint_max)
  542. time_cb = TimeMonitor(data_size=step_size)
  543. ckpt_cb = ModelCheckpoint(prefix=args_opt.net + '_' + args_opt.dataset,
  544. directory=ckpt_save_dir,
  545. config=config_ck)
  546. loss_cb = LossMonitor()
  547. # Start training
  548. model.train(config.epoch_size - config.pretrain_epoch_size, dataset,
  549. callbacks=[time_cb, ckpt_cb, loss_cb])
  550. print("train success")
  551. ```
  552. ### Transfer Learning
  553. To be added.
  554. # [Description of Random Situation](#contents)
  555. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  556. # [ModelZoo Homepage](#contents)
  557. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).