首页 > 其他 > 详细

Scale-Invariant Error

时间:2021-02-06 13:32:42      阅读:35      评论:0      收藏:0      [点我收藏+]

Eigen D., Puhrsch C. and Fergus R. Depth Map Prediction from a Single Image using a Multi-Scale Deep Network. NIPS 2014.

看这篇文章单纯是为了看一看这个scale-invariant error.

主要内容

我们时常通过平方误差来衡量两个图片的差异, 但是这个损失是很依赖与scale的.
比如, 有两个图片\(\bm{x}, \bm{x}‘\), 则其误差为

\[\|\bm{x} - \bm{x}‘\|_2^2 = \sum_{i=1}^n (\bm{x}_i - \bm{x}_i‘)^2, \]

倘若此时\(x\)的每一个元素都增加了\(c\), 则变成了

\[\|\bm{x} + c - \bm{x}‘\|_2^2, \]

这个实际不是非常友好的, 我们是希望这个损失最好是Scale-Invariant的, 所以我们在损失的部分加入一个值

\[\| \bm{x} - \bm{x}‘ + \alpha \|_2^2, \]

注意, 这里的\(\bm{x}\)可以理解为\(\bm{x} + c\), 那么选择一个怎样的\(\alpha\)能够使得上述的误差最小呢(关于特定的\(\bm{x}, \bm{x}‘\)).

\[2(\bm{x} - \bm{x}‘ + \alpha)^T \bm{1} = 0 \Rightarrow \alpha = \frac{1}{n} (\bm{x}‘- \bm{x})^T \bm{1} = \frac{1}{n}\sum_{i=1}^n (x_i‘ - x_i). \]

故, 最后的损失函数是

\[\| \bm{x} - \bm{x}‘ + \frac{1}{n}(\bm{x} - \bm{x}‘)^T \bm{1}\|_2^2 = \|\bm{x} - \bm{x}‘\|_2^2 - \frac{1}{n} ((\bm{x} - \bm{x}‘)^T \bm{1})^2. \]

注: 如果我们将像素置于对数空间, 即考虑\(\log \bm{x}\), 则上述实际上考虑的\(c \cdot \bm{x}\) 的scale.

代码

import torch
import torch.nn as nn
import torch.nn.functional as F

def scale_invariant_loss(outs: torch.Tensor, targets: torch.Tensor, reduction="mean"):
    """
    outs: N ( x C) x H x W
    targets: N ( x C) x H x W
    reduction: ...
    """
    outs = outs.flatten(start_dim=1)
    targets = targets.flatten(start_dim=1)
    alpha = (targets - outs).mean(dim=1, keepdim=True)
    return F.mse_loss(outs + alpha, targets, reduction=reduction)

Scale-Invariant Error

原文:https://www.cnblogs.com/MTandHJ/p/14381010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!