Caffe
infogain_loss_layer.hpp
1 #ifndef CAFFE_INFOGAIN_LOSS_LAYER_HPP_
2 #define CAFFE_INFOGAIN_LOSS_LAYER_HPP_
3 
4 #include <vector>
5 
6 #include "caffe/blob.hpp"
7 #include "caffe/layer.hpp"
8 #include "caffe/proto/caffe.pb.h"
9 
10 #include "caffe/layers/loss_layer.hpp"
11 #include "caffe/layers/softmax_layer.hpp"
12 
13 namespace caffe {
14 
47 template <typename Dtype>
48 class InfogainLossLayer : public LossLayer<Dtype> {
49  public:
50  explicit InfogainLossLayer(const LayerParameter& param)
51  : LossLayer<Dtype>(param), infogain_() {}
52  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
53  const vector<Blob<Dtype>*>& top);
54  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
55  const vector<Blob<Dtype>*>& top);
56 
57  // InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should
58  // be the infogain matrix. (Otherwise the infogain matrix is loaded from a
59  // file specified by LayerParameter.)
60  virtual inline int ExactNumBottomBlobs() const { return -1; }
61  virtual inline int MinBottomBlobs() const { return 2; }
62  virtual inline int MaxBottomBlobs() const { return 3; }
63 
64  // InfogainLossLayer computes softmax prob internally.
65  // optional second "top" outputs the softmax prob
66  virtual inline int ExactNumTopBlobs() const { return -1; }
67  virtual inline int MinTopBlobs() const { return 1; }
68  virtual inline int MaxTopBlobs() const { return 2; }
69 
70  virtual inline const char* type() const { return "InfogainLoss"; }
71 
72  protected:
74  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
75  const vector<Blob<Dtype>*>& top);
76 
109  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
110  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
111 
116  virtual Dtype get_normalizer(
117  LossParameter_NormalizationMode normalization_mode, int valid_count);
119  virtual void sum_rows_of_H(const Blob<Dtype>* H);
120 
122  shared_ptr<Layer<Dtype> > softmax_layer_;
126  vector<Blob<Dtype>*> softmax_bottom_vec_;
128  vector<Blob<Dtype>*> softmax_top_vec_;
129 
130  Blob<Dtype> infogain_;
131  Blob<Dtype> sum_rows_H_; // cache the row sums of H.
132 
138  LossParameter_NormalizationMode normalization_;
139 
140  int infogain_axis_, outer_num_, inner_num_, num_labels_;
141 };
142 
143 } // namespace caffe
144 
145 #endif // CAFFE_INFOGAIN_LOSS_LAYER_HPP_
virtual int MinTopBlobs() const
Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required...
Definition: infogain_loss_layer.hpp:67
virtual void LayerSetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: infogain_loss_layer.cpp:12
vector< Blob< Dtype > * > softmax_bottom_vec_
bottom vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: infogain_loss_layer.hpp:126
int ignore_label_
The label indicating that an instance should be ignored.
Definition: infogain_loss_layer.hpp:136
Blob< Dtype > prob_
prob stores the output probability predictions from the SoftmaxLayer.
Definition: infogain_loss_layer.hpp:124
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
virtual int ExactNumTopBlobs() const
Returns the exact number of top blobs required by the layer, or -1 if no exact number is required...
Definition: infogain_loss_layer.hpp:66
virtual const char * type() const
Returns the layer type.
Definition: infogain_loss_layer.hpp:70
LossParameter_NormalizationMode normalization_
How to normalize the output loss.
Definition: infogain_loss_layer.hpp:138
A generalization of MultinomialLogisticLossLayer that takes an "information gain" (infogain) matrix s...
Definition: infogain_loss_layer.hpp:48
virtual void Backward_cpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Computes the infogain loss error gradient w.r.t. the predictions.
Definition: infogain_loss_layer.cpp:168
shared_ptr< Layer< Dtype > > softmax_layer_
The internal SoftmaxLayer used to map predictions to a distribution.
Definition: infogain_loss_layer.hpp:122
virtual int MaxBottomBlobs() const
Returns the maximum number of bottom blobs required by the layer, or -1 if no maximum number is requi...
Definition: infogain_loss_layer.hpp:62
bool has_ignore_label_
Whether to ignore instances with a certain label.
Definition: infogain_loss_layer.hpp:134
vector< Blob< Dtype > * > softmax_top_vec_
top vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: infogain_loss_layer.hpp:128
virtual void Forward_cpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
A generalization of MultinomialLogisticLossLayer that takes an "information gain" (infogain) matrix s...
Definition: infogain_loss_layer.cpp:129
virtual int ExactNumBottomBlobs() const
Returns the exact number of bottom blobs required by the layer, or -1 if no exact number is required...
Definition: infogain_loss_layer.hpp:60
virtual int MaxTopBlobs() const
Returns the maximum number of top blobs required by the layer, or -1 if no maximum number is required...
Definition: infogain_loss_layer.hpp:68
virtual void sum_rows_of_H(const Blob< Dtype > *H)
fill sum_rows_H_ according to matrix H
Definition: infogain_loss_layer.cpp:115
An interface for Layers that take two Blobs as input – usually (1) predictions and (2) ground-truth ...
Definition: loss_layer.hpp:23
virtual Dtype get_normalizer(LossParameter_NormalizationMode normalization_mode, int valid_count)
Definition: infogain_loss_layer.cpp:85
virtual int MinBottomBlobs() const
Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is requi...
Definition: infogain_loss_layer.hpp:61
virtual void Reshape(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs...
Definition: infogain_loss_layer.cpp:51
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24