Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/unit/nnc/forward.tests.c
Line
Count
Source
1
#include "case.h"
2
#include "ccv_case.h"
3
#include <ccv.h>
4
#include <nnc/ccv_nnc.h>
5
#include <nnc/ccv_nnc_easy.h>
6
#include "3rdparty/dsfmt/dSFMT.h"
7
8
TEST_SETUP()
9
{
10
  ccv_nnc_init();
11
}
12
13
TEST_CASE("convolutional network of 11x11 on 225x185 with uniform weights")
14
1
{
15
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 225, 185, 3), 0);
16
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 55, 45, 4), 0);
17
1
  ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, 4, 11, 11, 3);
18
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
19
1
  ccv_nnc_tensor_t* w = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 11, 11, 3), 0);
20
1
  ccv_nnc_tensor_t* bias = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4), 0);
21
1
  // configure the inlets.
22
1
  int i;
23
1.45k
  for (i = 0; i < 11 * 11 * 3 * 4; 
i++1.45k
)
24
1.45k
    w->data.f32[i] = 1;
25
124k
  for (i = 0; i < 225 * 185 * 3; 
i++124k
)
26
124k
    a->data.f32[i] = 1;
27
5
  for (i = 0; i < 4; 
i++4
)
28
4
    bias->data.f32[i] = 0;
29
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w, bias), TENSOR_LIST(b), 0);
30
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(55, 45, CCV_32F | 4, 0, 0);
31
1
  int x, y;
32
56
  for (y = 0; y < 55; 
y++55
)
33
2.53k
    
for (x = 0; 55
x < 45;
x++2.47k
)
34
12.3k
      
for (i = 0; 2.47k
i < 4;
i++9.90k
)
35
9.90k
      c->data.f32[(y * 45 + x) * 4 + i] = ((x == 0 && 
y == 0220
) ||
(9.89k
x == 09.89k
&&
y == 54216
) ||
(9.89k
x == 449.89k
&&
y == 0220
) ||
(9.88k
x == 449.88k
&&
y == 54216
)) ?
30016
:
((9.88k
x == 09.88k
||
y == 09.67k
||
x == 449.50k
||
y == 549.28k
) ?
330768
:
3639.11k
);
36
1
  REQUIRE_MATRIX_EQ(b, c, "55x45 matrix should be exactly a matrix fill 363, with 300 on the corner and 330 on the border");
37
1
  ccv_matrix_free(c);
38
1
  ccv_nnc_tensor_free(bias);
39
1
  ccv_nnc_tensor_free(w);
40
1
  ccv_nnc_tensor_free(b);
41
1
  ccv_nnc_tensor_free(a);
42
1
}
43
44
TEST_CASE("convolutional network of 5x3 on 17x27 with uniform weights")
45
1
{
46
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 17, 27, 1), 0);
47
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 17, 27, 4), 0);
48
1
  ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, 4, 5, 3, 1);
49
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
50
1
  ccv_nnc_tensor_t* w = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 5, 3, 1), 0);
51
1
  ccv_nnc_tensor_t* bias = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4), 0);
52
1
  // configure the inlets.
53
1
  int i;
54
61
  for (i = 0; i < 5 * 3 * 4; 
i++60
)
55
60
    w->data.f32[i] = 1;
56
460
  for (i = 0; i < 17 * 27; 
i++459
)
57
459
    a->data.f32[i] = 1;
58
5
  for (i = 0; i < 4; 
i++4
)
59
4
    bias->data.f32[i] = 0;
60
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w, bias), TENSOR_LIST(b), 0);
61
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(17, 27, CCV_32F | 4, 0, 0);
62
1
  int x, y;
63
18
  for (y = 0; y < 17; 
y++17
)
64
476
    
for (x = 0; 17
x < 27;
x++459
)
65
2.29k
      
