caffe-custom network input/image_data_layer/hdf5_data_layer

当需要输入定制自己的数据时候,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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值