-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_pooling.cc
140 lines (129 loc) · 4.8 KB
/
test_pooling.cc
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
/************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
#include "../src/model/layer/pooling.h"
#include "gtest/gtest.h"
using singa::Pooling;
using singa::Shape;
TEST(Pooling, Setup) {
Pooling pool;
// EXPECT_EQ("Pooling", pool.layer_type());
singa::LayerConf conf;
singa::PoolingConf *poolconf = conf.mutable_pooling_conf();
poolconf->set_pool(singa::PoolingConf_PoolMethod_MAX);
poolconf->set_kernel_h(1);
poolconf->set_kernel_w(2);
poolconf->set_pad_h(1);
poolconf->set_pad_w(0);
poolconf->set_stride_h(2);
poolconf->set_stride_w(1);
pool.Setup(Shape{1, 3, 3}, conf);
EXPECT_EQ(singa::PoolingConf_PoolMethod_MAX, pool.pool_method());
EXPECT_EQ(1u, pool.kernel_h());
EXPECT_EQ(2u, pool.kernel_w());
EXPECT_EQ(1u, pool.pad_h());
EXPECT_EQ(0u, pool.pad_w());
EXPECT_EQ(2u, pool.stride_h());
EXPECT_EQ(1u, pool.stride_w());
EXPECT_EQ(1u, pool.channels());
EXPECT_EQ(3u, pool.height());
EXPECT_EQ(3u, pool.width());
}
TEST(Pooling, Forward) {
const size_t batchsize = 2, c = 1, h = 3, w = 3;
const float x[batchsize * c * h * w] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
7.0f, 8.0f, 9.0f, 1.0f, 2.0f, 3.0f,
4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
singa::Tensor in(singa::Shape{batchsize, c, h, w});
in.CopyDataFromHostPtr(x, batchsize * c * h * w);
Pooling pool;
singa::LayerConf conf;
singa::PoolingConf *poolconf = conf.mutable_pooling_conf();
poolconf->set_pool(singa::PoolingConf_PoolMethod_MAX);
poolconf->set_kernel_h(2);
poolconf->set_kernel_w(2);
poolconf->set_pad_h(0);
poolconf->set_pad_w(0);
poolconf->set_stride_h(1);
poolconf->set_stride_w(1);
pool.Setup(Shape{1, 3, 3}, conf);
// Parameter "flag" does not influence pooling
singa::Tensor out1 = pool.Forward(singa::kTrain, in);
const float *outptr1 = out1.data<float>();
// Input: 3*3; kernel: 2*2; stride: 1*1; no padding.
EXPECT_EQ(8u, out1.Size());
EXPECT_EQ(5.0f, outptr1[0]);
EXPECT_EQ(6.0f, outptr1[1]);
EXPECT_EQ(8.0f, outptr1[2]);
EXPECT_EQ(9.0f, outptr1[3]);
EXPECT_EQ(5.0f, outptr1[4]);
EXPECT_EQ(6.0f, outptr1[5]);
EXPECT_EQ(8.0f, outptr1[6]);
EXPECT_EQ(9.0f, outptr1[7]);
}
TEST(Pooling, Backward) {
// src_data
const size_t batchsize = 2, c = 1, src_h = 3, src_w = 3;
const float x[batchsize * c * src_h * src_w] = {
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
singa::Tensor in(singa::Shape{batchsize, c, src_h, src_w});
in.CopyDataFromHostPtr(x, batchsize * c * src_h * src_w);
Pooling pool;
singa::LayerConf conf;
singa::PoolingConf *poolconf = conf.mutable_pooling_conf();
poolconf->set_pool(singa::PoolingConf_PoolMethod_MAX);
poolconf->set_kernel_h(2);
poolconf->set_kernel_w(2);
poolconf->set_pad_h(0);
poolconf->set_pad_w(0);
poolconf->set_stride_h(1);
poolconf->set_stride_w(1);
pool.Setup(Shape{1, 3, 3}, conf);
singa::Tensor out1 = pool.Forward(singa::kTrain, in);
// grad
const size_t grad_h = 2, grad_w = 2;
const float dy[batchsize * c * grad_h * grad_w] = {0.1f, 0.2f, 0.3f, 0.4f,
0.1f, 0.2f, 0.3f, 0.4f};
singa::Tensor grad(singa::Shape{batchsize, c, grad_h, grad_w});
grad.CopyDataFromHostPtr(dy, batchsize * c * grad_h * grad_w);
const auto ret = pool.Backward(singa::kTrain, grad);
singa::Tensor in_grad = ret.first;
const float *dx = in_grad.data<float>();
EXPECT_EQ(18u, in_grad.Size());
EXPECT_EQ(0.0f, dx[0]);
EXPECT_EQ(0.0f, dx[1]);
EXPECT_EQ(0.0f, dx[2]);
EXPECT_EQ(0.0f, dx[3]);
EXPECT_EQ(0.1f, dx[4]);
EXPECT_EQ(0.2f, dx[5]);
EXPECT_EQ(0.0f, dx[6]);
EXPECT_EQ(0.3f, dx[7]);
EXPECT_EQ(0.4f, dx[8]);
EXPECT_EQ(0.0f, dx[9]);
EXPECT_EQ(0.0f, dx[10]);
EXPECT_EQ(0.0f, dx[11]);
EXPECT_EQ(0.0f, dx[12]);
EXPECT_EQ(0.1f, dx[13]);
EXPECT_EQ(0.2f, dx[14]);
EXPECT_EQ(0.0f, dx[15]);
EXPECT_EQ(0.3f, dx[16]);
EXPECT_EQ(0.4f, dx[17]);
}