for (i = 0; 459
i < 4;
i++1.83k
)
66
1.83k
      {
67
1.83k
        if ((x == 0 && 
y == 068
) ||
(1.83k
x == 01.83k
&&
y == 1664
) ||
(1.82k
x == 261.82k
&&
y == 068
) ||
(1.82k
x == 261.82k
&&
y == 1664
))
68
16
          c->data.f32[(y * 27 + x) * 4 + i] = 6;
69
1.82k
        else if ((x == 0 && 
y == 160
) ||
(1.81k
x == 261.81k
&&
y == 160
) ||
(1.81k
x == 01.81k
&&
y == 1556
) ||
(1.80k
x == 261.80k
&&
y == 1556
))
70
16
          c->data.f32[(y * 27 + x) * 4 + i] = 8;
71
1.80k
        else if (y == 0 || 
y == 161.70k
)
72
200
          c->data.f32[(y * 27 + x) * 4 + i] = 9;
73
1.60k
        else if (x == 0 || 
x == 261.55k
)
74
104
          c->data.f32[(y * 27 + x) * 4 + i] = 10;
75
1.50k
        else if (y == 1 || 
y == 151.40k
)
76
200
          c->data.f32[(y * 27 + x) * 4 + i] = 12;
77
1.30k
        else
78
1.30k
          c->data.f32[(y * 27 + x) * 4 + i] = 15;
79
1.83k
      }
80
1
  REQUIRE_MATRIX_EQ(b, c, "17x27 matrix should be exactly a matrix fill 15, with 6, 8 on the corner and 9, 10, 12 on the border");
81
1
  ccv_matrix_free(c);
82
1
  ccv_nnc_tensor_free(bias);
83
1
  ccv_nnc_tensor_free(w);
84
1
  ccv_nnc_tensor_free(b);
85
1
  ccv_nnc_tensor_free(a);
86
1
}
87
88
TEST_CASE("convolutional network of 11x11 on 225x185 with non-uniform weights")
89
1
{
90
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 225, 185, 1), 0);
91
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 55, 45, 4), 0);
92
1
  ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, 4, 11, 11, 1);
93
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
94
1
  ccv_nnc_tensor_t* w = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 11, 11, 1), 0);
95
1
  ccv_nnc_tensor_t* bias = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4), 0);
96
1
  // configure the inlets.
97
1
  int i, x, y;
98
5
  for (x = 0; x < 4; 
x++4
)
99
488
    
for (i = 0; 4
i < 11 * 11;
i++484
)
100
484
      w->data.f32[x * 11 * 11 + i] = i + 1;
101
41.6k
  for (i = 0; i < 225 * 185; 
i++41.6k
)
102
41.6k
    a->data.f32[i] = i + 1;
103
5
  for (i = 0; i < 4; 
i++4
)
104
4
    bias->data.f32[i] = 0;
105
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w, bias), TENSOR_LIST(b), 0);
106
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(55, 45, CCV_32F | 4, 0, 0);
107
1
  float sum = 0;
108
1
  // first column
109
11
  for (y = 0; y < 10; 
y++10
)
110
110
    
for (x = 0; 10
x < 10;
x++100
)
111
100
      sum += ((y + 1) * 11 + x + 2) * (y * 185 + x + 1);
112
5
  for (i = 0; i < 4; 
i++4
)
113
4
    c->data.f32[i] = sum;
114
1
  sum = 0;
115
11
  for (y = 0; y < 10; 
y++10
)
116
120
    
for (x = 0; 10
x < 11;
x++110
)
117
110
      sum += ((y + 1) * 11 + x + 1) * (y * 185 + (x + 3) + 1);
118
44
  for (x = 1; x < 44; 
x++43
)
119
215
    
for (i = 0; 43
i < 4;
i++172
)
120
172
      c->data.f32[x * 4 + i] = sum + (x - 1) * 4 * (11 * 11 + 12) * 11 * 10 / 2;
121
1
  sum = 0;
122
11
  for (y = 0; y < 10; 
y++10
)
123
110
    
for (x = 0; 10
x < 10;
x++100
)
124
100
      sum += ((y + 1) * 11 + x + 1) * (y * 185 + (x + 175) + 1);
125
5
  for (i = 0; i < 4; 
i++4
)
126
4
    c->data.f32[44 * 4 + i] = sum;
127
1
  // last column
128
1
  sum = 0;
129
11
  for (y = 0; y < 10; 
y++10
)
130
110
    
for (x = 0; 10
x < 10;
x++100
)
131
100
      sum += (y * 11 + x + 2) * ((y + 215) * 185 + x + 1);
