GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add pad_type and non-square shape into pooling layers (#374) · tiny-dnn/tiny-dnn@55167af

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test
    • test_average_pooling_layer.h
    • test_max_pooling_layer.h
    • testhelper.h
  • tiny_dnn
    • core
      • backend_nnp.h
      • kernels
        • nnp_maxpool_kernel.h
      • params
        • conv_params.h
        • maxpool_params.h
    • io/caffe
      • layer_factory_impl.h
    • layers
      • average_pooling_layer.h
      • convolutional_layer.h
      • layer.h
      • max_pooling_layer.h
    • util
      • util.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/test_average_pooling_layer.h‎

Copy file name to clipboardExpand all lines: test/test_average_pooling_layer.h
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,50 @@ TEST(ave_pool, gradient_check) { // sigmoid - cross-entropy
4646
EXPECT_TRUE(nn.gradient_check<loss_func>(test_data.first, test_data.second, epsilon<float_t>(), GRAD_CHECK_ALL));
4747
}
4848

49+
TEST(ave_pool, gradient_check2) { // x-stride
50+
typedef cross_entropy loss_func;
51+
typedef activation::sigmoid activation;
52+
typedef network<sequential> network;
53+
54+
network nn;
55+
nn << fully_connected_layer<activation>(3, 8)
56+
<< average_pooling_layer<activation>(4, 2, 1, 2, 1, 2, 1); // 4x2 => 2x2
57+
58+
const auto test_data = generate_gradient_check_data(nn.in_data_size());
59+
nn.init_weight();
60+
61+
EXPECT_TRUE(nn.gradient_check<loss_func>(test_data.first, test_data.second, epsilon<float_t>(), GRAD_CHECK_ALL));
62+
}
63+
64+
TEST(ave_pool, gradient_check3) { // y-stride
65+
typedef cross_entropy loss_func;
66+
typedef activation::sigmoid activation;
67+
typedef network<sequential> network;
68+
69+
network nn;
70+
nn << fully_connected_layer<activation>(3, 8)
71+
<< average_pooling_layer<activation>(4, 2, 1, 1, 2, 1, 2); // 4x2 => 4x1
72+
73+
const auto test_data = generate_gradient_check_data(nn.in_data_size());
74+
nn.init_weight();
75+
76+
EXPECT_TRUE(nn.gradient_check<loss_func>(test_data.first, test_data.second, epsilon<float_t>(), GRAD_CHECK_ALL));
77+
}
78+
79+
TEST(ave_pool, gradient_check4) { // padding-same
80+
typedef cross_entropy loss_func;
81+
typedef activation::sigmoid activation;
82+
typedef network<sequential> network;
83+
84+
network nn;
85+
nn << average_pooling_layer<activation>(4, 2, 1, 2, 2, 1, 1, padding::same); // 4x2 => 4x1
86+
87+
const auto test_data = generate_gradient_check_data(nn.in_data_size());
88+
nn.init_weight();
89+
90+
EXPECT_TRUE(nn.gradient_check<loss_func>(test_data.first, test_data.second, epsilon<float_t>(), GRAD_CHECK_ALL));
91+
}
92+
4993
TEST(ave_pool, forward) {
5094
average_pooling_layer<identity> l(4, 4, 1, 2);
5195
vec_t in = {
@@ -108,4 +152,5 @@ TEST(ave_pool, read_write) {
108152
serialization_test(l1, l2);
109153
}
110154

155+
111156
} // namespace tiny-dnn
Collapse file

‎test/test_max_pooling_layer.h‎

Copy file name to clipboardExpand all lines: test/test_max_pooling_layer.h
+150Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,79 @@ TEST(max_pool, forward_stride_tiny) {
107107
}
108108
}
109109

110+
TEST(max_pool, forward_padding_same) {
111+
max_pooling_layer<identity> l(4, 4, 1, 2, 2, 1, 1, padding::same, core::backend_t::tiny_dnn);
112+
vec_t in = {
113+
0, 1, 2, 3,
114+
8, 7, 5, 6,
115+
4, 3, 1, 2,
116+
0,-1,-2,-3
117+
};
118+
119+
vec_t expected = {
120+
8, 7, 6, 6,
121+
8, 7, 6, 6,
122+
4, 3, 2, 2,
123+
0,-1,-2,-3
124+
};
125+
126+
vec_t res = l.forward({ { in } })[0][0];
127+
128+
for (size_t i = 0; i < expected.size(); i++) {
129+
EXPECT_FLOAT_EQ(expected[i], res[i]);
130+
}
131+
}
132+
133+
TEST(max_pool, forward_stride_x) {
134+
max_pooling_layer<identity> l(4, 4, 1, 2, 1, 2, 1, padding::valid);
135+
vec_t in = {
136+
0, 1, 2, 3,
137+
8, 7, 5, 6,
138+
4, 3, 1, 2,
139+
0,-1,-2,-3
140+
};
141+
142+
vec_t expected = {
143+
1, 3,
144+
8, 6,
145+
4, 2,
146+
0, -2
147+
};
148+
149+
EXPECT_EQ(l.out_shape()[0].width_, 2);
150+
EXPECT_EQ(l.out_shape()[0].height_, 4);
151+
152+
vec_t res = l.forward({ { in } })[0][0];
153+
154+
for (size_t i = 0; i < expected.size(); i++) {
155+
EXPECT_FLOAT_EQ(expected[i], res[i]);
156+
}
157+
}
158+
159+
TEST(max_pool, forward_stride_y) {
160+
max_pooling_layer<identity> l(4, 4, 1, 1, 2, 1, 2, padding::valid);
161+
vec_t in = {
162+
0, 1, 2, 3,
163+
8, 7, 5, 6,
164+
4, 3, 1, 2,
165+
0,-1,-2,-3
166+
};
167+
168+
vec_t expected = {
169+
8, 7, 5, 6,
170+
4, 3, 1, 2
171+
};
172+
173+
EXPECT_EQ(l.out_shape()[0].width_, 4);
174+
EXPECT_EQ(l.out_shape()[0].height_, 2);
175+
176+
vec_t res = l.forward({ { in } })[0][0];
177+
178+
for (size_t i = 0; i < expected.size(); i++) {
179+
EXPECT_FLOAT_EQ(expected[i], res[i]);
180+
}
181+
}
182+
110183
#ifdef CNN_USE_NNPACK
111184
TEST(max_pool, forward_stride_nnp) {
112185
max_pooling_layer<identity> l(4, 4, 1, 2, 2, core::backend_t::nnpack);
@@ -181,4 +254,81 @@ TEST(max_pool, backward) {
181254
}
182255
}
183256

257+
#ifndef CNN_NO_SERIALIZATION
258+
TEST(max_pool, serialization) {
259+
max_pooling_layer<identity> src(4, 4, 1, 2);
260+
261+
std::string str = layer_to_json(src);
262+
263+
auto dst = json_to_layer(str);
264+
265+
EXPECT_EQ(src.in_shape()[0], dst->in_shape()[0]);
266+
EXPECT_EQ(src.out_shape()[0], dst->out_shape()[0]);
267+
268+
vec_t in = {
269+
9, 4, 8, 8,
270+
0, 7, 3, 0,
271+
4, 8, 1, 7,
272+
0, 3,-2, 9
273+
};
274+
275+
vec_t res1 = src.forward({ { in } })[0][0];
276+
vec_t res2 = dst->forward({ { in } })[0][0];
277+
278+
for (size_t i = 0; i < res1.size(); i++) {
279+
EXPECT_FLOAT_EQ(res1[i], res2[i]);
280+
}
281+
}
282+
283+
TEST(max_pool, serialization_stride) {
284+
max_pooling_layer<identity> src(4, 4, 1, 2, 1, 1, 2, padding::valid);
285+
286+
std::string str = layer_to_json(src);
287+
288+
auto dst = json_to_layer(str);
289+
290+
EXPECT_EQ(src.in_shape()[0], dst->in_shape()[0]);
291+
EXPECT_EQ(src.out_shape()[0], dst->out_shape()[0]);
292+
293+
vec_t in = {
294+
9, 4, 8, 8,
295+
0, 7, 3, 0,
296+
4, 8, 1, 7,
297+
0, 3,-2, 9
298+
};
299+
300+
vec_t res1 = src.forward({ { in } })[0][0];
301+
vec_t res2 = dst->forward({ { in } })[0][0];
302+
303+
for (size_t i = 0; i < res1.size(); i++) {
304+
EXPECT_FLOAT_EQ(res1[i], res2[i]);
305+
}
306+
}
307+
308+
TEST(max_pool, serialization_padding) {
309+
max_pooling_layer<identity> src(4, 4, 1, 2, 2, 1, 1, padding::same);
310+
311+
std::string str = layer_to_json(src);
312+
313+
auto dst = json_to_layer(str);
314+
315+
EXPECT_EQ(src.in_shape()[0], dst->in_shape()[0]);
316+
EXPECT_EQ(src.out_shape()[0], dst->out_shape()[0]);
317+
318+
vec_t in = {
319+
9, 4, 8, 8,
320+
0, 7, 3, 0,
321+
4, 8, 1, 7,
322+
0, 3,-2, 9
323+
};
324+
325+
vec_t res1 = src.forward({ { in } })[0][0];
326+
vec_t res2 = dst->forward({ { in } })[0][0];
327+
328+
for (size_t i = 0; i < res1.size(); i++) {
329+
EXPECT_FLOAT_EQ(res1[i], res2[i]);
330+
}
331+
}
332+
#endif
333+
184334
} // namespace tiny_dnn
Collapse file

