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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. 'nump_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. For more configuration details, please refer the script `config.py`.
  130. ## [Training Process](#contents)
  131. ### Training
  132. - running on Ascend
  133. ```
  134. python train.py > train.log 2>&1 &
  135. ```
  136. The python command above will run in the background, you can view the results through the file `train.log`.
  137. After training, you'll get some checkpoint files under the script folder by default. The loss value will be achieved as follows:
  138. ```
  139. # grep "loss is " train.log
  140. epoch: 1 step: 390, loss is 1.4842823
  141. epcoh: 2 step: 390, loss is 1.0897788
  142. ...
  143. ```
  144. The model checkpoint will be saved in the current directory.
  145. - running on GPU
  146. ```
  147. export CUDA_VISIBLE_DEVICES=0
  148. python train.py > train.log 2>&1 &
  149. ```
  150. The python command above will run in the background, you can view the results through the file `train.log`.
  151. After training, you'll get some checkpoint files under the folder `./ckpt_0/` by default.
  152. ### Distributed Training
  153. - running on Ascend
  154. ```
  155. sh scripts/run_train.sh rank_table.json
  156. ```
  157. 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:
  158. ```
  159. # grep "result: " train_parallel*/log
  160. train_parallel0/log:epoch: 1 step: 48, loss is 1.4302931
  161. train_parallel0/log:epcoh: 2 step: 48, loss is 1.4023874
  162. ...
  163. train_parallel1/log:epoch: 1 step: 48, loss is 1.3458025
  164. train_parallel1/log:epcoh: 2 step: 48, loss is 1.3729336
  165. ...
  166. ...
  167. ```
  168. - running on GPU
  169. ```
  170. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  171. ```
  172. The above shell script will run distribute training in the background. You can view the results through the file `train/train.log`.
  173. ## [Evaluation Process](#contents)
  174. ### Evaluation
  175. - evaluation on CIFAR-10 dataset when running on Ascend
  176. 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".
  177. ```
  178. python eval.py > eval.log 2>&1 &
  179. OR
  180. sh scripts/run_eval.sh
  181. ```
  182. 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:
  183. ```
  184. # grep "accuracy: " eval.log
  185. accuracy: {'acc': 0.934}
  186. ```
  187. 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:
  188. ```
  189. # grep "accuracy: " eval.log
  190. accuracy: {'acc': 0.9217}
  191. ```
  192. - evaluation on CIFAR-10 dataset when running on GPU
  193. 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".
  194. ```
  195. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  196. ```
  197. 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:
  198. ```
  199. # grep "accuracy: " eval.log
  200. accuracy: {'acc': 0.930}
  201. ```
  202. OR,
  203. ```
  204. sh scripts/run_eval_gpu.sh [CHECKPOINT_PATH]
  205. ```
  206. 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:
  207. ```
  208. # grep "accuracy: " eval/eval.log
  209. accuracy: {'acc': 0.930}
  210. ```
  211. # [Model Description](#contents)
  212. ## [Performance](#contents)
  213. ### Evaluation Performance
  214. #### GoogleNet on CIFAR-10
  215. | Parameters | Ascend | GPU |
  216. | -------------------------- | ----------------------------------------------------------- | ---------------------- |
  217. | Model Version | Inception V1 | Inception V1 |
  218. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G | NV SMX2 V100-32G |
  219. | uploaded Date | 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year) |
  220. | MindSpore Version | 1.0.0 | 1.0.0 |
  221. | Dataset | CIFAR-10 | CIFAR-10 |
  222. | Training Parameters | epoch=125, steps=390, batch_size = 128, lr=0.1 | epoch=125, steps=390, batch_size=128, lr=0.1 |
  223. | Optimizer | Momentum | Momentum |
  224. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  225. | outputs | probability | probobility |
  226. | Loss | 0.0016 | 0.0016 |
  227. | Speed | 1pc: 79 ms/step; 8pcs: 82 ms/step | 1pc: 150 ms/step; 8pcs: 164 ms/step |
  228. | Total time | 1pc: 63.85 mins; 8pcs: 11.28 mins | 1pc: 126.87 mins; 8pcs: 21.65 mins |
  229. | Parameters (M) | 13.0 | 13.0 |
  230. | Checkpoint for Fine tuning | 43.07M (.ckpt file) | 43.07M (.ckpt file) |
  231. | Model for inference | 21.50M (.onnx file), 21.60M(.air file) | |
  232. | 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) |
  233. #### GoogleNet on 1200k images
  234. | Parameters | Ascend |
  235. | -------------------------- | ----------------------------------------------------------- |
  236. | Model Version | Inception V1 |
  237. | Resource | Ascend 910, CPU 2.60GHz, 56cores, Memory 314G |
  238. | uploaded Date | 10/28/2020 (month/day/year) |
  239. | MindSpore Version | 1.0.0 |
  240. | Dataset | 1200k images |
  241. | Training Parameters | epoch=300, steps=5000, batch_size=256, lr=0.1 |
  242. | Optimizer | Momentum |
  243. | Loss Function | Softmax Cross Entropy |
  244. | outputs | probability |
  245. | Loss | 2.0 |
  246. | Speed | 1pc: 152 ms/step; 8pcs: 171 ms/step |
  247. | Total time | 8pcs: 8.8 hours |
  248. | Parameters (M) | 13.0 |
  249. | Checkpoint for Fine tuning | 52M (.ckpt file) |
  250. | Scripts | [googlenet script](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) |
  251. ### Inference Performance
  252. #### GoogleNet on CIFAR-10
  253. | Parameters | Ascend | GPU |
  254. | ------------------- | --------------------------- | --------------------------- |
  255. | Model Version | Inception V1 | Inception V1 |
  256. | Resource | Ascend 910 | GPU |
  257. | Uploaded Date | 10/28/2020 (month/day/year) | 10/28/2020 (month/day/year) |
  258. | MindSpore Version | 1.0.0 | 1.0.0 |
  259. | Dataset | CIFAR-10, 10,000 images | CIFAR-10, 10,000 images |
  260. | batch_size | 128 | 128 |
  261. | outputs | probability | probability |
  262. | Accuracy | 1pc: 93.4%; 8pcs: 92.17% | 1pc: 93%, 8pcs: 92.89% |
  263. | Model for inference | 21.50M (.onnx file) | |
  264. #### GoogleNet on 1200k images
  265. | Parameters | Ascend |
  266. | ------------------- | --------------------------- |
  267. | Model Version | Inception V1 |
  268. | Resource | Ascend 910 |
  269. | Uploaded Date | 10/28/2020 (month/day/year) |
  270. | MindSpore Version | 1.0.0 |
  271. | Dataset | 1200k images |
  272. | batch_size | 256 |
  273. | outputs | probability |
  274. | Accuracy | 8pcs: 71.81% |
  275. ## [How to use](#contents)
  276. ### Inference
  277. 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:
  278. - Running on Ascend
  279. ```
  280. # Set context
  281. context.set_context(mode=context.GRAPH_HOME, device_target=cfg.device_target)
  282. context.set_context(device_id=cfg.device_id)
  283. # Load unseen dataset for inference
  284. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  285. # Define model
  286. net = GoogleNet(num_classes=cfg.num_classes)
  287. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  288. cfg.momentum, weight_decay=cfg.weight_decay)
  289. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  290. is_grad=False)
  291. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  292. # Load pre-trained model
  293. param_dict = load_checkpoint(cfg.checkpoint_path)
  294. load_param_into_net(net, param_dict)
  295. net.set_train(False)
  296. # Make predictions on the unseen dataset
  297. acc = model.eval(dataset)
  298. print("accuracy: ", acc)
  299. ```
  300. - Running on GPU:
  301. ```
  302. # Set context
  303. context.set_context(mode=context.GRAPH_HOME, device_target="GPU")
  304. # Load unseen dataset for inference
  305. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  306. # Define model
  307. net = GoogleNet(num_classes=cfg.num_classes)
  308. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  309. cfg.momentum, weight_decay=cfg.weight_decay)
  310. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  311. is_grad=False)
  312. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  313. # Load pre-trained model
  314. param_dict = load_checkpoint(args_opt.checkpoint_path)
  315. load_param_into_net(net, param_dict)
  316. net.set_train(False)
  317. # Make predictions on the unseen dataset
  318. acc = model.eval(dataset)
  319. print("accuracy: ", acc)
  320. ```
  321. ### Continue Training on the Pretrained Model
  322. - running on Ascend
  323. ```
  324. # Load dataset
  325. dataset = create_dataset(cfg.data_path, 1)
  326. batch_num = dataset.get_dataset_size()
  327. # Define model
  328. net = GoogleNet(num_classes=cfg.num_classes)
  329. # Continue training if set pre_trained to be True
  330. if cfg.pre_trained:
  331. param_dict = load_checkpoint(cfg.checkpoint_path)
  332. load_param_into_net(net, param_dict)
  333. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  334. steps_per_epoch=batch_num)
  335. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  336. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  337. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  338. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  339. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  340. # Set callbacks
  341. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  342. keep_checkpoint_max=cfg.keep_checkpoint_max)
  343. time_cb = TimeMonitor(data_size=batch_num)
  344. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./",
  345. config=config_ck)
  346. loss_cb = LossMonitor()
  347. # Start training
  348. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  349. print("train success")
  350. ```
  351. - running on GPU
  352. ```
  353. # Load dataset
  354. dataset = create_dataset(cfg.data_path, 1)
  355. batch_num = dataset.get_dataset_size()
  356. # Define model
  357. net = GoogleNet(num_classes=cfg.num_classes)
  358. # Continue training if set pre_trained to be True
  359. if cfg.pre_trained:
  360. param_dict = load_checkpoint(cfg.checkpoint_path)
  361. load_param_into_net(net, param_dict)
  362. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  363. steps_per_epoch=batch_num)
  364. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  365. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  366. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  367. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  368. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  369. # Set callbacks
  370. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  371. keep_checkpoint_max=cfg.keep_checkpoint_max)
  372. time_cb = TimeMonitor(data_size=batch_num)
  373. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./ckpt_" + str(get_rank()) + "/",
  374. config=config_ck)
  375. loss_cb = LossMonitor()
  376. # Start training
  377. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  378. print("train success")
  379. ```
  380. ### Transfer Learning
  381. To be added.
  382. # [Description of Random Situation](#contents)
  383. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  384. # [ModelZoo Homepage](#contents)
  385. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).