我在尝试构建Caffe的InfogainLoss层,但始终无法正常工作。我已经查看了相关的帖子和解决方案,但对我来说还是不行。
我的数据lmdb尺寸是Nx1xHxW(灰度图像),我的目标图像lmdb尺寸是Nx3xH/8xW/8(RGB图像)。我的最后一个卷积层的尺寸是1x3x20x80。输出尺寸是3,因此我的标签数量有3类,即(0,1,2),在目标lmdb图像数据集中。
我想尝试使用InfogainLoss层,因为我认为我有类别不平衡的问题。我的大多数图像包含过多的背景。
在我的最后一个卷积层(conv3)之后,我有以下设置:
layer { name: "loss" type: "SoftmaxWithLoss" bottom: "conv3" top: "loss" } layer { bottom: "loss" bottom: "label" top: "infoGainLoss" name: "infoGainLoss" type: "InfogainLoss" infogain_loss_param { source: "infogainH.binaryproto" } }
我的Infogain矩阵是根据InfogainLoss层帖子(如Shai建议的)生成的,因此我的H矩阵尺寸是1x1x3x3(一个单位矩阵)。因此,我的L
是3,因为我有3个类别。当我运行prototxt文件时,一切正常(尺寸没问题),但在我的最后一个卷积层(conv3层)之后,我得到了以下错误:
I0320 14:42:16.722874 5591 net.cpp:157] Top shape: 1 3 20 80 (4800) I0320 14:42:16.722882 5591 net.cpp:165] Memory required for data: 2892800 I0320 14:42:16.722892 5591 layer_factory.hpp:77] Creating layer loss I0320 14:42:16.722900 5591 net.cpp:106] Creating Layer loss I0320 14:42:16.722906 5591 net.cpp:454] loss <- conv3 I0320 14:42:16.722913 5591 net.cpp:411] loss -> loss F0320 14:42:16.722928 5591 layer.hpp:374] Check failed: ExactNumBottomBlobs() == bottom.size() (2 vs. 1) SoftmaxWithLoss Layer takes 2 bottom blob(s) as input.
我已经仔细检查过,每个lmdb数据集的文件名都设置正确。我不知道问题出在哪里。有什么想法吗?
亲爱的@
感谢您的回答。我按照您提到的做了以下操作:
layer { name: "prob" type: "Softmax" bottom: "conv3" top: "prob" softmax_param { axis: 1 } } layer { bottom: "prob" bottom: "label" top: "infoGainLoss" name: "infoGainLoss" type: "InfogainLoss" infogain_loss_param { source: "infogainH.binaryproto" } }
但我仍然有错误:
Top shape: 1 3 20 80 (4800)I0320 16:30:25.110862 6689 net.cpp:165] Memory required for data: 2912000I0320 16:30:25.110867 6689 layer_factory.hpp:77] Creating layer infoGainLossI0320 16:30:25.110877 6689 net.cpp:106] Creating Layer infoGainLossI0320 16:30:25.110884 6689 net.cpp:454] infoGainLoss <- probI0320 16:30:25.110889 6689 net.cpp:454] infoGainLoss <- labelI0320 16:30:25.110896 6689 net.cpp:411] infoGainLoss -> infoGainLossF0320 16:30:25.110965 6689 infogain_loss_layer.cpp:35] Check failed: bottom[1]->height() == 1 (20 vs. 1)
回答:
哪里出了问题?
您的错误来自"loss"
层,而不是"InfogainLoss"
层:您混淆了输出类别概率的"Softmax"
层和输出(标量)损失值的"SoftmaxWithLoss"
层。
您应该怎么做?
-
用类型为
"Softmax"
的"prob"
层替换"loss"
层:layer { name: "prob" type: "Softmax" # 不是SoftmaxWithLoss bottom: "conv3" top: "prob" softmax_param { axis: 1 } # 沿第二轴计算概率}
-
您需要沿第二维度计算损失,目前看来
"InfogainLoss"
层不支持此功能。您可能需要调整"InfogainLoss"
层,使其具有类似"SoftmaxWithLoss"
的功能,允许沿任意轴计算损失。
更新: 我在BVLC/caffe
上创建了一个拉取请求,它“升级”了InfogainLoss层。这个升级版本支持您所需的“沿轴计算损失”。此外,它使“Softmax”层变得多余,因为它在内部计算概率(参见这个线程)。
升级后的层可以这样使用:layer { bottom: "conv3" # 内部计算概率 bottom: "label" top: "infoGainLoss" name: "infoGainLoss" type: "InfogainLoss" infogain_loss_param { source: "infogainH.binaryproto" axis: 1 # 沿轴计算损失和概率 }}