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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. Squeezenet training on GPU performs badly now, and it is still in research.
  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. ```bash
  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. ```bash
  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. ```shell
  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. ```bash
  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. ```bash
  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. ```shell
  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. ```shell
  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. ```shell
  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. ```shell
  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. ```shell
  245. # evaluation
  246. Usage: sh scripts/run_eval.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  247. ```
  248. ```shell
  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. ```shell
  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. ```shell
  261. result: {'top_1_accuracy': 0.8896233974358975, 'top_5_accuracy': 0.9965945512820513}
  262. ```
  263. - Evaluating SqueezeNet with ImageNet dataset
  264. ```shell
  265. result: {'top_1_accuracy': 0.5851472471190781, 'top_5_accuracy': 0.8105393725992317}
  266. ```
  267. - Evaluating SqueezeNet_Residual with CIFAR-10 dataset
  268. ```shell
  269. result: {'top_1_accuracy': 0.9077524038461539, 'top_5_accuracy': 0.9969951923076923}
  270. ```
  271. - Evaluating SqueezeNet_Residual with ImageNet dataset
  272. ```shell
  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 | Contents |
  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.1 |
  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(Ascend) | 1pc: 16.7 ms/step; 8pcs: 17.0 ms/step |
  292. | Speed(GPU) | 1pc: 44.27 ms/step; |
  293. | Total time(Ascend) | 1pc: 55.5 mins; 8pcs: 15.0 mins |
  294. | Parameters (M) | 4.8 |
  295. | Checkpoint for Fine tuning | 6.4M (.ckpt file) |
  296. #### SqueezeNet on ImageNet
  297. | Parameters | Contents |
  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.1 |
  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(Ascend) | 8pcs: 19.9 ms/step |
  310. | Speed(GPU) | 1pcs: 47.59 ms/step |
  311. | Total time(Ascend) | 8pcs: 5.2 hours |
  312. | Parameters (M) | 4.8 |
  313. | Checkpoint for Fine tuning | 13.3M (.ckpt file) |
  314. #### SqueezeNet_Residual on CIFAR-10
  315. | Parameters | Contents |
  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.1 |
  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(Ascend) | 1pc: 16.9 ms/step; 8pcs: 17.3 ms/step |
  328. | Speed(GPU) | 1pc: 45.23 ms/step; |
  329. | Total time(Ascend) | 1pc: 68.6 mins; 8pcs: 20.9 mins |
  330. | Parameters (M) | 4.8 |
  331. | Checkpoint for Fine tuning | 6.5M (.ckpt file) |
  332. #### SqueezeNet_Residual on ImageNet
  333. | Parameters | Contents |
  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.1 |
  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(Ascend) | 8pcs: 20.2 ms/step |
  346. | Total time(Ascend) | 8pcs: 8.0 hours |
  347. | Parameters (M) | 4.8 |
  348. | Checkpoint for Fine tuning | 15.3M (.ckpt file) |
  349. ### Inference Performance
  350. #### SqueezeNet on CIFAR-10
  351. | Parameters | Contents |
  352. | ------------------- | --------------------------- |
  353. | Model Version | SqueezeNet |
  354. | Resource | Ascend 910 |
  355. | Uploaded Date | 11/06/2020 (month/day/year) |
  356. | MindSpore Version | 1.0.1 |
  357. | Dataset | CIFAR-10 |
  358. | batch_size | 32 |
  359. | outputs | probability |
  360. | Accuracy | 1pc: 89.0%; 8pcs: 84.4% |
  361. #### SqueezeNet on ImageNet
  362. | Parameters | Contents |
  363. | ------------------- | --------------------------- |
  364. | Model Version | SqueezeNet |
  365. | Resource | Ascend 910 |
  366. | Uploaded Date | 11/06/2020 (month/day/year) |
  367. | MindSpore Version | 1.0.1 |
  368. | Dataset | ImageNet |
  369. | batch_size | 32 |
  370. | outputs | probability |
  371. | Accuracy | 8pcs: 58.5%(TOP1), 81.1%(TOP5) |
  372. #### SqueezeNet_Residual on CIFAR-10
  373. | Parameters | Contents |
  374. | ------------------- | --------------------------- |
  375. | Model Version | SqueezeNet_Residual |
  376. | Resource | Ascend 910 |
  377. | Uploaded Date | 11/06/2020 (month/day/year) |
  378. | MindSpore Version | 1.0.1 |
  379. | Dataset | CIFAR-10 |
  380. | batch_size | 32 |
  381. | outputs | probability |
  382. | Accuracy | 1pc: 90.8%; 8pcs: 87.4% |
  383. #### SqueezeNet_Residual on ImageNet
  384. | Parameters | Contents |
  385. | ------------------- | --------------------------- |
  386. | Model Version | SqueezeNet_Residual |
  387. | Resource | Ascend 910 |
  388. | Uploaded Date | 11/06/2020 (month/day/year) |
  389. | MindSpore Version | 1.0.1 |
  390. | Dataset | ImageNet |
  391. | batch_size | 32 |
  392. | outputs | probability |
  393. | Accuracy | 8pcs: 60.9%(TOP1), 82.6%(TOP5) |
  394. ## [How to use](#contents)
  395. ### Inference
  396. 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:
  397. - Running on Ascend
  398. ```py
  399. # Set context
  400. device_id = int(os.getenv('DEVICE_ID'))
  401. context.set_context(mode=context.GRAPH_MODE,
  402. device_target='Ascend',
  403. device_id=device_id)
  404. # Load unseen dataset for inference
  405. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  406. do_train=False,
  407. batch_size=config.batch_size,
  408. target='Ascend')
  409. # Define model
  410. net = squeezenet(num_classes=config.class_num)
  411. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  412. model = Model(net,
  413. loss_fn=loss,
  414. metrics={'top_1_accuracy', 'top_5_accuracy'})
  415. # Load pre-trained model
  416. param_dict = load_checkpoint(args_opt.checkpoint_path)
  417. load_param_into_net(net, param_dict)
  418. net.set_train(False)
  419. # Make predictions on the unseen dataset
  420. acc = model.eval(dataset)
  421. print("accuracy: ", acc)
  422. ```
  423. - Running on GPU:
  424. ```py
  425. # Set context
  426. device_id = int(os.getenv('DEVICE_ID'))
  427. context.set_context(mode=context.GRAPH_MODE,
  428. device_target='GPU',
  429. device_id=device_id)
  430. # Load unseen dataset for inference
  431. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  432. do_train=False,
  433. batch_size=config.batch_size,
  434. target='GPU')
  435. # Define model
  436. net = squeezenet(num_classes=config.class_num)
  437. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  438. model = Model(net,
  439. loss_fn=loss,
  440. metrics={'top_1_accuracy', 'top_5_accuracy'})
  441. # Load pre-trained model
  442. param_dict = load_checkpoint(args_opt.checkpoint_path)
  443. load_param_into_net(net, param_dict)
  444. net.set_train(False)
  445. # Make predictions on the unseen dataset
  446. acc = model.eval(dataset)
  447. print("accuracy: ", acc)
  448. ```
  449. ### Continue Training on the Pretrained Model
  450. - running on Ascend
  451. ```py
  452. # Load dataset
  453. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  454. do_train=True,
  455. repeat_num=1,
  456. batch_size=config.batch_size,
  457. target='Ascend')
  458. step_size = dataset.get_dataset_size()
  459. # define net
  460. net = squeezenet(num_classes=config.class_num)
  461. # load checkpoint
  462. if args_opt.pre_trained:
  463. param_dict = load_checkpoint(args_opt.pre_trained)
  464. load_param_into_net(net, param_dict)
  465. # init lr
  466. lr = get_lr(lr_init=config.lr_init,
  467. lr_end=config.lr_end,
  468. lr_max=config.lr_max,
  469. total_epochs=config.epoch_size,
  470. warmup_epochs=config.warmup_epochs,
  471. pretrain_epochs=config.pretrain_epoch_size,
  472. steps_per_epoch=step_size,
  473. lr_decay_mode=config.lr_decay_mode)
  474. lr = Tensor(lr)
  475. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  476. loss_scale = FixedLossScaleManager(config.loss_scale,
  477. drop_overflow_update=False)
  478. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  479. lr,
  480. config.momentum,
  481. config.weight_decay,
  482. config.loss_scale,
  483. use_nesterov=True)
  484. model = Model(net,
  485. loss_fn=loss,
  486. optimizer=opt,
  487. loss_scale_manager=loss_scale,
  488. metrics={'acc'},
  489. amp_level="O2",
  490. keep_batchnorm_fp32=False)
  491. # Set callbacks
  492. config_ck = CheckpointConfig(
  493. save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
  494. keep_checkpoint_max=config.keep_checkpoint_max)
  495. time_cb = TimeMonitor(data_size=step_size)
  496. ckpt_cb = ModelCheckpoint(prefix=args_opt.net + '_' + args_opt.dataset,
  497. directory=ckpt_save_dir,
  498. config=config_ck)
  499. loss_cb = LossMonitor()
  500. # Start training
  501. model.train(config.epoch_size - config.pretrain_epoch_size, dataset,
  502. callbacks=[time_cb, ckpt_cb, loss_cb])
  503. print("train success")
  504. ```
  505. - running on GPU
  506. ```py
  507. # Load dataset
  508. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  509. do_train=True,
  510. repeat_num=1,
  511. batch_size=config.batch_size,
  512. target='Ascend')
  513. step_size = dataset.get_dataset_size()
  514. # define net
  515. net = squeezenet(num_classes=config.class_num)
  516. # load checkpoint
  517. if args_opt.pre_trained:
  518. param_dict = load_checkpoint(args_opt.pre_trained)
  519. load_param_into_net(net, param_dict)
  520. # init lr
  521. lr = get_lr(lr_init=config.lr_init,
  522. lr_end=config.lr_end,
  523. lr_max=config.lr_max,
  524. total_epochs=config.epoch_size,
  525. warmup_epochs=config.warmup_epochs,
  526. pretrain_epochs=config.pretrain_epoch_size,
  527. steps_per_epoch=step_size,
  528. lr_decay_mode=config.lr_decay_mode)
  529. lr = Tensor(lr)
  530. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  531. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  532. lr,
  533. config.momentum,
  534. config.weight_decay,
  535. use_nesterov=True)
  536. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  537. # Set callbacks
  538. config_ck = CheckpointConfig(
  539. save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
  540. keep_checkpoint_max=config.keep_checkpoint_max)
  541. time_cb = TimeMonitor(data_size=step_size)
  542. ckpt_cb = ModelCheckpoint(prefix=args_opt.net + '_' + args_opt.dataset,
  543. directory=ckpt_save_dir,
  544. config=config_ck)
  545. loss_cb = LossMonitor()
  546. # Start training
  547. model.train(config.epoch_size - config.pretrain_epoch_size, dataset,
  548. callbacks=[time_cb, ckpt_cb, loss_cb])
  549. print("train success")
  550. ```
  551. ### Transfer Learning
  552. To be added.
  553. # [Description of Random Situation](#contents)
  554. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  555. # [ModelZoo Homepage](#contents)
  556. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).