12 #include <Eigen/CXX11/Tensor>
28 single_pixel_patch = tensor.extract_image_patches(1, 1);
37 single_pixel_patch_row_major = tensor_row_major.extract_image_patches(1, 1);
44 for (
int i = 0;
i < tensor.
size(); ++
i) {
46 if (tensor.
data()[
i] != single_pixel_patch.
data()[
i]) {
47 std::cout <<
"Mismatch detected at index " <<
i <<
" : "
48 << tensor.
data()[
i] <<
" vs " << single_pixel_patch.
data()[
i]
53 if (tensor_row_major.
data()[
i] != single_pixel_patch_row_major.
data()[
i]) {
54 std::cout <<
"Mismatch detected at index " <<
i <<
" : "
55 << tensor.
data()[
i] <<
" vs "
56 << single_pixel_patch_row_major.
data()[
i] << std::endl;
59 tensor_row_major.
data()[
i]);
62 single_pixel_patch_row_major.
data()[
i]);
67 entire_image_patch = tensor.extract_image_patches(3, 5);
76 entire_image_patch_row_major = tensor_row_major.extract_image_patches(3, 5);
83 for (
int i = 0;
i < 3; ++
i) {
84 for (
int j = 0;
j < 5; ++
j) {
86 for (
int r = 0; r < 3; ++r) {
87 for (
int c = 0;
c < 5; ++
c) {
88 for (
int d = 0;
d < 2; ++
d) {
89 for (
int b = 0;
b < 7; ++
b) {
91 float expected_row_major = 0.0f;
92 if (r-1+
i >= 0 &&
c-2+
j >= 0 && r-1+
i < 3 &&
c-2+
j < 5) {
94 expected_row_major = tensor_row_major(
b,
c-2+
j, r-1+
i,
d);
97 if (entire_image_patch(
d, r,
c, patchId,
b) !=
expected) {
98 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
102 if (entire_image_patch_row_major(
b, patchId,
c, r,
d) !=
103 expected_row_major) {
104 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j
105 <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b
121 twod_patch = tensor.extract_image_patches(2, 2);
130 twod_patch_row_major = tensor_row_major.extract_image_patches(2, 2);
143 for (
int i = 0;
i < 3; ++
i) {
144 for (
int j = 0;
j < 5; ++
j) {
146 for (
int r = 0; r < 2; ++r) {
147 for (
int c = 0;
c < 2; ++
c) {
148 for (
int d = 0;
d < 2; ++
d) {
149 for (
int b = 0;
b < 7; ++
b) {
151 float expected_row_major = 0.0f;
152 int row_offset = r*stride +
i - row_padding;
153 int col_offset =
c*stride +
j - col_padding;
155 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor.
dimension(1) && col_offset < tensor.
dimension(2)) {
156 expected = tensor(
d, row_offset, col_offset,
b);
159 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
164 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor_row_major.
dimension(2) && col_offset < tensor_row_major.
dimension(1)) {
165 expected_row_major = tensor_row_major(
b, col_offset, row_offset,
d);
168 if (twod_patch_row_major(
b, patchId,
c, r,
d) != expected_row_major) {
169 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
188 int input_batches = 1;
191 Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches);
193 for (
int i = 0;
i < tensor.
size(); ++
i) {
223 for (
int i = 0; (
i+stride+ksize-1) < input_rows;
i += stride) {
224 for (
int j = 0; (
j+stride+ksize-1) < input_cols;
j += stride) {
225 int patchId =
i+input_rows*
j;
226 for (
int r = 0; r < ksize; ++r) {
227 for (
int c = 0;
c < ksize; ++
c) {
228 for (
int d = 0;
d < input_depth; ++
d) {
229 for (
int b = 0;
b < input_batches; ++
b) {
231 float expected_row_major = 0.0f;
232 int row_offset = r +
i - row_padding;
233 int col_offset =
c +
j - col_padding;
234 if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
235 expected = tensor(
d, row_offset, col_offset,
b);
236 expected_row_major = tensor_row_major(
b, col_offset, row_offset,
d);
240 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
244 if (result_row_major(
b, patchId,
c, r,
d) != expected_row_major) {
245 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
264 int input_batches = 2;
268 Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches);
269 tensor = tensor.constant(11.0
f);
296 for (
int i = 0; (
i+stride+ksize-1) <= input_rows;
i += stride) {
297 for (
int j = 0; (
j+stride+ksize-1) <= input_cols;
j += stride) {
298 int patchId =
i+input_rows*
j;
299 for (
int r = 0; r < ksize; ++r) {
300 for (
int c = 0;
c < ksize; ++
c) {
301 for (
int d = 0;
d < input_depth; ++
d) {
302 for (
int b = 0;
b < input_batches; ++
b) {
304 float expected_row_major = 0.0f;
305 int row_offset = r +
i - row_padding;
306 int col_offset =
c +
j - col_padding;
307 if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
308 expected = tensor(
d, row_offset, col_offset,
b);
309 expected_row_major = tensor_row_major(
b, col_offset, row_offset,
d);
313 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
317 if (result_row_major(
b, patchId,
c, r,
d) != expected_row_major) {
318 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
337 int input_batches = 1;
341 Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches);
343 for (
int i = 0;
i < tensor.
size(); ++
i) {
373 for (
int i = 0; (
i+stride+ksize-1) <= input_rows;
i += stride) {
374 for (
int j = 0; (
j+stride+ksize-1) <= input_cols;
j += stride) {
375 int patchId =
i+input_rows*
j;
376 for (
int r = 0; r < ksize; ++r) {
377 for (
int c = 0;
c < ksize; ++
c) {
378 for (
int d = 0;
d < input_depth; ++
d) {
379 for (
int b = 0;
b < input_batches; ++
b) {
381 float expected_row_major = 0.0f;
382 int row_offset = r*stride +
i - row_padding;
383 int col_offset =
c*stride +
j - col_padding;
384 if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
385 expected = tensor(
d, row_offset, col_offset,
b);
386 expected_row_major = tensor_row_major(
b, col_offset, row_offset,
d);
390 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
394 if (result_row_major(
b, patchId,
c, r,
d) != expected_row_major) {
395 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
414 int input_batches = 1;
420 Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches);
422 for (
int i = 0;
i < tensor.
size(); ++
i) {
426 ksize, ksize, row_stride, col_stride, 1, 1,
PADDING_SAME);
446 tensor_row_major.extract_image_patches(ksize, ksize, row_stride,
470 single_pixel_patch = tensor.extract_image_patches(1, 1);
478 single_pixel_patch_row_major = tensor_row_major.extract_image_patches(1, 1);
484 for (
int i = 0;
i < tensor.
size(); ++
i) {
486 if (tensor.
data()[
i] != single_pixel_patch.
data()[
i]) {
487 std::cout <<
"Mismatch detected at index " <<
i <<
" : " << tensor.
data()[
i] <<
" vs " << single_pixel_patch.
data()[
i] << std::endl;
491 if (tensor_row_major.
data()[
i] != single_pixel_patch_row_major.
data()[
i]) {
492 std::cout <<
"Mismatch detected at index " <<
i <<
" : "
493 << tensor.
data()[
i] <<
" vs "
494 << single_pixel_patch_row_major.
data()[
i] << std::endl;
497 tensor_row_major.
data()[
i]);
500 single_pixel_patch_row_major.
data()[
i]);
505 entire_image_patch = tensor.extract_image_patches(3, 5);
513 entire_image_patch_row_major = tensor_row_major.extract_image_patches(3, 5);
519 for (
int i = 0;
i < 3; ++
i) {
520 for (
int j = 0;
j < 5; ++
j) {
522 for (
int r = 0; r < 3; ++r) {
523 for (
int c = 0;
c < 5; ++
c) {
524 for (
int d = 0;
d < 2; ++
d) {
526 float expected_row_major = 0.0f;
527 if (r-1+
i >= 0 &&
c-2+
j >= 0 && r-1+
i < 3 &&
c-2+
j < 5) {
529 expected_row_major = tensor_row_major(
c-2+
j, r-1+
i,
d);
532 if (entire_image_patch(
d, r,
c, patchId) !=
expected) {
533 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
537 if (entire_image_patch_row_major(patchId,
c, r,
d) !=
538 expected_row_major) {
539 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
553 twod_patch = tensor.extract_image_patches(2, 2);
561 twod_patch_row_major = tensor_row_major.extract_image_patches(2, 2);
572 for (
int i = 0;
i < 3; ++
i) {
573 for (
int j = 0;
j < 5; ++
j) {
575 for (
int r = 0; r < 2; ++r) {
576 for (
int c = 0;
c < 2; ++
c) {
577 for (
int d = 0;
d < 2; ++
d) {
579 float expected_row_major = 0.0f;
580 int row_offset = r*stride +
i - row_padding;
581 int col_offset =
c*stride +
j - col_padding;
583 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor.
dimension(1) && col_offset < tensor.
dimension(2)) {
584 expected = tensor(
d, row_offset, col_offset);
586 if (twod_patch(
d, r,
c, patchId) !=
expected) {
587 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
591 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor_row_major.
dimension(1) && col_offset < tensor_row_major.
dimension(0)) {
592 expected_row_major = tensor_row_major(col_offset, row_offset,
d);
594 if (twod_patch_row_major(patchId,
c, r,
d) != expected_row_major) {
595 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
629 for (
int b = 0;
b < 16; ++
b) {
630 for (
int i = 0;
i < 128; ++
i) {
631 for (
int j = 0;
j < 128; ++
j) {
632 int patchId =
i+128*
j;
633 for (
int c = 0;
c < 11; ++
c) {
634 for (
int r = 0; r < 11; ++r) {
635 for (
int d = 0;
d < 3; ++
d) {
637 if (r-5+
i >= 0 &&
c-5+
j >= 0 && r-5+
i < 128 &&
c-5+
j < 128) {
642 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
646 if (l_out_row_major(
b, patchId,
c, r,
d) !=
648 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j
649 <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b
662 l_in.
resize(16, 64, 64, 32);
664 l_out = l_in.extract_image_patches(9, 9);
672 l_out_row_major = l_in.
swap_layout().extract_image_patches(9, 9);
679 for (
int b = 0;
b < 32; ++
b) {
680 for (
int i = 0;
i < 64; ++
i) {
681 for (
int j = 0;
j < 64; ++
j) {
682 int patchId =
i+64*
j;
683 for (
int c = 0;
c < 9; ++
c) {
684 for (
int r = 0; r < 9; ++r) {
685 for (
int d = 0;
d < 16; ++
d) {
687 if (r-4+
i >= 0 &&
c-4+
j >= 0 && r-4+
i < 64 &&
c-4+
j < 64) {
692 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
696 if (l_out_row_major(
b, patchId,
c, r,
d) !=
expected) {
697 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
708 l_in.
resize(32, 16, 16, 32);
710 l_out = l_in.extract_image_patches(7, 7);
718 l_out_row_major = l_in.
swap_layout().extract_image_patches(7, 7);
725 for (
int b = 0;
b < 32; ++
b) {
726 for (
int i = 0;
i < 16; ++
i) {
727 for (
int j = 0;
j < 16; ++
j) {
728 int patchId =
i+16*
j;
729 for (
int c = 0;
c < 7; ++
c) {
730 for (
int r = 0; r < 7; ++r) {
731 for (
int d = 0;
d < 32; ++
d) {
733 if (r-3+
i >= 0 &&
c-3+
j >= 0 && r-3+
i < 16 &&
c-3+
j < 16) {
738 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
742 if (l_out_row_major(
b, patchId,
c, r,
d) !=
expected) {
743 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
754 l_in.
resize(64, 13, 13, 32);
756 l_out = l_in.extract_image_patches(3, 3);
764 l_out_row_major = l_in.
swap_layout().extract_image_patches(3, 3);
771 for (
int b = 0;
b < 32; ++
b) {
772 for (
int i = 0;
i < 13; ++
i) {
773 for (
int j = 0;
j < 13; ++
j) {
774 int patchId =
i+13*
j;
775 for (
int c = 0;
c < 3; ++
c) {
776 for (
int r = 0; r < 3; ++r) {
777 for (
int d = 0;
d < 64; ++
d) {
779 if (r-1+
i >= 0 &&
c-1+
j >= 0 && r-1+
i < 13 &&
c-1+
j < 13) {
784 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
788 if (l_out_row_major(
b, patchId,
c, r,
d) !=
expected) {
789 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" <<
j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;