Skip to content

Commit

Permalink
AccuracyLayerTest: add tests for ignore_label and spatial axes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdonahue committed Mar 9, 2015
1 parent 6ea7a66 commit 00c8f16
Showing 1 changed file with 92 additions and 4 deletions.
96 changes: 92 additions & 4 deletions src/caffe/test/test_accuracy_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class AccuracyLayerTest : public ::testing::Test {
blob_bottom_data_->Reshape(shape);
shape.resize(1);
blob_bottom_label_->Reshape(shape);
FillBottoms();

blob_bottom_vec_.push_back(blob_bottom_data_);
blob_bottom_vec_.push_back(blob_bottom_label_);
blob_top_vec_.push_back(blob_top_);
}

virtual void FillBottoms() {
// fill the probability values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
Expand All @@ -42,11 +50,8 @@ class AccuracyLayerTest : public ::testing::Test {
for (int i = 0; i < 100; ++i) {
label_data[i] = (*prefetch_rng)() % 10;
}

blob_bottom_vec_.push_back(blob_bottom_data_);
blob_bottom_vec_.push_back(blob_bottom_label_);
blob_top_vec_.push_back(blob_top_);
}

virtual ~AccuracyLayerTest() {
delete blob_bottom_data_;
delete blob_bottom_label_;
Expand Down Expand Up @@ -112,6 +117,89 @@ TYPED_TEST(AccuracyLayerTest, TestForwardCPU) {
num_correct_labels / 100.0, 1e-4);
}

TYPED_TEST(AccuracyLayerTest, TestForwardWithSpatialAxes) {
Caffe::set_mode(Caffe::CPU);
this->blob_bottom_data_->Reshape(2, 10, 4, 5);
vector<int> label_shape(3);
label_shape[0] = 2; label_shape[1] = 4; label_shape[2] = 5;
this->blob_bottom_label_->Reshape(label_shape);
this->FillBottoms();
LayerParameter layer_param;
layer_param.mutable_accuracy_param()->set_axis(1);
AccuracyLayer<TypeParam> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);

TypeParam max_value;
const int num_labels = this->blob_bottom_label_->count();
int max_id;
int num_correct_labels = 0;
vector<int> label_offset(3);
for (int n = 0; n < this->blob_bottom_data_->num(); ++n) {
for (int h = 0; h < this->blob_bottom_data_->height(); ++h) {
for (int w = 0; w < this->blob_bottom_data_->width(); ++w) {
max_value = -FLT_MAX;
max_id = 0;
for (int c = 0; c < this->blob_bottom_data_->channels(); ++c) {
const TypeParam pred_value =
this->blob_bottom_data_->data_at(n, c, h, w);
if (pred_value > max_value) {
max_value = pred_value;
max_id = c;
}
}
label_offset[0] = n; label_offset[1] = h; label_offset[2] = w;
const int correct_label =
static_cast<int>(this->blob_bottom_label_->data_at(label_offset));
if (max_id == correct_label) {
++num_correct_labels;
}
}
}
}
EXPECT_NEAR(this->blob_top_->data_at(0, 0, 0, 0),
num_correct_labels / TypeParam(num_labels), 1e-4);
}

TYPED_TEST(AccuracyLayerTest, TestForwardIgnoreLabel) {
Caffe::set_mode(Caffe::CPU);
LayerParameter layer_param;
const TypeParam kIgnoreLabelValue = -1;
layer_param.mutable_accuracy_param()->set_ignore_label(kIgnoreLabelValue);
AccuracyLayer<TypeParam> layer(layer_param);
// Manually set some labels to the ignore label value (-1).
this->blob_bottom_label_->mutable_cpu_data()[2] = kIgnoreLabelValue;
this->blob_bottom_label_->mutable_cpu_data()[5] = kIgnoreLabelValue;
this->blob_bottom_label_->mutable_cpu_data()[32] = kIgnoreLabelValue;
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);

TypeParam max_value;
int max_id;
int num_correct_labels = 0;
int count = 0;
for (int i = 0; i < 100; ++i) {
if (kIgnoreLabelValue == this->blob_bottom_label_->data_at(i, 0, 0, 0)) {
continue;
}
++count;
max_value = -FLT_MAX;
max_id = 0;
for (int j = 0; j < 10; ++j) {
if (this->blob_bottom_data_->data_at(i, j, 0, 0) > max_value) {
max_value = this->blob_bottom_data_->data_at(i, j, 0, 0);
max_id = j;
}
}
if (max_id == this->blob_bottom_label_->data_at(i, 0, 0, 0)) {
++num_correct_labels;
}
}
EXPECT_EQ(count, 97); // We set 3 out of 100 labels to kIgnoreLabelValue.
EXPECT_NEAR(this->blob_top_->data_at(0, 0, 0, 0),
num_correct_labels / TypeParam(count), 1e-4);
}

TYPED_TEST(AccuracyLayerTest, TestForwardCPUTopK) {
LayerParameter layer_param;
AccuracyParameter* accuracy_param = layer_param.mutable_accuracy_param();
Expand Down

0 comments on commit 00c8f16

Please sign in to comment.