Caffe
All Classes Namespaces Functions Variables Typedefs
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 
48 template <typename Dtype>
49 class InfogainLossLayer : public LossLayer<Dtype> {
50  public:
51  explicit InfogainLossLayer(const LayerParameter& param)
52  : LossLayer<Dtype>(param), infogain_() {}
53  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
54  const vector<Blob<Dtype>*>& top);
55  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
56  const vector<Blob<Dtype>*>& top);
57 
58  // InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should
59  // be the infogain matrix. (Otherwise the infogain matrix is loaded from a
60  // file specified by LayerParameter.)
61  virtual inline int ExactNumBottomBlobs() const { return -1; }
62  virtual inline int MinBottomBlobs() const { return 2; }
63  virtual inline int MaxBottomBlobs() const { return 3; }
64 
65  // InfogainLossLayer computes softmax prob internally.
66  // optional second "top" outputs the softmax prob
67  virtual inline int ExactNumTopBlobs() const { return -1; }
68  virtual inline int MinTopBlobs() const { return 1; }
69  virtual inline int MaxTopBlobs() const { return 2; }
70 
71  virtual inline const char* type() const { return "InfogainLoss"; }
72 
73  protected:
75  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
76  const vector<Blob<Dtype>*>& top);
77 
110  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
111  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
112 
117  virtual Dtype get_normalizer(
118  LossParameter_NormalizationMode normalization_mode, int valid_count);
120  virtual void sum_rows_of_H(const Blob<Dtype>* H);
121 
123  shared_ptr<Layer<Dtype> > softmax_layer_;
127  vector<Blob<Dtype>*> softmax_bottom_vec_;
129  vector<Blob<Dtype>*> softmax_top_vec_;
130 
131  Blob<Dtype> infogain_;
132  Blob<Dtype> sum_rows_H_; // cache the row sums of H.
133 
139  LossParameter_NormalizationMode normalization_;
140 
141  int infogain_axis_, outer_num_, inner_num_, num_labels_;
142 };
143 
144 } // namespace caffe
145 
146 #endif // CAFFE_INFOGAIN_LOSS_LAYER_HPP_
caffe::InfogainLossLayer::prob_
Blob< Dtype > prob_
prob stores the output probability predictions from the SoftmaxLayer.
Definition: infogain_loss_layer.hpp:125
caffe::InfogainLossLayer::MaxBottomBlobs
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:63
caffe::InfogainLossLayer::MinBottomBlobs
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:62
caffe::InfogainLossLayer::normalization_
LossParameter_NormalizationMode normalization_
How to normalize the output loss.
Definition: infogain_loss_layer.hpp:139
caffe::InfogainLossLayer::type
virtual const char * type() const
Returns the layer type.
Definition: infogain_loss_layer.hpp:71
caffe::InfogainLossLayer::MaxTopBlobs
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:69
caffe::InfogainLossLayer::Reshape
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
caffe::InfogainLossLayer::LayerSetUp
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
caffe::InfogainLossLayer::softmax_bottom_vec_
vector< Blob< Dtype > * > softmax_bottom_vec_
bottom vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: infogain_loss_layer.hpp:127
caffe::InfogainLossLayer::ignore_label_
int ignore_label_
The label indicating that an instance should be ignored.
Definition: infogain_loss_layer.hpp:137
caffe::InfogainLossLayer::Backward_cpu
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
caffe::InfogainLossLayer::ExactNumBottomBlobs
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:61
caffe::Blob
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers,...
Definition: blob.hpp:24
caffe::InfogainLossLayer::softmax_top_vec_
vector< Blob< Dtype > * > softmax_top_vec_
top vector holder used in call to the underlying SoftmaxLayer::Forward
Definition: infogain_loss_layer.hpp:129
caffe::LossLayer
An interface for Layers that take two Blobs as input – usually (1) predictions and (2) ground-truth l...
Definition: loss_layer.hpp:23
caffe::InfogainLossLayer::ExactNumTopBlobs
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:67
caffe::InfogainLossLayer::sum_rows_of_H
virtual void sum_rows_of_H(const Blob< Dtype > *H)
fill sum_rows_H_ according to matrix H
Definition: infogain_loss_layer.cpp:115
caffe::InfogainLossLayer::MinTopBlobs
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:68
caffe::InfogainLossLayer
A generalization of SoftmaxWithLossLayer that takes an "information gain" (infogain) matrix specifyin...
Definition: infogain_loss_layer.hpp:49
caffe::InfogainLossLayer::Forward_cpu
virtual void Forward_cpu(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
A generalization of SoftmaxWithLossLayer that takes an "information gain" (infogain) matrix specifyin...
Definition: infogain_loss_layer.cpp:129
caffe::InfogainLossLayer::softmax_layer_
shared_ptr< Layer< Dtype > > softmax_layer_
The internal SoftmaxLayer used to map predictions to a distribution.
Definition: infogain_loss_layer.hpp:123
caffe::InfogainLossLayer::get_normalizer
virtual Dtype get_normalizer(LossParameter_NormalizationMode normalization_mode, int valid_count)
Definition: infogain_loss_layer.cpp:85
caffe
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
caffe::InfogainLossLayer::has_ignore_label_
bool has_ignore_label_
Whether to ignore instances with a certain label.
Definition: infogain_loss_layer.hpp:135