132
5
  for (i = 0; i < 4; 
i++4
)
133
4
    c->data.f32[54 * 45 * 4 + i] = sum;
134
1
  sum = 0;
135
11
  for (y = 0; y < 10; 
y++10
)
136
120
    
for (x = 0; 10
x < 11;
x++110
)
137
110
      sum += (y * 11 + x + 1) * ((y + 215) * 185 + (x + 3) + 1);
138
44
  for (x = 1; x < 44; 
x++43
)
139
215
    
for (i = 0; 43
i < 4;
i++172
)
140
172
      c->data.f32[(54 * 45 + x) * 4 + i] = sum + (x - 1) * 4 * (10 * 11 + 1) * 11 * 10 / 2;
141
1
  sum = 0;
142
11
  for (y = 0; y < 10; 
y++10
)
143
110
    
for (x = 0; 10
x < 10;
x++100
)
144
100
      sum += (y * 11 + x + 1) * ((y + 215) * 185 + (x + 175) + 1);
145
5
  for (i = 0; i < 4; 
i++4
)
146
4
    c->data.f32[(54 * 45 + 44) * 4 + i] = sum;
147
1
  float border[] = {
148
1
    0, 0
149
1
  };
150
12
  for (y = 0; y < 11; 
y++11
)
151
121
    
for (x = 0; 11
x < 10;
x++110
)
152
110
      border[0] += (y * 11 + x + 2) * ((y + 3) * 185 + x + 1);
153
12
  for (y = 0; y < 11; 
y++11
)
154
121
    
for (x = 0; 11
x < 10;
x++110
)
155
110
      border[1] += (y * 11 + x + 1) * ((y + 3) * 185 + (x + 175) + 1);
156
1
  sum = 0;
157
12
  for (y = 0; y < 11; 
y++11
)
158
132
    
for (x = 0; 11
x < 11;
x++121
)
159
121
      sum += (y * 11 + x + 1) * ((y + 3) * 185 + (x + 3) + 1);
160
54
  for (y = 1; y < 54; 
y++53
)
161
53
  {
162
265
    for (i = 0; i < 4; 
i++212
)
163
212
      c->data.f32[y * 45 * 4 + i] = border[0];
164
2.33k
    for (x = 1; x < 44; 
x++2.27k
)
165
11.3k
      
for (i = 0; 2.27k
i < 4;
i++9.11k
)
166
9.11k
        c->data.f32[(y * 45 + x) * 4 + i] = sum + (x - 1) * 4 * (11 * 11 + 1) * 11 * 11 / 2;
167
265
    for (i = 0; i < 4; 
i++212
)
168
212
      c->data.f32[(y * 45 + 44) * 4 + i] = border[1];
169
53
    sum += 185 * 4 * (11 * 11 + 1) * 11 * 11 / 2;
170
53
    border[0] += 185 * 4 * ((11 * 11 + 1) * 11 * 11 / 2 - (10 * 11 + 1 + 1) * 11 / 2);
171
53
    border[1] += 185 * 4 * ((11 * 11 + 1) * 11 * 11 / 2 - (11 * 11 + 11) * 11 / 2);
172
53
  }
173
1
  // regularize the output so it is within the tolerance
174
9.90k
  for (i = 0; i < 55 * 45 * 4; 
i++9.90k
)
175
9.90k
    c->data.f32[i] = c->data.f32[i] * 1e-7, b->data.f32[i] = b->data.f32[i] * 1e-7;
176
1
  REQUIRE_MATRIX_EQ(b, c, "55x55 matrix should be exactly the same");
177
1
  ccv_matrix_free(c);
178
1
  ccv_nnc_tensor_free(bias);
179
1
  ccv_nnc_tensor_free(w);
180
1
  ccv_nnc_tensor_free(b);
181
1
  ccv_nnc_tensor_free(a);
182
1
}
183
184
TEST_CASE("convolutional network of 3x5 on 27x27 with non-uniform weights")
185
1
{
186
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
187
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 4), 0);
188
1
  ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, 4, 3, 5, 1);
189
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
190
1
  ccv_nnc_tensor_t* w = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 3, 5, 1), 0);
191
1
  ccv_nnc_tensor_t* bias = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4), 0);
