DeepLearningAI 深度学习 2025 笔记(五)
2026/7/18 3:12:07 网站建设 项目流程

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_24.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_26.png

这个生成器会输出一张假的斑马图像。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_26.png

循环一致性要求,这张重新生成的假斑马图像应该与最初输入的真实斑马图像在内容上保持一致。因此,我们计算两者之间的像素级差异(如 L1 损失)。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_28.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_28.png

用公式表示,从斑马(Z)到马(H)再回到斑马的循环一致性损失为:

L_cyc_Z = || G_H2Z(G_Z2H(zebra)) - zebra ||_1

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_30.png

同理,反向过程(从马到斑马再回到马)也会计算一个对称的循环一致性损失。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_30.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_32.png

L_cyc_H = || G_Z2H(G_H2Z(horse)) - horse ||_1

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_32.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_34.png

身份损失(可选)

为了进一步增强颜色和纹理的稳定性,CycleGAN 可以引入可选的身份损失

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_36.png

其思想是:如果一个斑马图像已经属于目标域(斑马域),那么通过从马到斑马的生成器G_H2Z后,它应该基本保持不变。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_36.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_38.png

因此,我们将真实的斑马图像也输入G_H2Z,得到输出,并计算输出与输入之间的像素差异作为身份损失。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_38.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_40.png

用公式表示,斑马的身份损失为:

L_id_Z = || G_H2Z(zebra) - zebra ||_1

同样,对马图像和生成器G_Z2H也进行相同的操作。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_42.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_40.png

L_id_H = || G_Z2H(horse) - horse ||_1

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_44.png

损失函数总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_46.png

现在,让我们将所有损失项汇总起来。CycleGAN 的巧妙之处在于,通过简单叠加这些直观的损失项,就能实现强大的无监督图像转换。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_44.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_48.png

以下是生成器的总损失构成,共有六项:

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_46.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_50.png

  1. 对抗损失(两项):每个生成器对应一项,用于欺骗其对应的判别器。

    • G_Z2H的对抗损失:L_adv_Z2H

    • G_H2Z的对抗损失:L_adv_H2Z

  2. 循环一致性损失(两项):确保转换的可逆性。

    • 前向循环损失:L_cyc_Z

    • 后向循环损失:L_cyc_H

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_52.png

  1. 身份损失(两项,可选):稳定生成器的颜色和纹理。

    • G_H2Z的身份损失:L_id_Z

    • G_Z2H的身份损失:L_id_H

生成器的总损失是这些项的加权和:

L_G_total = L_adv_Z2H + L_adv_H2Z + λ_cyc * (L_cyc_Z + L_cyc_H) + λ_id * (L_id_Z + L_id_H)

其中λ_cycλ_id是超参数,用于平衡各项损失的重要性。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_54.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_50.png

这个总损失函数同时用于优化两个生成器。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_56.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_52.png

对于判别器,其任务则相对简单,每个判别器只负责一项最小二乘对抗损失:

  • 马的判别器D_H的损失:L_D_H

  • 斑马的判别器D_Z的损失:L_D_Z

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_58.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_54.png

总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_60.png

本节课中我们一起学习了 CycleGAN 的整体架构和损失函数。总结来说,CycleGAN 由两个GAN组成一个循环,它们相互依赖,通过计算多种损失项来学习无配对的域间映射。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_58.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_62.png

生成器的损失包含六项:核心的最小二乘对抗损失、保证内容不变的循环一致性损失以及可选的身份损失

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_64.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_60.png

判别器的目标则相对单一,仅使用最小二乘对抗损失来提升自身的判别能力。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_66.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/04f1c5f0d146fcb22fbce9aea7af5827_64.png

理解这些损失项如何共同作用,是掌握 CycleGAN 工作原理的关键。

83:CycleGAN应用及变种 🎨➡️🦓

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_0.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_2.png

在本节课中,我们将学习CycleGAN的实际应用场景以及其重要的变体模型。我们将了解CycleGAN如何被用于艺术创作、医疗数据增强等领域,并探索UNIT和MUNIT这两种基于共享潜在空间假设的先进无监督图像翻译模型。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_4.png


应用领域

上一节我们介绍了CycleGAN的基本原理,本节中我们来看看它在现实世界中的具体应用。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_6.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_8.png

CycleGAN可以用于许多不同的事情。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_10.png

