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

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