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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.
  32. # [Dataset](#contents)
  33. Dataset used: [CIFAR-10](<http://www.cs.toronto.edu/~kriz/cifar.html>)
  34. - Dataset size:175M,60,000 32*32 colorful images in 10 classes
  35. - Train:146M,50,000 images
  36. - Test:29M,10,000 images
  37. - Data format:binary files
  38. - Note:Data will be processed in src/dataset.py
  39. Dataset used can refer to paper.
  40. - Dataset size: 125G, 1250k colorful images in 1000 classes
  41. - Train: 120G, 1200k images
  42. - Test: 5G, 50k images
  43. - Data format: RGB images.
  44. - Note: Data will be processed in src/dataset.py
  45. # [Features](#contents)
  46. ## Mixed Precision
  47. 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.
  48. 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’.
  49. # [Environment Requirements](#contents)
  50. - Hardware(Ascend/GPU)
  51. - 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.
  52. - Framework
  53. - [MindSpore](https://www.mindspore.cn/install/en)
  54. - For more information, please check the resources below:
  55. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  56. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  57. # [Quick Start](#contents)
  58. After installing MindSpore via the official website, you can start training and evaluation as follows:
  59. - runing on Ascend
  60. ```python
  61. # run training example
  62. python train.py > train.log 2>&1 &
  63. # run distributed training example
  64. sh scripts/run_train.sh rank_table.json
  65. # run evaluation example
  66. python eval.py > eval.log 2>&1 &
  67. OR
  68. sh run_eval.sh
  69. ```
  70. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  71. Please follow the instructions in the link below:
  72. https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools.
  73. - running on GPU
  74. For running on GPU, please change `device_target` from `Ascend` to `GPU` in configuration file src/config.py
  75. ```python
  76. # run training example
  77. export CUDA_VISIBLE_DEVICES=0
  78. python train.py > train.log 2>&1 &
  79. # run distributed training example
  80. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  81. # run evaluation example
  82. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  83. OR
  84. sh run_eval_gpu.sh [CHECKPOINT_PATH]
  85. ```
  86. 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.
  87. # [Script Description](#contents)
  88. ## [Script and Sample Code](#contents)
  89. ```
  90. ├── model_zoo
  91. ├── README.md // descriptions about all the models
  92. ├── googlenet
  93. ├── README.md // descriptions about googlenet
  94. ├── scripts
  95. │ ├──run_train.sh // shell script for distributed on Ascend
  96. │ ├──run_train_gpu.sh // shell script for distributed on GPU
  97. │ ├──run_eval.sh // shell script for evaluation on Ascend
  98. │ ├──run_eval_gpu.sh // shell script for evaluation on GPU
  99. ├── src
  100. │ ├──dataset.py // creating dataset
  101. │ ├──googlenet.py // googlenet architecture
  102. │ ├──config.py // parameter configuration
  103. ├── train.py // training script
  104. ├── eval.py // evaluation script
  105. ├── export.py // export checkpoint files into air/onnx
  106. ```
  107. ## [Script Parameters](#contents)
  108. Parameters for both training and evaluation can be set in config.py
  109. - config for GoogleNet, CIFAR-10 dataset
  110. ```python
  111. 'pre_trained': 'False' # whether training based on the pre-trained model
  112. 'nump_classes': 10 # the number of classes in the dataset
  113. 'lr_init': 0.1 # initial learning rate
  114. 'batch_size': 128 # training batch size
  115. 'epoch_size': 125 # total training epochs
  116. 'momentum': 0.9 # momentum
  117. 'weight_decay': 5e-4 # weight decay value
  118. 'image_height': 224 # image height used as input to the model
  119. 'image_width': 224 # image width used as input to the model
  120. 'data_path': './cifar10' # absolute full path to the train and evaluation datasets
  121. 'device_target': 'Ascend' # device running the program
  122. 'device_id': 4 # device ID used to train or evaluate the dataset. Ignore it when you use run_train.sh for distributed training
  123. 'keep_checkpoint_max': 10 # only keep the last keep_checkpoint_max checkpoint
  124. 'checkpoint_path': './train_googlenet_cifar10-125_390.ckpt' # the absolute full path to save the checkpoint file
  125. 'onnx_filename': 'googlenet.onnx' # file name of the onnx model used in export.py
  126. 'geir_filename': 'googlenet.geir' # file name of the geir model used in export.py
  127. ```
  128. For more configuration details, please refer the script `config.py`.
  129. ## [Training Process](#contents)
  130. ### Training
  131. - running on Ascend
  132. ```
  133. python train.py > train.log 2>&1 &
  134. ```
  135. The python command above will run in the background, you can view the results through the file `train.log`.
  136. After training, you'll get some checkpoint files under the script folder by default. The loss value will be achieved as follows:
  137. ```
  138. # grep "loss is " train.log
  139. epoch: 1 step: 390, loss is 1.4842823
  140. epcoh: 2 step: 390, loss is 1.0897788
  141. ...
  142. ```
  143. The model checkpoint will be saved in the current directory.
  144. - running on GPU
  145. ```
  146. export CUDA_VISIBLE_DEVICES=0
  147. python train.py > train.log 2>&1 &
  148. ```
  149. The python command above will run in the background, you can view the results through the file `train.log`.
  150. After training, you'll get some checkpoint files under the folder `./ckpt_0/` by default.
  151. ### Distributed Training
  152. - running on Ascend
  153. ```
  154. sh scripts/run_train.sh rank_table.json
  155. ```
  156. 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:
  157. ```
  158. # grep "result: " train_parallel*/log
  159. train_parallel0/log:epoch: 1 step: 48, loss is 1.4302931
  160. train_parallel0/log:epcoh: 2 step: 48, loss is 1.4023874
  161. ...
  162. train_parallel1/log:epoch: 1 step: 48, loss is 1.3458025
  163. train_parallel1/log:epcoh: 2 step: 48, loss is 1.3729336
  164. ...
  165. ...
  166. ```
  167. - running on GPU
  168. ```
  169. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  170. ```
  171. The above shell script will run distribute training in the background. You can view the results through the file `train/train.log`.
  172. ## [Evaluation Process](#contents)
  173. ### Evaluation
  174. - evaluation on CIFAR-10 dataset when running on Ascend
  175. 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".
  176. ```
  177. python eval.py > eval.log 2>&1 &
  178. OR
  179. sh scripts/run_eval.sh
  180. ```
  181. 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:
  182. ```
  183. # grep "accuracy: " eval.log
  184. accuracy: {'acc': 0.934}
  185. ```
  186. 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:
  187. ```
  188. # grep "accuracy: " dist.eval.log
  189. accuracy: {'acc': 0.9217}
  190. ```
  191. - evaluation on CIFAR-10 dataset when running on GPU
  192. 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".
  193. ```
  194. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  195. ```
  196. 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:
  197. ```
  198. # grep "accuracy: " eval.log
  199. accuracy: {'acc': 0.930}
  200. ```
  201. OR,
  202. ```
  203. sh scripts/run_eval_gpu.sh [CHECKPOINT_PATH]
  204. ```
  205. 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:
  206. ```
  207. # grep "accuracy: " eval/eval.log
  208. accuracy: {'acc': 0.930}
  209. ```
  210. # [Model Description](#contents)
  211. ## [Performance](#contents)
  212. ### Evaluation Performance
  213. #### GoogleNet on CIFAR-10
  214. | Parameters | Ascend | GPU |
  215. | -------------------------- | ----------------------------------------------------------- | ---------------------- |
  216. | Model Version | Inception V1 | Inception V1 |
  217. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G | NV SMX2 V100-32G |
  218. | uploaded Date | 08/31/2020 (month/day/year) | 08/20/2020 (month/day/year) |
  219. | MindSpore Version | 0.7.0-alpha | 0.6.0-alpha |
  220. | Dataset | CIFAR-10 | CIFAR-10 |
  221. | Training Parameters | epoch=125, steps=390, batch_size = 128, lr=0.1 | epoch=125, steps=390, batch_size=128, lr=0.1 |
  222. | Optimizer | Momentum | Momentum |
  223. | Loss Function | Softmax Cross Entropy | Softmax Cross Entropy |
  224. | outputs | probability | probobility |
  225. | Loss | 0.0016 | 0.0016 |
  226. | Speed | 1pc: 79 ms/step; 8pcs: 82 ms/step | 1pc: 150 ms/step; 8pcs: 164 ms/step |
  227. | Total time | 1pc: 63.85 mins; 8pcs: 11.28 mins | 1pc: 126.87 mins; 8pcs: 21.65 mins |
  228. | Parameters (M) | 13.0 | 13.0 |
  229. | Checkpoint for Fine tuning | 43.07M (.ckpt file) | 43.07M (.ckpt file) |
  230. | Model for inference | 21.50M (.onnx file), 21.60M(.air file) | |
  231. | Scripts | [googlenet script](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) | [googlenet script](https://gitee.com/mindspore/mindspore/tree/r0.6/model_zoo/official/cv/googlenet) |
  232. #### GoogleNet on 1200k images
  233. | Parameters | Ascend |
  234. | -------------------------- | ----------------------------------------------------------- |
  235. | Model Version | Inception V1 |
  236. | Resource | Ascend 910, CPU 2.60GHz, 56cores, Memory 314G |
  237. | uploaded Date | 09/20/2020 (month/day/year) |
  238. | MindSpore Version | 0.7.0-alpha |
  239. | Dataset | 1200k images |
  240. | Training Parameters | epoch=300, steps=5000, batch_size=256, lr=0.1 |
  241. | Optimizer | Momentum |
  242. | Loss Function | Softmax Cross Entropy |
  243. | outputs | probability |
  244. | Loss | 2.0 |
  245. | Speed | 1pc: 152 ms/step; 8pcs: 171 ms/step |
  246. | Total time | 8pcs: 8.8 hours |
  247. | Parameters (M) | 13.0 |
  248. | Checkpoint for Fine tuning | 52M (.ckpt file) |
  249. | Scripts | [googlenet script](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) |
  250. ### Inference Performance
  251. #### GoogleNet on CIFAR-10
  252. | Parameters | Ascend | GPU |
  253. | ------------------- | --------------------------- | --------------------------- |
  254. | Model Version | Inception V1 | Inception V1 |
  255. | Resource | Ascend 910 | GPU |
  256. | Uploaded Date | 08/31/2020 (month/day/year) | 08/20/2020 (month/day/year) |
  257. | MindSpore Version | 0.7.0-alpha | 0.6.0-alpha |
  258. | Dataset | CIFAR-10, 10,000 images | CIFAR-10, 10,000 images |
  259. | batch_size | 128 | 128 |
  260. | outputs | probability | probability |
  261. | Accuracy | 1pc: 93.4%; 8pcs: 92.17% | 1pc: 93%, 8pcs: 92.89% |
  262. | Model for inference | 21.50M (.onnx file) | |
  263. #### GoogleNet on 1200k images
  264. | Parameters | Ascend |
  265. | ------------------- | --------------------------- |
  266. | Model Version | Inception V1 |
  267. | Resource | Ascend 910 |
  268. | Uploaded Date | 09/20/2020 (month/day/year) |
  269. | MindSpore Version | 0.7.0-alpha |
  270. | Dataset | 1200k images |
  271. | batch_size | 256 |
  272. | outputs | probability |
  273. | Accuracy | 8pcs: 71.81% |
  274. ## [How to use](#contents)
  275. ### Inference
  276. 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:
  277. - Running on Ascend
  278. ```
  279. # Set context
  280. context.set_context(mode=context.GRAPH_HOME, device_target=cfg.device_target)
  281. context.set_context(device_id=cfg.device_id)
  282. # Load unseen dataset for inference
  283. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  284. # Define model
  285. net = GoogleNet(num_classes=cfg.num_classes)
  286. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  287. cfg.momentum, weight_decay=cfg.weight_decay)
  288. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  289. is_grad=False)
  290. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  291. # Load pre-trained model
  292. param_dict = load_checkpoint(cfg.checkpoint_path)
  293. load_param_into_net(net, param_dict)
  294. net.set_train(False)
  295. # Make predictions on the unseen dataset
  296. acc = model.eval(dataset)
  297. print("accuracy: ", acc)
  298. ```
  299. - Running on GPU:
  300. ```
  301. # Set context
  302. context.set_context(mode=context.GRAPH_HOME, device_target="GPU")
  303. # Load unseen dataset for inference
  304. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  305. # Define model
  306. net = GoogleNet(num_classes=cfg.num_classes)
  307. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  308. cfg.momentum, weight_decay=cfg.weight_decay)
  309. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  310. is_grad=False)
  311. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  312. # Load pre-trained model
  313. param_dict = load_checkpoint(args_opt.checkpoint_path)
  314. load_param_into_net(net, param_dict)
  315. net.set_train(False)
  316. # Make predictions on the unseen dataset
  317. acc = model.eval(dataset)
  318. print("accuracy: ", acc)
  319. ```
  320. ### Continue Training on the Pretrained Model
  321. - running on Ascend
  322. ```
  323. # Load dataset
  324. dataset = create_dataset(cfg.data_path, 1)
  325. batch_num = dataset.get_dataset_size()
  326. # Define model
  327. net = GoogleNet(num_classes=cfg.num_classes)
  328. # Continue training if set pre_trained to be True
  329. if cfg.pre_trained:
  330. param_dict = load_checkpoint(cfg.checkpoint_path)
  331. load_param_into_net(net, param_dict)
  332. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  333. steps_per_epoch=batch_num)
  334. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  335. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  336. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  337. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  338. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  339. # Set callbacks
  340. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  341. keep_checkpoint_max=cfg.keep_checkpoint_max)
  342. time_cb = TimeMonitor(data_size=batch_num)
  343. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./",
  344. config=config_ck)
  345. loss_cb = LossMonitor()
  346. # Start training
  347. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  348. print("train success")
  349. ```
  350. - running on GPU
  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', is_grad=False)
  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="./ckpt_" + str(get_rank()) + "/",
  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. ### Transfer Learning
  380. To be added.
  381. # [Description of Random Situation](#contents)
  382. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  383. # [ModelZoo Homepage](#contents)
  384. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).