[{"data":1,"prerenderedAt":517},["ShallowReactive",2],{"content-query-LuCWX1Pa6C":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":511,"_id":512,"_source":513,"_file":514,"_stem":515,"_extension":516},"/technology-blogs/zh/236","zh",false,"","MindSpore技术博客（四）","本文介绍了如何在MindSpore上实现FasterRcnn网络的训练与推理，通过使用MindSpore可以很方便、高效地完成CV典型应用的构建、训练、验证、部署等过程。","2020-07-28","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/09/21/5d2e47ef202f445ab8fa51de2364294e.png","technology-blogs","实践",{"type":15,"children":16,"toc":496},"root",[17,25,31,37,42,50,57,62,67,74,79,85,90,101,106,117,122,129,136,145,150,155,162,167,175,180,188,193,201,206,214,219,227,232,240,245,253,258,266,271,279,284,289,296,301,308,315,322,331,336,343,352,357,364,369,374,381,390,395,402,407,414,423,428,435,440,447,452,457,462,473,484,489],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore技术博客四",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"使用mindspore实现fasterrcnn网络的训练和推理",[29],{"type":24,"value":30},"使用MindSpore实现FasterRCNN网络的训练和推理",{"type":18,"tag":32,"props":33,"children":34},"p",{},[35],{"type":24,"value":36},"计算机视觉（Compute Vision，CV）给计算机装上了“眼睛”，让计算机像人类一样也有“视觉”能力，能够“看”懂图片里的内容。作为深度学习领域的最重要的应用场景之一，在手机拍照、智能安防、自动驾驶等场景均有广泛的应用，而检测类任务作为CV领域的一类经典任务，也在以上场景中广泛应用。",{"type":18,"tag":32,"props":38,"children":39},{},[40],{"type":24,"value":41},"本文以经典的目标检测网络FasterRcnn为例，介绍一下如何使用MindSpore来完成一个检测模型的开发及部署。",{"type":18,"tag":32,"props":43,"children":44},{},[45],{"type":18,"tag":46,"props":47,"children":49},"img",{"alt":7,"src":48},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/03d96c8643784f3999a66181af35b119.jpg",[],{"type":18,"tag":51,"props":52,"children":54},"h2",{"id":53},"fasterrcnn算法简介",[55],{"type":24,"value":56},"FasterRcnn算法简介",{"type":18,"tag":32,"props":58,"children":59},{},[60],{"type":24,"value":61},"FasterRCNN是一个two-stage结构的目标检测网络框架，其中主体结构包含4个部分，包括由Resnet50构成的网络主干，由FPN（Feature Paramid Network）构成的高分辨率特征融合模块，由RPN（Region Proposal Network）构成的兴趣区域（ROI）检测模块，以及由卷积和全连接层构成的分类和位置调整模块（RCNN）。",{"type":18,"tag":32,"props":63,"children":64},{},[65],{"type":24,"value":66},"下图是论文中给出的FasterRcnn网络的结构图。在本文中，我们对整体结构做了一些调整：在网络结构上使用ROIAlign模组代替了ROIPooling，并增加了FPN作为高分辨特征的融合组件。",{"type":18,"tag":32,"props":68,"children":69},{},[70],{"type":18,"tag":46,"props":71,"children":73},{"alt":7,"src":72},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/e4175a034cbd481489eac1a45d7492a4.jpg",[],{"type":18,"tag":32,"props":75,"children":76},{},[77],{"type":24,"value":78},"从图中可以看到，一张图片通过FasterRcnn网络，就可以获取到目标的位置与目标的类别，因此，我们可以将FasterRcnn应用到安防，自动驾驶等各种场景，让自动一定程度上减少人工的工作量。",{"type":18,"tag":51,"props":80,"children":82},{"id":81},"使用mindspore训练推理fasterrcnn",[83],{"type":24,"value":84},"使用MindSpore训练/推理FasterRcnn",{"type":18,"tag":32,"props":86,"children":87},{},[88],{"type":24,"value":89},"使用MindSpore来复现FasterRCNN这个经典的检测网络。这里仅列出了部分重要代码片段，完整代码请参考：",{"type":18,"tag":32,"props":91,"children":92},{},[93],{"type":18,"tag":94,"props":95,"children":99},"a",{"href":96,"rel":97},"https://gitee.com/mindspore/mindspore/",[98],"nofollow",[100],{"type":24,"value":96},{"type":18,"tag":32,"props":102,"children":103},{},[104],{"type":24,"value":105},"FasterRcnn主体的网络结构定义在src/FasterRcnn内，生成数据集的相关代码在src/dataset.py中，src/network_define.py封装放了训练相关的类，src/config.py中存放了配置信息。",{"type":18,"tag":107,"props":108,"children":110},"h3",{"id":109},"_1-配置信息",[111],{"type":18,"tag":112,"props":113,"children":114},"strong",{},[115],{"type":24,"value":116},"1. 配置信息",{"type":18,"tag":32,"props":118,"children":119},{},[120],{"type":24,"value":121},"配置文件里包含了网络中各种参数配置，包括resnet的层数，fpn的特征层数，学习率，batchsize，momentum等等，下图列举了部分参数，完整参数可以查看src/config.py。",{"type":18,"tag":32,"props":123,"children":124},{},[125],{"type":18,"tag":46,"props":126,"children":128},{"alt":7,"src":127},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/675dc7ccfe1046aaa13cfe4eb102dd97.jpg",[],{"type":18,"tag":32,"props":130,"children":131},{},[132],{"type":18,"tag":46,"props":133,"children":135},{"alt":7,"src":134},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/44140b0a86a34375918dc36b53beaa55.jpg",[],{"type":18,"tag":107,"props":137,"children":139},{"id":138},"_2-网络结构",[140],{"type":18,"tag":112,"props":141,"children":142},{},[143],{"type":24,"value":144},"2. 网络结构",{"type":18,"tag":32,"props":146,"children":147},{},[148],{"type":24,"value":149},"网络结构的定义是整个代码的核心部分，在FasterRcnn中，这一部分代码在src/FasterRcnn文件夹内，其中总体网络结构入口在src/faster_rcnn.r50.py",{"type":18,"tag":32,"props":151,"children":152},{},[153],{"type":24,"value":154},"文件中，其余文件是网络中各个子模块的网络结构，如下图：",{"type":18,"tag":32,"props":156,"children":157},{},[158],{"type":18,"tag":46,"props":159,"children":161},{"alt":7,"src":160},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/17e3bee345d04020a34802407a9497db.jpg",[],{"type":18,"tag":32,"props":163,"children":164},{},[165],{"type":24,"value":166},"每个模块的定义是：",{"type":18,"tag":32,"props":168,"children":169},{},[170],{"type":18,"tag":112,"props":171,"children":172},{},[173],{"type":24,"value":174},"ResnetFea：",{"type":18,"tag":32,"props":176,"children":177},{},[178],{"type":24,"value":179},"resnet的网络结构定义，为FasterRcnn的backbone的网络结构",{"type":18,"tag":32,"props":181,"children":182},{},[183],{"type":18,"tag":112,"props":184,"children":185},{},[186],{"type":24,"value":187},"· FeatPyramidNeck：",{"type":18,"tag":32,"props":189,"children":190},{},[191],{"type":24,"value":192},"FPN（特征金字塔网络）的网络结构定义，为FasterRcnn提供不同的高分辨率特征",{"type":18,"tag":32,"props":194,"children":195},{},[196],{"type":18,"tag":112,"props":197,"children":198},{},[199],{"type":24,"value":200},"· RPN：",{"type":18,"tag":32,"props":202,"children":203},{},[204],{"type":24,"value":205},"RPN(Region proposal network)的网络结构定义，为FasterRcnn第一阶段计算分类与回归loss的模块",{"type":18,"tag":32,"props":207,"children":208},{},[209],{"type":18,"tag":112,"props":210,"children":211},{},[212],{"type":24,"value":213},"· BboxAssignSample：",{"type":18,"tag":32,"props":215,"children":216},{},[217],{"type":24,"value":218},"为RPN模块的子模块，为RPN选择固定比率的正负样本参与loss计算",{"type":18,"tag":32,"props":220,"children":221},{},[222],{"type":18,"tag":112,"props":223,"children":224},{},[225],{"type":24,"value":226},"· Proposal：",{"type":18,"tag":32,"props":228,"children":229},{},[230],{"type":24,"value":231},"选取候选框的模块，后续第二阶段，只对这一模块输出的候选框进行计算",{"type":18,"tag":32,"props":233,"children":234},{},[235],{"type":18,"tag":112,"props":236,"children":237},{},[238],{"type":24,"value":239},"· BboxAssignSampleForRcnn：",{"type":18,"tag":32,"props":241,"children":242},{},[243],{"type":24,"value":244},"对Proposal模块输出的候选框，再次进行一轮正负样本的筛选，用于第二阶段的计算",{"type":18,"tag":32,"props":246,"children":247},{},[248],{"type":18,"tag":112,"props":249,"children":250},{},[251],{"type":24,"value":252},"· SingleRoIExtractor：",{"type":18,"tag":32,"props":254,"children":255},{},[256],{"type":24,"value":257},"该模块主要是用来提取每个候选框的对应特征，并保证特征大小一致",{"type":18,"tag":32,"props":259,"children":260},{},[261],{"type":18,"tag":112,"props":262,"children":263},{},[264],{"type":24,"value":265},"· RCNN：",{"type":18,"tag":32,"props":267,"children":268},{},[269],{"type":24,"value":270},"为FasterRcnn第二阶段计算分类与回归loss的模块",{"type":18,"tag":32,"props":272,"children":273},{},[274],{"type":18,"tag":112,"props":275,"children":276},{},[277],{"type":24,"value":278},"· AnchorGenerator：",{"type":18,"tag":32,"props":280,"children":281},{},[282],{"type":24,"value":283},"预先生成anchor框 的模块",{"type":18,"tag":32,"props":285,"children":286},{},[287],{"type":24,"value":288},"通过以上这些模块的组合，结合之前的网络结构介绍，我们就可以获取到一个完整的FasterRcnn网络的模型定义，下图就是部分整网定义的代码，完整的整网定义可以查看src/faster_rcnn.r50.py文件：",{"type":18,"tag":32,"props":290,"children":291},{},[292],{"type":18,"tag":46,"props":293,"children":295},{"alt":7,"src":294},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/a53f64735e9641f9a99031b103b8921a.jpg",[],{"type":18,"tag":32,"props":297,"children":298},{},[299],{"type":24,"value":300},"接下来，我们就可以在MindSpore中定义网络的执行顺序了，在MindSpore中，执行顺序参考construct函数，整网执行顺序如下：",{"type":18,"tag":32,"props":302,"children":303},{},[304],{"type":18,"tag":46,"props":305,"children":307},{"alt":7,"src":306},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/5cfd73e4dbc14a31afbf1d7150eeb6f2.jpg",[],{"type":18,"tag":32,"props":309,"children":310},{},[311],{"type":18,"tag":46,"props":312,"children":314},{"alt":7,"src":313},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/a6632a018f314fff83ec56e41c8da572.jpg",[],{"type":18,"tag":32,"props":316,"children":317},{},[318],{"type":18,"tag":46,"props":319,"children":321},{"alt":7,"src":320},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/4aa6a1f0fd7841768ac3d8236fb15161.jpg",[],{"type":18,"tag":107,"props":323,"children":325},{"id":324},"_3-lr定义",[326],{"type":18,"tag":112,"props":327,"children":328},{},[329],{"type":24,"value":330},"3. Lr定义",{"type":18,"tag":32,"props":332,"children":333},{},[334],{"type":24,"value":335},"为了得到更好的训练效果，我们可以使用动态学习率来进行训练，在本文中，我们结合了warmup与cosine学习率来进行训练：",{"type":18,"tag":32,"props":337,"children":338},{},[339],{"type":18,"tag":46,"props":340,"children":342},{"alt":7,"src":341},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/4617fec020c14d6880cb5753bf79c28c.jpg",[],{"type":18,"tag":107,"props":344,"children":346},{"id":345},"_4-数据生成与数据增强",[347],{"type":18,"tag":112,"props":348,"children":349},{},[350],{"type":24,"value":351},"4. 数据生成与数据增强",{"type":18,"tag":32,"props":353,"children":354},{},[355],{"type":24,"value":356},"MindSpore中提供了MindRecord的接口来存储数据，方便用户使用，我们可以先把图片与标签数据生成MindRecord格式的数据，方便后续使用：",{"type":18,"tag":32,"props":358,"children":359},{},[360],{"type":18,"tag":46,"props":361,"children":363},{"alt":7,"src":362},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/9804c7042a4e4332a45e5f916acc8295.jpg",[],{"type":18,"tag":32,"props":365,"children":366},{},[367],{"type":24,"value":368},"在训练与推理的时候，因为采用的数据增强方式不同，所以我们可以通过is_training标志位来区分数据处理，并且MindData中提供了大量高效的数据增强方式，我们可以快速调用这些数据增强，来提升我们的网络精度。",{"type":18,"tag":32,"props":370,"children":371},{},[372],{"type":24,"value":373},"如下图所示，我们为训练增加了随机的图片翻转，来提升模型精度：",{"type":18,"tag":32,"props":375,"children":376},{},[377],{"type":18,"tag":46,"props":378,"children":380},{"alt":7,"src":379},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/f7b997801c13464b93bf24293d57b622.jpg",[],{"type":18,"tag":107,"props":382,"children":384},{"id":383},"_5-训练fasterrcnn网络",[385],{"type":18,"tag":112,"props":386,"children":387},{},[388],{"type":24,"value":389},"5. 训练FasterRcnn网络",{"type":18,"tag":32,"props":391,"children":392},{},[393],{"type":24,"value":394},"做完上面一系列准备后，我们就可以着手开始训练我们的网络了：",{"type":18,"tag":32,"props":396,"children":397},{},[398],{"type":18,"tag":46,"props":399,"children":401},{"alt":7,"src":400},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/27d5161c7a7847e3b0538249c5bd407f.jpg",[],{"type":18,"tag":32,"props":403,"children":404},{},[405],{"type":24,"value":406},"在训练过程中，我们可以在loss.log中看到loss打印：",{"type":18,"tag":32,"props":408,"children":409},{},[410],{"type":18,"tag":46,"props":411,"children":413},{"alt":7,"src":412},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/55c744d7268e4381b91d2b7eaf92d8b2.png",[],{"type":18,"tag":107,"props":415,"children":417},{"id":416},"_6-推理fasterrcnn网络",[418],{"type":18,"tag":112,"props":419,"children":420},{},[421],{"type":24,"value":422},"6. 推理FasterRcnn网络",{"type":18,"tag":32,"props":424,"children":425},{},[426],{"type":24,"value":427},"当我们完成训练后，想查看我们训练的效果，这时候可以加载我们训练好的模型，来获取推理的精度：",{"type":18,"tag":32,"props":429,"children":430},{},[431],{"type":18,"tag":46,"props":432,"children":434},{"alt":7,"src":433},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/80007d0733cd46a39c5dd2d9bc23eda3.jpg",[],{"type":18,"tag":32,"props":436,"children":437},{},[438],{"type":24,"value":439},"推理完成后，我们可以看到如下推理结果：",{"type":18,"tag":32,"props":441,"children":442},{},[443],{"type":18,"tag":46,"props":444,"children":446},{"alt":7,"src":445},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/4e55dac0d91a458281b2eac2f732a1fa.jpg",[],{"type":18,"tag":51,"props":448,"children":450},{"id":449},"总结",[451],{"type":24,"value":449},{"type":18,"tag":32,"props":453,"children":454},{},[455],{"type":24,"value":456},"本文介绍了如何在MindSpore上实现FasterRcnn网络的训练与推理，通过使用MindSpore可以很方便、高效地完成CV典型应用的构建、训练、验证、部署等过程。感兴趣的朋友们可以试一试~",{"type":18,"tag":32,"props":458,"children":459},{},[460],{"type":24,"value":461},"MindSpore官方资料",{"type":18,"tag":32,"props":463,"children":464},{},[465,467],{"type":24,"value":466},"GitHub:",{"type":18,"tag":94,"props":468,"children":471},{"href":469,"rel":470},"https://github.com/mindspore-ai/mindspore",[98],[472],{"type":24,"value":469},{"type":18,"tag":32,"props":474,"children":475},{},[476,478],{"type":24,"value":477},"Gitee:",{"type":18,"tag":94,"props":479,"children":482},{"href":480,"rel":481},"https://gitee.com/mindspore/mindspore",[98],[483],{"type":24,"value":480},{"type":18,"tag":32,"props":485,"children":486},{},[487],{"type":24,"value":488},"官方QQ群: 871543426",{"type":18,"tag":32,"props":490,"children":491},{},[492],{"type":18,"tag":46,"props":493,"children":495},{"alt":7,"src":494},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/07/28/6142f1ba75954069a78922acc169c579.jpg",[],{"title":7,"searchDepth":497,"depth":497,"links":498},4,[499,501,510],{"id":53,"depth":500,"text":56},2,{"id":81,"depth":500,"text":84,"children":502},[503,505,506,507,508,509],{"id":109,"depth":504,"text":116},3,{"id":138,"depth":504,"text":144},{"id":324,"depth":504,"text":330},{"id":345,"depth":504,"text":351},{"id":383,"depth":504,"text":389},{"id":416,"depth":504,"text":422},{"id":449,"depth":500,"text":449},"markdown","content:technology-blogs:zh:236.md","content","technology-blogs/zh/236.md","technology-blogs/zh/236","md",1776506121705]