迁移学习(Deep CORAL)《Deep CORAL: Correlation Alignment for Deep Domain Adaptation》【已复现迁移】

论文信息

论文标题:Deep CORAL: Correlation Alignment for Deep Domain Adaptation
论文作者:Baochen Sun, Kate Saenko
论文来源:ECCV 2016
论文地址:download 
论文代码:download
引用次数:2203

1 介绍

  解决的问题:深度神经网络可以在大规模的标注数据中学校到特征,但是输入数据分布不同的时候泛化不是很好。因此提出了域适应来弥补性能。本文针对目标域没有标注数据情况,对 $\text{CORAL}$ 进行了改进。 

  $\text{CORAL}$ 方法用线性变换方法将源域和目标域分布的二阶统计特征进行对齐,对于无监督域适应效果很好。问题出在依赖的是线性变换,而且不是端到端训练。训练分为两步,首先提取特征,应用变换,然后训练 $\text{SVM}$ 分类。

2 方法

  模型框架:

  

  设源域训练样本 $D_{s}=\left\{\mathrm{x}_{\mathrm{i}}\right\}, \mathrm{x} \in \mathbb{R}^{\mathrm{d}}$ ,标签 $L_{s}=y_{i}, i \in\{1, \cdots, L\}$。无标签的目标域数据  $D_{T}=\left\{\mathrm{u}_{\mathrm{i}}\right\}, \mathrm{u} \in \mathbb{R}^{\mathrm{d}}$  。其中,$d$  为网络  $\mathrm{fc} 8$  的输出维度。令  $D_{S}^{i j}$、$D_{T}^{i j}$  分别表示第  $i$  个源域、目标域样本的第  $j$  维特征。$C_{S}\left(C_{T}\right)$  表示特征协方差矩阵。

  $\text{CORAL loss}$ 是源域和目标域特征的 协方差距离:

    $\ell_{C O R A L}=\frac{1}{4 d^{2}}\left\|C_{S}-C_{T}\right\|_{F}^{2}  \quad\quad\quad(1)$

  其中,

    $C_{S}=\frac{1}{n_{S}-1}\left(D_{S}^{\top} D_{S}-\frac{1}{n_{S}}\left(\mathbf{1}^{\top} D_{S}\right)^{\top}\left(\mathbf{1}^{\top} D_{S}\right)\right) \quad\quad\quad(2)$

    $C_{T}=\frac{1}{n_{T}-1}\left(D_{T}^{\top} D_{T}-\frac{1}{n_{T}}\left(\mathbf{1}^{\top} D_{T}\right)^{\top}\left(\mathbf{1}^{\top} D_{T}\right)\right)\quad\quad\quad(3)$

  其中,$\mathbf{1} $ 代表全 $1$ 的列向量。

  上述公式的梯度表达式:

    $\frac{\partial \ell_{C O R A L}}{\partial D_{S}^{i j}}=\frac{1}{d^{2}\left(n_{S}-1\right)}\left(\left(D_{S}^{\top}-\frac{1}{n_{S}}\left(\mathbf{1}^{\top} D_{S}\right)^{\top} \mathbf{1}^{\top}\right)^{\top}\left(C_{S}-C_{T}\right)\right)^{i j}\quad\quad\quad(4)$

    $\frac{\partial \ell_{C O R A L}}{\partial D_{T}^{i j}}=-\frac{1}{d^{2}\left(n_{T}-1\right)}\left(\left(D_{T}^{\top}-\frac{1}{n_{T}}\left(\mathbf{1}^{\top} D_{T}\right)^{\top} \mathbf{1}^{\top}\right)^{\top}\left(C_{S}-C_{T}\right)\right)^{i j}\quad\quad\quad(5)$

  损失函数:

    $\ell=\ell_{C L A S S .}+\sum\limits _{i=1}^{t} \lambda_{i} \ell_{C O R A L}\quad\quad\quad(6)$

 

三个版本的 $\text{CORAL Loss}$

def CORAL(source, target):
    # source.shape = torch.Size([200, 31])
    # target.shape = torch.Size([56, 31])
    d = source.data.shape[1]  #31

    # source covariance  计算协方差矩阵
    xm = torch.mean(source, 0, keepdim=True) - source  #torch.Size([200, 31])
    xc = xm.t() @ xm  #torch.Size([31, 31])            #torch.Size([31, 31])

    # target covariance
    xmt = torch.mean(target, 0, keepdim=True) - target
    xct = xmt.t() @ xmt      #torch.Size([31, 31])

    # frobenius norm between source and target
    loss = torch.mean(torch.mul((xc - xct), (xc - xct)))
    loss = loss/(4*d*d)

    return loss
    def CORAL1(self, source, target):
        device = source.device
        d = source.size(1)
        ns, nt = source.size(0), target.size(0)

        # source covariance
        tmp_s = torch.ones((1, ns)).to(device) @ source
        cs = (source.t() @ source - (tmp_s.t() @ tmp_s) / ns) / (ns - 1)

        # target covariance
        tmp_t = torch.ones((1, nt)).to(device) @ target
        ct = (target.t() @ target - (tmp_t.t() @ tmp_t) / nt) / (nt - 1)

        # frobenius norm
        loss = (cs - ct).pow(2).sum().sqrt()
        loss = loss / (4 * d * d)
        return loss
    def CORAL2(self, x, y):
        # x.shape = torch.Size([32, 2048])
        # y.shape = torch.Size([32, 2048])
        mean_x = x.mean(0, keepdim=True)  # torch.Size([1, 2048])
        mean_y = y.mean(0, keepdim=True)  # torch.Size([1, 2048])
        cent_x = x - mean_x
        cent_y = y - mean_y
        cova_x = (cent_x.t() @ cent_x) / (len(x) - 1)
        cova_y = (cent_y.t() @ cent_y) / (len(y) - 1)

        mean_diff = (mean_x - mean_y).pow(2).mean()
        cova_diff = (cova_x - cova_y).pow(2).mean()

        return mean_diff + cova_diff

 

 

posted @ 2023-01-25 22:36  多发Paper哈  阅读(319)  评论(0编辑  收藏  举报
Live2D