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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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)
  49. - Prepare hardware environment with Ascend 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. See [squeezenet in research](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/research/cv/squeezenet) to get up-to-date details.
  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. # [Script Description](#contents)
  67. ## [Script and Sample Code](#contents)
  68. ```shell
  69. .
  70. └── squeezenet
  71. ├── README.md
  72. ├── scripts
  73. ├── run_distribute_train.sh # launch ascend distributed training(8 pcs)
  74. ├── run_standalone_train.sh # launch ascend standalone training(1 pcs)
  75. ├── run_eval.sh # launch ascend evaluation
  76. ├── src
  77. ├── config.py # parameter configuration
  78. ├── dataset.py # data preprocessing
  79. ├── CrossEntropySmooth.py # loss definition for ImageNet dataset
  80. ├── lr_generator.py # generate learning rate for each step
  81. └── squeezenet.py # squeezenet architecture, including squeezenet and squeezenet_residual
  82. ├── train.py # train net
  83. ├── eval.py # eval net
  84. └── export.py # export checkpoint files into geir/onnx
  85. ```
  86. ## [Script Parameters](#contents)
  87. Parameters for both training and evaluation can be set in config.py
  88. - config for SqueezeNet, CIFAR-10 dataset
  89. ```py
  90. "class_num": 10, # dataset class num
  91. "batch_size": 32, # batch size of input tensor
  92. "loss_scale": 1024, # loss scale
  93. "momentum": 0.9, # momentum
  94. "weight_decay": 1e-4, # weight decay
  95. "epoch_size": 120, # only valid for taining, which is always 1 for inference
  96. "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
  97. "save_checkpoint": True, # whether save checkpoint or not
  98. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  99. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  100. "save_checkpoint_path": "./", # path to save checkpoint
  101. "warmup_epochs": 5, # number of warmup epoch
  102. "lr_decay_mode": "poly" # decay mode for generating learning rate
  103. "lr_init": 0, # initial learning rate
  104. "lr_end": 0, # final learning rate
  105. "lr_max": 0.01, # maximum learning rate
  106. ```
  107. - config for SqueezeNet, ImageNet dataset
  108. ```py
  109. "class_num": 1000, # dataset class num
  110. "batch_size": 32, # batch size of input tensor
  111. "loss_scale": 1024, # loss scale
  112. "momentum": 0.9, # momentum
  113. "weight_decay": 7e-5, # weight decay
  114. "epoch_size": 200, # only valid for taining, which is always 1 for inference
  115. "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
  116. "save_checkpoint": True, # whether save checkpoint or not
  117. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  118. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  119. "save_checkpoint_path": "./", # path to save checkpoint
  120. "warmup_epochs": 0, # number of warmup epoch
  121. "lr_decay_mode": "poly" # decay mode for generating learning rate
  122. "use_label_smooth": True, # label smooth
  123. "label_smooth_factor": 0.1, # label smooth factor
  124. "lr_init": 0, # initial learning rate
  125. "lr_end": 0, # final learning rate
  126. "lr_max": 0.01, # maximum learning rate
  127. ```
  128. - config for SqueezeNet_Residual, CIFAR-10 dataset
  129. ```py
  130. "class_num": 10, # dataset class num
  131. "batch_size": 32, # batch size of input tensor
  132. "loss_scale": 1024, # loss scale
  133. "momentum": 0.9, # momentum
  134. "weight_decay": 1e-4, # weight decay
  135. "epoch_size": 150, # only valid for taining, which is always 1 for inference
  136. "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
  137. "save_checkpoint": True, # whether save checkpoint or not
  138. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  139. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  140. "save_checkpoint_path": "./", # path to save checkpoint
  141. "warmup_epochs": 5, # number of warmup epoch
  142. "lr_decay_mode": "linear" # decay mode for generating learning rate
  143. "lr_init": 0, # initial learning rate
  144. "lr_end": 0, # final learning rate
  145. "lr_max": 0.01, # maximum learning rate
  146. ```
  147. - config for SqueezeNet_Residual, ImageNet dataset
  148. ```py
  149. "class_num": 1000, # dataset class num
  150. "batch_size": 32, # batch size of input tensor
  151. "loss_scale": 1024, # loss scale
  152. "momentum": 0.9, # momentum
  153. "weight_decay": 7e-5, # weight decay
  154. "epoch_size": 300, # only valid for taining, which is always 1 for inference
  155. "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
  156. "save_checkpoint": True, # whether save checkpoint or not
  157. "save_checkpoint_epochs": 1, # the epoch interval between two checkpoints. By default, the last checkpoint will be saved after the last step
  158. "keep_checkpoint_max": 10, # only keep the last keep_checkpoint_max checkpoint
  159. "save_checkpoint_path": "./", # path to save checkpoint
  160. "warmup_epochs": 0, # number of warmup epoch
  161. "lr_decay_mode": "cosine" # decay mode for generating learning rate
  162. "use_label_smooth": True, # label smooth
  163. "label_smooth_factor": 0.1, # label smooth factor
  164. "lr_init": 0, # initial learning rate
  165. "lr_end": 0, # final learning rate
  166. "lr_max": 0.01, # maximum learning rate
  167. ```
  168. For more configuration details, please refer the script `config.py`.
  169. ## [Training Process](#contents)
  170. ### Usage
  171. #### Running on Ascend
  172. ```shell
  173. # distributed training
  174. Usage: sh scripts/run_distribute_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [RANK_TABLE_FILE] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  175. # standalone training
  176. Usage: sh scripts/run_standalone_train.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [PRETRAINED_CKPT_PATH](optional)
  177. ```
  178. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  179. Please follow the instructions in the link [hccl_tools](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools).
  180. 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.
  181. ### Result
  182. - Training SqueezeNet with CIFAR-10 dataset
  183. ```shell
  184. # standalone training result
  185. epoch: 1 step 1562, loss is 1.7103254795074463
  186. epoch: 2 step 1562, loss is 2.06101131439209
  187. epoch: 3 step 1562, loss is 1.5594401359558105
  188. epoch: 4 step 1562, loss is 1.4127278327941895
  189. epoch: 5 step 1562, loss is 1.2140142917633057
  190. ...
  191. ```
  192. - Training SqueezeNet with ImageNet dataset
  193. ```shell
  194. # distribute training result(8 pcs)
  195. epoch: 1 step 5004, loss is 5.716324329376221
  196. epoch: 2 step 5004, loss is 5.350603103637695
  197. epoch: 3 step 5004, loss is 4.580031394958496
  198. epoch: 4 step 5004, loss is 4.784664154052734
  199. epoch: 5 step 5004, loss is 4.136358261108398
  200. ...
  201. ```
  202. - Training SqueezeNet_Residual with CIFAR-10 dataset
  203. ```shell
  204. # standalone training result
  205. epoch: 1 step 1562, loss is 2.298271656036377
  206. epoch: 2 step 1562, loss is 2.2728664875030518
  207. epoch: 3 step 1562, loss is 1.9493038654327393
  208. epoch: 4 step 1562, loss is 1.7553865909576416
  209. epoch: 5 step 1562, loss is 1.3370063304901123
  210. ...
  211. ```
  212. - Training SqueezeNet_Residual with ImageNet dataset
  213. ```shell
  214. # distribute training result(8 pcs)
  215. epoch: 1 step 5004, loss is 6.802495002746582
  216. epoch: 2 step 5004, loss is 6.386072158813477
  217. epoch: 3 step 5004, loss is 5.513605117797852
  218. epoch: 4 step 5004, loss is 5.312961101531982
  219. epoch: 5 step 5004, loss is 4.888848304748535
  220. ...
  221. ```
  222. ## [Evaluation Process](#contents)
  223. ### Usage
  224. #### Running on Ascend
  225. ```shell
  226. # evaluation
  227. Usage: sh scripts/run_eval.sh [squeezenet|squeezenet_residual] [cifar10|imagenet] [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]
  228. ```
  229. ```shell
  230. # evaluation example
  231. sh scripts/run_eval.sh squeezenet cifar10 0 ~/cifar-10-verify-bin train/squeezenet_cifar10-120_1562.ckpt
  232. ```
  233. checkpoint can be produced in training process.
  234. ### Result
  235. 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.
  236. - Evaluating SqueezeNet with CIFAR-10 dataset
  237. ```shell
  238. result: {'top_1_accuracy': 0.8896233974358975, 'top_5_accuracy': 0.9965945512820513}
  239. ```
  240. - Evaluating SqueezeNet with ImageNet dataset
  241. ```shell
  242. result: {'top_1_accuracy': 0.5851472471190781, 'top_5_accuracy': 0.8105393725992317}
  243. ```
  244. - Evaluating SqueezeNet_Residual with CIFAR-10 dataset
  245. ```shell
  246. result: {'top_1_accuracy': 0.9077524038461539, 'top_5_accuracy': 0.9969951923076923}
  247. ```
  248. - Evaluating SqueezeNet_Residual with ImageNet dataset
  249. ```shell
  250. result: {'top_1_accuracy': 0.6094950384122919, 'top_5_accuracy': 0.826324423815621}
  251. ```
  252. # [Model Description](#contents)
  253. ## [Performance](#contents)
  254. ### Evaluation Performance
  255. #### SqueezeNet on CIFAR-10
  256. | Parameters | Ascend |
  257. | -------------------------- | ----------------------------------------------------------- |
  258. | Model Version | SqueezeNet |
  259. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  260. | uploaded Date | 11/06/2020 (month/day/year) |
  261. | MindSpore Version | 1.0.0 |
  262. | Dataset | CIFAR-10 |
  263. | Training Parameters | epoch=120, steps=195, batch_size=32, lr=0.01 |
  264. | Optimizer | Momentum |
  265. | Loss Function | Softmax Cross Entropy |
  266. | outputs | probability |
  267. | Loss | 0.0496 |
  268. | Speed | 1pc: 16.7 ms/step; 8pcs: 17.0 ms/step |
  269. | Total time | 1pc: 55.5 mins; 8pcs: 15.0 mins |
  270. | Parameters (M) | 4.8 |
  271. | Checkpoint for Fine tuning | 6.4M (.ckpt file) |
  272. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  273. #### SqueezeNet on ImageNet
  274. | Parameters | Ascend |
  275. | -------------------------- | ----------------------------------------------------------- |
  276. | Model Version | SqueezeNet |
  277. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  278. | uploaded Date | 11/06/2020 (month/day/year) |
  279. | MindSpore Version | 1.0.0 |
  280. | Dataset | ImageNet |
  281. | Training Parameters | epoch=200, steps=5004, batch_size=32, lr=0.01 |
  282. | Optimizer | Momentum |
  283. | Loss Function | Softmax Cross Entropy |
  284. | outputs | probability |
  285. | Loss | 2.9150 |
  286. | Speed | 8pcs: 19.9 ms/step |
  287. | Total time | 8pcs: 5.2 hours |
  288. | Parameters (M) | 4.8 |
  289. | Checkpoint for Fine tuning | 13.3M (.ckpt file) |
  290. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  291. #### SqueezeNet_Residual on CIFAR-10
  292. | Parameters | Ascend |
  293. | -------------------------- | ----------------------------------------------------------- |
  294. | Model Version | SqueezeNet_Residual |
  295. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  296. | uploaded Date | 11/06/2020 (month/day/year) |
  297. | MindSpore Version | 1.0.0 |
  298. | Dataset | CIFAR-10 |
  299. | Training Parameters | epoch=150, steps=195, batch_size=32, lr=0.01 |
  300. | Optimizer | Momentum |
  301. | Loss Function | Softmax Cross Entropy |
  302. | outputs | probability |
  303. | Loss | 0.0641 |
  304. | Speed | 1pc: 16.9 ms/step; 8pcs: 17.3 ms/step |
  305. | Total time | 1pc: 68.6 mins; 8pcs: 20.9 mins |
  306. | Parameters (M) | 4.8 |
  307. | Checkpoint for Fine tuning | 6.5M (.ckpt file) |
  308. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  309. #### SqueezeNet_Residual on ImageNet
  310. | Parameters | Ascend |
  311. | -------------------------- | ----------------------------------------------------------- |
  312. | Model Version | SqueezeNet_Residual |
  313. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  314. | uploaded Date | 11/06/2020 (month/day/year) |
  315. | MindSpore Version | 1.0.0 |
  316. | Dataset | ImageNet |
  317. | Training Parameters | epoch=300, steps=5004, batch_size=32, lr=0.01 |
  318. | Optimizer | Momentum |
  319. | Loss Function | Softmax Cross Entropy |
  320. | outputs | probability |
  321. | Loss | 2.9040 |
  322. | Speed | 8pcs: 20.2 ms/step |
  323. | Total time | 8pcs: 8.0 hours |
  324. | Parameters (M) | 4.8 |
  325. | Checkpoint for Fine tuning | 15.3M (.ckpt file) |
  326. | Scripts | [squeezenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/squeezenet) |
  327. ### Inference Performance
  328. #### SqueezeNet on CIFAR-10
  329. | Parameters | Ascend |
  330. | ------------------- | --------------------------- |
  331. | Model Version | SqueezeNet |
  332. | Resource | Ascend 910 |
  333. | Uploaded Date | 11/06/2020 (month/day/year) |
  334. | MindSpore Version | 1.0.0 |
  335. | Dataset | CIFAR-10 |
  336. | batch_size | 32 |
  337. | outputs | probability |
  338. | Accuracy | 1pc: 89.0%; 8pcs: 84.4% |
  339. #### SqueezeNet on ImageNet
  340. | Parameters | Ascend |
  341. | ------------------- | --------------------------- |
  342. | Model Version | SqueezeNet |
  343. | Resource | Ascend 910 |
  344. | Uploaded Date | 11/06/2020 (month/day/year) |
  345. | MindSpore Version | 1.0.0 |
  346. | Dataset | ImageNet |
  347. | batch_size | 32 |
  348. | outputs | probability |
  349. | Accuracy | 8pcs: 58.5%(TOP1), 81.1%(TOP5) |
  350. #### SqueezeNet_Residual on CIFAR-10
  351. | Parameters | Ascend |
  352. | ------------------- | --------------------------- |
  353. | Model Version | SqueezeNet_Residual |
  354. | Resource | Ascend 910 |
  355. | Uploaded Date | 11/06/2020 (month/day/year) |
  356. | MindSpore Version | 1.0.0 |
  357. | Dataset | CIFAR-10 |
  358. | batch_size | 32 |
  359. | outputs | probability |
  360. | Accuracy | 1pc: 90.8%; 8pcs: 87.4% |
  361. #### SqueezeNet_Residual on ImageNet
  362. | Parameters | Ascend |
  363. | ------------------- | --------------------------- |
  364. | Model Version | SqueezeNet_Residual |
  365. | Resource | Ascend 910 |
  366. | Uploaded Date | 11/06/2020 (month/day/year) |
  367. | MindSpore Version | 1.0.0 |
  368. | Dataset | ImageNet |
  369. | batch_size | 32 |
  370. | outputs | probability |
  371. | Accuracy | 8pcs: 60.9%(TOP1), 82.6%(TOP5) |
  372. ## [How to use](#contents)
  373. ### Inference
  374. 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:
  375. - Running on Ascend
  376. ```py
  377. # Set context
  378. device_id = int(os.getenv('DEVICE_ID'))
  379. context.set_context(mode=context.GRAPH_MODE,
  380. device_target='Ascend',
  381. device_id=device_id)
  382. # Load unseen dataset for inference
  383. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  384. do_train=False,
  385. batch_size=config.batch_size,
  386. target='Ascend')
  387. # Define model
  388. net = squeezenet(num_classes=config.class_num)
  389. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  390. model = Model(net,
  391. loss_fn=loss,
  392. metrics={'top_1_accuracy', 'top_5_accuracy'})
  393. # Load pre-trained model
  394. param_dict = load_checkpoint(args_opt.checkpoint_path)
  395. load_param_into_net(net, param_dict)
  396. net.set_train(False)
  397. # Make predictions on the unseen dataset
  398. acc = model.eval(dataset)
  399. print("accuracy: ", acc)
  400. ```
  401. ### Continue Training on the Pretrained Model
  402. - running on Ascend
  403. ```py
  404. # Load dataset
  405. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  406. do_train=True,
  407. repeat_num=1,
  408. batch_size=config.batch_size,
  409. target='Ascend')
  410. step_size = dataset.get_dataset_size()
  411. # define net
  412. net = squeezenet(num_classes=config.class_num)
  413. # load checkpoint
  414. if args_opt.pre_trained:
  415. param_dict = load_checkpoint(args_opt.pre_trained)
  416. load_param_into_net(net, param_dict)
  417. # init lr
  418. lr = get_lr(lr_init=config.lr_init,
  419. lr_end=config.lr_end,
  420. lr_max=config.lr_max,
  421. total_epochs=config.epoch_size,
  422. warmup_epochs=config.warmup_epochs,
  423. pretrain_epochs=config.pretrain_epoch_size,
  424. steps_per_epoch=step_size,
  425. lr_decay_mode=config.lr_decay_mode)
  426. lr = Tensor(lr)
  427. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  428. loss_scale = FixedLossScaleManager(config.loss_scale,
  429. drop_overflow_update=False)
  430. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  431. lr,
  432. config.momentum,
  433. config.weight_decay,
  434. config.loss_scale,
  435. use_nesterov=True)
  436. model = Model(net,
  437. loss_fn=loss,
  438. optimizer=opt,
  439. loss_scale_manager=loss_scale,
  440. metrics={'acc'},
  441. amp_level="O2",
  442. keep_batchnorm_fp32=False)
  443. # Set callbacks
  444. config_ck = CheckpointConfig(
  445. save_checkpoint_steps=config.save_checkpoint_epochs * step_size,
  446. keep_checkpoint_max=config.keep_checkpoint_max)
  447. time_cb = TimeMonitor(data_size=step_size)
  448. ckpt_cb = ModelCheckpoint(prefix=args_opt.net + '_' + args_opt.dataset,
  449. directory=ckpt_save_dir,
  450. config=config_ck)
  451. loss_cb = LossMonitor()
  452. # Start training
  453. model.train(config.epoch_size - config.pretrain_epoch_size, dataset,
  454. callbacks=[time_cb, ckpt_cb, loss_cb])
  455. print("train success")
  456. ```
  457. ### Transfer Learning
  458. To be added.
  459. # [Description of Random Situation](#contents)
  460. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  461. # [ModelZoo Homepage](#contents)
  462. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).