machine-learning - 使用caffe从图像回归年龄时如何设计损失层

标签 machine-learning neural-network deep-learning regression caffe

我正在尝试用 caffe 重现以下论文

Deep EXpectation

最后一层有 100 个输出,每一层都隐含着预测年龄的概率。最终预测年龄通过以下公式计算:

Equation

所以我想使用带有标签和预测值的 EUCLIDEAN_LOSS 来产生损失。

我展示了最后一个输出层和损失层的原型(prototype)文本。

layer {
    bottom: "pool5"
    top: "fc100"
    name: "fc100"
    type: "InnerProduct"
    inner_product_param {
        num_output: 100
    }
}

layer {
    bottom: "fc100"
    top: "prob"
    name: "prob"
    type: "Softmax"
}

layer {
    name: "loss"
    type: "SoftmaxWithLoss"
    bottom: "fc100"
    bottom: "label"
    top: "loss"
    loss_weight: 1
}

现在,我正在使用 SoftmaxWithLoss 尝试这些。然而,这种损失更适合分类而不是回归。在这种情况下我该如何设计损失层?

提前致谢。

最佳答案

TL;DR
我曾经完成过类似的任务,根据我的经验,训练离散标签和回归单个连续值之间几乎没有区别(就输出准确性而言)。

<小时/>

有多种方法可以解决此问题:

1。回归单个输出

由于您只需要预测单个标量值,因此您应该训练您的网络来这样做:

layer {
    bottom: "pool5"
    top: "fc1"
    name: "fc1"
    type: "InnerProduct"
    inner_product_param {
        num_output: 1  # predict single output
    }
}

您需要确保预测值在 [0..99] 范围内:

layer {
  bottom: "fc1"
  top: "pred01"  # map to [0..1] range
  type: "Sigmoid"
  name: "pred01"
}
layer {
  bottom: "pred01"
  top: "pred_age"
  type: "Scale"
  name: "pred_age"
  param { lr_mult: 0 }  # do not learn this scale - it is fixed
  scale_param {
    bias_term: false
    filler { type: "constant" value: 99 }
  }
}

一旦你在 pred_age 中得到了预测你可以添加一个损失层

layer {
  bottom: "pred_age"
  bottom: "true_age"
  top: "loss"
  type: "EuclideanLoss"
  name: "loss"
}

不过,我建议使用 "SmoothL1" 在这种情况下,因为它更稳健。

2。回归离散预测的期望

您可以在 caffe 中实现您的预​​测公式。为此,您需要一个固定值向量[0..99]。有很多方法可以做到这一点,但没有一种方法是非常直接的。这是使用 net-surgery 的一种方法:

首先,定义网络

layer {
  bottom: "prob"
  top: "pred_age"
  name: "pred_age"
  type: "Convolution"
  param { lr_mult: 0 }  # fixed layer. 
  convolution_param { 
    num_output: 1
    bias_term: false
  }
}
layer {
  bottom: "pred_age"
  bottom: "true_age"
  top: "loss"
  type: "EuclideanLoss"  # same comment about type of loss as before
  name: "loss"
}

您还不能使用该网络,首先需要设置pred_age的内核层到 0..99。

在 python 中,加载新的

net = caffe.Net('path/to/train_val.prototxt', caffe.TRAIN)
li = list(net._layer_names).index('pred_age')  # get layer index
net.layers[li].blobs[0].data[...] = np.arange(100, dtype=np.float32)  # set the kernel
net.save('/path/to/init_weights.caffemodel')  # save the weights

现在您可以训练您的网络,但请确保您将从 '/path/to/init_weights.caffemodel' 中保存的权重开始训练。 .

关于machine-learning - 使用caffe从图像回归年龄时如何设计损失层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419058/

相关文章:

neural-network - 假设顺序为Conv2d->ReLU->BN,Conv2d层是否应该有偏置参数?

python-3.x - “KerasClassifier”对象没有属性 'loss'

python - 如何让我的 Spyder 代码在 GPU 而不是 Ubuntu 上的 cpu 上运行?

python - 如何从Python中的URL中提取特征?

r - 在 r 中使用 SVM 对 pincode 类型进行分类

statistics - 使用高斯族分布来预测 GLM 中的离散量

machine-learning - 超出样本定义

machine-learning - Tensorflow 多 GPU MNIST 分类器 : low accuracy

neural-network - 预测数字序列中的模式

math - 平移等方差及其与卷积层和空间池化层的关系