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_CN.md 22 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. # 目录
  2. [View English](./README.md)
  3. <!-- TOC -->
  4. - [目录](#目录)
  5. - [GoogleNet描述](#googlenet描述)
  6. - [模型架构](#模型架构)
  7. - [数据集](#数据集)
  8. - [特性](#特性)
  9. - [混合精度](#混合精度)
  10. - [环境要求](#环境要求)
  11. - [快速入门](#快速入门)
  12. - [脚本说明](#脚本说明)
  13. - [脚本及样例代码](#脚本及样例代码)
  14. - [脚本参数](#脚本参数)
  15. - [训练过程](#训练过程)
  16. - [训练](#训练)
  17. - [分布式训练](#分布式训练)
  18. - [评估过程](#评估过程)
  19. - [评估](#评估)
  20. - [模型描述](#模型描述)
  21. - [性能](#性能)
  22. - [评估性能](#评估性能)
  23. - [CIFAR-10上的GoogleNet](#cifar-10上的googlenet)
  24. - [120万张图像上的GoogleNet](#120万张图像上的googlenet)
  25. - [推理性能](#推理性能)
  26. - [CIFAR-10上的GoogleNet](#cifar-10上的googlenet-1)
  27. - [120万张图像上的GoogleNet](#120万张图像上的googlenet-1)
  28. - [使用流程](#使用流程)
  29. - [推理](#推理)
  30. - [继续训练预训练模型](#继续训练预训练模型)
  31. - [迁移学习](#迁移学习)
  32. - [随机情况说明](#随机情况说明)
  33. - [ModelZoo主页](#modelzoo主页)
  34. <!-- /TOC -->
  35. # GoogleNet描述
  36. GoogleNet是2014年提出的22层深度网络,在2014年ImageNet大型视觉识别挑战赛(ILSVRC14)中获得第一名。 GoogleNet,也称Inception v1,比ZFNet(2013年获奖者)和AlexNet(2012年获奖者)改进明显,与VGGNet相比,错误率相对较低。 深度学习网络包含的参数更多,更容易过拟合。网络规模变大也会增加使用计算资源。为了解决这些问题,GoogleNet采用1*1卷积核来降维,从而进一步减少计算量。在网络末端使用全局平均池化,而不是使用全连接的层。 inception模块为相同的输入设置不同大小的卷积,并堆叠所有输出。
  37. [论文](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.
  38. # 模型架构
  39. GoogleNet由多个inception模块串联起来,可以更加深入。 降维的inception模块一般包括**1×1卷积**、**3×3卷积**、**5×5卷积**和**3×3最大池化**,同时完成前一次的输入,并在输出处再次堆叠在一起。
  40. # 数据集
  41. 使用的数据集:[CIFAR-10](<http://www.cs.toronto.edu/~kriz/cifar.html>)
  42. - 数据集大小:175M,共10个类、6万张32*32彩色图像
  43. - 训练集:146M,共5万张图像
  44. - 测试集:29M,共1万张图像
  45. - 数据格式:二进制文件
  46. - 注:数据将在src/dataset.py中处理。
  47. 所用数据集可参照论文。
  48. - 数据集大小:125G,共1000个类、125万张彩色图像
  49. - 训练集:120G,共120万张图像
  50. - 测试集:5G,共5万张图像
  51. - 数据格式:RGB
  52. - 注:数据将在src/dataset.py中处理。
  53. # 特性
  54. ## 混合精度
  55. 采用[混合精度](https://www.mindspore.cn/tutorial/training/zh-CN/master/advanced_use/enable_mixed_precision.html)的训练方法使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
  56. 以FP16算子为例,如果输入数据类型为FP32,MindSpore后台会自动降低精度来处理数据。用户可打开INFO日志,搜索“reduce precision”查看精度降低的算子。
  57. # 环境要求
  58. - 硬件(Ascend/GPU)
  59. - 使用Ascend或GPU处理器来搭建硬件环境。
  60. - 框架
  61. - [MindSpore](https://www.mindspore.cn/install/en)
  62. - 如需查看详情,请参见如下资源:
  63. - [MindSpore教程](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)
  64. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  65. # 快速入门
  66. 通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
  67. - Ascend处理器环境运行
  68. ```python
  69. # 运行训练示例
  70. python train.py > train.log 2>&1 &
  71. # 运行分布式训练示例
  72. sh scripts/run_train.sh rank_table.json
  73. # 运行评估示例
  74. python eval.py > eval.log 2>&1 &
  75. sh run_eval.sh
  76. ```
  77. 对于分布式训练,需要提前创建JSON格式的hccl配置文件。
  78. 请遵循以下链接中的说明:
  79. <https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools.>
  80. - GPU处理器环境运行
  81. 为了在GPU处理器环境运行,请将配置文件src/config.py中的`device_target`从`Ascend`改为`GPU`
  82. ```python
  83. # 运行训练示例
  84. export CUDA_VISIBLE_DEVICES=0
  85. python train.py > train.log 2>&1 &
  86. # 运行分布式训练示例
  87. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  88. # 运行评估示例
  89. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  90. OR
  91. sh run_eval_gpu.sh [CHECKPOINT_PATH]
  92. ```
  93. 默认使用CIFAR-10数据集。您也可以将`$dataset_type`传入脚本,以便选择其他数据集。如需查看更多详情,请参考指定脚本。
  94. # 脚本说明
  95. ## 脚本及样例代码
  96. ```bash
  97. ├── model_zoo
  98. ├── README.md // 所有模型相关说明
  99. ├── googlenet
  100. ├── README.md // googlenet相关说明
  101. ├── scripts
  102. │ ├──run_train.sh // 分布式到Ascend的shell脚本
  103. │ ├──run_train_gpu.sh // 分布式到GPU处理器的shell脚本
  104. │ ├──run_eval.sh // Ascend评估的shell脚本
  105. │ ├──run_eval_gpu.sh // GPU处理器评估的shell脚本
  106. ├── src
  107. │ ├──dataset.py // 创建数据集
  108. │ ├──googlenet.py // googlenet架构
  109. │ ├──config.py // 参数配置
  110. ├── train.py // 训练脚本
  111. ├── eval.py // 评估脚本
  112. ├── export.py // 将checkpoint文件导出到air/onnx下
  113. ```
  114. ## 脚本参数
  115. 在config.py中可以同时配置训练参数和评估参数。
  116. - 配置GoogleNet和CIFAR-10数据集。
  117. ```python
  118. 'pre_trained':'False' # 是否基于预训练模型训练
  119. 'nump_classes':10 # 数据集类数
  120. 'lr_init':0.1 # 初始学习率
  121. 'batch_size':128 # 训练批次大小
  122. 'epoch_size':125 # 总计训练epoch数
  123. 'momentum':0.9 # 动量
  124. 'weight_decay':5e-4 # 权重衰减值
  125. 'image_height':224 # 输入到模型的图像高度
  126. 'image_width':224 # 输入到模型的图像宽度
  127. 'data_path':'./cifar10' # 训练和评估数据集的绝对全路径
  128. 'device_target':'Ascend' # 运行设备
  129. 'device_id':4 # 用于训练或评估数据集的设备ID使用run_train.sh进行分布式训练时可以忽略。
  130. 'keep_checkpoint_max':10 # 只保存最后一个keep_checkpoint_max检查点
  131. 'checkpoint_path':'./train_googlenet_cifar10-125_390.ckpt' # checkpoint文件保存的绝对全路径
  132. 'onnx_filename':'googlenet.onnx' # export.py中使用的onnx模型文件名
  133. 'geir_filename':'googlenet.geir' # export.py中使用的geir模型文件名
  134. ```
  135. 更多配置细节请参考脚本`config.py`。
  136. ## 训练过程
  137. ### 训练
  138. - Ascend处理器环境运行
  139. ```bash
  140. python train.py > train.log 2>&1 &
  141. ```
  142. 上述python命令将在后台运行,您可以通过train.log文件查看结果。
  143. 训练结束后,您可在默认脚本文件夹下找到检查点文件。采用以下方式达到损失值:
  144. ```bash
  145. # grep "loss is " train.log
  146. epoch:1 step:390, loss is 1.4842823
  147. epcoh:2 step:390, loss is 1.0897788
  148. ...
  149. ```
  150. 模型检查点保存在当前目录下。
  151. - GPU处理器环境运行
  152. ```bash
  153. export CUDA_VISIBLE_DEVICES=0
  154. python train.py > train.log 2>&1 &
  155. ```
  156. 上述python命令将在后台运行,您可以通过train.log文件查看结果。
  157. 训练结束后,您可在默认`./ckpt_0/`脚本文件夹下找到检查点文件。
  158. ### 分布式训练
  159. - Ascend处理器环境运行
  160. ```bash
  161. sh scripts/run_train.sh rank_table.json
  162. ```
  163. 上述shell脚本将在后台运行分布训练。您可以通过train_parallel[X]/log文件查看结果。采用以下方式达到损失值:
  164. ```bash
  165. # grep "result:" train_parallel*/log
  166. train_parallel0/log:epoch:1 step:48, loss is 1.4302931
  167. train_parallel0/log:epcoh:2 step:48, loss is 1.4023874
  168. ...
  169. train_parallel1/log:epoch:1 step:48, loss is 1.3458025
  170. train_parallel1/log:epcoh:2 step:48, loss is 1.3729336
  171. ...
  172. ...
  173. ```
  174. - GPU处理器环境运行
  175. ```bash
  176. sh scripts/run_train_gpu.sh 8 0,1,2,3,4,5,6,7
  177. ```
  178. 上述shell脚本将在后台运行分布训练。您可以通过train/train.log文件查看结果。
  179. ## 评估过程
  180. ### 评估
  181. - 在Ascend环境运行时评估CIFAR-10数据集
  182. 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/googlenet/train_googlenet_cifar10-125_390.ckpt”。
  183. ```bash
  184. python eval.py > eval.log 2>&1 &
  185. OR
  186. sh scripts/run_eval.sh
  187. ```
  188. 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下:
  189. ```bash
  190. # grep "accuracy:" eval.log
  191. accuracy:{'acc':0.934}
  192. ```
  193. 注:对于分布式训练后评估,请将checkpoint_path设置为最后保存的检查点文件,如“username/googlenet/train_parallel0/train_googlenet_cifar10-125_48.ckpt”。测试数据集的准确性如下:
  194. ```bash
  195. # grep "accuracy:" dist.eval.log
  196. accuracy:{'acc':0.9217}
  197. ```
  198. - 在GPU处理器环境运行时评估CIFAR-10数据集
  199. 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/googlenet/train/ckpt_0/train_googlenet_cifar10-125_390.ckpt”。
  200. ```bash
  201. python eval.py --checkpoint_path=[CHECKPOINT_PATH] > eval.log 2>&1 &
  202. ```
  203. 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下:
  204. ```bash
  205. # grep "accuracy:" eval.log
  206. accuracy:{'acc':0.930}
  207. ```
  208. 或者,
  209. ```bash
  210. sh scripts/run_eval_gpu.sh [CHECKPOINT_PATH]
  211. ```
  212. 上述python命令将在后台运行,您可以通过eval/eval.log文件查看结果。测试数据集的准确性如下:
  213. ```bash
  214. # grep "accuracy:" eval/eval.log
  215. accuracy:{'acc':0.930}
  216. ```
  217. # 模型描述
  218. ## 性能
  219. ### 评估性能
  220. #### CIFAR-10上的GoogleNet
  221. | 参数 | Ascend | GPU |
  222. | -------------------------- | ----------------------------------------------------------- | ---------------------- |
  223. | 模型版本 | Inception V1 | Inception V1 |
  224. | 资源 | Ascend 910;CPU 2.60GHz,192核;内存 755G;系统 Euler2.8 | NV SMX2 V100-32G |
  225. | 上传日期 | 2020-08-31 | 2020-08-20 |
  226. | MindSpore版本 | 0.7.0-alpha | 0.6.0-alpha |
  227. | 数据集 | CIFAR-10 | CIFAR-10 |
  228. | 训练参数 | epoch=125, steps=390, batch_size = 128, lr=0.1 | epoch=125, steps=390, batch_size=128, lr=0.1 |
  229. | 优化器 | Momentum | Momentum |
  230. | 损失函数 | Softmax交叉熵 | Softmax交叉熵 |
  231. | 输出 | 概率 | 概率 |
  232. | 损失 | 0.0016 | 0.0016 |
  233. | 速度 | 单卡:79毫秒/步; 8卡:82毫秒/步 | 单卡:150毫秒/步; 8卡:164毫秒/步 |
  234. | 总时长 | 单卡:63.85分钟; 8卡:11.28分钟 | 单卡:126.87分钟; 8卡:21.65分钟 |
  235. | 参数(M) | 13.0 | 13.0 |
  236. | 微调检查点 | 43.07M (.ckpt文件) | 43.07M (.ckpt文件) |
  237. | 推理模型 | 21.50M (.onnx文件), 21.60M(.air文件) | |
  238. | 脚本 | [googlenet脚本](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) | [googlenet 脚本](https://gitee.com/mindspore/mindspore/tree/r0.6/model_zoo/official/cv/googlenet) |
  239. #### 120万张图像上的GoogleNet
  240. | 参数 | Ascend |
  241. | -------------------------- | ----------------------------------------------------------- |
  242. | 模型版本 | Inception V1 |
  243. | 资源 | Ascend 910;CPU 2.60GHz,56核;内存 314G;系统 Euler2.8 |
  244. | 上传日期 | 2020-09-20 |
  245. | MindSpore版本 | 0.7.0-alpha |
  246. | 数据集 | 120万张图像 |
  247. | 训练参数 | epoch=300, steps=5000, batch_size=256, lr=0.1 |
  248. | 优化器 | Momentum |
  249. | 损失函数 | Softmax交叉熵 |
  250. | 输出 | 概率 |
  251. | 损失 | 2.0 |
  252. | 速度 | 单卡:152毫秒/步; 8卡:171毫秒/步 |
  253. | 总时长 | 8卡:8.8小时 |
  254. | 参数(M) | 13.0 |
  255. | 微调检查点 | 52M (.ckpt文件) |
  256. | 脚本 | [googlenet脚本](https://gitee.com/mindspore/mindspore/tree/r0.7/model_zoo/official/cv/googlenet) |
  257. ### 推理性能
  258. #### CIFAR-10上的GoogleNet
  259. | 参数 | Ascend | GPU |
  260. | ------------------- | --------------------------- | --------------------------- |
  261. | 模型版本 | Inception V1 | Inception V1 |
  262. | 资源 | Ascend 910;系统 Euler2.8 | GPU |
  263. | 上传日期 | 2020-08-31 | 2020-08-20 |
  264. | MindSpore 版本 | 0.7.0-alpha | 0.6.0-alpha |
  265. | 数据集 | CIFAR-10, 1万张图像 | CIFAR-10, 1万张图像 |
  266. | batch_size | 128 | 128 |
  267. | 输出 | 概率 | 概率 |
  268. | 准确性 | 单卡: 93.4%; 8卡:92.17% | 单卡:93%, 8卡:92.89% |
  269. | 推理模型 | 21.50M (.onnx文件) | |
  270. #### 120万张图像上的GoogleNet
  271. | 参数 | Ascend |
  272. | ------------------- | --------------------------- |
  273. | 模型版本 | Inception V1 |
  274. | 资源 | Ascend 910;系统 Euler2.8 |
  275. | 上传日期 | 2020-09-20 |
  276. | MindSpore版本 | 0.7.0-alpha |
  277. | 数据集 | 12万张图像 |
  278. | batch_size | 256 |
  279. | 输出 | 概率 |
  280. | 准确性 | 8卡: 71.81% |
  281. ## 使用流程
  282. ### 推理
  283. 如果您需要使用此训练模型在GPU、Ascend 910、Ascend 310等多个硬件平台上进行推理,可参考此[链接](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/migrate_3rd_scripts.html)。下面是操作步骤示例:
  284. - Ascend处理器环境运行
  285. ```python
  286. # 设置上下文
  287. context.set_context(mode=context.GRAPH_HOME, device_target=cfg.device_target)
  288. context.set_context(device_id=cfg.device_id)
  289. # 加载未知数据集进行推理
  290. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  291. # 定义模型
  292. net = GoogleNet(num_classes=cfg.num_classes)
  293. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  294. cfg.momentum, weight_decay=cfg.weight_decay)
  295. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  296. is_grad=False)
  297. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  298. # 加载预训练模型
  299. param_dict = load_checkpoint(cfg.checkpoint_path)
  300. load_param_into_net(net, param_dict)
  301. net.set_train(False)
  302. # 对未知数据集进行预测
  303. acc = model.eval(dataset)
  304. print("accuracy:", acc)
  305. ```
  306. - GPU处理器环境运行
  307. ```python
  308. # 设置上下文
  309. context.set_context(mode=context.GRAPH_HOME, device_target="GPU")
  310. # 加载未知数据集进行推理
  311. dataset = dataset.create_dataset(cfg.data_path, 1, False)
  312. # 定义模型
  313. net = GoogleNet(num_classes=cfg.num_classes)
  314. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01,
  315. cfg.momentum, weight_decay=cfg.weight_decay)
  316. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean',
  317. is_grad=False)
  318. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  319. # 加载预训练模型
  320. param_dict = load_checkpoint(args_opt.checkpoint_path)
  321. load_param_into_net(net, param_dict)
  322. net.set_train(False)
  323. # Make predictions on the unseen dataset
  324. acc = model.eval(dataset)
  325. print("accuracy:", acc)
  326. ```
  327. ### 继续训练预训练模型
  328. - Ascend处理器环境运行
  329. ```python
  330. # 加载数据集
  331. dataset = create_dataset(cfg.data_path, 1)
  332. batch_num = dataset.get_dataset_size()
  333. # 定义模型
  334. net = GoogleNet(num_classes=cfg.num_classes)
  335. # 若pre_trained为True,继续训练
  336. if cfg.pre_trained:
  337. param_dict = load_checkpoint(cfg.checkpoint_path)
  338. load_param_into_net(net, param_dict)
  339. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  340. steps_per_epoch=batch_num)
  341. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  342. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  343. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  344. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  345. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  346. # 设置回调
  347. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  348. keep_checkpoint_max=cfg.keep_checkpoint_max)
  349. time_cb = TimeMonitor(data_size=batch_num)
  350. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./",
  351. config=config_ck)
  352. loss_cb = LossMonitor()
  353. # 开始训练
  354. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  355. print("train success")
  356. ```
  357. - GPU处理器环境运行
  358. ```python
  359. # 加载数据集
  360. dataset = create_dataset(cfg.data_path, 1)
  361. batch_num = dataset.get_dataset_size()
  362. # 定义模型
  363. net = GoogleNet(num_classes=cfg.num_classes)
  364. # 若pre_trained为True,继续训练
  365. if cfg.pre_trained:
  366. param_dict = load_checkpoint(cfg.checkpoint_path)
  367. load_param_into_net(net, param_dict)
  368. lr = lr_steps(0, lr_max=cfg.lr_init, total_epochs=cfg.epoch_size,
  369. steps_per_epoch=batch_num)
  370. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
  371. Tensor(lr), cfg.momentum, weight_decay=cfg.weight_decay)
  372. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  373. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'},
  374. amp_level="O2", keep_batchnorm_fp32=False, loss_scale_manager=None)
  375. # 设置回调
  376. config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5,
  377. keep_checkpoint_max=cfg.keep_checkpoint_max)
  378. time_cb = TimeMonitor(data_size=batch_num)
  379. ckpoint_cb = ModelCheckpoint(prefix="train_googlenet_cifar10", directory="./ckpt_" + str(get_rank()) + "/",
  380. config=config_ck)
  381. loss_cb = LossMonitor()
  382. # 开始训练
  383. model.train(cfg.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb])
  384. print("train success")
  385. ```
  386. ### 迁移学习
  387. 待补充
  388. # 随机情况说明
  389. 在dataset.py中,我们设置了“create_dataset”函数内的种子,同时还使用了train.py中的随机种子。
  390. # ModelZoo主页
  391. 请浏览官网[主页](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)。