caffe:全连接层转换为 3D 数据

caffe: fully connected layer transform to 3D data

我有一个带有 SoftmaxWithLoss 层的 fully connected layer。我想要做的是以 3D 而不是 1D 的形式检索数据。 我的输入 ground_truth 图像是 3x128x128,我的最后一层看起来像这样:

layer {
  name: "fc1"
  type: "InnerProduct"
  bottom: "conv"
  top: "fc1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 49152 # 128 x 128 x 3
     ...
  }
}

layer {
  name: "result"
  type: "SoftmaxWithLoss"
  bottom: "fc1"
  bottom: "label"
  top: "result"
}

这里我得到以下错误:

softmax_loss_layer.cpp:47] Check failed: outer_num_ * inner_num_ == bottom[1]->count() (1 vs. 49152) Number of labels must match number of predictions; e.g., if softmax axis == 1 and prediction shape is (N, C, H, W), label count (number of labels) must be NHW, with integer values in {0, 1, ..., C-1}.

这里有什么问题?我的标签是 3x128x128,我的 output_num 是 49152 = 3 x 128 x128??

我的后续问题是如何将此 1D 数据转换为 3D 数据:

我正在使用 python API 咖啡。我知道我 "just" 必须将 1D 矢量重塑为 3D 矢量。但是我怎么知道 "reshape" 在哪里,因为 1D 向量中的哪个位置对应于 3D 向量中的位置。谁能帮我?

提前致谢!

由于您正在寻找逐像素分类,并且标签是真实图像,因此最好使用 Eucledian loss 层而不是 Softmax with loss。 Softmax 通常用于多类分类。在您的情况下也可以使用 softmax,但要更改标签的格式等。

在执行欧几里德损失时,caffe 会将标签视为一维数组,同样适用于预测输出。 3D 到 1D 转换将具有宽度优先格式,然后是高度,然后是通道。

您的模型将学习预测格式与标签类似的输出。也就是说,如果标签中的通道是倒置的,模型最终将学习倒置的形式。这是因为上一层是fully connected

您可以阅读有关 Softmax 的更多信息 here

类似的答案,可以帮助您进行详细解释,