您为什么不简单地使用旧的 convert_imagest 创建两个数据集?
layer {
name: "data_a"
top: "data_a"
top: "label_a"
type: "Data"
data_param { source: "/path/to/first/data_lmdb" }
...
}
layer {
name: "data_b"
top: "data_b"
top: "label_b"
type: "Data"
data_param { source: "/path/to/second/data_lmdb" }
...
}
至于损失,由于每个示例都有一个类标签,您需要将label_a 和label_b 转换为same_not_same_label。我建议您使用 python 层“即时”执行此操作。在prototxt添加对python层的调用:
layer {
name: "a_b_to_same_not_same_label"
type: "Python"
bottom: "label_a"
bottom: "label_b"
top: "same_not_same_label"
python_param {
# the module name -- usually the filename -- that needs to be in $PYTHONPATH
module: "siamese"
# the layer name -- the class name in the module
layer: "SiameseLabels"
}
propagate_down: false
}
创建siamese.py(确保它在您的$PYTHONPATH 中)。在siamese.py 你应该有图层类:
import sys, os
sys.path.insert(0,os.environ['CAFFE_ROOT'] + '/python')
import caffe
class SiameseLabels(caffe.Layer):
def setup(self, bottom, top):
if len(bottom) != 2:
raise Exception('must have exactly two inputs')
if len(top) != 1:
raise Exception('must have exactly one output')
def reshape(self,bottom,top):
top[0].reshape( *bottom[0].shape )
def forward(self,bottom,top):
top[0].data[...] = (bottom[0].data == bottom[1].data).astype('f4')
def backward(self,top,propagate_down,bottom):
# no back prop
pass
确保您以不同的方式对两组中的示例进行洗牌,这样您就可以得到非平凡的配对。此外,如果您使用 不同 个示例构建第一个和第二个数据集,那么您将在每个 epoch 看到不同的对 ;)
确保您构建网络以共享重复层的权重,有关详细信息,请参阅this tutorial。