infogain_loss_layer.hpp
#ifndef CAFFE_INFOGAIN_LOSS_LAYER_HPP_
#define CAFFE_INFOGAIN_LOSS_LAYER_HPP_
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/layers/loss_layer.hpp"
#include "caffe/layers/softmax_layer.hpp"
namespace caffe {
template <typename Dtype>
class InfogainLossLayer : public LossLayer<Dtype> {
public:
explicit InfogainLossLayer(const LayerParameter& param)
: LossLayer<Dtype>(param), infogain_() {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
// InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should
// be the infogain matrix. (Otherwise the infogain matrix is loaded from a
// file specified by LayerParameter.)
virtual inline int ExactNumBottomBlobs() const { return -1; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int MaxBottomBlobs() const { return 3; }
// InfogainLossLayer computes softmax prob internally.
// optional second "top" outputs the softmax prob
virtual inline int ExactNumTopBlobs() const { return -1; }
virtual inline int MinTopBlobs() const { return 1; }
virtual inline int MaxTopBlobs() const { return 2; }
virtual inline const char* type() const { return "InfogainLoss"; }
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual Dtype get_normalizer(
LossParameter_NormalizationMode normalization_mode, int valid_count);
virtual void sum_rows_of_H(const Blob<Dtype>* H);
shared_ptr<Layer<Dtype> > softmax_layer_;
Blob<Dtype> prob_;
vector<Blob<Dtype>*> softmax_bottom_vec_;
vector<Blob<Dtype>*> softmax_top_vec_;
Blob<Dtype> infogain_;
Blob<Dtype> sum_rows_H_; // cache the row sums of H.
bool has_ignore_label_;
int ignore_label_;
LossParameter_NormalizationMode normalization_;
int infogain_axis_, outer_num_, inner_num_, num_labels_;
};
} // namespace caffe
#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