192
1
  // configure the inlets.
193
1
  int i, x, y;
194
5
  for (x = 0; x < 4; 
x++4
)
195
64
    
for (i = 0; 4
i < 3 * 5;
i++60
)
196
60
      w->data.f32[x * 3 * 5 + i] = i + 1;
197
730
  for (i = 0; i < 27 * 27; 
i++729
)
198
729
    a->data.f32[i] = i + 1;
199
5
  for (i = 0; i < 4; 
i++4
)
200
4
    bias->data.f32[i] = 0;
201
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w, bias), TENSOR_LIST(b), 0);
202
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(27, 27, CCV_32F | 4, 0, 0);
203
1
  // the first column
204
1
  float sum = 0;
205
3
  for (y = 0; y < 2; 
y++2
)
206
8
    
for (x = 0; 2
x < 3;
x++6
)
207
6
      sum += ((y + 1) * 5 + x + 3) * (y * 27 + x + 1);
208
5
  for (i = 0; i < 4; 
i++4
)
209
4
    c->data.f32[i] = sum;
210
1
  sum = 0;
211
3
  for (y = 0; y < 2; 
y++2
)
212
10
    
for (x = 0; 2
x < 4;
x++8
)
213
8
      sum += ((y + 1) * 5 + x + 2) * (y * 27 + x + 1);
214
5
  for (i = 0; i < 4; 
i++4
)
215
4
    c->data.f32[4 + i] = sum;
216
1
  sum = 0;
217
3
  for (y = 0; y < 2; 
y++2
)
218
12
    
for (x = 0; 2
x < 5;
x++10
)
219
10
      sum += ((y + 1) * 5 + x + 1) * (y * 27 + x + 1);
220
24
  for (x = 2; x < 25; 
x++23
)
221
115
    
for (i = 0; 23
i < 4;
i++92
)
222
92
      c->data.f32[x * 4 + i] = sum + (x - 2) * 21 * 10 / 2;
223
1
  sum = 0;
224
3
  for (y = 0; y < 2; 
y++2
)
225
10
    
for (x = 0; 2
x < 4;
x++8
)
226
8
      sum += ((y + 1) * 5 + x + 1) * (y * 27 + x + 24);
227
5
  for (i = 0; i < 4; 
i++4
)
228
4
    c->data.f32[25 * 4 + i] = sum;
229
1
  sum = 0;
230
3
  for (y = 0; y < 2; 
y++2
)
231
8
    
for (x = 0; 2
x < 3;
x++6
)
232
6
      sum += ((y + 1) * 5 + x + 1) * (y * 27 + x + 25);
233
5
  for (i = 0; i < 4; 
i++4
)
234
4
    c->data.f32[26 * 4 + i] = sum;
235
1
  // the last column
236
1
  sum = 0;
237
3
  for (y = 0; y < 2; 
y++2
)
238
8
    
for (x = 0; 2
x < 3;
x++6
)
239
6
      sum += (y * 5 + x + 3) * ((y + 25) * 27 + x + 1);
240
5
  for (i = 0; i < 4; 
i++4
)
241
4
    c->data.f32[27 * 26 * 4 + i] = sum;
242
1
  sum = 0;
243
3
  for (y = 0; y < 2; 
y++2
)
244
10
    
for (x = 0; 2
x < 4;
x++8
)
245
8
      sum += (y * 5 + x + 2) * ((y + 25) * 27 + x + 1);
246
5
  for (i = 0; i < 4; 
i++4
)
247
4
    c->data.f32[(27 * 26 + 1) * 4 + i] = sum;
248
1
  sum = 0;
249
3
  for (y = 0; y < 2; 
y++2
)
250
12
    
for (x = 0; 2
x < 5;
x++10
)
251
10
      sum += (y * 5 + x + 1) * ((y + 25) * 27 + x + 1);
252
24
  for (x = 2; x < 25; 
x++23
)
253
115
    
for (i = 0; 23
i < 4;
i++92
)
254
92
      c->data.f32[(27 * 26 + x) * 4 + i] = sum + (x - 2) * 11 * 10 / 2;
255
1
  sum = 0;
256
3
  for (y = 0; y < 2; 
y++2
)
257
10
    
