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) {
93 expected = tensor(
d, r-1+
i,
c-2+j,
b);
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);
158 if (twod_patch(
d, r,
c, patchId,
b) != expected) {
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);
239 if (
result(
d, r,
c, patchId,
b) != expected) {
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);
312 if (
result(
d, r,
c, patchId,
b) != expected) {
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);
389 if (
result(
d, r,
c, patchId,
b) != expected) {
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;
419 single_pixel_patch = tensor.extract_image_patches(1, 1);
427 single_pixel_patch_row_major = tensor_row_major.extract_image_patches(1, 1);
433 for (
int i = 0;
i < tensor.
size(); ++
i) {
435 if (tensor.
data()[
i] != single_pixel_patch.data()[
i]) {
436 std::cout <<
"Mismatch detected at index " <<
i <<
" : " << tensor.
data()[
i] <<
" vs " << single_pixel_patch.data()[
i] << std::endl;
440 if (tensor_row_major.
data()[
i] != single_pixel_patch_row_major.data()[
i]) {
441 std::cout <<
"Mismatch detected at index " <<
i <<
" : " 442 << tensor.
data()[
i] <<
" vs " 443 << single_pixel_patch_row_major.data()[
i] << std::endl;
446 tensor_row_major.
data()[
i]);
449 single_pixel_patch_row_major.data()[
i]);
454 entire_image_patch = tensor.extract_image_patches(3, 5);
462 entire_image_patch_row_major = tensor_row_major.extract_image_patches(3, 5);
468 for (
int i = 0;
i < 3; ++
i) {
469 for (
int j = 0;
j < 5; ++
j) {
471 for (
int r = 0; r < 3; ++r) {
472 for (
int c = 0;
c < 5; ++
c) {
473 for (
int d = 0;
d < 2; ++
d) {
475 float expected_row_major = 0.0f;
476 if (r-1+
i >= 0 &&
c-2+j >= 0 && r-1+
i < 3 &&
c-2+j < 5) {
477 expected = tensor(
d, r-1+
i,
c-2+j);
478 expected_row_major = tensor_row_major(
c-2+j, r-1+
i,
d);
481 if (entire_image_patch(
d, r,
c, patchId) != expected) {
482 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
486 if (entire_image_patch_row_major(patchId,
c, r,
d) !=
487 expected_row_major) {
488 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
502 twod_patch = tensor.extract_image_patches(2, 2);
510 twod_patch_row_major = tensor_row_major.extract_image_patches(2, 2);
521 for (
int i = 0;
i < 3; ++
i) {
522 for (
int j = 0;
j < 5; ++
j) {
524 for (
int r = 0; r < 2; ++r) {
525 for (
int c = 0;
c < 2; ++
c) {
526 for (
int d = 0;
d < 2; ++
d) {
528 float expected_row_major = 0.0f;
529 int row_offset = r*stride +
i - row_padding;
530 int col_offset =
c*stride + j - col_padding;
532 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor.
dimension(1) && col_offset < tensor.
dimension(2)) {
533 expected = tensor(
d, row_offset, col_offset);
535 if (twod_patch(
d, r,
c, patchId) != expected) {
536 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
540 if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor_row_major.
dimension(1) && col_offset < tensor_row_major.
dimension(0)) {
541 expected_row_major = tensor_row_major(col_offset, row_offset,
d);
543 if (twod_patch_row_major(patchId,
c, r,
d) != expected_row_major) {
544 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d << std::endl;
578 for (
int b = 0;
b < 16; ++
b) {
579 for (
int i = 0;
i < 128; ++
i) {
580 for (
int j = 0;
j < 128; ++
j) {
581 int patchId =
i+128*
j;
582 for (
int c = 0;
c < 11; ++
c) {
583 for (
int r = 0; r < 11; ++r) {
584 for (
int d = 0;
d < 3; ++
d) {
586 if (r-5+
i >= 0 &&
c-5+j >= 0 && r-5+
i < 128 &&
c-5+j < 128) {
587 expected = l_in(
d, r-5+
i,
c-5+j,
b);
590 if (l_out(
d, r,
c, patchId,
b) != expected) {
591 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
595 if (l_out_row_major(
b, patchId,
c, r,
d) !=
597 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j
598 <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b 611 l_in.
resize(16, 64, 64, 32);
613 l_out = l_in.extract_image_patches(9, 9);
621 l_out_row_major = l_in.
swap_layout().extract_image_patches(9, 9);
628 for (
int b = 0;
b < 32; ++
b) {
629 for (
int i = 0;
i < 64; ++
i) {
630 for (
int j = 0;
j < 64; ++
j) {
631 int patchId =
i+64*
j;
632 for (
int c = 0;
c < 9; ++
c) {
633 for (
int r = 0; r < 9; ++r) {
634 for (
int d = 0;
d < 16; ++
d) {
636 if (r-4+
i >= 0 &&
c-4+j >= 0 && r-4+
i < 64 &&
c-4+j < 64) {
637 expected = l_in(
d, r-4+
i,
c-4+j,
b);
640 if (l_out(
d, r,
c, patchId,
b) != expected) {
641 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
645 if (l_out_row_major(
b, patchId,
c, r,
d) != expected) {
646 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
657 l_in.
resize(32, 16, 16, 32);
659 l_out = l_in.extract_image_patches(7, 7);
667 l_out_row_major = l_in.
swap_layout().extract_image_patches(7, 7);
674 for (
int b = 0;
b < 32; ++
b) {
675 for (
int i = 0;
i < 16; ++
i) {
676 for (
int j = 0;
j < 16; ++
j) {
677 int patchId =
i+16*
j;
678 for (
int c = 0;
c < 7; ++
c) {
679 for (
int r = 0; r < 7; ++r) {
680 for (
int d = 0;
d < 32; ++
d) {
682 if (r-3+
i >= 0 &&
c-3+j >= 0 && r-3+
i < 16 &&
c-3+j < 16) {
683 expected = l_in(
d, r-3+
i,
c-3+j,
b);
686 if (l_out(
d, r,
c, patchId,
b) != expected) {
687 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
691 if (l_out_row_major(
b, patchId,
c, r,
d) != expected) {
692 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
703 l_in.
resize(64, 13, 13, 32);
705 l_out = l_in.extract_image_patches(3, 3);
713 l_out_row_major = l_in.
swap_layout().extract_image_patches(3, 3);
720 for (
int b = 0;
b < 32; ++
b) {
721 for (
int i = 0;
i < 13; ++
i) {
722 for (
int j = 0;
j < 13; ++
j) {
723 int patchId =
i+13*
j;
724 for (
int c = 0;
c < 3; ++
c) {
725 for (
int r = 0; r < 3; ++r) {
726 for (
int d = 0;
d < 64; ++
d) {
728 if (r-1+
i >= 0 &&
c-1+j >= 0 && r-1+
i < 13 &&
c-1+j < 13) {
729 expected = l_in(
d, r-1+
i,
c-1+j,
b);
732 if (l_out(
d, r,
c, patchId,
b) != expected) {
733 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
737 if (l_out_row_major(
b, patchId,
c, r,
d) != expected) {
738 std::cout <<
"Mismatch detected at index i=" <<
i <<
" j=" << j <<
" r=" << r <<
" c=" <<
c <<
" d=" <<
d <<
" b=" <<
b << std::endl;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
void test_patch_no_extra_dim()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
#define VERIFY_IS_EQUAL(a, b)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorLayoutSwapOp< const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > swap_layout() const
void test_cxx11_tensor_image_patch()
void test_patch_padding_valid_same_value()
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
void test_patch_padding_valid()
EIGEN_DEVICE_FUNC void resize(const array< Index, NumIndices > &dimensions)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
void test_patch_padding_same()
void test_imagenet_patches()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const