// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
} if (twod_patch_row_major(b, patchId, c, r, d) != expected_row_major) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(twod_patch_row_major(b, patchId, c, r, d), expected_row_major); // Check that ColMajor and RowMajor agree.
VERIFY_IS_EQUAL(expected, expected_row_major);
}
}
}
}
}
}
}
// Verifies VALID padding (no padding) with incrementing values. void test_patch_padding_valid()
{ int input_depth = 3; int input_rows = 3; int input_cols = 3; int input_batches = 1; int ksize = 2; // Corresponds to the Rows and Cols for tensor.extract_image_patches<>. int stride = 2; // Only same stride is supported.
Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches); // Initializes tensor with incrementing numbers. for (int i = 0; i < tensor.size(); ++i) {
tensor.data()[i] = i + 1;
} // ColMajor
Tensor<float, 5> result = tensor.extract_image_patches(ksize, ksize, stride, stride, 1, 1, PADDING_VALID);
VERIFY_IS_EQUAL(result.dimension(0), input_depth); // depth
VERIFY_IS_EQUAL(result.dimension(1), ksize); // kernel rows
VERIFY_IS_EQUAL(result.dimension(2), ksize); // kernel cols
VERIFY_IS_EQUAL(result.dimension(3), 1); // number of patches
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
// No padding is carried out. int row_padding = 0; int col_padding = 0;
for (int i = 0; (i+stride+ksize-1) < input_rows; i += stride) { // input rows for (int j = 0; (j+stride+ksize-1) < input_cols; j += stride) { // input cols int patchId = i+input_rows*j; for (int r = 0; r < ksize; ++r) { // patch rows for (int c = 0; c < ksize; ++c) { // patch cols for (int d = 0; d < input_depth; ++d) { // depth for (int b = 0; b < input_batches; ++b) { // batch float expected = 0.0f; float expected_row_major = 0.0f; int row_offset = r + i - row_padding; int col_offset = c + j - col_padding; if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
expected = tensor(d, row_offset, col_offset, b);
expected_row_major = tensor_row_major(b, col_offset, row_offset, d);
} // ColMajor if (result(d, r, c, patchId, b) != expected) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result(d, r, c, patchId, b), expected); // RowMajor if (result_row_major(b, patchId, c, r, d) != expected_row_major) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result_row_major(b, patchId, c, r, d), expected_row_major); // Check that ColMajor and RowMajor agree.
VERIFY_IS_EQUAL(expected, expected_row_major);
}
}
}
}
}
}
}
// Verifies VALID padding (no padding) with the same value. void test_patch_padding_valid_same_value()
{ int input_depth = 1; int input_rows = 5; int input_cols = 5; int input_batches = 2; int ksize = 3; // Corresponds to the Rows and Cols for tensor.extract_image_patches<>. int stride = 2; // Only same stride is supported. // ColMajor
Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches);
tensor = tensor.constant(11.0f);
Tensor<float, 5> result = tensor.extract_image_patches(ksize, ksize, stride, stride, 1, 1, PADDING_VALID);
VERIFY_IS_EQUAL(result.dimension(0), input_depth); // depth
VERIFY_IS_EQUAL(result.dimension(1), ksize); // kernel rows
VERIFY_IS_EQUAL(result.dimension(2), ksize); // kernel cols
VERIFY_IS_EQUAL(result.dimension(3), 4); // number of patches
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
// No padding is carried out. int row_padding = 0; int col_padding = 0;
for (int i = 0; (i+stride+ksize-1) <= input_rows; i += stride) { // input rows for (int j = 0; (j+stride+ksize-1) <= input_cols; j += stride) { // input cols int patchId = i+input_rows*j; for (int r = 0; r < ksize; ++r) { // patch rows for (int c = 0; c < ksize; ++c) { // patch cols for (int d = 0; d < input_depth; ++d) { // depth for (int b = 0; b < input_batches; ++b) { // batch float expected = 0.0f; float expected_row_major = 0.0f; int row_offset = r + i - row_padding; int col_offset = c + j - col_padding; if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
expected = tensor(d, row_offset, col_offset, b);
expected_row_major = tensor_row_major(b, col_offset, row_offset, d);
} // ColMajor if (result(d, r, c, patchId, b) != expected) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result(d, r, c, patchId, b), expected); // RowMajor if (result_row_major(b, patchId, c, r, d) != expected_row_major) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result_row_major(b, patchId, c, r, d), expected_row_major); // Check that ColMajor and RowMajor agree.
VERIFY_IS_EQUAL(expected, expected_row_major);
}
}
}
}
}
}
}
// Verifies SAME padding. void test_patch_padding_same()
{ int input_depth = 3; int input_rows = 4; int input_cols = 2; int input_batches = 1; int ksize = 2; // Corresponds to the Rows and Cols for tensor.extract_image_patches<>. int stride = 2; // Only same stride is supported. // ColMajor
Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches); // Initializes tensor with incrementing numbers. for (int i = 0; i < tensor.size(); ++i) {
tensor.data()[i] = i + 1;
}
Tensor<float, 5> result = tensor.extract_image_patches(ksize, ksize, stride, stride, PADDING_SAME);
VERIFY_IS_EQUAL(result.dimension(0), input_depth); // depth
VERIFY_IS_EQUAL(result.dimension(1), ksize); // kernel rows
VERIFY_IS_EQUAL(result.dimension(2), ksize); // kernel cols
VERIFY_IS_EQUAL(result.dimension(3), 2); // number of patches
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
// Based on the calculation described in TensorTraits.h, padding happens to be // 0. int row_padding = 0; int col_padding = 0;
for (int i = 0; (i+stride+ksize-1) <= input_rows; i += stride) { // input rows for (int j = 0; (j+stride+ksize-1) <= input_cols; j += stride) { // input cols int patchId = i+input_rows*j; for (int r = 0; r < ksize; ++r) { // patch rows for (int c = 0; c < ksize; ++c) { // patch cols for (int d = 0; d < input_depth; ++d) { // depth for (int b = 0; b < input_batches; ++b) { // batch float expected = 0.0f; float expected_row_major = 0.0f; int row_offset = r*stride + i - row_padding; int col_offset = c*stride + j - col_padding; if (row_offset >= 0 && col_offset >= 0 && row_offset < input_rows && col_offset < input_cols) {
expected = tensor(d, row_offset, col_offset, b);
expected_row_major = tensor_row_major(b, col_offset, row_offset, d);
} // ColMajor if (result(d, r, c, patchId, b) != expected) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result(d, r, c, patchId, b), expected); // RowMajor if (result_row_major(b, patchId, c, r, d) != expected_row_major) {
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << " b=" << b << std::endl;
}
VERIFY_IS_EQUAL(result_row_major(b, patchId, c, r, d), expected_row_major); // Check that ColMajor and RowMajor agree.
VERIFY_IS_EQUAL(expected, expected_row_major);
}
}
}
}
}
}
}
// Verifies that SAME padding, when computed as negative values, will be clipped // to zero. void test_patch_padding_same_negative_padding_clip_to_zero() { int input_depth = 1; int input_rows = 15; int input_cols = 1; int input_batches = 1; int ksize = 1; // Corresponds to the Rows and Cols for // tensor.extract_image_patches<>. int row_stride = 5; int col_stride = 1; // ColMajor
Tensor<float, 4> tensor(input_depth, input_rows, input_cols, input_batches); // Initializes tensor with incrementing numbers. for (int i = 0; i < tensor.size(); ++i) {
tensor.data()[i] = i + 1;
}
Tensor<float, 5> result = tensor.extract_image_patches(
ksize, ksize, row_stride, col_stride, 1, 1, PADDING_SAME); // row padding will be computed as -2 originally and then be clipped to 0.
VERIFY_IS_EQUAL(result.coeff(0), 1.0f);
VERIFY_IS_EQUAL(result.coeff(1), 6.0f);
VERIFY_IS_EQUAL(result.coeff(2), 11.0f);
VERIFY_IS_EQUAL(result.dimension(0), input_depth); // depth
VERIFY_IS_EQUAL(result.dimension(1), ksize); // kernel rows
VERIFY_IS_EQUAL(result.dimension(2), ksize); // kernel cols
VERIFY_IS_EQUAL(result.dimension(3), 3); // number of patches
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.