以下是其主要应用方向:

  • 社交媒体滤镜:例如Snapchat上的各种滤镜,使人变老或改变性别感知。

  • 物体转换:例如将马变成斑马,并且能可靠地保持转换后物体(如斑马条纹)的特征。

  • 场景风格迁移:改变场景的季节,或将照片转换为特定的绘画风格(如莫奈风格),反之亦然。

  • 数据增强:在医疗等领域,CycleGAN是一种常用的数据增强技术。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_12.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_14.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_16.png

医疗数据增强示例

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_18.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_20.png

现在,让我们深入了解CycleGAN在医疗数据增强中的具体作用。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_22.png

找到同时包含肿瘤和未包含肿瘤的配对医学图像非常困难。这种预知能力目前可能无法实现,因此CycleGAN提供了一种数据增强的方法。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_24.png

以下是CycleGAN在医疗图像处理中的两种应用:

  • 创造肿瘤:可以在健康图像上生成肿瘤,以模拟患病情况。

  • 消除肿瘤:可以从患病图像中移除肿瘤,以模拟健康情况。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_26.png

这些生成的配对示例可用于下游任务,例如肿瘤分类、分割以及监测肿瘤生长,这在医疗应用中非常重要。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_28.png

例如,使用CycleGAN可以移除CT扫描图像中的肿瘤大块,或者向图像中添加逼真的肿瘤。在数据增强中,像SegGAN这样的模型能够生成极其逼真的分割示例,其效果是标准数据增强方法无法达到的。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_30.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_32.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_34.png

无监督图像翻译变体:UNIT

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_36.png

CycleGAN并不是唯一能够进行无配对图像翻译的模型。接下来,我们介绍一种重要的变体——UNIT。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_38.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_40.png

有一种变体称为UNIT,它代表无监督图像到图像转换。无配对图像转换本身就是无监督的,因为你没有成对的图像标签。例如,实现白天与夜间驾驶场景的转换。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_42.png

这个模型的关键洞察被称为共享潜在空间。其核心思想是,两个不同域的图像可以映射到同一个潜在空间中的向量。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_44.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_46.png

我们可以用以下公式化描述:

  • 给定潜在空间中的一个噪声向量z,生成器G1可以在域X1中生成图像:X1 = G1(z)

  • 同时,存在一个编码器E1可以将X1域的图像映射回潜在向量:z' = E1(X1)

  • 同样的潜在向量z,也可以通过另一个生成器G2在域X2中生成图像:X2 = G2(z)

  • 并且,域X2的图像也能通过编码器E2映射回潜在空间。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_48.png

你可以想象这两个域,X1是白天的驾驶场景,X2是夜间的驾驶场景。X1X2共享相同的内容(即潜空间向量z),但表现出不同的风格(白天或夜间)。模型可以学习在这两个风格之间来回映射。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_50.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_52.png

多模态生成:MUNIT

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_54.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_56.png

UNIT模型的能力不仅限于单一风格的转换。它的一个扩展版本,称为多模态UNIT(MUNIT),能够实现更丰富的生成。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_58.png

多模态的意思是从一个域的输入(如草图),可以生成第二个域的多种不同风格输出。MUNIT不仅能找到一个映射,还能发现并生成目标域的多种潜在模式。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_60.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_62.png

这非常酷,因为你从未明确告诉模型关于所有不同的风格。例如,给定一个鞋子的草图,它能够自动找出所有不同的鞋类风格(如靴子、运动鞋、高跟鞋),并生成对应的多种图像。这一切都是在无监督的情况下完成的,模型自行发现了鞋子之间的风格差异。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_64.png


https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_66.png

技术背景与总结

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_68.png

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_70.png

最后,我们来总结一下本节课的核心内容。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_72.png

需要快速说明的是,UNIT和MUNIT这两种技术都依赖于将图像编码到潜在空间(从变分自编码器VAE中获得灵感),同时使用GAN的组件来确保生成图像的真实性。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_74.png

总结来说,CycleGAN的扩展模型包括UNIT和MUNIT,它们使用了共享潜在空间的假设。特别是MUNIT,实现了多模态生成,这非常强大。GAN,尤其是其变体,在艺术、医疗、视频和游戏等领域有大量应用。

https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/dlai-dl-25/img/10bc1ab3f7fe1740078b6fcd98a231cd_76.png

本节课中,我们一起学习了CycleGAN的广泛应用、其在医疗数据增强中的价值,以及UNIT和MUNIT这两种基于共享潜在空间的无监督图像翻译模型。它们为图像生成和转换任务提供了更强大、更灵活的解决方案。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询