caffe-custom network input/image_data_layer/hdf5_data_layer

来源:互联网 发布:群晖网络存储 编辑:程序博客网 时间:2024/06/13 05:59
当需要输入定制自己的数据时候,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: 1slice_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] = llf.close()with open('train.h5list', 'w') as f:    f.write('train0.h5' + '\n')    f.write('train1.h5' + '\n')name: "traintest"force_backward: truelayer {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: 15shuffle: 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 0
原创粉丝点击