当需要输入定制自己的数据时候,e.g. multi-label, bbox. etc.
option 1.
修改 image_data_layer.cpp 文件
batch类有data_ label_ 两个成员变量, 把图片之外的数据输入到label_中, 然后使用slice_layer就行了
e.g. test.jpg 1 2 3 4 5
修改lines_的结构
vector<std::pair<std::string, std::vector<int>> > lines_;
定制label_的的shape
vector<int> label_shape ;
label_shape.push_back(batch_size);
label_shape.push_back(5);
label_shape.push_back(1);
label_shape.push_back(1);
然后在读取文件的时候, 以此把1 2 3 4 5 放到 label_中即可
while(getline(infile, line)){
vector<int> posx = {0};
for (int pos=0; pos< line.length(); pos+=1)
{
pos = line.find(' ', pos);
if (pos ==-1) break;
posx.push_back(pos);
}
posx.push_back(line.length());
vector<int> labels;
for(int i=1; i<posx.size()-1; i++){
string tmp = line.substr(posx[i]+1, posx[i+1]);
//cout<< tmp <<"-----------------"<<endl;
labels.push_back(stoi(tmp));
}
lines_.push_back(std::make_pair(line.substr(posx[0], posx[1]), labels));
}//while
int offsetlabel = batch->label_.offset(item_id);
for(int i=0;i<5;i++)
{
*(prefetch_label + i + offsetlabel) = lines_[lines_id_].second[i];
}
最后在train的.prototxt中, 按照自己label中数据的排放方式使用slice_layer
layer {
name: "split"
bottom: "label"
top: "label1"
top: "bbox"
type: "Slice"
slice_param{
axis: 1
slice_point: 1
}
}
option-2
hdf5_data_layer
f = h5py.File('train0.h5', 'w')
f.create_dataset('data', (10, 2,3,3), dtype='f8')
f.create_dataset('label0', (10, 3), dtype='f4')
f.create_dataset('label1', (10, 5), dtype='f4')
for i in xrange(10):
a = np.random.randn(2,3,3)
l = np.random.randn(3)
ll = np.random.randn(5)
f['data'][i] = a
f['label0'][i] = l
f['label1'][i] = ll
f.close()
with open('train.h5list', 'w') as f:
f.write('train0.h5' + '\n')
f.write('train1.h5' + '\n')
name: "traintest"
force_backward: true
layer {
name: "hd5"
top: "data"
top: "label0"
top: "label1"
type: "HDF5Data"
hdf5_data_param {
#source: "D:\\workspace\\python\\trainh5list.txt"
source: "D:\\workspace\\python\\train.h5list"
batch_size: 15
shuffle: true
}
}
---------reference---------------------
http://www.meltycriss.com/2016/07/04/caffe_1_layer/
http://stackoverflow.com/questions/33140000/how-to-feed-caffe-multi-label-data-in-hdf5-format