|
| 1 | +// Copyright 2014 BVLC and contributors. |
| 2 | + |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "cuda_runtime.h" |
| 6 | +#include "gtest/gtest.h" |
| 7 | +#include "caffe/blob.hpp" |
| 8 | +#include "caffe/common.hpp" |
| 9 | +#include "caffe/filler.hpp" |
| 10 | +#include "caffe/vision_layers.hpp" |
| 11 | +#include "caffe/test/test_gradient_check_util.hpp" |
| 12 | + |
| 13 | +#include "caffe/test/test_caffe_main.hpp" |
| 14 | + |
| 15 | +namespace caffe { |
| 16 | + |
| 17 | +extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; |
| 18 | + |
| 19 | +template <typename Dtype> |
| 20 | +class ThresholdLayerTest : public ::testing::Test { |
| 21 | + protected: |
| 22 | + ThresholdLayerTest() |
| 23 | + : blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)), |
| 24 | + blob_top_(new Blob<Dtype>()) { |
| 25 | + Caffe::set_random_seed(1701); |
| 26 | + // fill the values |
| 27 | + FillerParameter filler_param; |
| 28 | + GaussianFiller<Dtype> filler(filler_param); |
| 29 | + filler.Fill(this->blob_bottom_); |
| 30 | + blob_bottom_vec_.push_back(blob_bottom_); |
| 31 | + blob_top_vec_.push_back(blob_top_); |
| 32 | + } |
| 33 | + virtual ~ThresholdLayerTest() { delete blob_bottom_; delete blob_top_; } |
| 34 | + Blob<Dtype>* const blob_bottom_; |
| 35 | + Blob<Dtype>* const blob_top_; |
| 36 | + vector<Blob<Dtype>*> blob_bottom_vec_; |
| 37 | + vector<Blob<Dtype>*> blob_top_vec_; |
| 38 | +}; |
| 39 | + |
| 40 | +typedef ::testing::Types<float, double> Dtypes; |
| 41 | +TYPED_TEST_CASE(ThresholdLayerTest, Dtypes); |
| 42 | + |
| 43 | + |
| 44 | +TYPED_TEST(ThresholdLayerTest, TestSetup) { |
| 45 | + LayerParameter layer_param; |
| 46 | + ThresholdLayer<TypeParam> layer(layer_param); |
| 47 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 48 | + EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num()); |
| 49 | + EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_->channels()); |
| 50 | + EXPECT_EQ(this->blob_top_->height(), this->blob_bottom_->height()); |
| 51 | + EXPECT_EQ(this->blob_top_->width(), this->blob_bottom_->width()); |
| 52 | +} |
| 53 | + |
| 54 | +TYPED_TEST(ThresholdLayerTest, TestCPU) { |
| 55 | + LayerParameter layer_param; |
| 56 | + Caffe::set_mode(Caffe::CPU); |
| 57 | + ThresholdLayer<TypeParam> layer(layer_param); |
| 58 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 59 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 60 | + // Now, check values |
| 61 | + const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); |
| 62 | + const TypeParam* top_data = this->blob_top_->cpu_data(); |
| 63 | + const TypeParam threshold_ = layer_param.threshold_param().threshold(); |
| 64 | + for (int i = 0; i < this->blob_bottom_->count(); ++i) { |
| 65 | + EXPECT_GE(top_data[i], 0.); |
| 66 | + EXPECT_LE(top_data[i], 1.); |
| 67 | + if (top_data[i] == 0) { |
| 68 | + EXPECT_LE(bottom_data[i], threshold_); |
| 69 | + } |
| 70 | + if (top_data[i] == 1) { |
| 71 | + EXPECT_GT(bottom_data[i], threshold_); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +TYPED_TEST(ThresholdLayerTest, TestCPU2) { |
| 77 | + LayerParameter layer_param; |
| 78 | + Caffe::set_mode(Caffe::CPU); |
| 79 | + ThresholdParameter* threshold_param = |
| 80 | + layer_param.mutable_threshold_param(); |
| 81 | + threshold_param->set_threshold(0.5); |
| 82 | + ThresholdLayer<TypeParam> layer(layer_param); |
| 83 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 84 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 85 | + // Now, check values |
| 86 | + const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); |
| 87 | + const TypeParam* top_data = this->blob_top_->cpu_data(); |
| 88 | + const TypeParam threshold_ = layer_param.threshold_param().threshold(); |
| 89 | + EXPECT_FLOAT_EQ(threshold_, 0.5); |
| 90 | + for (int i = 0; i < this->blob_bottom_->count(); ++i) { |
| 91 | + EXPECT_GE(top_data[i], 0.); |
| 92 | + EXPECT_LE(top_data[i], 1.); |
| 93 | + if (top_data[i] == 0) { |
| 94 | + EXPECT_LE(bottom_data[i], threshold_); |
| 95 | + } |
| 96 | + if (top_data[i] == 1) { |
| 97 | + EXPECT_GT(bottom_data[i], threshold_); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +TYPED_TEST(ThresholdLayerTest, TestGPU) { |
| 103 | + LayerParameter layer_param; |
| 104 | + Caffe::set_mode(Caffe::GPU); |
| 105 | + ThresholdLayer<TypeParam> layer(layer_param); |
| 106 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 107 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 108 | + // Now, check values |
| 109 | + const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); |
| 110 | + const TypeParam* top_data = this->blob_top_->cpu_data(); |
| 111 | + const TypeParam threshold_ = layer_param.threshold_param().threshold(); |
| 112 | + for (int i = 0; i < this->blob_bottom_->count(); ++i) { |
| 113 | + EXPECT_GE(top_data[i], 0.); |
| 114 | + EXPECT_LE(top_data[i], 1.); |
| 115 | + if (top_data[i] == 0) { |
| 116 | + EXPECT_LE(bottom_data[i], threshold_); |
| 117 | + } |
| 118 | + if (top_data[i] == 1) { |
| 119 | + EXPECT_GT(bottom_data[i], threshold_); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +TYPED_TEST(ThresholdLayerTest, TestGPU2) { |
| 125 | + LayerParameter layer_param; |
| 126 | + Caffe::set_mode(Caffe::GPU); |
| 127 | + ThresholdParameter* threshold_param = |
| 128 | + layer_param.mutable_threshold_param(); |
| 129 | + threshold_param->set_threshold(0.5); |
| 130 | + ThresholdLayer<TypeParam> layer(layer_param); |
| 131 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 132 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 133 | + // Now, check values |
| 134 | + const TypeParam* bottom_data = this->blob_bottom_->cpu_data(); |
| 135 | + const TypeParam* top_data = this->blob_top_->cpu_data(); |
| 136 | + const TypeParam threshold_ = layer_param.threshold_param().threshold(); |
| 137 | + EXPECT_FLOAT_EQ(threshold_, 0.5); |
| 138 | + for (int i = 0; i < this->blob_bottom_->count(); ++i) { |
| 139 | + EXPECT_GE(top_data[i], 0.); |
| 140 | + EXPECT_LE(top_data[i], 1.); |
| 141 | + if (top_data[i] == 0) { |
| 142 | + EXPECT_LE(bottom_data[i], threshold_); |
| 143 | + } |
| 144 | + if (top_data[i] == 1) { |
| 145 | + EXPECT_GT(bottom_data[i], threshold_); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace caffe |
0 commit comments