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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. # Contents
  2. - [GoogleNet Description](#googlenet-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. - [Training](#training)
  14. - [Distributed Training](#distributed-training)
  15. - [Evaluation Process](#evaluation-process)
  16. - [Evaluation](#evaluation)
  17. - [Model Description](#model-description)
  18. - [Performance](#performance)
  19. - [Evaluation Performance](#evaluation-performance)
  20. - [Inference Performance](#evaluation-performance)
  21. - [How to use](#how-to-use)
  22. - [Inference](#inference)
  23. - [Continue Training on the Pretrained Model](#continue-training-on-the-pretrained-model)
  24. - [Transfer Learning](#transfer-learning)
  25. - [Description of Random Situation](#description-of-random-situation)
  26. - [ModelZoo Homepage](#modelzoo-homepage)
  27. # [GoogleNet Description](#contents)
  28. GoogleNet, a 22 layers deep network, was proposed in 2014 and won the first place in the ImageNet Large-Scale Visual Recognition Challenge 2014 (ILSVRC14). GoogleNet, also called Inception v1, has significant improvement over ZFNet (The winner in 2013) and AlexNet (The winner in 2012), and has relatively lower error rate compared to VGGNet. Typically deeper deep learning network means larger number of parameters, which makes it more prone to overfitting. Furthermore, the increased network size leads to increased use of computational resources. To tackle these issues, GoogleNet adopts 1*1 convolution middle of the network to reduce dimension, and thus further reduce the computation. Global average pooling is used at the end of the network, instead of using fully connected layers. Another technique, called inception module, is to have different sizes of convolutions for the same input and stacking all the outputs.
  29. [Paper](https://arxiv.org/abs/1409.4842): Christian Szegedy, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed, Dragomir Anguelov, Dumitru Erhan, Vincent Vanhoucke, Andrew Rabinovich. "Going deeper with convolutions." *Proceedings of the IEEE conference on computer vision and pattern recognition*. 2015.
  30. # [Model Architecture](#contents)
  31. Specifically, the GoogleNet contains numerous inception modules, which are connected together to go deeper. In general, an inception module with dimensionality reduction consists of **1×1 conv**, **3×3 conv**, **5×5 conv**, and **3×3 max pooling**, which are done altogether for the previous input, and stack together again at output. In our model architecture, the kernel size used in inception module is 3×3 instead of 5×5.
  32. # [Dataset](#contents)
  33. Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
  34. Dataset used: [CIFAR-10](<http://www.cs.toronto.edu/~kriz/cifar.html>)
  35. - Dataset size:175M,60,000 32*32 colorful images in 10 classes
  36. - Train:146M,50,000 images
  37. - Test:29M,10,000 images
  38. - Data format:binary files
  39. - Note:Data will be processed in src/dataset.py
  40. Dataset used can refer to paper.
  41. - Dataset size: 125G, 1250k colorful images in 1000 classes
  42. - Train: 120G, 1200k images
  43. - Test: 5G, 50k images
  44. - Data format: RGB images.
  45. - Note: Data will be processed in src/dataset.py
  46. # [Features](#contents)
  47. ## Mixed Precision
  48. 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.
  49. 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’.
  50. # [Environment Requirements](#contents)
  51. - Hardware(Ascend/GPU)
  52. - 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.
  53. - Framework
  54. - [MindSpore](https://www.mindspore.cn/install/en)
  55. - For more information, please check the resources below:
  56. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  57. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  58. # [Quick Start](#contents)
  59. After installing MindSpore via the official website, you can start training and evaluation as follows:
  60. - runing on Ascend
  61. ```python
  62. # run training example
  63. python train.py > train.log 2>&1 &
  64. # run distributed training example
  65. sh scripts/run_train.sh rank_table.json
  66. # run evaluation example
  67. python eval.py > eval.log 2>&1 &
  68. OR
  69. sh run_eval.sh
  70. ```
  71. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  72. Please follow the instructions in the link below:
  73. https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools.
  74. - running on GPU
  75. For running on GPU, please change `device_target` from `Ascend` to `GPU` in configuration file src/config.py
  76. ```python
  77. # run training example
  78. export CUDA_VISIBLE_DEVICES=0
  79. python train.py > train.log 2>&1 &
  80. # run distributed training example
  81. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  82. # run evaluation example
  83. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  84. OR
  85. sh run_eval_gpu.sh [CHECKPOINT_PATH]
  86. ```
  87. We use CIFAR-10 dataset by default. Your can also pass `$dataset_type` to the scripts so that select different datasets. For more details, please refer the specify script.
  88. # [Script Description](#contents)
  89. ## [Script and Sample Code](#contents)
  90. ```
  91. ├── model_zoo
  92. ├── README.md // descriptions about all the models
  93. ├── googlenet
  94. ├── README.md // descriptions about googlenet
  95. ├── scripts
  96. │ ├──run_train.sh // shell script for distributed on Ascend
  97. │ ├──run_train_gpu.sh // shell script for distributed on GPU
  98. │ ├──run_eval.sh // shell script for evaluation on Ascend
  99. │ ├──run_eval_gpu.sh // shell script for evaluation on GPU
  100. ├── src
  101. │ ├──dataset.py // creating dataset
  102. │ ├──googlenet.py // googlenet architecture
  103. │ ├──config.py // parameter configuration
  104. ├── train.py // training script
  105. ├── eval.py // evaluation script
  106. ├── export.py // export checkpoint files into air/onnx
  107. ```
  108. ## [Script Parameters](#contents)
  109. Parameters for both training and evaluation can be set in config.py
  110. - config for GoogleNet, CIFAR-10 dataset
  111. ```python
  112. 'pre_trained': 'False' # whether training based on the pre-trained model
  113. 'num_classes': 10 # the number of classes in the dataset
  114. 'lr_init': 0.1 # initial learning rate
  115. 'batch_size': 128 # training batch size
  116. 'epoch_size': 125 # total training epochs
  117. 'momentum': 0.9 # momentum
  118. 'weight_decay': 5e-4 # weight decay value
  119. 'image_height': 224 # image height used as input to the model
  120. 'image_width': 224 # image width used as input to the model
  121. 'data_path': './cifar10' # absolute full path to the train and evaluation datasets
  122. 'device_target': 'Ascend' # device running the program
  123. 'device_id': 0 # device ID used to train or evaluate the dataset. Ignore it when you use run_train.sh for distributed training
  124. 'keep_checkpoint_max': 10 # only keep the last keep_checkpoint_max checkpoint
  125. 'checkpoint_path': './train_googlenet_cifar10-125_390.ckpt' # the absolute full path to save the checkpoint file
  126. 'onnx_filename': 'googlenet.onnx' # file name of the onnx model used in export.py
  127. 'air_filename': 'googlenet.air' # file name of the air model used in export.py
  128. ```
  129. - config for GoogleNet, ImageNet dataset
  130. ```python
  131. 'pre_trained': 'False' # whether training based on the pre-trained model
  132. 'num_classes': 1000 # the number of classes in the dataset
  133. 'lr_init': 0.1 # initial learning rate
  134. 'batch_size': 256 # training batch size
  135. 'epoch_size': 300 # total training epochs
  136. 'momentum': 0.9 # momentum
  137. 'weight_decay': 1e-4 # weight decay value
  138. 'image_height': 224 # image height used as input to the model
  139. 'image_width': 224 # image width used as input to the model
  140. 'data_path': './ImageNet_Original/train/' # absolute full path to the train datasets
  141. 'val_data_path': './ImageNet_Original/val/' # absolute full path to the evaluation datasets
  142. 'device_target': 'Ascend' # device running the program
  143. 'device_id': 0 # device ID used to train or evaluate the dataset. Ignore it when you use run_train.sh for distributed training
  144. 'keep_checkpoint_max': 10 # only keep the last keep_checkpoint_max checkpoint
  145. 'checkpoint_path': './train_googlenet_cifar10-125_390.ckpt' # the absolute full path to save the checkpoint file
  146. 'onnx_filename': 'googlenet.onnx' # file name of the onnx model used in export.py
  147. 'air_filename': 'googlenet.air' # file name of the air model used in export.py
  148. 'lr_scheduler': 'exponential' # learning rate scheduler
  149. 'lr_epochs': [70, 140, 210, 280] # epoch of lr changing
  150. 'lr_gamma': 0.3 # decrease lr by a factor of exponential lr_scheduler
  151. 'eta_min': 0.0 # eta_min in cosine_annealing scheduler
  152. 'T_max': 150 # T-max in cosine_annealing scheduler
  153. 'warmup_epochs': 0 # warmup epoch
  154. 'is_dynamic_loss_scale': 0 # dynamic loss scale
  155. 'loss_scale': 1024 # loss scale
  156. 'label_smooth_factor': 0.1 # label_smooth_factor
  157. 'use_label_smooth': True # label smooth
  158. ```
  159. For more configuration details, please refer the script `config.py`.
  160. ## [Training Process](#contents)
  161. ### Training
  162. - running on Ascend
  163. ```
  164. python train.py > train.log 2>&1 &
  165. ```
  166. The python command above will run in the background, you can view the results through the file `train.log`.
  167. After training, you'll get some checkpoint files under the script folder by default. The loss value will be achieved as follows:
  168. ```
  169. # grep "loss is " train.log
  170. epoch: 1 step: 390, loss is 1.4842823
  171. epcoh: 2 step: 390, loss is 1.0897788
  172. ...
  173. ```
  174. The model checkpoint will be saved in the current directory.
  175. - running on GPU
  176. ```
  177. export CUDA_VISIBLE_DEVICES=0
  178. python train.py > train.log 2>&1 &
  179. ```
  180. The python command above will run in the background, you can view the results through the file `train.log`.
  181. After training, you'll get some checkpoint files under the folder `./ckpt_0/` by default.
  182. ### Distributed Training
  183. - running on Ascend
  184. ```
  185. sh scripts/run_train.sh rank_table.json
  186. ```
  187. The above shell script will run distribute training in the background. You can view the results through the file `train_parallel[X]/log`. The loss value will be achieved as follows:
  188. ```
  189. # grep "result: " train_parallel*/log
  190. train_parallel0/log:epoch: 1 step: 48, loss is 1.4302931
  191. train_parallel0/log:epcoh: 2 step: 48, loss is 1.4023874
  192. ...
  193. train_parallel1/log:epoch: 1 step: 48, loss is 1.3458025
  194. train_parallel1/log:epcoh: 2 step: 48, loss is 1.3729336
  195. ...
  196. ...
  197. ```
  198. - running on GPU
  199. ```
  200. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  201. ```
  202. The above shell script will run distribute training in the background. You can view the results through the file `train/train.log`.
  203. ## [Evaluation Process](#contents)
  204. ### Evaluation
  205. - evaluation on CIFAR-10 dataset when running on Ascend
  206. Before running the command below, please check the checkpoint path used for evaluation. Please set the checkpoint path to be the absolute full path, e.g., "username/googlenet/train_googlenet_cifar10-125_390.ckpt".
  207. ```
  208. python eval.py > eval.log 2>&1 &
  209. OR
  210. sh scripts/run_eval.sh
  211. ```
  212. The above python command will run in the background. You can view the results through the file "eval.log". The accuracy of the test dataset will be as follows:
  213. ```
  214. # grep "accuracy: " eval.log
  215. accuracy: {'acc': 0.934}
  216. ```
  217. Note that for evaluation after distributed training, please set the checkpoint_path to be the last saved checkpoint file such as "username/googlenet/train_parallel0/train_googlenet_cifar10-125_48.ckpt". The accuracy of the test dataset will be as follows:
  218. ```
  219. # grep "accuracy: " eval.log
  220. accuracy: {'acc': 0.9217}
  221. ```
  222. - evaluation on CIFAR-10 dataset when running on GPU
  223. Before running the command below, please check the checkpoint path used for evaluation. Please set the checkpoint path to be the absolute full path, e.g., "username/googlenet/train/ckpt_0/train_googlenet_cifar10-125_390.ckpt".
  224. ```
  225. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  226. ```
  227. The above python command will run in the background. You can view the results through the file "eval.log". The accuracy of the test dataset will be as follows:
  228. ```
  229. # grep "accuracy: " eval.log
  230. accuracy: {'acc': 0.930}
  231. ```
  232. OR,
  233. ```
  234. sh scripts/run_eval_gpu.sh [CHECKPOINT_PATH]
  235. ```
  236. The above python command will run in the background. You can view the results through the file "eval/eval.log". The accuracy of the test dataset will be as follows:
  237. ```
  238. # grep "accuracy: " eval/eval.log
  239. accuracy: {'acc': 0.930}
  240. ```
  241. # [Model Description](#contents)
  242. ## [Performance](#contents)
  243. ### Evaluation Performance
  244. #### GoogleNet on CIFAR-10
  245. | Parameters | Ascend | GPU |
  246. | -------------------------- | ----------------------------------------------------------- | ---------------------- |
  247. | Model Version | Inception V1 | Inception V1 |
  248. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G | NV SMX2 V100-32G |
  249. | uploaded Date | 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year) |
  250. | MindSpore Version | 1.0.0 | 1.0.0 |
  251. | Dataset | CIFAR-10 | CIFAR-10 |
  252. | Training Parameters | epoch=125, steps=390, batch_size = 128, lr=0.1 | epoch=125, steps=390, batch_size=128, lr=0.1 |
  253. | Optimizer | Momentum | Momentum |
  254. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  255. | outputs | probability | probobility |
  256. | Loss | 0.0016 | 0.0016 |
  257. | Speed | 1pc: 79 ms/step; 8pcs: 82 ms/step | 1pc: 150 ms/step; 8pcs: 164 ms/step |
  258. | Total time | 1pc: 63.85 mins; 8pcs: 11.28 mins | 1pc: 126.87 mins; 8pcs: 21.65 mins |
  259. | Parameters (M) | 13.0 | 13.0 |
  260. | Checkpoint for Fine tuning | 43.07M (.ckpt file) | 43.07M (.ckpt file) |
  261. | Model for inference | 21.50M (.onnx file), 21.60M(.air file) | |
  262. | Scripts | [googlenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet) | [googlenet script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/googlenet) |
  263. #### GoogleNet on 1200k images
  264. | Parameters | Ascend |
  265. | -------------------------- | ----------------------------------------------------------- |
  266. | Model Version | Inception V1 |
  267. | Resource | Ascend 910, CPU 2.60GHz, 56cores, Memory 314G |
  268. | uploaded Date | 10/28/2020 (month/day/year) |
  269. | MindSpore Version | 1.0.0 |
  270. | Dataset | 1200k images |
  271. | Training Parameters | epoch=300, steps=5000, batch_size=256, lr=0.1 |
  272. | Optimizer | Momentum |
  273. | Loss Function | Softmax Cross Entropy |
  274. | outputs | probability |
  275. | Loss | 2.0 |
  276. | Speed | 1pc: 152 ms/step; 8pcs: 171 ms/step |
  277. | Total time | 8pcs: 8.8 hours |
  278. | Parameters (M) | 13.0 |
  279. | Checkpoint for Fine tuning | 52M (.ckpt file) |
  280. | Scripts | [googlenet script](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) |
  281. ### Inference Performance
  282. #### GoogleNet on CIFAR-10
  283. | Parameters | Ascend | GPU |
  284. | ------------------- | --------------------------- | --------------------------- |
  285. | Model Version | Inception V1 | Inception V1 |
  286. | Resource | Ascend 910 | GPU |
  287. | Uploaded Date | 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year) |
  288. | MindSpore Version | 1.0.0 | 1.0.0 |
  289. | Dataset | CIFAR-10, 10,000 images | CIFAR-10, 10,000 images |
  290. | batch_size | 128 | 128 |
  291. | outputs | probability | probability |
  292. | Accuracy | 1pc: 93.4%; 8pcs: 92.17% | 1pc: 93%, 8pcs: 92.89% |
  293. | Model for inference | 21.50M (.onnx file) | |
  294. #### GoogleNet on 1200k images
  295. | Parameters | Ascend |
  296. | ------------------- | --------------------------- |
  297. | Model Version | Inception V1 |
  298. | Resource | Ascend 910 |
  299. | Uploaded Date | 10/28/2020 (month/day/year) |
  300. | MindSpore Version | 1.0.0 |
  301. | Dataset | 1200k images |
  302. | batch_size | 256 |
  303. | outputs | probability |
  304. | Accuracy | 8pcs: 71.81% |
  305. ## [How to use](#contents)
  306. ### Inference
  307. 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:
  308. - Running on Ascend
  309. ```
  310. # Set context
  311. context.set_context(mode=context.GRAPH_HOME, device_target=cfg.device_target)
  312. context.set_context(device_id=cfg.device_id)
  313. # Load unseen dataset for inference
  314. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  315. # Define model
  316. net = GoogleNet(num_classes=cfg.num_classes)
  317. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  318. cfg.momentum, weight_decay=cfg.weight_decay)
  319. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  320. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  321. # Load pre-trained model
  322. param_dict = load_checkpoint(cfg.checkpoint_path)
  323. load_param_into_net(net, param_dict)
  324. net.set_train(False)
  325. # Make predictions on the unseen dataset
  326. acc = model.eval(dataset)
  327. print("accuracy: ", acc)
  328. ```
  329. - Running on GPU:
  330. ```
  331. # Set context
  332. context.set_context(mode=context.GRAPH_HOME, device_target="GPU")
  333. # Load unseen dataset for inference
  334. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  335. # Define model
  336. net = GoogleNet(num_classes=cfg.num_classes)
  337. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  338. cfg.momentum, weight_decay=cfg.weight_decay)
  339. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  340. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  341. # Load pre-trained model
  342. param_dict = load_checkpoint(args_opt.checkpoint_path)
  343. load_param_into_net(net, param_dict)
  344. net.set_train(False)
  345. # Make predictions on the unseen dataset
  346. acc = model.eval(dataset)
  347. print("accuracy: ", acc)
  348. ```
  349. ### Continue Training on the Pretrained Model
  350. - running on Ascend
  351. ```
  352. # Load dataset
  353. dataset = create_dataset(cfg.data_path, 1)
  354. batch_num = dataset.get_dataset_size()
  355. # Define model
  356. net = GoogleNet(num_classes=cfg.num_classes)
  357. # Continue training if set pre_trained to be True
  358. if cfg.pre_trained:
  359. param_dict = load_checkpoint(cfg.checkpoint_path)
  360. load_param_into_net(net, param_dict)
  361. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  362. steps_per_epoch=batch_num)
  363. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  364. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  365. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  366. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  367. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  368. # Set callbacks
  369. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  370. keep_checkpoint_max=cfg.keep_checkpoint_max)
  371. time_cb = TimeMonitor(data_size=batch_num)
  372. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./",
  373. config=config_ck)
  374. loss_cb = LossMonitor()
  375. # Start training
  376. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  377. print("train success")
  378. ```
  379. - running on GPU
  380. ```
  381. # Load dataset
  382. dataset = create_dataset(cfg.data_path, 1)
  383. batch_num = dataset.get_dataset_size()
  384. # Define model
  385. net = GoogleNet(num_classes=cfg.num_classes)
  386. # Continue training if set pre_trained to be True
  387. if cfg.pre_trained:
  388. param_dict = load_checkpoint(cfg.checkpoint_path)
  389. load_param_into_net(net, param_dict)
  390. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  391. steps_per_epoch=batch_num)
  392. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  393. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  394. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  395. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  396. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  397. # Set callbacks
  398. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  399. keep_checkpoint_max=cfg.keep_checkpoint_max)
  400. time_cb = TimeMonitor(data_size=batch_num)
  401. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./ckpt_" + str(get_rank()) + "/",
  402. config=config_ck)
  403. loss_cb = LossMonitor()
  404. # Start training
  405. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  406. print("train success")
  407. ```
  408. ### Transfer Learning
  409. To be added.
  410. # [Description of Random Situation](#contents)
  411. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  412. # [ModelZoo Homepage](#contents)
  413. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).