From 2abbaca165e8fbebf35dad683d985e87b58af8ba Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Mon, 9 Mar 2015 12:16:30 -0700 Subject: [PATCH] AccuracyLayerTest: add tests for ignore_label and spatial axes --- src/caffe/test/test_accuracy_layer.cpp | 98 ++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 5 deletions(-) diff --git a/src/caffe/test/test_accuracy_layer.cpp b/src/caffe/test/test_accuracy_layer.cpp index 1c58b767bfc..6cbf51df45e 100644 --- a/src/caffe/test/test_accuracy_layer.cpp +++ b/src/caffe/test/test_accuracy_layer.cpp @@ -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 filler(filler_param); @@ -39,14 +47,11 @@ class AccuracyLayerTest : public ::testing::Test { caffe::rng_t* prefetch_rng = static_cast(rng->generator()); Dtype* label_data = blob_bottom_label_->mutable_cpu_data(); - for (int i = 0; i < 100; ++i) { + for (int i = 0; i < blob_bottom_label_->count(); ++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_; @@ -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 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 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 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(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 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();