[{"data":1,"prerenderedAt":435},["ShallowReactive",2],{"content-query-gv0OpwMGdJ":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"body":13,"_type":429,"_id":430,"_source":431,"_file":432,"_stem":433,"_extension":434},"/technology-blogs/zh/3870","zh",false,"","使用昇思MindSpore实现广告Banner图片生成：从理论到实践","通过结合深度学习和MindSpore框架的强大能力，我们为自动化广告设计开辟了新的可能性。","2025-10-21","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/da3f5126a3fe4734b7c8cbbe624b01d6.png","technology-blogs",{"type":14,"children":15,"toc":426},"root",[16,24,30,35,52,57,62,67,72,87,91,101,106,111,119,127,145,150,158,173,178,186,191,198,213,218,226,230,237,245,252,262,272,282,292,307,315,319,326,341,349,354,359,364,372,377,382,387,392,397,402,407,412],{"type":17,"tag":18,"props":19,"children":21},"element","h1",{"id":20},"使用昇思mindspore实现广告banner图片生成从理论到实践",[22],{"type":23,"value":8},"text",{"type":17,"tag":25,"props":26,"children":27},"p",{},[28],{"type":23,"value":29},"作者：qianduanjidi",{"type":17,"tag":25,"props":31,"children":32},{},[33],{"type":23,"value":34},"来源：昇思论坛",{"type":17,"tag":18,"props":36,"children":38},{"id":37},"_01-案例介绍",[39,45,47],{"type":17,"tag":40,"props":41,"children":42},"strong",{},[43],{"type":23,"value":44},"# 01",{"type":23,"value":46}," ",{"type":17,"tag":40,"props":48,"children":49},{},[50],{"type":23,"value":51},"案例介绍",{"type":17,"tag":25,"props":53,"children":54},{},[55],{"type":23,"value":56},"本文将详细介绍如何使用昇思MindSpore框架构建生成对抗网络(GAN)来创造独特的广告Banner图片，为数字营销提供AI助力。",{"type":17,"tag":25,"props":58,"children":59},{},[60],{"type":23,"value":61},"在数字营销时代，吸引眼球的广告Banner对于品牌推广至关重要。然而，手动设计大量高质量的Banner既耗时又费力。本文将展示如何利用昇思MindSpore框架和生成对抗网络(GAN)技术，自动生成具有视觉吸引力的广告Banner图片。",{"type":17,"tag":25,"props":63,"children":64},{},[65],{"type":23,"value":66},"生成对抗网络(GAN)简介：GAN由两个神经网络组成：生成器(Generator)和判别器(Discriminator)。生成器负责创建假数据，而判别器则尝试区分真实数据和生成数据。通过这种对抗训练过程，生成器逐渐学会生成越来越逼真的数据。",{"type":17,"tag":25,"props":68,"children":69},{},[70],{"type":23,"value":71},"昇思MindSpore框架：MindSpore是华为开源的深度学习框架，以其高效的计算性能和简洁的API设计而闻名。它特别适合构建和训练复杂的神经网络模型。",{"type":17,"tag":18,"props":73,"children":75},{"id":74},"_02-环境设置与数据准备",[76,81,82],{"type":17,"tag":40,"props":77,"children":78},{},[79],{"type":23,"value":80},"# 02",{"type":23,"value":46},{"type":17,"tag":40,"props":83,"children":84},{},[85],{"type":23,"value":86},"环境设置与数据准备",{"type":17,"tag":25,"props":88,"children":89},{},[90],{"type":23,"value":86},{"type":17,"tag":92,"props":93,"children":95},"pre",{"code":94},"import mindspore as ms\n",[96],{"type":17,"tag":97,"props":98,"children":99},"code",{"__ignoreMap":7},[100],{"type":23,"value":94},{"type":17,"tag":25,"props":102,"children":103},{},[104],{"type":23,"value":105},"数据预处理是训练成功的关键。我们创建了一个自定义数据集类来处理本地图片：",{"type":17,"tag":25,"props":107,"children":108},{},[109],{"type":23,"value":110},"本地图片：",{"type":17,"tag":25,"props":112,"children":113},{},[114],{"type":17,"tag":115,"props":116,"children":118},"img",{"alt":7,"src":117},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/5bdf8c9459f248e1a9941de57ce29188.png",[],{"type":17,"tag":92,"props":120,"children":122},{"code":121},"# 数据路径\n",[123],{"type":17,"tag":97,"props":124,"children":125},{"__ignoreMap":7},[126],{"type":23,"value":121},{"type":17,"tag":18,"props":128,"children":130},{"id":129},"_03-构建生成器网络",[131,136,137],{"type":17,"tag":40,"props":132,"children":133},{},[134],{"type":23,"value":135},"# 03",{"type":23,"value":46},{"type":17,"tag":40,"props":138,"children":139},{},[140],{"type":17,"tag":40,"props":141,"children":142},{},[143],{"type":23,"value":144},"构建生成器网络",{"type":17,"tag":25,"props":146,"children":147},{},[148],{"type":23,"value":149},"生成器负责从随机噪声中创建图片。我们使用转置卷积层来逐步上采样：",{"type":17,"tag":92,"props":151,"children":153},{"code":152},"# 构建生成器\n",[154],{"type":17,"tag":97,"props":155,"children":156},{"__ignoreMap":7},[157],{"type":23,"value":152},{"type":17,"tag":18,"props":159,"children":161},{"id":160},"_04-构建判别器网络",[162,167,168],{"type":17,"tag":40,"props":163,"children":164},{},[165],{"type":23,"value":166},"# 04",{"type":23,"value":46},{"type":17,"tag":40,"props":169,"children":170},{},[171],{"type":23,"value":172},"构建判别器网络",{"type":17,"tag":25,"props":174,"children":175},{},[176],{"type":23,"value":177},"判别器评估输入图片的真实性：",{"type":17,"tag":92,"props":179,"children":181},{"code":180},"# 构建判别器\n",[182],{"type":17,"tag":97,"props":183,"children":184},{"__ignoreMap":7},[185],{"type":23,"value":180},{"type":17,"tag":25,"props":187,"children":188},{},[189],{"type":23,"value":190},"运行结果：",{"type":17,"tag":25,"props":192,"children":193},{},[194],{"type":17,"tag":115,"props":195,"children":197},{"alt":7,"src":196},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/a79d31d94c074e259b37b120a810487b.png",[],{"type":17,"tag":18,"props":199,"children":201},{"id":200},"_05-训练策略",[202,207,208],{"type":17,"tag":40,"props":203,"children":204},{},[205],{"type":23,"value":206},"# 05",{"type":23,"value":46},{"type":17,"tag":40,"props":209,"children":210},{},[211],{"type":23,"value":212},"训练策略",{"type":17,"tag":25,"props":214,"children":215},{},[216],{"type":23,"value":217},"我们使用自定义的训练步骤类来管理GAN的训练过程（此处仅展示部分代码）：",{"type":17,"tag":92,"props":219,"children":221},{"code":220},"# 使用MindSpore的训练步骤类\n",[222],{"type":17,"tag":97,"props":223,"children":224},{"__ignoreMap":7},[225],{"type":23,"value":220},{"type":17,"tag":25,"props":227,"children":228},{},[229],{"type":23,"value":190},{"type":17,"tag":25,"props":231,"children":232},{},[233],{"type":17,"tag":115,"props":234,"children":236},{"alt":7,"src":235},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/cb608ae4dac84cc7ba4a190a22b8690c.png",[],{"type":17,"tag":92,"props":238,"children":240},{"code":239},"生成图片：\n",[241],{"type":17,"tag":97,"props":242,"children":243},{"__ignoreMap":7},[244],{"type":23,"value":239},{"type":17,"tag":25,"props":246,"children":247},{},[248],{"type":17,"tag":115,"props":249,"children":251},{"alt":7,"src":250},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/a2fb023a001f4cb5a336035627748f21.png",[],{"type":17,"tag":25,"props":253,"children":254},{},[255,260],{"type":17,"tag":40,"props":256,"children":257},{},[258],{"type":23,"value":259},"训练过程与结果：",{"type":23,"value":261}," 在30个训练周期后，我们的GAN模型能够生成具有相当质量的Banner图片。以下是训练过程中的关键观察：",{"type":17,"tag":25,"props":263,"children":264},{},[265,270],{"type":17,"tag":40,"props":266,"children":267},{},[268],{"type":23,"value":269},"损失函数变化：",{"type":23,"value":271}," 判别器和生成器的损失逐渐收敛，表明模型正在学习",{"type":17,"tag":25,"props":273,"children":274},{},[275,280],{"type":17,"tag":40,"props":276,"children":277},{},[278],{"type":23,"value":279},"图片质量提升：",{"type":23,"value":281}," 随着训练进行，生成的图片从噪声逐渐变得清晰可辨",{"type":17,"tag":25,"props":283,"children":284},{},[285,290],{"type":17,"tag":40,"props":286,"children":287},{},[288],{"type":23,"value":289},"多样性保持：",{"type":23,"value":291}," 模型能够生成多种不同风格的Banner图片",{"type":17,"tag":18,"props":293,"children":295},{"id":294},"_06-生成最终的banner图片",[296,301,302],{"type":17,"tag":40,"props":297,"children":298},{},[299],{"type":23,"value":300},"# 06",{"type":23,"value":46},{"type":17,"tag":40,"props":303,"children":304},{},[305],{"type":23,"value":306},"生成最终的Banner图片",{"type":17,"tag":92,"props":308,"children":310},{"code":309},"# 生成最终的Banner图片\n",[311],{"type":17,"tag":97,"props":312,"children":313},{"__ignoreMap":7},[314],{"type":23,"value":309},{"type":17,"tag":25,"props":316,"children":317},{},[318],{"type":23,"value":190},{"type":17,"tag":25,"props":320,"children":321},{},[322],{"type":17,"tag":115,"props":323,"children":325},{"alt":7,"src":324},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/10/25/f778633fb3934c7c82513d27f33bd971.png",[],{"type":17,"tag":18,"props":327,"children":329},{"id":328},"_07-总结与展望",[330,335,336],{"type":17,"tag":40,"props":331,"children":332},{},[333],{"type":23,"value":334},"# 07",{"type":23,"value":46},{"type":17,"tag":40,"props":337,"children":338},{},[339],{"type":23,"value":340},"总结与展望",{"type":17,"tag":25,"props":342,"children":343},{},[344],{"type":17,"tag":40,"props":345,"children":346},{},[347],{"type":23,"value":348},"MindSpore的优势：",{"type":17,"tag":25,"props":350,"children":351},{},[352],{"type":23,"value":353},"动态图模式：使用PYNATIVE_MODE便于调试和开发",{"type":17,"tag":25,"props":355,"children":356},{},[357],{"type":23,"value":358},"高效的梯度计算：利用GradOperation简化反向传播",{"type":17,"tag":25,"props":360,"children":361},{},[362],{"type":23,"value":363},"内存优化：自动内存管理提高训练效率",{"type":17,"tag":25,"props":365,"children":366},{},[367],{"type":17,"tag":40,"props":368,"children":369},{},[370],{"type":23,"value":371},"GAN训练技巧：",{"type":17,"tag":25,"props":373,"children":374},{},[375],{"type":23,"value":376},"标签平滑：使用0.9和0.1而不是1和0作为真实/假标签",{"type":17,"tag":25,"props":378,"children":379},{},[380],{"type":23,"value":381},"梯度惩罚：避免模式崩溃问题",{"type":17,"tag":25,"props":383,"children":384},{},[385],{"type":23,"value":386},"学习率调度：自适应调整学习率提高训练稳定性",{"type":17,"tag":25,"props":388,"children":389},{},[390],{"type":23,"value":391},"本文展示了如何使用昇思MindSpore框架实现一个功能完整的GAN模型来生成广告Banner图片。虽然当前模型在64×64分辨率下工作良好，但未来可以进一步改进：",{"type":17,"tag":25,"props":393,"children":394},{},[395],{"type":23,"value":396},"提高分辨率：使用渐进式GAN生成更高分辨率的图片",{"type":17,"tag":25,"props":398,"children":399},{},[400],{"type":23,"value":401},"条件生成：根据文本描述生成特定风格的Banner",{"type":17,"tag":25,"props":403,"children":404},{},[405],{"type":23,"value":406},"风格控制：允许用户控制生成图片的颜色、布局等属性",{"type":17,"tag":25,"props":408,"children":409},{},[410],{"type":23,"value":411},"通过结合深度学习和MindSpore框架的强大能力，我们为自动化广告设计开辟了新的可能性。这种技术不仅能够节省设计时间，还能通过数据驱动的方法优化营销效果。",{"type":17,"tag":25,"props":413,"children":414},{},[415,417],{"type":23,"value":416},"代码仓地址：",{"type":17,"tag":418,"props":419,"children":423},"a",{"href":420,"rel":421},"https://xihe.mindspore.cn/models/qianduanjidi/MindSpore%5C_Demo/tree",[422],"nofollow",[424],{"type":23,"value":425},"https://xihe.mindspore.cn/models/qianduanjidi/MindSpore\\_Demo/tree",{"title":7,"searchDepth":427,"depth":427,"links":428},4,[],"markdown","content:technology-blogs:zh:3870.md","content","technology-blogs/zh/3870.md","technology-blogs/zh/3870","md",1776506136415]