深度卷积对抗神经网络 进阶 第三部分 GANs Unpaired Translation with Cycle GAN 模型

非配对的图像转换应用 Unpaired Image-to-Image Translation

Unpaired image-to-image translation 主要用于学习两组图像之间的对应关系,检查和寻找两堆数据中的共同内容(content)以及每堆独有的特点(style)。而这个模型是非监督的,也就意味着不需要进行数据的标注便可以进行训练。

CycleGAN 循环GANs模型

循环一致性 Cycle Consistency

循环一致性指的是当图片经过生成模型后再次经过生成模型的反向模型而得到的循环后的模型,其图片与原图片的一致性问题。

当然,如果循环后图片与原图片一模一样当然是最好的,这证明了了模型的稳定性。而在循环模型中,我们通过这种方式来告诉两个模型哪些是特征,哪些不是特征。这样,我们不用通过对数据进行标注,而是原图像与循环后的图像互为配对,以此来互相纠正模型的错误,并使其能够达到我们想要的效果。

模型结构 - Two GANs

Cycle会存在两个GANs,分别代表两个方向。比如说我们要把正常马的身上涂上白色斑纹变为斑马。那么在Cycle GANs中,模型会由两个生成器(正常马生成斑马,斑马生成正常马)和两个分辨器(分辨生成的斑马与实际斑马的分辨器,以及分辨生成的马与实际马的分辨器)所组成。

因此Cycle GAN使用两个GAN来进行图片到图片的转化 CycleGAN uses two GANs for unpaired image-to-image translation。并且其分辨器是经典的PatchGAN's,也就是分辨结果是一个分类矩阵而不是一个简单的值。其生成器十分类似于U-Net和DCGAN,但是在其中加入了SKIP CONNECTIONs, 也就是增加了层之间的信息交互。

其中和U-Net最大的区别就是中间的Residual Block (残留区块),这种区块可以相当于一种skip function,它不是作为新的通道被串联起来,而是直接添加到卷积的输出。在下面的可视化中,你可以想象条纹是由卷积产生的,然后添加到马的原始图像中,将其转化为斑马。这些跳过连接也允许网络更加深入,因为它们有助于解决梯度消失的问题,当神经网络变得太深,梯度在反向传播中倍增变得非常小的时候,就会出现梯度消失的问题;相反,这些跳过连接能够实现更多的梯度流动。一个更深的网络通常能够学习更复杂的特征。Residual Block的解释还有待进一步研究(*)。

正向的从斑马到正常的马

反向的从正常的马到斑马

总之,由于是无监督的学习,数据并没有配对,因此并没有明确的目标输出。而且不同的分辨器分辨的是不同类型的数据。

损失函数

循环一致性损失 Cycle Consistency Loss

正向的从斑马到马再到斑马的循环一致性

经过连个生成器保持一致是非常重要的性质,其可以让模型去分析两组图片中的唯一特性(有条纹和没有条纹)和共同特性(马的形状),并将其他模型所误判断的特性进行逐渐的纠正。

反向的从马到斑马再到马的循环一致性

另一种理解便是循环一致性Cycle consistency可以将不一致的特征进行传递,而保持一致的内容。而且这种损失可以缓慢地矫正生成器生成结果的趋势,且两个方向的一致性都需要保证。

具体的实例代码如下。

def get_identity_loss(real_X, gen_YX, identity_criterion):
    '''
    Return the identity loss of the generator given inputs
    (and the generated images for testing purposes).
    Parameters:
        real_X: the real images from pile X
        gen_YX: the generator for class Y to X; takes images and returns the images 
            transformed to class X
        identity_criterion: the identity loss function; takes the real images from X and
                        those images put through a Y->X generator and returns the identity 
                        loss (which you aim to minimize)
    '''
    identity_X = gen_YX(real_X)
    identity_loss = identity_criterion(identity_X, real_X)
    return identity_loss, identity_X
 

最小均方误差 Least Squares Loss

最小均方误差的损失函数在某些情况下可以解决vanishing gradients 和 mode collapse的问题,其定义如下所示。

其定义的原理主要是预测值与真实值的差距,类似于BCEloss, 但是其的平方特性使得其比BCELoss更加的稳定。

不变性损失 Identity Loss

加入该损失可以非常有效地避免生成器改变图片颜色的问题,其主要利用像素距离来告诉生成器进行一定的纠正。

具体的操作便是将图片经过反向的生成器,将生成的结果与原结果比较像素距离。如果相同则说明其能够分辨出这个结果不需要进行任何变化,而不相同则说明生成器还有待进步。

而加入不变性损失会非常高效的保留原相片的颜色(Color preservation),如下图所示。

具体的示例代码如下。

def get_identity_loss(real_X, gen_YX, identity_criterion):
    '''
    Return the identity loss of the generator given inputs
    (and the generated images for testing purposes).
    Parameters:
        real_X: the real images from pile X
        gen_YX: the generator for class Y to X; takes images and returns the images 
            transformed to class X
        identity_criterion: the identity loss function; takes the real images from X and
                        those images put through a Y->X generator and returns the identity 
                        loss (which you aim to minimize)
    '''
    identity_X = gen_YX(real_X)
    identity_loss = identity_criterion(identity_X, real_X)
    return identity_loss, identity_X

模型总结

正向结构的模样

反向结构的模样

损失函数的结构

参考文献 Reference

(Optional) The CycleGAN Paper

Compelled to learn more about CycleGAN? Take a look at the original paper!

Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks (Zhu, Park, Isola, and Efros, 2020): https://arxiv.org/abs/1703.10593

(Optional) CycleGAN for Medical Imaging

Intrigued by the application of CycleGANs in the medical field? See how they can be used to help augment data for medical imaging!

Data augmentation using generative adversarial networks (CycleGAN) to improve generalizability in CT segmentation tasks (Sandfort, Yan, Pickhardt, and Summers, 2019):

(Optional Notebook) MUNIT

https://colab.research.google.com/github/https-deeplearning-ai/GANs-Public/blob/master/C3W3_MUNIT_(Optional).ipynb

Please note that this is an optional notebook, meant to introduce more advanced concepts if you're up for a challenge, so don't worry if you don't completely follow!

In this notebook, you will learn about and implement MUNIT, a method for unsupervised image-to-image translation, as proposed in Multimodal Unsupervised Image-to-Image Translation (Huang et al. 2018).

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Volavion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值