for (x = 0; 2
x < 4;
x++8
)
258
8
      sum += (y * 5 + x + 1) * ((y + 25) * 27 + x + 24);
259
5
  for (i = 0; i < 4; 
i++4
)
260
4
    c->data.f32[(27 * 26 + 25) * 4 + i] = sum;
261
1
  sum = 0;
262
3
  for (y = 0; y < 2; 
y++2
)
263
8
    
for (x = 0; 2
x < 3;
x++6
)
264
6
      sum += (y * 5 + x + 1) * ((y + 25) * 27 + x + 25);
265
5
  for (i = 0; i < 4; 
i++4
)
266
4
    c->data.f32[(27 * 26 + 26) * 4 + i] = sum;
267
1
  float border[] = {
268
1
    0, 0, 0, 0
269
1
  };
270
4
  for (y = 0; y < 3; 
y++3
)
271
12
    
for (x = 0; 3
x < 3;
x++9
)
272
9
      border[0] += (y * 5 + x + 3) * (y * 27 + x + 1);
273
4
  for (y = 0; y < 3; 
y++3
)
274
15
    
for (x = 0; 3
x < 4;
x++12
)
275
12
      border[1] += (y * 5 + x + 2) * (y * 27 + x + 1);
276
4
  for (y = 0; y < 3; 
y++3
)
277
15
    
for (x = 0; 3
x < 4;
x++12
)
278
12
      border[2] += (y * 5 + x + 1) * (y * 27 + x + 24);
279
4
  for (y = 0; y < 3; 
y++3
)
280
12
    
for (x = 0; 3
x < 3;
x++9
)
281
9
      border[3] += (y * 5 + x + 1) * (y * 27 + x + 25);
282
1
  sum = 0;
283
4
  for (y = 0; y < 3; 
y++3
)
284
18
    
for (x = 0; 3
x < 5;
x++15
)
285
15
      sum += (y * 5 + x + 1) * (y * 27 + x + 1);
286
26
  for (y = 1; y < 26; 
y++25
)
287
25
  {
288
125
    for (i = 0; i < 4; 
i++100
)
289
100
    {
290
100
      c->data.f32[y * 27 * 4 + i] = border[0] + (y - 1) * 27 * (3 + 4 + 5 + 8 + 9 + 10 + 13 + 14 + 15);
291
100
      c->data.f32[(y * 27 + 1) * 4 + i] = border[1] + (y - 1) * 27 * (2 + 3 + 4 + 5 + 7 + 8 + 9 + 10 + 12 + 13 + 14 + 15);
292
2.40k
      for (x = 2; x < 25; 
x++2.30k
)
293
2.30k
        c->data.f32[(y * 27 + x) * 4 + i] = sum + ((y - 1) * 27 + x - 2) * 16 * 15 / 2;
294
100
      c->data.f32[(y * 27 + 25) * 4 + i] = border[2] + (y - 1) * 27 * (1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 + 11 + 12 + 13 + 14);
295
100
      c->data.f32[(y * 27 + 26) * 4 + i] = border[3] + (y - 1) * 27 * (1 + 2 + 3 + 6 + 7 + 8 + 11 + 12 + 13);
296
100
    }
297
25
  }
298
1
  REQUIRE_MATRIX_EQ(b, c, "27x27 matrix should be exactly the same");
299
1
  ccv_matrix_free(c);
300
1
  ccv_nnc_tensor_free(bias);
301
1
  ccv_nnc_tensor_free(w);
302
1
  ccv_nnc_tensor_free(b);
303
1
  ccv_nnc_tensor_free(a);
304
1
}
305
306
TEST_CASE("convolution with no bias")
307
1
{
308
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
309
1
  ccv_nnc_tensor_t* bg = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 4), 0);
310
1
  ccv_nnc_cmd_t cmd = CMD_CONVOLUTION_FORWARD(1, 4, 3, 5, 1);
311
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, bg->info);
312
1
  ccv_nnc_tensor_t* w = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4, 3, 5, 1), 0);
313
1
  ccv_nnc_tensor_t* bias = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 4), 0);
314
1
  dsfmt_t dsfmt;
315
1
  int i;
