[{"data":1,"prerenderedAt":265},["ShallowReactive",2],{"content-query-o82BU4d0j6":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":259,"_id":260,"_source":261,"_file":262,"_stem":263,"_extension":264},"/technology-blogs/zh/3557","zh",false,"","开源之夏系列 | 基于昇思MindSpore动态图的MindSpore One套件开发","开源之夏，是由中国科学院软件研究所发起，专为高校学生精心打造的活动。旨在鼓励广大学子积极参与开源软件的开发与维护，推动优秀开源软件社区的蓬勃发展。","2024-12-20","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/08/2827874829824517a222a8355ff57b2d.png","technology-blogs","实践",{"type":15,"children":16,"toc":256},"root",[17,25,30,35,43,52,57,62,75,80,85,92,97,105,110,118,123,128,135,140,145,150,155,163,168,173,181,186,195,202,207,214,219,226,233,241,246,251],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"开源之夏系列-基于昇思mindspore动态图的mindspore-one套件开发",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":9},{"type":18,"tag":26,"props":31,"children":32},{},[33],{"type":24,"value":34},"目前，开源之夏2024已圆满结项！在本届开源之夏中，不少开发者跟随昇思MindSpore一起，在开源的世界里畅游，成功完成项目任务。在此，昇思 MindSpore 开源社区邀请了开源之夏的开发者们，分享他们在本次活动中的宝贵经验与心得。我们希望通过这些精彩的项目经历和实战技巧，能够激发更多创意火花，帮助大家提升技术能力。本文为昇思MindSpore 开源之夏项目经验分享系列第3篇。",{"type":18,"tag":26,"props":36,"children":37},{},[38],{"type":18,"tag":39,"props":40,"children":42},"img",{"alt":7,"src":41},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/6fb4d1d1e7884ba1bfb255f402a71772.png",[],{"type":18,"tag":26,"props":44,"children":45},{},[46],{"type":18,"tag":47,"props":48,"children":49},"strong",{},[50],{"type":24,"value":51},"项目基本介绍",{"type":18,"tag":26,"props":53,"children":54},{},[55],{"type":24,"value":56},"1、项目名称：基于昇思MindSpore动态图的MindSpore One套件开发",{"type":18,"tag":26,"props":58,"children":59},{},[60],{"type":24,"value":61},"2、项目导师：张俊龙",{"type":18,"tag":26,"props":63,"children":64},{},[65,67],{"type":24,"value":66},"3、项目链接：",{"type":18,"tag":68,"props":69,"children":73},"a",{"href":70,"rel":71},"https://github.com/mindspore-lab/models/tree/master/research/marigold",[72],"nofollow",[74],{"type":24,"value":70},{"type":18,"tag":26,"props":76,"children":77},{},[78],{"type":24,"value":79},"4、项目描述：基于MindSpore动态图开发MindSpore One套件的功能，具体是将深度估计领域论文Marigold ([2312.02145] Repurposing Diffusion-Based Image Generators for Monocular Depth Estimation) 模型从Pytorch框架移植到MindSpore框架，支持在MindSpore的动态图模式下进行深度估计的推理。",{"type":18,"tag":26,"props":81,"children":82},{},[83],{"type":24,"value":84},"深度估计是对给定RGB图像预测相对深度图的任务，即对输入图像可以预测出所有像素位置到相机的相对远近（示例图中偏红更近，偏蓝更远）。",{"type":18,"tag":26,"props":86,"children":87},{},[88],{"type":18,"tag":39,"props":89,"children":91},{"alt":7,"src":90},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/1d116d7a6bc74abf906e71faa4c1784e.png",[],{"type":18,"tag":26,"props":93,"children":94},{},[95],{"type":24,"value":96},"经过开发，项目目前移植了训练、测试和推理的完整流程，支持深度估计、测试指标计算及模型训练。同时项目移植了各功能脚本，保证了易用性，通过写好的脚本，一个命令即可获取任意自定义图像对应的相对深度图，并支持根据需求自定义推理参数。",{"type":18,"tag":26,"props":98,"children":99},{},[100],{"type":18,"tag":47,"props":101,"children":102},{},[103],{"type":24,"value":104},"项目选择初衷",{"type":18,"tag":26,"props":106,"children":107},{},[108],{"type":24,"value":109},"选择本项目主要是因为我的研究方向就是视觉感知，目前深度估计是感知中一个比较有应用前景的方向，我也了解了很多相关的算法。Marigold作为近年来用Diffusion做深度估计比较广为人知的工作，我详细阅读过源码和运行过，比较了解；另一方面我也对MindSpore感兴趣，Mindone仓库主要是基于MindSpore支持各类生成模型，用Diffusion生成深度图的功能正好合适，也有应用价值，我就申请了这个项目。",{"type":18,"tag":26,"props":111,"children":112},{},[113],{"type":18,"tag":47,"props":114,"children":115},{},[116],{"type":24,"value":117},"项目方案介绍",{"type":18,"tag":26,"props":119,"children":120},{},[121],{"type":24,"value":122},"我首先了解了MindSpore One已有代码，MindSpore one代码完善，功能丰富，支持的模型也很多，Marigold基于的Stable Diffusion就在其中有训练和推理代码的实现。",{"type":18,"tag":26,"props":124,"children":125},{},[126],{"type":24,"value":127},"然后总结了Marigold相比Stable Diffusion推理的主要不同点：",{"type":18,"tag":26,"props":129,"children":130},{},[131],{"type":18,"tag":39,"props":132,"children":134},{"alt":7,"src":133},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/4b74f763c2c84252a8357ca88c9a2497.png",[],{"type":18,"tag":26,"props":136,"children":137},{},[138],{"type":24,"value":139},"最后分析了需要移植的内容：",{"type":18,"tag":26,"props":141,"children":142},{},[143],{"type":24,"value":144},"1、所有数据集、前后处理函数，包括读图及缩放等预处理、。",{"type":18,"tag":26,"props":146,"children":147},{},[148],{"type":24,"value":149},"2、类Diffusion Pipeline的Marigold Pipeline的实现。",{"type":18,"tag":26,"props":151,"children":152},{},[153],{"type":24,"value":154},"3、推理、测试脚本等方便使用的代码。",{"type":18,"tag":26,"props":156,"children":157},{},[158],{"type":18,"tag":47,"props":159,"children":160},{},[161],{"type":24,"value":162},"项目实现思路",{"type":18,"tag":26,"props":164,"children":165},{},[166],{"type":24,"value":167},"在推理方面，项目的核心思路是将原本使用的diffusers模块，如UNet和VAE，替换为MindSpore One的diffusers实现，然后将各种Torch API转换为Mindspore API。在转换的过程中关注两者API功能的区别，确保实现逻辑上的一致，最终整合各函数并优化整体处理效率，以提高易用性。",{"type":18,"tag":26,"props":169,"children":170},{},[171],{"type":24,"value":172},"但是在训练方面，使用动态图模式实现的训练过程速度比较慢，因此训练代码基于MindSpore One已有Diffusion训练代码修改，主要修改数据集创建、损失函数、学习率下降方式及两者推理的不同。",{"type":18,"tag":26,"props":174,"children":175},{},[176],{"type":18,"tag":47,"props":177,"children":178},{},[179],{"type":24,"value":180},"最终方案",{"type":18,"tag":26,"props":182,"children":183},{},[184],{"type":24,"value":185},"推理主要使用的MarigoldPipeline，继承自DiffusionPipeline，对应的VAE、UNet、Scheduler在mindone的diffuers中都有定义，直接使用：",{"type":18,"tag":26,"props":187,"children":188},{},[189,193],{"type":18,"tag":39,"props":190,"children":192},{"alt":7,"src":191},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/2760166c0f11489abfa3665aa6a6043c.png",[],{"type":24,"value":194}," 输入直接使用VAE输出的均值，输出对三通道取平均：",{"type":18,"tag":26,"props":196,"children":197},{},[198],{"type":18,"tag":39,"props":199,"children":201},{"alt":7,"src":200},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/1003e3017e3f4ee29c66b305b8aca698.png",[],{"type":18,"tag":26,"props":203,"children":204},{},[205],{"type":24,"value":206},"推理多次然后集成获得最终结果，减轻随机性的影响：",{"type":18,"tag":26,"props":208,"children":209},{},[210],{"type":18,"tag":39,"props":211,"children":213},{"alt":7,"src":212},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/0369b521e76f41e68456818d1941138d.png",[],{"type":18,"tag":26,"props":215,"children":216},{},[217],{"type":24,"value":218},"修改MindSpore One原有diffusion训练代码，支持静态图模式训练：",{"type":18,"tag":26,"props":220,"children":221},{},[222],{"type":18,"tag":39,"props":223,"children":225},{"alt":7,"src":224},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/bc3d615beaf94c51a207796590422f08.png",[],{"type":18,"tag":26,"props":227,"children":228},{},[229],{"type":18,"tag":39,"props":230,"children":232},{"alt":7,"src":231},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/27/46338f0b7b8d4a309accbff27f845df1.png",[],{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":18,"tag":47,"props":237,"children":238},{},[239],{"type":24,"value":240},"项目总结",{"type":18,"tag":26,"props":242,"children":243},{},[244],{"type":24,"value":245},"本次项目我移植了Marigold，能完成这个项目首先要感谢老师的支持。从总体开发工作来说，确定思路的时间并不长，大部分时间在处理Bug，以及查阅文档看Torch API和MindSpore API的区别，整体写完之后再在逻辑上进行一些优化。",{"type":18,"tag":26,"props":247,"children":248},{},[249],{"type":24,"value":250},"最大的收获一方面是熟悉了MindSpore框架，另一方面是在调试代码上的，因为代码要放到卡上执行，过程复杂，从代码出错到实际观察到错误有时会相差很久，所以我写了很多调试脚本来减少调试时间，也锻炼了代码能力。熟悉MindSpore One仓库时见识了很多高质量、注释清晰的代码，以后我也会更高地要求自己。",{"type":18,"tag":26,"props":252,"children":253},{},[254],{"type":24,"value":255},"最后推荐一下“开源之夏”活动，参与优秀开源社区项目，提高能力，积累经验，一起为我国开源社区发展做贡献。",{"title":7,"searchDepth":257,"depth":257,"links":258},4,[],"markdown","content:technology-blogs:zh:3557.md","content","technology-blogs/zh/3557.md","technology-blogs/zh/3557","md",1776506130945]