/
test_threshold_layer.cpp
98 lines (87 loc) · 3.2 KB
/
test_threshold_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layers/threshold_layer.hpp"
#include "caffe/test/test_caffe_main.hpp"
namespace caffe {
template <typename TypeParam>
class ThresholdLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
ThresholdLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)),
blob_top_(new Blob<Dtype>()) {
Caffe::set_random_seed(1701);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~ThresholdLayerTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(ThresholdLayerTest, TestDtypesAndDevices);
TYPED_TEST(ThresholdLayerTest, TestSetup) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ThresholdLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num());
EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_->channels());
EXPECT_EQ(this->blob_top_->height(), this->blob_bottom_->height());
EXPECT_EQ(this->blob_top_->width(), this->blob_bottom_->width());
}
TYPED_TEST(ThresholdLayerTest, Test) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ThresholdLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
const Dtype threshold_ = layer_param.threshold_param().threshold();
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_GE(top_data[i], 0.);
EXPECT_LE(top_data[i], 1.);
if (top_data[i] == 0) {
EXPECT_LE(bottom_data[i], threshold_);
}
if (top_data[i] == 1) {
EXPECT_GT(bottom_data[i], threshold_);
}
}
}
TYPED_TEST(ThresholdLayerTest, Test2) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ThresholdParameter* threshold_param =
layer_param.mutable_threshold_param();
threshold_param->set_threshold(0.5);
ThresholdLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
const Dtype threshold_ = layer_param.threshold_param().threshold();
EXPECT_FLOAT_EQ(threshold_, 0.5);
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
EXPECT_GE(top_data[i], 0.);
EXPECT_LE(top_data[i], 1.);
if (top_data[i] == 0) {
EXPECT_LE(bottom_data[i], threshold_);
}
if (top_data[i] == 1) {
EXPECT_GT(bottom_data[i], threshold_);
}
}
}
} // namespace caffe