316
1
  dsfmt_init_gen_rand(&dsfmt, 1);
317
730
  for (i = 0; i < 27 * 27; 
i++729
)
318
729
    a->data.f32[i] = dsfmt_genrand_open_close(&dsfmt);
319
61
  for (i = 0; i < 4 * 3 * 5; 
i++60
)
320
60
    w->data.f32[i] = dsfmt_genrand_open_close(&dsfmt);
321
5
  for (i = 0; i < 4; 
i++4
)
322
4
    bias->data.f32[i] = 0;
323
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w, bias), TENSOR_LIST(bg), 0);
324
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 4), 0);
325
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a, w), TENSOR_LIST(b), 0);
326
1
  REQUIRE_MATRIX_EQ(b, bg, "convolution with no bias should equal to with bias = 0");
327
1
  ccv_nnc_tensor_free(a);
328
1
  ccv_nnc_tensor_free(b);
329
1
  ccv_nnc_tensor_free(bg);
330
1
  ccv_nnc_tensor_free(w);
331
1
  ccv_nnc_tensor_free(bias);
332
1
}
333
334
TEST_CASE("maximum pool network of 55x55 with window of 3x3 and stride of 2")
335
1
{
336
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 55, 55, 1), 0);
337
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
338
1
  ccv_nnc_cmd_t cmd = CMD_MAX_POOL_FORWARD(3, 3);
339
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
340
1
  // configure the inlets.
341
1
  int i;
342
3.02k
  for (i = 0; i < 55 * 55; 
i++3.02k
)
343
3.02k
    a->data.f32[i] = i + 1;
344
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
345
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(27, 27, CCV_32F | CCV_C1, 0, 0);
346
1
  int x, y;
347
28
  for (y = 0; y < 27; 
y++27
)
348
756
    
for (x = 0; 27
x < 27;
x++729
)
349
729
      c->data.f32[y * 27 + x] = 113 + y * 110 + x * 2;
350
1
  REQUIRE_MATRIX_EQ(b, c, "max pool network output should be exactly the same");
351
1
  ccv_matrix_free(c);
352
1
  ccv_nnc_tensor_free(b);
353
1
  ccv_nnc_tensor_free(a);
354
1
}
355
356
TEST_CASE("maximum pool network of 57x57 with window of 3x3 and stride of 3")
357
1
{
358
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 57, 57, 1), 0);
359
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 19, 19, 1), 0);
360
1
  ccv_nnc_cmd_t cmd = CMD_MAX_POOL_FORWARD(3, 3);
361
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
362
1
  // configure the inlets.
363
1
  int i;
364
3.25k
  for (i = 0; i < 57 * 57; 
i++3.24k
)
365
3.24k
    a->data.f32[i] = i + 1;
366
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
367
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(19, 19, CCV_32F | CCV_C1, 0, 0);
368
1
  int x, y;
369
20
  for (y = 0; y < 19; 
y++19
)
370
380
    
for (x = 0; 19
x < 19;
x++361
)
371
361
      c->data.f32[y * 19 + x] = 117 + y * 171 + x * 3;
372
1
  REQUIRE_MATRIX_EQ(b, c, "max pool network output should be exactly the same");
373
1
  ccv_matrix_free(c);
374
1
  ccv_nnc_tensor_free(b);
375
1
  ccv_nnc_tensor_free(a);
376
1
}
377
378
TEST_CASE("maximum pool network of 54x54 with window of 2x2 and stride of 2")
379
1
{
380
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 54, 54, 1), 0);
381
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
382
1
  ccv_nnc_cmd_t cmd = CMD_MAX_POOL_FORWARD(2, 2);
383
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
384
1
  // configure the inlets.
385
1
  int i;
386
2.91k
  for (i = 0; i < 54 * 54; 
i++2.91k
)
387
2.91k
    a->data.f32[i] = i + 1;
388
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
389
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(27, 27, CCV_32F | CCV_C1, 0, 0);
390
1
  int x, y;
391
28
  for (y = 0; y < 27; 
y++27
)
392
756
    
for (x = 0; 27
x < 27;
x++729
)
393
729
      c->data.f32[y * 27 + x] = 56 + y * 108 + x * 2;
