/
test_deconvolution_layer.cpp
304 lines (289 loc) · 11.9 KB
/
test_deconvolution_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layers/deconv_layer.hpp"
#include "caffe/test/test_caffe_main.hpp"
#include "caffe/test/test_gradient_check_util.hpp"
namespace caffe {
// Since ConvolutionLayerTest checks the shared conv/deconv code in detail,
// we'll just do a simple forward test and a gradient check.
template <typename TypeParam>
class DeconvolutionLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
DeconvolutionLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 6, 4)),
blob_bottom_2_(new Blob<Dtype>(2, 3, 6, 4)),
blob_top_(new Blob<Dtype>()),
blob_top_2_(new Blob<Dtype>()) {}
virtual void SetUp() {
// fill the values
FillerParameter filler_param;
filler_param.set_value(1.);
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
filler.Fill(this->blob_bottom_2_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~DeconvolutionLayerTest() {
delete blob_bottom_;
delete blob_bottom_2_;
delete blob_top_;
delete blob_top_2_;
}
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_bottom_2_;
Blob<Dtype>* const blob_top_;
Blob<Dtype>* const blob_top_2_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(DeconvolutionLayerTest, TestDtypesAndDevices);
TYPED_TEST(DeconvolutionLayerTest, TestSetup) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ConvolutionParameter* convolution_param =
layer_param.mutable_convolution_param();
convolution_param->add_kernel_size(3);
convolution_param->add_stride(2);
convolution_param->set_num_output(4);
this->blob_bottom_vec_.push_back(this->blob_bottom_2_);
this->blob_top_vec_.push_back(this->blob_top_2_);
shared_ptr<Layer<Dtype> > layer(
new DeconvolutionLayer<Dtype>(layer_param));
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_->num(), 2);
EXPECT_EQ(this->blob_top_->channels(), 4);
EXPECT_EQ(this->blob_top_->height(), 13);
EXPECT_EQ(this->blob_top_->width(), 9);
EXPECT_EQ(this->blob_top_2_->num(), 2);
EXPECT_EQ(this->blob_top_2_->channels(), 4);
EXPECT_EQ(this->blob_top_2_->height(), 13);
EXPECT_EQ(this->blob_top_2_->width(), 9);
// setting group should not change the shape
convolution_param->set_num_output(3);
convolution_param->set_group(3);
layer.reset(new DeconvolutionLayer<Dtype>(layer_param));
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_->num(), 2);
EXPECT_EQ(this->blob_top_->channels(), 3);
EXPECT_EQ(this->blob_top_->height(), 13);
EXPECT_EQ(this->blob_top_->width(), 9);
EXPECT_EQ(this->blob_top_2_->num(), 2);
EXPECT_EQ(this->blob_top_2_->channels(), 3);
EXPECT_EQ(this->blob_top_2_->height(), 13);
EXPECT_EQ(this->blob_top_2_->width(), 9);
}
TYPED_TEST(DeconvolutionLayerTest, TestSimpleDeconvolution) {
typedef typename TypeParam::Dtype Dtype;
this->blob_bottom_vec_.push_back(this->blob_bottom_2_);
this->blob_top_vec_.push_back(this->blob_top_2_);
LayerParameter layer_param;
ConvolutionParameter* convolution_param =
layer_param.mutable_convolution_param();
convolution_param->add_kernel_size(3);
convolution_param->add_stride(2);
convolution_param->set_num_output(4);
convolution_param->mutable_weight_filler()->set_type("constant");
convolution_param->mutable_weight_filler()->set_value(1);
convolution_param->mutable_bias_filler()->set_type("constant");
convolution_param->mutable_bias_filler()->set_value(0.1);
shared_ptr<Layer<Dtype> > layer(
new DeconvolutionLayer<Dtype>(layer_param));
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
// constant-fill the bottom blobs
FillerParameter filler_param;
filler_param.set_value(1.);
ConstantFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
filler.Fill(this->blob_bottom_2_);
layer->Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// simply check that accumulation works with overlapping filters
const Dtype* top_data = this->blob_top_->cpu_data();
for (int n = 0; n < this->blob_top_->num(); ++n) {
for (int c = 0; c < this->blob_top_->channels(); ++c) {
for (int h = 0; h < this->blob_top_->height(); ++h) {
for (int w = 0; w < this->blob_top_->width(); ++w) {
Dtype expected = 3.1;
bool h_overlap = h % 2 == 0 && h > 0
&& h < this->blob_top_->height() - 1;
bool w_overlap = w % 2 == 0 && w > 0
&& w < this->blob_top_->width() - 1;
if (h_overlap && w_overlap) {
expected += 9;
} else if (h_overlap || w_overlap) {
expected += 3;
}
EXPECT_NEAR(top_data[this->blob_top_->offset(n, c, h, w)],
expected, 1e-4);
}
}
}
}
}
TYPED_TEST(DeconvolutionLayerTest, TestGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
ConvolutionParameter* convolution_param =
layer_param.mutable_convolution_param();
this->blob_bottom_vec_.push_back(this->blob_bottom_2_);
this->blob_top_vec_.push_back(this->blob_top_2_);
convolution_param->add_kernel_size(2);
convolution_param->add_stride(1);
convolution_param->set_num_output(1);
convolution_param->mutable_weight_filler()->set_type("gaussian");
convolution_param->mutable_bias_filler()->set_type("gaussian");
DeconvolutionLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
TYPED_TEST(DeconvolutionLayerTest, TestNDAgainst2D) {
typedef typename TypeParam::Dtype Dtype;
const int kernel_h = 11;
const int kernel_w = 13;
vector<int> bottom_shape(4);
bottom_shape[0] = 15;
bottom_shape[1] = 12;
bottom_shape[2] = kernel_h * 2;
bottom_shape[3] = kernel_w * 2;
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
for (int i = 0; i < this->blob_bottom_vec_.size(); ++i) {
this->blob_bottom_vec_[i]->Reshape(bottom_shape);
filler.Fill(this->blob_bottom_vec_[i]);
}
LayerParameter layer_param;
ConvolutionParameter* convolution_param =
layer_param.mutable_convolution_param();
convolution_param->set_num_output(18);
convolution_param->set_bias_term(false);
convolution_param->set_group(6);
convolution_param->set_kernel_h(kernel_h);
convolution_param->set_kernel_w(kernel_w);
convolution_param->mutable_weight_filler()->set_type("gaussian");
Blob<Dtype> weights;
Blob<Dtype> top_diff;
// Shape and fill weights and top_diff.
bool copy_diff;
bool reshape;
{
DeconvolutionLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
top_diff.ReshapeLike(*this->blob_top_);
filler.Fill(&top_diff);
ASSERT_EQ(1, layer.blobs().size());
copy_diff = false; reshape = true;
weights.CopyFrom(*layer.blobs()[0], copy_diff, reshape);
}
vector<bool> propagate_down(1, true);
Blob<Dtype> result_2d;
Blob<Dtype> backward_result_2d;
Blob<Dtype> backward_weight_result_2d;
// Test with 2D im2col
{
caffe_set(this->blob_top_->count(), Dtype(0),
this->blob_top_->mutable_cpu_data());
caffe_set(this->blob_bottom_->count(), Dtype(0),
this->blob_bottom_->mutable_cpu_diff());
caffe_set(weights.count(), Dtype(0), weights.mutable_cpu_diff());
// Do SetUp and Forward; save Forward result in result_2d.
convolution_param->set_force_nd_im2col(false);
DeconvolutionLayer<Dtype> layer_2d(layer_param);
layer_2d.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
ASSERT_EQ(1, layer_2d.blobs().size());
copy_diff = false; reshape = false;
layer_2d.blobs()[0]->CopyFrom(weights, copy_diff, reshape);
layer_2d.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
copy_diff = false; reshape = true;
result_2d.CopyFrom(*this->blob_top_, copy_diff, reshape);
// Copy pre-generated top diff into actual top diff;
// do Backward and save result in backward_result_2d.
ASSERT_EQ(this->blob_top_->shape(), top_diff.shape());
caffe_copy(top_diff.count(), top_diff.cpu_data(),
this->blob_top_->mutable_cpu_diff());
layer_2d.Backward(this->blob_top_vec_, propagate_down,
this->blob_bottom_vec_);
copy_diff = true; reshape = true;
backward_result_2d.CopyFrom(*this->blob_bottom_, copy_diff, reshape);
backward_weight_result_2d.CopyFrom(weights, copy_diff, reshape);
}
Blob<Dtype> result_nd;
Blob<Dtype> backward_result_nd;
Blob<Dtype> backward_weight_result_nd;
// Test with ND im2col
{
caffe_set(this->blob_top_->count(), Dtype(0),
this->blob_top_->mutable_cpu_data());
caffe_set(this->blob_bottom_->count(), Dtype(0),
this->blob_bottom_->mutable_cpu_diff());
caffe_set(weights.count(), Dtype(0), weights.mutable_cpu_diff());
// Do SetUp and Forward; save Forward result in result_nd.
convolution_param->set_force_nd_im2col(true);
DeconvolutionLayer<Dtype> layer_nd(layer_param);
layer_nd.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
ASSERT_EQ(1, layer_nd.blobs().size());
copy_diff = false; reshape = false;
layer_nd.blobs()[0]->CopyFrom(weights, copy_diff, reshape);
layer_nd.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
copy_diff = false; reshape = true;
result_nd.CopyFrom(*this->blob_top_, copy_diff, reshape);
// Copy pre-generated top diff into actual top diff;
// do Backward and save result in backward_result_nd.
ASSERT_EQ(this->blob_top_->shape(), top_diff.shape());
caffe_copy(top_diff.count(), top_diff.cpu_data(),
this->blob_top_->mutable_cpu_diff());
layer_nd.Backward(this->blob_top_vec_, propagate_down,
this->blob_bottom_vec_);
copy_diff = true; reshape = true;
backward_result_nd.CopyFrom(*this->blob_bottom_, copy_diff, reshape);
backward_weight_result_nd.CopyFrom(weights, copy_diff, reshape);
}
ASSERT_EQ(result_nd.count(), result_2d.count());
for (int i = 0; i < result_2d.count(); ++i) {
EXPECT_EQ(result_2d.cpu_data()[i], result_nd.cpu_data()[i]);
}
ASSERT_EQ(backward_result_nd.count(), backward_result_2d.count());
for (int i = 0; i < backward_result_2d.count(); ++i) {
EXPECT_EQ(backward_result_2d.cpu_diff()[i],
backward_result_nd.cpu_diff()[i]);
}
ASSERT_EQ(backward_weight_result_nd.count(),
backward_weight_result_2d.count());
for (int i = 0; i < backward_weight_result_2d.count(); ++i) {
EXPECT_EQ(backward_weight_result_2d.cpu_diff()[i],
backward_weight_result_nd.cpu_diff()[i]);
}
}
TYPED_TEST(DeconvolutionLayerTest, TestGradient3D) {
typedef typename TypeParam::Dtype Dtype;
vector<int> bottom_shape(5);
bottom_shape[0] = this->blob_bottom_vec_[0]->shape(0);
bottom_shape[1] = this->blob_bottom_vec_[0]->shape(1);
bottom_shape[2] = 2;
bottom_shape[3] = 3;
bottom_shape[4] = 2;
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
for (int i = 0; i < this->blob_bottom_vec_.size(); ++i) {
this->blob_bottom_vec_[i]->Reshape(bottom_shape);
filler.Fill(this->blob_bottom_vec_[i]);
}
LayerParameter layer_param;
ConvolutionParameter* convolution_param =
layer_param.mutable_convolution_param();
convolution_param->add_kernel_size(2);
convolution_param->add_stride(2);
convolution_param->add_pad(1);
convolution_param->set_num_output(2);
convolution_param->mutable_weight_filler()->set_type("gaussian");
convolution_param->mutable_bias_filler()->set_type("gaussian");
DeconvolutionLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-3);
checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
} // namespace caffe