‎test/testhelper.h‎

Copy file name to clipboardExpand all lines: test/testhelper.h
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,22 @@ namespace {
208208
}
209209
}
210210

211+
#ifndef CNN_NO_SERIALIZATION
212+
inline std::string layer_to_json(const layer& src) {
213+
std::ostringstream os;
214+
{
215+
cereal::JSONOutputArchive oa(os);
216+
layer::save_layer(oa, src);
217+
}
218+
return os.str();
219+
}
220+
221+
inline std::shared_ptr<layer> json_to_layer(const std::string& src) {
222+
std::stringstream ss;
223+
ss << src;
224+
cereal::JSONInputArchive oa(ss);
225+
return layer::load_layer(oa);
226+
}
227+
#endif
228+
211229
} // namespace tiny_dnn
Collapse file

‎tiny_dnn/core/backend_nnp.h‎

Copy file name to clipboardExpand all lines: tiny_dnn/core/backend_nnp.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ class nnp_backend : public backend {
146146

147147
void maxpool(const std::vector<tensor_t*>& in_data,
148148
std::vector<tensor_t*>& out_data) override {
149-
if (params_m_->stride_ != 2) {
149+
if (params_m_->stride_x != 2 || params_m_->stride_y != 2) {
150150
throw nn_error("NNPACK Max-Pool requires a stride == 2.");
151151
}
152152

153-
if (params_m_->pool_size_ != 2) {
153+
if (params_m_->pool_size_x != 2 || params_m_->pool_size_y != 2) {
154154
throw nn_error("NNPACK Max-Pool requires a pool size == 2.");
155155
}
156156

Collapse file

‎tiny_dnn/core/kernels/nnp_maxpool_kernel.h‎

Copy file name to clipboardExpand all lines: tiny_dnn/core/kernels/nnp_maxpool_kernel.h
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ inline void nnp_maxpool_kernel(const maxpool_params& params,
3939
tensor_t& a) {
4040
#ifdef CNN_USE_NNPACK
4141

42-
const cnn_size_t input_channels = params.in_.depth_;
43-
const cnn_size_t output_channels = params.out_.depth_;
42+
const cnn_size_t input_channels = params.in.depth_;
43+
const cnn_size_t output_channels = params.out.depth_;
4444

4545
const nnp_size input_size = {
46-
static_cast<size_t>(params.in_.width_),
47-
static_cast<size_t>(params.in_.height_)
46+
static_cast<size_t>(params.in.width_),
47+
static_cast<size_t>(params.in.height_)
4848
};
4949

5050
const nnp_padding input_padding = {
@@ -55,13 +55,13 @@ inline void nnp_maxpool_kernel(const maxpool_params& params,
5555
};
5656

5757
const nnp_size pooling_size = {
58-
static_cast<size_t>(params.pool_size_),
59-
static_cast<size_t>(params.pool_size_)
58+
static_cast<size_t>(params.pool_size_x),
59+
static_cast<size_t>(params.pool_size_y)
6060
};
6161

6262
const nnp_size pooling_stride = {
63-
static_cast<size_t>(params.stride_),
64-
static_cast<size_t>(params.stride_)
63+
static_cast<size_t>(params.stride_x),
64+
static_cast<size_t>(params.stride_y)
6565
};
6666

6767
const float* input_ptr = reinterpret_cast<const float*>(&in[0]);
Collapse file

‎tiny_dnn/core/params/conv_params.h‎

Copy file name to clipboardExpand all lines: tiny_dnn/core/params/conv_params.h
+2-7Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
namespace tiny_dnn {
3232
namespace core {
3333

34-
enum class padding {
35-
valid, ///< use valid pixels of input
36-
same ///< add zero-padding around input so as to keep image size
37-
};
38-
3934
struct conv_layer_worker_specific_storage {
4035
std::vector<const vec_t*> prev_out_padded_;
4136
std::vector<vec_t> prev_out_buf_;
@@ -134,7 +129,7 @@ class Conv2dPadding {
134129
* @param out The output tensor with padding applied
135130
*/
136131
void copy_and_pad_input(const tensor_t& in, tensor_t& out) {
137-
if (params_.pad_type == core::padding::valid) {
132+
if (params_.pad_type == padding::valid) {
138133
return;
139134
}
140135

@@ -169,7 +164,7 @@ class Conv2dPadding {
169164
* @param out The output tensor with unpadding applied
170165
*/
171166
void copy_and_unpad_delta(const tensor_t& delta, tensor_t& delta_unpadded) {
172-
if (params_.pad_type == core::padding::valid) {
167+
if (params_.pad_type == padding::valid) {
173168
return;
174169
}
175170

Collapse file

‎tiny_dnn/core/params/maxpool_params.h‎

Copy file name to clipboardExpand all lines: tiny_dnn/core/params/maxpool_params.h
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@
2525
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*/
2727
#pragma once
28+
#include "tiny_dnn/core/params/params.h"
2829

2930
namespace tiny_dnn {
3031
namespace core {
3132

3233
struct maxpool_params {
33-
index3d<cnn_size_t> in_;
34-
index3d<cnn_size_t> out_;
35-
size_t pool_size_;
36-
size_t stride_;
34+
index3d<cnn_size_t> in;
35+
index3d<cnn_size_t> out;
36+
cnn_size_t pool_size_x;
37+
cnn_size_t pool_size_y;
38+
size_t stride_x;
39+
size_t stride_y;
40+
padding pad_type;
3741
};
3842

3943
struct max_pooling_layer_worker_specific_storage {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.