【问题标题】:Trying to understand custom loss layer in caffe试图理解 caffe 中的自定义损失层
【发布时间】:2017-11-24 06:17:36
【问题描述】:

我已经看到可以像这样在 caffe 中定义一个自定义损失层,例如 EuclideanLoss:

import caffe
import numpy as np


    class EuclideanLossLayer(caffe.Layer):
        """
        Compute the Euclidean Loss in the same manner as the C++ 
EuclideanLossLayer
        to demonstrate the class interface for developing layers in Python.
        """

        def setup(self, bottom, top):
            # check input pair
            if len(bottom) != 2:
                raise Exception("Need two inputs to compute distance.")

        def reshape(self, bottom, top):
            # check input dimensions match
            if bottom[0].count != bottom[1].count:
                raise Exception("Inputs must have the same dimension.")
            # difference is shape of inputs
            self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
            # loss output is scalar
            top[0].reshape(1)

        def forward(self, bottom, top):
            self.diff[...] = bottom[0].data - bottom[1].data
            top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

        def backward(self, top, propagate_down, bottom):
            for i in range(2):
                if not propagate_down[i]:
                    continue
                if i == 0:
                    sign = 1
                else:
                    sign = -1
                bottom[i].diff[...] = sign * self.diff / bottom[i].num

但是,我对该代码有几个问题:

如果我想自定义这一层并改变这一行中损失的计算:

top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

让我们说:

channelsAxis = bottom[0].data.shape[1]
self.diff[...] = np.sum(bottom[0].data, axis=channelAxis) - np.sum(bottom[1].data, axis=channelAxis)
top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

如何更改后退功能?对于 EuclideanLoss 它是:

bottom[i].diff[...] = sign * self.diff / bottom[i].num

它必须如何查找我描述的损失?

标志是什么?

【问题讨论】:

  • 欧几里得损失有什么权重和偏差??
  • 对不起,我也有点糊涂了。我已经更新了问题! @Shai
  • top[0].data[...] = euclidean_weight * euclidean + other_weight * other 不是这样做的正确方法。您可以使用loss_weight: euclidean_weight 使用常规欧几里得损失层,使用loss_weight: other_weight 使用您自己的"OtherLoss" 层。
  • bottom[i].datashape (num, channel, height, width)np.arraybottom[i].data.shape[0] == bottom[i].num

标签: python neural-network deep-learning caffe pycaffe


【解决方案1】:

虽然将loss you are after 实现为"Python" 层可能是一项非常有教育意义的练习,但您可以使用现有 层获得相同的损失。您只需在调用常规 "EuclideanLoss" 层之前为每个 blob 添加一个 "Reduction" 层:

layer {
  type: "Reduction"
  name: "rx1"
  bottom: "x1"
  top: "rx1"
  reduction_param { axis: 1 operation: SUM }
} 
layer {
  type: "Reduction"
  name: "rx2"
  bottom: "x2"
  top: "rx2"
  reduction_param { axis: 1 operation: SUM }
} 
layer {
  type: "EuclideanLoss"
  name: "loss"
  bottom: "rx1"
  bottom: "rx2"
  top: "loss"
}

更新:
基于your comment,如果您只想对通道维度求和而保持所有其他维度不变,则可以使用固定的 1x1 conv(如您所建议的那样):

layer {
  type: "Convolution"
  name: "rx1"
  bottom: "x1"
  top: "rx1"
  param { lr_mult: 0 decay_mult: 0 } # make this layer *fixed*
  convolution_param {
    num_output: 1
    kernel_size: 1
    bias_term: 0  # no need for bias
    weight_filler: { type: "constant" value: 1 } # sum
  }
}

【讨论】:

  • 好的,那就完美了!现在我可以轻松添加 weight_loss 并获得两个 EuclideanLosses 对吗?
  • @thigi 完全正确。了解现有层可以让你变得非常懒惰;)
  • 我已经考虑过解决方法,但是错了!它对所有值求和,而不是对所有通道值求和。所以但我想创建这样的总和:y = channel1 + channel2 + channel3 ... channelN。所以通道的总和,而不是所有轴。你能帮我解决这个问题吗?我认为可以使用带有num_output = 1weight_filler = constant, value = 1 的卷积层,这样对吗?你能更新你的答案吗?
  • 好的,谢谢!我不明白的是,如果输入可以说有 16 个通道,那么求和是如何工作的?我以为 caffe 在卷积层中分别处理每个通道?您能否通过简短的解释快速扩展您的答案?那只是为了更好地理解:) @Shai
猜你喜欢
  • 2015-09-14
  • 2017-03-14
  • 2017-06-20
  • 2019-12-01
  • 2017-11-29
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
相关资源
最近更新 更多