Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions include/caffe/loss_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ class AccuracyLayer : public Layer<Dtype> {
return LayerParameter_LayerType_ACCURACY;
}

virtual inline int ExactNumBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
// AccuracyLayer takes 2-3 bottom Blobs; if there are 3 the second and third
// are compared to compute a 0/1 label. (Otherwise the label comes directly
// from the second.)
virtual inline int ExactNumBottomBlobs() const { return -1; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int MaxBottomBlobs() const { return 3; }

private:
int ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i);

protected:
/**
Expand Down Expand Up @@ -355,6 +362,13 @@ class HingeLossLayer : public LossLayer<Dtype> {
return LayerParameter_LayerType_HINGE_LOSS;
}

// HingeLossLayer takes 2-3 bottom Blobs; if there are 3 the second and third
// are compared to compute a 0/1 label. (Otherwise the label comes directly
// from the second.)
virtual inline int ExactNumBottomBlobs() const { return -1; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int MaxBottomBlobs() const { return 3; }

protected:
/// @copydoc HingeLossLayer
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -389,6 +403,9 @@ class HingeLossLayer : public LossLayer<Dtype> {
*/
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

private:
int ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i);
};

/**
Expand Down
22 changes: 20 additions & 2 deletions src/caffe/layers/accuracy_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

namespace caffe {

template <typename Dtype>
int AccuracyLayer<Dtype>::ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i) {
int label;
if (bottom.size() == 2) {
label = static_cast<int>(bottom[1]->cpu_data()[i]);
} else { // bottom.size() == 3
// label == 1 if bottom[1] == bottom[2] (same)
// label == 0 if bottom[1] != bottom[2] (not same)
label = (bottom[1]->cpu_data()[i] ==
bottom[2]->cpu_data()[i]) ? 1 : 0;

if (i == 0) {
}
}
return label;
}

template <typename Dtype>
void AccuracyLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
Expand All @@ -34,12 +51,13 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype accuracy = 0;
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* bottom_label = bottom[1]->cpu_data();
int num = bottom[0]->num();
int dim = bottom[0]->count() / bottom[0]->num();
vector<Dtype> maxval(top_k_+1);
vector<int> max_id(top_k_+1);
int label;
for (int i = 0; i < num; ++i) {
label = ComputeLabel(bottom, i);
// Top-k accuracy
std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int j = 0; j < dim; ++j) {
Expand All @@ -51,7 +69,7 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
// check if true label is in top k predictions
for (int k = 0; k < top_k_; k++) {
if (bottom_data_vector[k].second == static_cast<int>(bottom_label[i])) {
if (bottom_data_vector[k].second == label) {
++accuracy;
break;
}
Expand Down
24 changes: 20 additions & 4 deletions src/caffe/layers/hinge_loss_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,34 @@

namespace caffe {

template <typename Dtype>
int HingeLossLayer<Dtype>::ComputeLabel(const vector<Blob<Dtype>*>& bottom, int i) {
int label;
if (bottom.size() == 2) {
label = static_cast<int>(bottom[1]->cpu_data()[i]);
} else { // bottom.size() == 3
// label == 1 if bottom[1] == bottom[2] (same)
// label == 0 if bottom[1] != bottom[2] (not same)
label = (bottom[1]->cpu_data()[i] ==
bottom[2]->cpu_data()[i]) ? 1 : 0;
}
return label;
}

template <typename Dtype>
void HingeLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const Dtype* label = bottom[1]->cpu_data();
int num = bottom[0]->num();
int count = bottom[0]->count();
int dim = count / num;
int label;

caffe_copy(count, bottom_data, bottom_diff);
for (int i = 0; i < num; ++i) {
bottom_diff[i * dim + static_cast<int>(label[i])] *= -1;
label = ComputeLabel(bottom, i);
bottom_diff[i * dim + label] *= -1;
}
for (int i = 0; i < num; ++i) {
for (int j = 0; j < dim; ++j) {
Expand Down Expand Up @@ -52,13 +67,14 @@ void HingeLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
}
if (propagate_down[0]) {
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const Dtype* label = bottom[1]->cpu_data();
int num = bottom[0]->num();
int count = bottom[0]->count();
int dim = count / num;
int label;

for (int i = 0; i < num; ++i) {
bottom_diff[i * dim + static_cast<int>(label[i])] *= -1;
label = ComputeLabel(bottom, i);
bottom_diff[i * dim + label] *= -1;
}

const Dtype loss_weight = top[0]->cpu_diff()[0];
Expand Down