394
1
  REQUIRE_MATRIX_EQ(b, c, "max pool network output should be exactly the same");
395
1
  ccv_matrix_free(c);
396
1
  ccv_nnc_tensor_free(b);
397
1
  ccv_nnc_tensor_free(a);
398
1
}
399
400
TEST_CASE("average pool network of 55x55 with window of 3x3 and stride of 2")
401
1
{
402
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 55, 55, 1), 0);
403
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
404
1
  ccv_nnc_cmd_t cmd = CMD_AVERAGE_POOL_FORWARD(3, 3);
405
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
406
1
  // configure the inlets.
407
1
  int i;
408
3.02k
  for (i = 0; i < 55 * 55; 
i++3.02k
)
409
3.02k
    a->data.f32[i] = i + 1;
410
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
411
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(27, 27, CCV_32F | CCV_C1, 0, 0);
412
1
  int x, y;
413
28
  for (y = 0; y < 27; 
y++27
)
414
756
    
for (x = 0; 27
x < 27;
x++729
)
415
729
      c->data.f32[y * 27 + x] = 57 + y * 110 + x * 2;
416
1
  REQUIRE_MATRIX_EQ(b, c, "average pool network output should be exactly the same");
417
1
  ccv_matrix_free(c);
418
1
  ccv_nnc_tensor_free(b);
419
1
  ccv_nnc_tensor_free(a);
420
1
}
421
422
TEST_CASE("average pool network of 57x57 with window of 3x3 and stride of 3")
423
1
{
424
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 57, 57, 1), 0);
425
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 19, 19, 1), 0);
426
1
  ccv_nnc_cmd_t cmd = CMD_AVERAGE_POOL_FORWARD(3, 3);
427
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
428
1
  // configure the inlets.
429
1
  int i;
430
3.25k
  for (i = 0; i < 57 * 57; 
i++3.24k
)
431
3.24k
    a->data.f32[i] = i + 1;
432
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
433
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(19, 19, CCV_32F | CCV_C1, 0, 0);
434
1
  int x, y;
435
20
  for (y = 0; y < 19; 
y++19
)
436
380
    
for (x = 0; 19
x < 19;
x++361
)
437
361
      c->data.f32[y * 19 + x] = 59 + y * 171 + x * 3;
438
1
  REQUIRE_MATRIX_EQ(b, c, "average pool network output should be exactly the same");
439
1
  ccv_matrix_free(c);
440
1
  ccv_nnc_tensor_free(b);
441
1
  ccv_nnc_tensor_free(a);
442
1
}
443
444
TEST_CASE("average pool network of 54x54 with window of 2x2 and stride of 2")
445
1
{
446
1
  ccv_nnc_tensor_t* a = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 54, 54, 1), 0);
447
1
  ccv_nnc_tensor_t* b = ccv_nnc_tensor_new(0, CPU_TENSOR_NHWC(32F, 27, 27, 1), 0);
448
1
  ccv_nnc_cmd_t cmd = CMD_AVERAGE_POOL_FORWARD(2, 2);
449
1
  ccv_nnc_hint_t hint = ccv_nnc_hint_auto(cmd.info, a->info, b->info);
450
1
  // configure the inlets.
451
1
  int i;
452
2.91k
  for (i = 0; i < 54 * 54; 
i++2.91k
)
453
2.91k
    a->data.f32[i] = i + 1;
454
1
  ccv_nnc_cmd_exec(cmd, hint, 0, TENSOR_LIST(a), TENSOR_LIST(b), 0);
455
1
  ccv_dense_matrix_t* c = ccv_dense_matrix_new(27, 27, CCV_32F | CCV_C1, 0, 0);
456
1
  int x, y;
457
28
  for (y = 0; y < 27; 
y++27
)
458
756
    
for (x = 0; 27
x < 27;
x++729
)
459
729
      c->data.f32[y * 27 + x] = 28.5 + y * 108 + x * 2;
460
1
  REQUIRE_MATRIX_EQ(b, c, "average pool network output should be exactly the same");
461
1
  ccv_matrix_free(c);
462
1
  ccv_nnc_tensor_free(b);
463
1
  ccv_nnc_tensor_free(a);
464
1
}
465
466
#include "case_main.h"