Repository navigation
Expand file tree
/
Copy pathtest_batch_reindex_layer.cpp
More file actions
118 lines (102 loc) · 3.44 KB
/
Copy pathtest_batch_reindex_layer.cpp
File metadata and controls
118 lines (102 loc) · 3.44 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layers/batch_reindex_layer.hpp"
#include "caffe/test/test_caffe_main.hpp"
#include "caffe/test/test_gradient_check_util.hpp"
namespace caffe {
template<typename TypeParam>
class BatchReindexLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
BatchReindexLayerTest()
: blob_bottom_(new Blob<Dtype>()),
blob_bottom_permute_(new Blob<Dtype>()),
blob_top_(new Blob<Dtype>()) {
}
virtual void SetUp() {
Caffe::set_random_seed(1701);
vector<int> sz;
sz.push_back(5);
sz.push_back(4);
sz.push_back(3);
sz.push_back(2);
blob_bottom_->Reshape(sz);
vector<int> permsz;
permsz.push_back(6);
blob_bottom_permute_->Reshape(permsz);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
int perm[] = { 4, 0, 4, 0, 1, 2 };
for (int i = 0; i < blob_bottom_permute_->count(); ++i) {
blob_bottom_permute_->mutable_cpu_data()[i] = perm[i];
}
blob_bottom_vec_.push_back(blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_permute_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~BatchReindexLayerTest() {
delete blob_bottom_permute_;
delete blob_bottom_;
delete blob_top_;
}
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_bottom_permute_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
void TestForward() {
LayerParameter layer_param;
vector<int> sz;
sz.push_back(5);
sz.push_back(4);
sz.push_back(3);
sz.push_back(2);
blob_bottom_->Reshape(sz);
for (int i = 0; i < blob_bottom_->count(); ++i) {
blob_bottom_->mutable_cpu_data()[i] = i;
}
vector<int> permsz;
permsz.push_back(6);
blob_bottom_permute_->Reshape(permsz);
int perm[] = { 4, 0, 4, 0, 1, 2 };
for (int i = 0; i < blob_bottom_permute_->count(); ++i) {
blob_bottom_permute_->mutable_cpu_data()[i] = perm[i];
}
BatchReindexLayer<Dtype> layer(layer_param);
layer.SetUp(blob_bottom_vec_, blob_top_vec_);
EXPECT_EQ(blob_top_->num(), blob_bottom_permute_->num());
EXPECT_EQ(blob_top_->channels(), blob_bottom_->channels());
EXPECT_EQ(blob_top_->height(), blob_bottom_->height());
EXPECT_EQ(blob_top_->width(), blob_bottom_->width());
layer.Forward(blob_bottom_vec_, blob_top_vec_);
int channels = blob_top_->channels();
int height = blob_top_->height();
int width = blob_top_->width();
for (int i = 0; i < blob_top_->count(); ++i) {
int n = i / (channels * width * height);
int inner_idx = (i % (channels * width * height));
EXPECT_EQ(
blob_top_->cpu_data()[i],
blob_bottom_->cpu_data()[perm[n] * channels * width * height
+ inner_idx]);
}
}
};
TYPED_TEST_CASE(BatchReindexLayerTest, TestDtypesAndDevices);
TYPED_TEST(BatchReindexLayerTest, TestForward) {
this->TestForward();
}
TYPED_TEST(BatchReindexLayerTest, TestGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
BatchReindexLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-4, 1e-2);
checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_,
this->blob_top_vec_, 0);
}
} // namespace caffe