mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-03-13 18:37:27 +08:00
Added support for RowMajor layout to the image patch extraction code
Speeded up the unsupported_cxx11_tensor_image_patch test and reduced its memory footprint
This commit is contained in:
parent
531fa9de77
commit
8afce86e64
@ -106,7 +106,8 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
{
|
||||
typedef TensorImagePatchOp<Rows, Cols, ArgType> XprType;
|
||||
typedef typename XprType::Index Index;
|
||||
static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value + 1;
|
||||
static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
|
||||
static const int NumDims = NumInputDims + 1;
|
||||
typedef DSizes<Index, NumDims> Dimensions;
|
||||
typedef typename XprType::Scalar Scalar;
|
||||
|
||||
@ -120,16 +121,18 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
|
||||
: m_impl(op.expression(), device)
|
||||
{
|
||||
// Only column major tensors are supported for now.
|
||||
EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
|
||||
|
||||
EIGEN_STATIC_ASSERT(NumDims >= 4, YOU_MADE_A_PROGRAMMING_MISTAKE);
|
||||
|
||||
const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
|
||||
|
||||
// Caches a few variables.
|
||||
m_inputRows = input_dims[1];
|
||||
m_inputCols = input_dims[2];
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
m_inputRows = input_dims[1];
|
||||
m_inputCols = input_dims[2];
|
||||
} else {
|
||||
m_inputRows = input_dims[NumInputDims-2];
|
||||
m_inputCols = input_dims[NumInputDims-3];
|
||||
}
|
||||
|
||||
m_row_strides = op.row_strides();
|
||||
m_col_strides = op.col_strides();
|
||||
@ -157,28 +160,57 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
}
|
||||
|
||||
// Dimensions for result of extraction.
|
||||
// 0: depth
|
||||
// 1: patch_rows
|
||||
// 2: patch_cols
|
||||
// 3: number of patches
|
||||
// 4 and beyond: anything else (such as batch).
|
||||
m_dimensions[0] = input_dims[0];
|
||||
m_dimensions[1] = op.patch_rows();
|
||||
m_dimensions[2] = op.patch_cols();
|
||||
m_dimensions[3] = m_outputRows * m_outputCols;
|
||||
for (int i = 4; i < NumDims; ++i) {
|
||||
m_dimensions[i] = input_dims[i-1];
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
// ColMajor
|
||||
// 0: depth
|
||||
// 1: patch_rows
|
||||
// 2: patch_cols
|
||||
// 3: number of patches
|
||||
// 4 and beyond: anything else (such as batch).
|
||||
m_dimensions[0] = input_dims[0];
|
||||
m_dimensions[1] = op.patch_rows();
|
||||
m_dimensions[2] = op.patch_cols();
|
||||
m_dimensions[3] = m_outputRows * m_outputCols;
|
||||
for (int i = 4; i < NumDims; ++i) {
|
||||
m_dimensions[i] = input_dims[i-1];
|
||||
}
|
||||
} else {
|
||||
// RowMajor
|
||||
// NumDims-1: depth
|
||||
// NumDims-2: patch_rows
|
||||
// NumDims-3: patch_cols
|
||||
// NumDims-4: number of patches
|
||||
// NumDims-5 and beyond: anything else (such as batch).
|
||||
m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
|
||||
m_dimensions[NumDims-2] = op.patch_rows();
|
||||
m_dimensions[NumDims-3] = op.patch_cols();
|
||||
m_dimensions[NumDims-4] = m_outputRows * m_outputCols;
|
||||
for (int i = NumDims-5; i >= 0; --i) {
|
||||
m_dimensions[i] = input_dims[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Strides for moving the patch in various dimensions.
|
||||
m_colStride = m_dimensions[1];
|
||||
m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
|
||||
m_otherStride = m_patchStride * m_dimensions[3];
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
m_colStride = m_dimensions[1];
|
||||
m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
|
||||
m_otherStride = m_patchStride * m_dimensions[3];
|
||||
} else {
|
||||
m_colStride = m_dimensions[NumDims-2];
|
||||
m_patchStride = m_colStride * m_dimensions[NumDims-3] * m_dimensions[NumDims-1];
|
||||
m_otherStride = m_patchStride * m_dimensions[NumDims-4];
|
||||
}
|
||||
|
||||
// Strides for navigating through the input tensor.
|
||||
m_rowInputStride = input_dims[0];
|
||||
m_colInputStride = input_dims[0] * input_dims[1];
|
||||
m_patchInputStride = input_dims[0] * input_dims[1] * input_dims[2];
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
m_rowInputStride = input_dims[0];
|
||||
m_colInputStride = input_dims[0] * input_dims[1];
|
||||
m_patchInputStride = input_dims[0] * input_dims[1] * input_dims[2];
|
||||
} else {
|
||||
m_rowInputStride = input_dims[NumInputDims-1];
|
||||
m_colInputStride = input_dims[NumInputDims-1] * input_dims[NumInputDims-2];
|
||||
m_patchInputStride = input_dims[NumInputDims-1] * input_dims[NumInputDims-2] * input_dims[NumInputDims-3];
|
||||
}
|
||||
|
||||
// Fast representations of different variables.
|
||||
m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
|
||||
@ -186,7 +218,11 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
|
||||
// Number of patches in the width dimension.
|
||||
m_fastOutputRows = internal::TensorIntDivisor<Index>(m_outputRows);
|
||||
m_fastDimZero = internal::TensorIntDivisor<Index>(m_dimensions[0]);
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
m_fastDimZero = internal::TensorIntDivisor<Index>(m_dimensions[0]);
|
||||
} else {
|
||||
m_fastDimZero = internal::TensorIntDivisor<Index>(m_dimensions[NumDims-1]);
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename XprType::CoeffReturnType CoeffReturnType;
|
||||
@ -207,7 +243,6 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
{
|
||||
// Patch index corresponding to the passed in index.
|
||||
const Index patchIndex = index / m_fastPatchStride;
|
||||
|
||||
// Find the offset of the element wrt the location of the first element.
|
||||
const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastDimZero;
|
||||
|
||||
@ -232,7 +267,8 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
return Scalar(0);
|
||||
}
|
||||
|
||||
const Index depth = index - (index / m_fastDimZero) * m_dimensions[0];
|
||||
const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
|
||||
const Index depth = index - (index / m_fastDimZero) * m_dimensions[depth_index];
|
||||
|
||||
const Index inputIndex = depth + inputRow * m_rowInputStride + inputCol * m_colInputStride + otherIndex * m_patchInputStride;
|
||||
return m_impl.coeff(inputIndex);
|
||||
@ -286,7 +322,8 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
|
||||
if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) {
|
||||
// no padding
|
||||
const Index depth = index - (index / m_fastDimZero) * m_dimensions[0];
|
||||
const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
|
||||
const Index depth = index - (index / m_fastDimZero) * m_dimensions[depth_index];
|
||||
const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride;
|
||||
return m_impl.template packet<Unaligned>(inputIndex);
|
||||
}
|
||||
@ -309,14 +346,24 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(const array<Index, NumDims>& coords) const
|
||||
{
|
||||
// Location of the first element of the patch.
|
||||
// ColMajor
|
||||
// 0: d, 1: patch_rows, 2: patch_cols, 3: number of patches, 4: number of batches
|
||||
const Index patchIndex = coords[3];
|
||||
// RowMajor
|
||||
// 0: number of batches, 1: number of patches, 2: patch_cols , 3: patch_rows, 4: d
|
||||
const Index patchIndex = coords[static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 3 : 1];
|
||||
|
||||
array<Index, NumDims-1> inputCoords;
|
||||
inputCoords[0] = coords[0]; // depth
|
||||
inputCoords[1] = patchIndex / m_inputCols + coords[1] - m_rowPaddingTop;
|
||||
inputCoords[2] = patchIndex - patchIndex / m_inputCols * m_inputCols + coords[2] - m_colPaddingLeft;
|
||||
inputCoords[3] = coords[4]; // batch
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
inputCoords[0] = coords[0]; // depth
|
||||
inputCoords[1] = patchIndex / m_inputCols + coords[1] - m_rowPaddingTop;
|
||||
inputCoords[2] = patchIndex - patchIndex / m_inputCols * m_inputCols + coords[2] - m_colPaddingLeft;
|
||||
inputCoords[3] = coords[4]; // batch
|
||||
} else {
|
||||
inputCoords[3] = coords[4]; // depth
|
||||
inputCoords[2] = patchIndex / m_inputCols + coords[3] - m_rowPaddingTop;
|
||||
inputCoords[1] = patchIndex - patchIndex / m_inputCols * m_inputCols + coords[2] - m_colPaddingLeft;
|
||||
inputCoords[0] = coords[0]; // batch
|
||||
}
|
||||
// If the computed coordinates are outside the original image perimeter, return 0.
|
||||
if (inputCoords[1] < 0 || inputCoords[1] >= m_inputRows ||
|
||||
inputCoords[2] < 0 || inputCoords[2] >= m_inputCols) {
|
||||
@ -325,11 +372,20 @@ struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
|
||||
if (TensorEvaluator<ArgType, Device>::CoordAccess) {
|
||||
return m_impl.coeff(inputCoords);
|
||||
} else {
|
||||
Index inputIndex =
|
||||
Index inputIndex;
|
||||
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
|
||||
inputIndex =
|
||||
inputCoords[3] * m_patchInputStride +
|
||||
inputCoords[2] * m_colInputStride +
|
||||
inputCoords[1] * m_rowInputStride +
|
||||
inputCoords[0];
|
||||
} else {
|
||||
inputIndex =
|
||||
inputCoords[1] * m_patchInputStride +
|
||||
inputCoords[2] * m_colInputStride +
|
||||
inputCoords[3] * m_rowInputStride +
|
||||
inputCoords[4];
|
||||
}
|
||||
return m_impl.coeff(inputIndex);
|
||||
}
|
||||
}
|
||||
|
@ -17,32 +17,69 @@ static void test_simple_patch()
|
||||
{
|
||||
Tensor<float, 4> tensor(2,3,5,7);
|
||||
tensor.setRandom();
|
||||
Tensor<float, 4, RowMajor> tensor_row_major = tensor.swap_layout();
|
||||
VERIFY_IS_EQUAL(tensor.dimension(0), tensor_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(1), tensor_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(2), tensor_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(3), tensor_row_major.dimension(0));
|
||||
|
||||
// Single pixel patch: ColMajor
|
||||
Tensor<float, 5> single_pixel_patch;
|
||||
single_pixel_patch = tensor.extract_image_patches<1, 1>();
|
||||
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(1), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(2), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(3), 3*5);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(4), 7);
|
||||
|
||||
// Single pixel patch: RowMajor
|
||||
Tensor<float, 5, RowMajor> single_pixel_patch_row_major;
|
||||
single_pixel_patch_row_major = tensor_row_major.extract_image_patches<1, 1>();
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(0), 7);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(1), 3*5);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(2), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(3), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(4), 2);
|
||||
|
||||
for (int i = 0; i < tensor.size(); ++i) {
|
||||
// ColMajor
|
||||
if (tensor.data()[i] != single_pixel_patch.data()[i]) {
|
||||
std::cout << "Mismatch detected at index " << i << " : " << tensor.data()[i] << " vs " << single_pixel_patch.data()[i] << std::endl;
|
||||
std::cout << "Mismatch detected at index " << i << " : "
|
||||
<< tensor.data()[i] << " vs " << single_pixel_patch.data()[i]
|
||||
<< std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.data()[i], tensor.data()[i]);
|
||||
// RowMajor
|
||||
if (tensor_row_major.data()[i] != single_pixel_patch_row_major.data()[i]) {
|
||||
std::cout << "Mismatch detected at index " << i << " : "
|
||||
<< tensor.data()[i] << " vs "
|
||||
<< single_pixel_patch_row_major.data()[i] << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.data()[i],
|
||||
tensor_row_major.data()[i]);
|
||||
VERIFY_IS_EQUAL(tensor.data()[i], tensor_row_major.data()[i]);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.data()[i],
|
||||
single_pixel_patch_row_major.data()[i]);
|
||||
}
|
||||
|
||||
// Entire image patch: ColMajor
|
||||
Tensor<float, 5> entire_image_patch;
|
||||
entire_image_patch = tensor.extract_image_patches<3, 5>();
|
||||
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(1), 3);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(2), 5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(3), 3*5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(4), 7);
|
||||
|
||||
// Entire image patch: RowMajor
|
||||
Tensor<float, 5, RowMajor> entire_image_patch_row_major;
|
||||
entire_image_patch_row_major = tensor_row_major.extract_image_patches<3, 5>();
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(0), 7);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(1), 3*5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(2), 5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(3), 3);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(4), 2);
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
int patchId = i+3*j;
|
||||
@ -51,13 +88,27 @@ static void test_simple_patch()
|
||||
for (int d = 0; d < 2; ++d) {
|
||||
for (int b = 0; b < 7; ++b) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-1+i >= 0 && c-2+j >= 0 && r-1+i < 3 && c-2+j < 5) {
|
||||
expected = tensor(d, r-1+i, c-2+j, b);
|
||||
expected_row_major = tensor_row_major(b, c-2+j, r-1+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (entire_image_patch(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(entire_image_patch(d, r, c, patchId, b), expected);
|
||||
// RowMajor
|
||||
if (entire_image_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(entire_image_patch_row_major(b, patchId, c, r, d),
|
||||
expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,15 +116,25 @@ static void test_simple_patch()
|
||||
}
|
||||
}
|
||||
|
||||
// 2D patch: ColMajor
|
||||
Tensor<float, 5> twod_patch;
|
||||
twod_patch = tensor.extract_image_patches<2, 2>();
|
||||
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(1), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(2), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(3), 3*5);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(4), 7);
|
||||
|
||||
// 2D patch: RowMajor
|
||||
Tensor<float, 5, RowMajor> twod_patch_row_major;
|
||||
twod_patch_row_major = tensor_row_major.extract_image_patches<2, 2>();
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(0), 7);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(1), 3*5);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(2), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(3), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(4), 2);
|
||||
|
||||
|
||||
// Based on the calculation described in TensorTraits.h, padding happens to be 0.
|
||||
int row_padding = 0;
|
||||
int col_padding = 0;
|
||||
@ -87,8 +148,10 @@ static void test_simple_patch()
|
||||
for (int d = 0; d < 2; ++d) {
|
||||
for (int b = 0; b < 7; ++b) {
|
||||
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;
|
||||
// ColMajor
|
||||
if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor.dimension(1) && col_offset < tensor.dimension(2)) {
|
||||
expected = tensor(d, row_offset, col_offset, b);
|
||||
}
|
||||
@ -96,6 +159,18 @@ static void test_simple_patch()
|
||||
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(d, r, c, patchId, b), expected);
|
||||
|
||||
// RowMajor
|
||||
if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor_row_major.dimension(2) && col_offset < tensor_row_major.dimension(1)) {
|
||||
expected_row_major = tensor_row_major(b, col_offset, row_offset, d);
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -118,6 +193,7 @@ static void test_patch_padding_valid()
|
||||
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, PADDING_VALID);
|
||||
|
||||
VERIFY_IS_EQUAL(result.dimension(0), input_depth); // depth
|
||||
@ -126,6 +202,20 @@ static void test_patch_padding_valid()
|
||||
VERIFY_IS_EQUAL(result.dimension(3), 1); // number of patches
|
||||
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
|
||||
|
||||
// RowMajor
|
||||
Tensor<float, 4, RowMajor> tensor_row_major = tensor.swap_layout();
|
||||
VERIFY_IS_EQUAL(tensor.dimension(0), tensor_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(1), tensor_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(2), tensor_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(3), tensor_row_major.dimension(0));
|
||||
|
||||
Tensor<float, 5, RowMajor> result_row_major = tensor_row_major.extract_image_patches(ksize, ksize, stride, stride, PADDING_VALID);
|
||||
VERIFY_IS_EQUAL(result.dimension(0), result_row_major.dimension(4));
|
||||
VERIFY_IS_EQUAL(result.dimension(1), result_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(result.dimension(2), result_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(result.dimension(3), result_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(result.dimension(4), result_row_major.dimension(0));
|
||||
|
||||
// No padding is carried out.
|
||||
int row_padding = 0;
|
||||
int col_padding = 0;
|
||||
@ -138,15 +228,25 @@ static void test_patch_padding_valid()
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -164,6 +264,7 @@ static void test_patch_padding_valid_same_value()
|
||||
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, PADDING_VALID);
|
||||
@ -174,6 +275,20 @@ static void test_patch_padding_valid_same_value()
|
||||
VERIFY_IS_EQUAL(result.dimension(3), 4); // number of patches
|
||||
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
|
||||
|
||||
// RowMajor
|
||||
Tensor<float, 4, RowMajor> tensor_row_major = tensor.swap_layout();
|
||||
VERIFY_IS_EQUAL(tensor.dimension(0), tensor_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(1), tensor_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(2), tensor_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(3), tensor_row_major.dimension(0));
|
||||
|
||||
Tensor<float, 5, RowMajor> result_row_major = tensor_row_major.extract_image_patches(ksize, ksize, stride, stride, PADDING_VALID);
|
||||
VERIFY_IS_EQUAL(result.dimension(0), result_row_major.dimension(4));
|
||||
VERIFY_IS_EQUAL(result.dimension(1), result_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(result.dimension(2), result_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(result.dimension(3), result_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(result.dimension(4), result_row_major.dimension(0));
|
||||
|
||||
// No padding is carried out.
|
||||
int row_padding = 0;
|
||||
int col_padding = 0;
|
||||
@ -186,15 +301,25 @@ static void test_patch_padding_valid_same_value()
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -212,6 +337,7 @@ static void test_patch_padding_same()
|
||||
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) {
|
||||
@ -225,6 +351,20 @@ static void test_patch_padding_same()
|
||||
VERIFY_IS_EQUAL(result.dimension(3), 2); // number of patches
|
||||
VERIFY_IS_EQUAL(result.dimension(4), input_batches); // number of batches
|
||||
|
||||
// RowMajor
|
||||
Tensor<float, 4, RowMajor> tensor_row_major = tensor.swap_layout();
|
||||
VERIFY_IS_EQUAL(tensor.dimension(0), tensor_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(1), tensor_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(2), tensor_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(3), tensor_row_major.dimension(0));
|
||||
|
||||
Tensor<float, 5, RowMajor> result_row_major = tensor_row_major.extract_image_patches(ksize, ksize, stride, stride, PADDING_SAME);
|
||||
VERIFY_IS_EQUAL(result.dimension(0), result_row_major.dimension(4));
|
||||
VERIFY_IS_EQUAL(result.dimension(1), result_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(result.dimension(2), result_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(result.dimension(3), result_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(result.dimension(4), result_row_major.dimension(0));
|
||||
|
||||
// Based on the calculation described in TensorTraits.h, padding happens to be
|
||||
// 0.
|
||||
int row_padding = 0;
|
||||
@ -238,15 +378,25 @@ static void test_patch_padding_same()
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -259,30 +409,62 @@ static void test_patch_no_extra_dim()
|
||||
{
|
||||
Tensor<float, 3> tensor(2,3,5);
|
||||
tensor.setRandom();
|
||||
Tensor<float, 3, RowMajor> tensor_row_major = tensor.swap_layout();
|
||||
VERIFY_IS_EQUAL(tensor.dimension(0), tensor_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(1), tensor_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(tensor.dimension(2), tensor_row_major.dimension(0));
|
||||
|
||||
// Single pixel patch: ColMajor
|
||||
Tensor<float, 4> single_pixel_patch;
|
||||
single_pixel_patch = tensor.extract_image_patches<1, 1>();
|
||||
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(1), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(2), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.dimension(3), 3*5);
|
||||
|
||||
// Single pixel patch: RowMajor
|
||||
Tensor<float, 4, RowMajor> single_pixel_patch_row_major;
|
||||
single_pixel_patch_row_major = tensor_row_major.extract_image_patches<1, 1>();
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(0), 3*5);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(1), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(2), 1);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.dimension(3), 2);
|
||||
|
||||
for (int i = 0; i < tensor.size(); ++i) {
|
||||
// ColMajor
|
||||
if (tensor.data()[i] != single_pixel_patch.data()[i]) {
|
||||
std::cout << "Mismatch detected at index " << i << " : " << tensor.data()[i] << " vs " << single_pixel_patch.data()[i] << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.data()[i], tensor.data()[i]);
|
||||
// RowMajor
|
||||
if (tensor_row_major.data()[i] != single_pixel_patch_row_major.data()[i]) {
|
||||
std::cout << "Mismatch detected at index " << i << " : "
|
||||
<< tensor.data()[i] << " vs "
|
||||
<< single_pixel_patch_row_major.data()[i] << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(single_pixel_patch_row_major.data()[i],
|
||||
tensor_row_major.data()[i]);
|
||||
VERIFY_IS_EQUAL(tensor.data()[i], tensor_row_major.data()[i]);
|
||||
VERIFY_IS_EQUAL(single_pixel_patch.data()[i],
|
||||
single_pixel_patch_row_major.data()[i]);
|
||||
}
|
||||
|
||||
// Entire image patch: ColMajor
|
||||
Tensor<float, 4> entire_image_patch;
|
||||
entire_image_patch = tensor.extract_image_patches<3, 5>();
|
||||
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(1), 3);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(2), 5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch.dimension(3), 3*5);
|
||||
|
||||
// Entire image patch: RowMajor
|
||||
Tensor<float, 4, RowMajor> entire_image_patch_row_major;
|
||||
entire_image_patch_row_major = tensor_row_major.extract_image_patches<3, 5>();
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(0), 3*5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(1), 5);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(2), 3);
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major.dimension(3), 2);
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
int patchId = i+3*j;
|
||||
@ -290,27 +472,47 @@ static void test_patch_no_extra_dim()
|
||||
for (int c = 0; c < 5; ++c) {
|
||||
for (int d = 0; d < 2; ++d) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-1+i >= 0 && c-2+j >= 0 && r-1+i < 3 && c-2+j < 5) {
|
||||
expected = tensor(d, r-1+i, c-2+j);
|
||||
expected_row_major = tensor_row_major(c-2+j, r-1+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (entire_image_patch(d, r, c, patchId) != expected) {
|
||||
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(entire_image_patch(d, r, c, patchId), expected);
|
||||
// RowMajor
|
||||
if (entire_image_patch_row_major(patchId, c, r, d) !=
|
||||
expected_row_major) {
|
||||
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(entire_image_patch_row_major(patchId, c, r, d),
|
||||
expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2D patch: ColMajor
|
||||
Tensor<float, 4> twod_patch;
|
||||
twod_patch = tensor.extract_image_patches<2, 2>();
|
||||
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(0), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(1), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(2), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch.dimension(3), 3*5);
|
||||
|
||||
// 2D patch: RowMajor
|
||||
Tensor<float, 4, RowMajor> twod_patch_row_major;
|
||||
twod_patch_row_major = tensor_row_major.extract_image_patches<2, 2>();
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(0), 3*5);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(1), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(2), 2);
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major.dimension(3), 2);
|
||||
|
||||
// Based on the calculation described in TensorTraits.h, padding happens to be 0.
|
||||
int row_padding = 0;
|
||||
int col_padding = 0;
|
||||
@ -323,8 +525,10 @@ static void test_patch_no_extra_dim()
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
for (int d = 0; d < 2; ++d) {
|
||||
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;
|
||||
// ColMajor
|
||||
if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor.dimension(1) && col_offset < tensor.dimension(2)) {
|
||||
expected = tensor(d, row_offset, col_offset);
|
||||
}
|
||||
@ -332,6 +536,16 @@ static void test_patch_no_extra_dim()
|
||||
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(twod_patch(d, r, c, patchId), expected);
|
||||
// RowMajor
|
||||
if (row_offset >= 0 && col_offset >= 0 && row_offset < tensor_row_major.dimension(1) && col_offset < tensor_row_major.dimension(0)) {
|
||||
expected_row_major = tensor_row_major(col_offset, row_offset, d);
|
||||
}
|
||||
if (twod_patch_row_major(patchId, c, r, d) != expected_row_major) {
|
||||
std::cout << "Mismatch detected at index i=" << i << " j=" << j << " r=" << r << " c=" << c << " d=" << d << std::endl;
|
||||
}
|
||||
VERIFY_IS_EQUAL(twod_patch_row_major(patchId, c, r, d), expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -339,20 +553,35 @@ static void test_patch_no_extra_dim()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void test_imagenet_patches()
|
||||
{
|
||||
// Test the code on typical configurations used by the 'imagenet' benchmarks at
|
||||
// https://github.com/soumith/convnet-benchmarks
|
||||
Tensor<float, 4> l_in(3, 128, 128, 128);
|
||||
// ColMajor
|
||||
Tensor<float, 4> l_in(3, 128, 128, 16);
|
||||
l_in.setRandom();
|
||||
Tensor<float, 5> l_out = l_in.extract_image_patches(11, 11);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 3);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(1), 11);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(2), 11);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(3), 128*128);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 128);
|
||||
for (int b = 0; b < 128; ++b) {
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 16);
|
||||
|
||||
// RowMajor
|
||||
Tensor<float, 4, RowMajor> l_in_row_major = l_in.swap_layout();
|
||||
VERIFY_IS_EQUAL(l_in.dimension(0), l_in_row_major.dimension(3));
|
||||
VERIFY_IS_EQUAL(l_in.dimension(1), l_in_row_major.dimension(2));
|
||||
VERIFY_IS_EQUAL(l_in.dimension(2), l_in_row_major.dimension(1));
|
||||
VERIFY_IS_EQUAL(l_in.dimension(3), l_in_row_major.dimension(0));
|
||||
|
||||
Tensor<float, 5, RowMajor> l_out_row_major = l_in_row_major.extract_image_patches(11, 11);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(0), 16);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(1), 128*128);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(2), 11);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(3), 11);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(4), 3);
|
||||
|
||||
for (int b = 0; b < 16; ++b) {
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
for (int j = 0; j < 128; ++j) {
|
||||
int patchId = i+128*j;
|
||||
@ -360,13 +589,27 @@ static void test_imagenet_patches()
|
||||
for (int r = 0; r < 11; ++r) {
|
||||
for (int d = 0; d < 3; ++d) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-5+i >= 0 && c-5+j >= 0 && r-5+i < 128 && c-5+j < 128) {
|
||||
expected = l_in(d, r-5+i, c-5+j, b);
|
||||
expected_row_major = l_in_row_major(b, c-5+j, r-5+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (l_out(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(l_out(d, r, c, patchId, b), expected);
|
||||
// RowMajor
|
||||
if (l_out_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(l_out_row_major(b, patchId, c, r, d),
|
||||
expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -374,29 +617,50 @@ static void test_imagenet_patches()
|
||||
}
|
||||
}
|
||||
|
||||
l_in.resize(64, 64, 64, 128);
|
||||
// ColMajor
|
||||
l_in.resize(16, 64, 64, 32);
|
||||
l_in.setRandom();
|
||||
l_out = l_in.extract_image_patches(9, 9);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 64);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 16);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(1), 9);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(2), 9);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(3), 64*64);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 128);
|
||||
for (int b = 0; b < 128; ++b) {
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 32);
|
||||
|
||||
// RowMajor
|
||||
l_in_row_major = l_in.swap_layout();
|
||||
l_out_row_major = l_in_row_major.extract_image_patches(9, 9);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(0), 32);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(1), 64*64);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(2), 9);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(3), 9);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(4), 16);
|
||||
|
||||
for (int b = 0; b < 32; ++b) {
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
for (int j = 0; j < 64; ++j) {
|
||||
int patchId = i+64*j;
|
||||
for (int c = 0; c < 9; ++c) {
|
||||
for (int r = 0; r < 9; ++r) {
|
||||
for (int d = 0; d < 64; ++d) {
|
||||
for (int d = 0; d < 16; ++d) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-4+i >= 0 && c-4+j >= 0 && r-4+i < 64 && c-4+j < 64) {
|
||||
expected = l_in(d, r-4+i, c-4+j, b);
|
||||
expected_row_major = l_in_row_major(b, c-4+j, r-4+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (l_out(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(l_out(d, r, c, patchId, b), expected);
|
||||
// RowMajor
|
||||
if (l_out_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(l_out_row_major(b, patchId, c, r, d), expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -404,29 +668,50 @@ static void test_imagenet_patches()
|
||||
}
|
||||
}
|
||||
|
||||
l_in.resize(128, 16, 16, 128);
|
||||
// ColMajor
|
||||
l_in.resize(32, 16, 16, 32);
|
||||
l_in.setRandom();
|
||||
l_out = l_in.extract_image_patches(7, 7);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 128);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 32);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(1), 7);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(2), 7);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(3), 16*16);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 128);
|
||||
for (int b = 0; b < 128; ++b) {
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 32);
|
||||
|
||||
// RowMajor
|
||||
l_in_row_major = l_in.swap_layout();
|
||||
l_out_row_major = l_in_row_major.extract_image_patches(7, 7);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(0), 32);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(1), 16*16);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(2), 7);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(3), 7);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(4), 32);
|
||||
|
||||
for (int b = 0; b < 32; ++b) {
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
for (int j = 0; j < 16; ++j) {
|
||||
int patchId = i+16*j;
|
||||
for (int c = 0; c < 7; ++c) {
|
||||
for (int r = 0; r < 7; ++r) {
|
||||
for (int d = 0; d < 128; ++d) {
|
||||
for (int d = 0; d < 32; ++d) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-3+i >= 0 && c-3+j >= 0 && r-3+i < 16 && c-3+j < 16) {
|
||||
expected = l_in(d, r-3+i, c-3+j, b);
|
||||
expected_row_major = l_in_row_major(b, c-3+j, r-3+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (l_out(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(l_out(d, r, c, patchId, b), expected);
|
||||
// RowMajor
|
||||
if (l_out_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(l_out_row_major(b, patchId, c, r, d), expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -434,29 +719,50 @@ static void test_imagenet_patches()
|
||||
}
|
||||
}
|
||||
|
||||
l_in.resize(384, 13, 13, 128);
|
||||
// ColMajor
|
||||
l_in.resize(64, 13, 13, 32);
|
||||
l_in.setRandom();
|
||||
l_out = l_in.extract_image_patches(3, 3);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 384);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(0), 64);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(1), 3);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(2), 3);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(3), 13*13);
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 128);
|
||||
for (int b = 0; b < 128; ++b) {
|
||||
VERIFY_IS_EQUAL(l_out.dimension(4), 32);
|
||||
|
||||
// RowMajor
|
||||
l_in_row_major = l_in.swap_layout();
|
||||
l_out_row_major = l_in_row_major.extract_image_patches(3, 3);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(0), 32);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(1), 13*13);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(2), 3);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(3), 3);
|
||||
VERIFY_IS_EQUAL(l_out_row_major.dimension(4), 64);
|
||||
|
||||
for (int b = 0; b < 32; ++b) {
|
||||
for (int i = 0; i < 13; ++i) {
|
||||
for (int j = 0; j < 13; ++j) {
|
||||
int patchId = i+13*j;
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
for (int d = 0; d < 384; ++d) {
|
||||
for (int d = 0; d < 64; ++d) {
|
||||
float expected = 0.0f;
|
||||
float expected_row_major = 0.0f;
|
||||
if (r-1+i >= 0 && c-1+j >= 0 && r-1+i < 13 && c-1+j < 13) {
|
||||
expected = l_in(d, r-1+i, c-1+j, b);
|
||||
expected_row_major = l_in_row_major(b, c-1+j, r-1+i, d);
|
||||
}
|
||||
// ColMajor
|
||||
if (l_out(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(l_out(d, r, c, patchId, b), expected);
|
||||
// RowMajor
|
||||
if (l_out_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(l_out_row_major(b, patchId, c, r, d), expected_row_major);
|
||||
// Check that ColMajor and RowMajor agree.
|
||||
VERIFY_IS_EQUAL(expected, expected_row_major);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user