2

I have a highly imbalanced data, I know that some users suggesting using InfoGainLoss loss function, however, I am facing few errors when I tried to add this function to Caffe layers.
I have the following questions, I really appreciate if someone guides me:

  1. How can I add this layer to Caffe? Does anyone know any sources/ codes of this layer?
  2. I want to apply it for image segmentation and the proportion of some classes varies. How can I create the H matrix (a stack of weights) for my images? And how infoGainLoss layer can read a specific weight matrix (H) related to that specific image?
  3. After adding the cpp and cu version of InforGainLoss layer to caffe, should I remake Caffe?

I am sorry for few question, but all are my concern and related to each other. I will be thankful to get some help and support. Thanks

Community
  • 1
  • 1
S.EB
  • 1,966
  • 4
  • 29
  • 54

1 Answers1

2

1.If you copy from current infogain_loss_layer.cpp you can easily adapt. For forward pass change line 59-66 like:

// assuming num = batch size, dim = label size, image_dim = image height * width
Dtype loss = 0;
for (int i = 0; i < num; ++i) {
  for(int k = 0; k < image_dim; k++) {
    int label = static_cast<int>(bottom_label[i*image_dim+k]);
    for (int j = 0; j < dim; ++j) {
      Dtype prob = std::max(bottom_data[i *image_dim *dim+ k * dim + j], Dtype(kLOG_THRESHOLD));
      loss -= infogain_mat[label * dim + j] * log(prob);
    }
  }
}

Similarly for backward pass you could change line 95-101 like:

for (int i = 0; i < num; ++i) {
  for(int k = 0; k < image_dim; k++) {
    const int label = static_cast<int>(bottom_label[i*image_dim+k]);
    for (int j = 0; j < dim; ++j) {
      Dtype prob = std::max(bottom_data[i *image_dim *dim+ k * dim + j], Dtype(kLOG_THRESHOLD));
      bottom_diff[i *image_dim *dim+ k * dim + j] = scale * infogain_mat[label * dim + j] / prob;
    }
  }
}

This is kind of naive. I don't seem to find any option for optimization. You will also need to change some setup code in reshape.

2.In this PR suggestion is that for diagonal entries in H put min_count/|i| where |i| is the number of samples has label i. Everything else as 0. Also see this . As for loading the weight matrix H is fixed for all input. You can load it as lmdb file or in other ways.

3.Yes you will need to rebuild.

Update: As Shai pointed out the infogain pull for this has already been approved this week. So current version of caffe supports pixelwise infogain loss.

Community
  • 1
  • 1
nomem
  • 1,568
  • 4
  • 17
  • 33
  • HI, Thanks for your help, I changed according to this [link](https://github.com/BVLC/caffe/pull/3855/commits/337b07589f4e44761bdb9ef4c242f83ca40c9da5). But it is showing this error `[libprotobuf ERROR google/protobuf/text_format.cc:274] Error parsing text-format caffe.NetParameter: 604:9: Message type "caffe.InfogainLossParameter" has no field named "axis".` I dont know why????!!! :(( – S.EB Apr 20 '17 at 21:35
  • @S.EB you didn't add `optional int32 axis = 2 [default = 1];` in `InfogainLossParameter` in caffe.proto – nomem Apr 20 '17 at 22:05
  • the aforementioned pull request was recently merged to caffe master. why don't you simply pull the recent version and rebuild? – Shai Apr 20 '17 at 22:51
  • if you want your H matrix to change for each image, you might consider implementing a Python layer computing it on the fly – Shai Apr 20 '17 at 22:52