From a93af659383002063513099ed35efa9fe177bec8 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Tue, 7 Jul 2015 08:52:14 -0700 Subject: Improved and cleaned up the 2d patch extraction code --- .../Eigen/CXX11/src/Tensor/TensorImagePatch.h | 246 +++++++++++++++------ 1 file changed, 181 insertions(+), 65 deletions(-) (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h') diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h b/unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h index 478696c65..f2c56a9ac 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h @@ -30,7 +30,7 @@ namespace internal { template struct traits > : public traits { - typedef typename XprType::Scalar Scalar; + typedef typename internal::remove_const::type Scalar; typedef traits XprTraits; typedef typename packet_traits::type Packet; typedef typename XprTraits::StorageKind StorageKind; @@ -70,10 +70,30 @@ class TensorImagePatchOp : public TensorBase::type& @@ -96,10 +136,19 @@ class TensorImagePatchOp : public TensorBase struct TensorEvaluator, Device> @@ -109,7 +158,10 @@ struct TensorEvaluator, Device> static const int NumInputDims = internal::array_size::Dimensions>::value; static const int NumDims = NumInputDims + 1; typedef DSizes Dimensions; - typedef typename XprType::Scalar Scalar; + typedef typename internal::remove_const::type Scalar; + typedef TensorEvaluator, + Device> Self; + typedef TensorEvaluator Impl; enum { IsAligned = false, @@ -123,13 +175,17 @@ struct TensorEvaluator, Device> { EIGEN_STATIC_ASSERT(NumDims >= 4, YOU_MADE_A_PROGRAMMING_MISTAKE); + m_paddingValue = op.padding_value(); + const typename TensorEvaluator::Dimensions& input_dims = m_impl.dimensions(); // Caches a few variables. if (static_cast(Layout) == static_cast(ColMajor)) { + m_inputDepth = input_dims[0]; m_inputRows = input_dims[1]; m_inputCols = input_dims[2]; } else { + m_inputDepth = input_dims[NumInputDims-1]; m_inputRows = input_dims[NumInputDims-2]; m_inputCols = input_dims[NumInputDims-3]; } @@ -137,27 +193,57 @@ struct TensorEvaluator, Device> m_row_strides = op.row_strides(); m_col_strides = op.col_strides(); - // We only support same strides for both dimensions and square patches. - eigen_assert(m_row_strides == m_col_strides); - - switch (op.padding_type()) { - case PADDING_VALID: - m_outputRows = numext::ceil((m_inputRows - op.patch_rows() + 1.f) / static_cast(m_row_strides)); - m_outputCols = numext::ceil((m_inputCols - op.patch_cols() + 1.f) / static_cast(m_col_strides)); - // Calculate the padding - m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + op.patch_rows() - m_inputRows) / 2; - m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + op.patch_cols() - m_inputCols) / 2; - break; - case PADDING_SAME: - m_outputRows = numext::ceil(m_inputRows / static_cast(m_row_strides)); - m_outputCols = numext::ceil(m_inputCols / static_cast(m_col_strides)); - // Calculate the padding - m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + op.patch_rows() - m_inputRows) / 2; - m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + op.patch_cols() - m_inputCols) / 2; - break; - default: - eigen_assert(false && "unexpected padding"); + // Input strides and effective input/patch size + m_in_row_strides = op.in_row_strides(); + m_in_col_strides = op.in_col_strides(); + m_row_inflate_strides = op.row_inflate_strides(); + m_col_inflate_strides = op.col_inflate_strides(); + // The "effective" input rows and input cols are the input rows and cols + // after inflating them with zeros. + // For examples, a 2x3 matrix with row_inflate_strides and + // col_inflate_strides of 2 comes from: + // A B C + // D E F + // + // to a matrix is 3 x 5: + // + // A . B . C + // . . . . . + // D . E . F + + m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1; + m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1; + m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1); + m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1); + + if (op.padding_explicit()) { + m_outputRows = numext::ceil((m_input_rows_eff + op.padding_top() + op.padding_bottom() - m_patch_rows_eff + 1.f) / static_cast(m_row_strides)); + m_outputCols = numext::ceil((m_input_cols_eff + op.padding_left() + op.padding_right() - m_patch_cols_eff + 1.f) / static_cast(m_col_strides)); + m_rowPaddingTop = op.padding_top(); + m_colPaddingLeft = op.padding_left(); + } else { + // Computing padding from the type + switch (op.padding_type()) { + case PADDING_VALID: + m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast(m_row_strides)); + m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast(m_col_strides)); + // Calculate the padding + m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2; + m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2; + break; + case PADDING_SAME: + m_outputRows = numext::ceil(m_input_rows_eff / static_cast(m_row_strides)); + m_outputCols = numext::ceil(m_input_cols_eff / static_cast(m_col_strides)); + // Calculate the padding + m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2; + m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2; + break; + default: + eigen_assert(false && "unexpected padding"); } + } + eigen_assert(m_outputRows > 0); + eigen_assert(m_outputCols > 0); // Dimensions for result of extraction. if (static_cast(Layout) == static_cast(ColMajor)) { @@ -202,26 +288,24 @@ struct TensorEvaluator, Device> } // Strides for navigating through the input tensor. - if (static_cast(Layout) == static_cast(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]; - } + m_rowInputStride = m_inputDepth; + m_colInputStride = m_inputDepth * m_inputRows; + m_patchInputStride = m_inputDepth * m_inputRows * m_inputCols; // Fast representations of different variables. m_fastOtherStride = internal::TensorIntDivisor(m_otherStride); m_fastPatchStride = internal::TensorIntDivisor(m_patchStride); m_fastColStride = internal::TensorIntDivisor(m_colStride); + m_fastInputRowStride = internal::TensorIntDivisor(m_row_inflate_strides); + m_fastInputColStride = internal::TensorIntDivisor(m_col_inflate_strides); + m_fastInputColsEff = internal::TensorIntDivisor(m_input_cols_eff); + // Number of patches in the width dimension. m_fastOutputRows = internal::TensorIntDivisor(m_outputRows); if (static_cast(Layout) == static_cast(ColMajor)) { - m_fastDimZero = internal::TensorIntDivisor(m_dimensions[0]); + m_fastOutputDepth = internal::TensorIntDivisor(m_dimensions[0]); } else { - m_fastDimZero = internal::TensorIntDivisor(m_dimensions[NumDims-1]); + m_fastOutputDepth = internal::TensorIntDivisor(m_dimensions[NumDims-1]); } } @@ -244,33 +328,36 @@ struct TensorEvaluator, 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; + const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth; // Other ways to index this element. const Index otherIndex = (NumDims == 4) ? 0 : index / m_fastOtherStride; const Index patch2DIndex = (NumDims == 4) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride; + // Calculate col index in the input original tensor. const Index colIndex = patch2DIndex / m_fastOutputRows; const Index colOffset = patchOffset / m_fastColStride; - - // Calculate col index in the input original tensor. - const Index inputCol = colIndex * m_col_strides + colOffset - m_colPaddingLeft; - if (inputCol < 0 || inputCol >= m_inputCols) { - return Scalar(0); + const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft; + const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0); + if (inputCol < 0 || inputCol >= m_input_cols_eff || + ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) { + return Scalar(m_paddingValue); } - const Index rowIndex = patch2DIndex - colIndex * m_outputRows; - const Index rowOffset = patchOffset - colOffset * m_colStride; // Calculate row index in the original input tensor. - const Index inputRow = rowIndex * m_row_strides + rowOffset - m_rowPaddingTop; - if (inputRow < 0 || inputRow >= m_inputRows) { - return Scalar(0); + const Index rowIndex = patch2DIndex - colIndex * m_outputRows; + const Index rowOffset = patchOffset - colOffset * m_colStride; + const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop; + const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0); + if (inputRow < 0 || inputRow >= m_input_rows_eff || + ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) { + return Scalar(m_paddingValue); } const int depth_index = static_cast(Layout) == static_cast(ColMajor) ? 0 : NumDims - 1; - const Index depth = index - (index / m_fastDimZero) * m_dimensions[depth_index]; + const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index]; - const Index inputIndex = depth + inputRow * m_rowInputStride + inputCol * m_colInputStride + otherIndex * m_patchInputStride; + const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride + otherIndex * m_patchInputStride; return m_impl.coeff(inputIndex); } @@ -281,6 +368,10 @@ struct TensorEvaluator, Device> EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE) eigen_assert(index+packetSize-1 < dimensions().TotalSize()); + if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1) { + return packetWithPossibleZero(index); + } + const Index indices[2] = {index, index + packetSize - 1}; const Index patchIndex = indices[0] / m_fastPatchStride; if (patchIndex != indices[1] / m_fastPatchStride) { @@ -290,8 +381,8 @@ struct TensorEvaluator, Device> eigen_assert(otherIndex == indices[1] / m_fastOtherStride); // Find the offset of the element wrt the location of the first element. - const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastDimZero, - (indices[1] - patchIndex * m_patchStride) / m_fastDimZero}; + const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth, + (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth}; const Index patch2DIndex = (NumDims == 4) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride; eigen_assert(patch2DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride); @@ -303,8 +394,7 @@ struct TensorEvaluator, Device> const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] - m_colPaddingLeft, colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft}; if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) { - // all zeros - return internal::pset1(Scalar(0)); + return internal::pset1(Scalar(m_paddingValue)); } if (inputCols[0] == inputCols[1]) { @@ -316,14 +406,13 @@ struct TensorEvaluator, Device> m_rowPaddingTop, rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop}; if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) { - // all zeros - return internal::pset1(Scalar(0)); + return internal::pset1(Scalar(m_paddingValue)); } if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) { // no padding const int depth_index = static_cast(Layout) == static_cast(ColMajor) ? 0 : NumDims - 1; - const Index depth = index - (index / m_fastDimZero) * m_dimensions[depth_index]; + const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index]; const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride; return m_impl.template packet(inputIndex); } @@ -342,6 +431,10 @@ struct TensorEvaluator, Device> Index outputCols() const { return m_outputCols; } Index userRowStride() const { return m_row_strides; } Index userColStride() const { return m_col_strides; } + Index userInRowStride() const { return m_in_row_strides; } + Index userInColStride() const { return m_in_col_strides; } + Index rowInflateStride() const { return m_row_inflate_strides; } + Index colInflateStride() const { return m_col_inflate_strides; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(const array& coords) const { @@ -350,24 +443,30 @@ struct TensorEvaluator, Device> // 0: d, 1: patch_rows, 2: patch_cols, 3: number of patches, 4: number of batches // RowMajor // 0: number of batches, 1: number of patches, 2: patch_cols , 3: patch_rows, 4: d - const Index patchIndex = coords[static_cast(Layout) == static_cast(ColMajor) ? 3 : 1]; + const Index patch2DIndex = coords[static_cast(Layout) == static_cast(ColMajor) ? 3 : 1]; array inputCoords; + Index input_col_idx = patch2DIndex / m_fastInputColsEff; + Index inputCol = input_col_idx + coords[1] * m_in_row_strides - m_rowPaddingTop; + Index inputRow = patch2DIndex - input_col_idx * m_input_cols_eff + coords[2] * m_in_col_strides - m_colPaddingLeft; + const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInputColStride) : 0); + const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInputRowStride) : 0); if (static_cast(Layout) == static_cast(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[1] = origInputCol; + inputCoords[2] = origInputRow; 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[2] = origInputCol; + inputCoords[1] = origInputRow; 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) { - return Scalar(0); + if (inputCol < 0 || inputCol >= m_input_cols_eff || inputRow < 0 || inputRow >= m_input_rows_eff || + ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides)) || + ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) { + return Scalar(m_paddingValue); } if (TensorEvaluator::CoordAccess) { return m_impl.coeff(inputCoords); @@ -409,14 +508,29 @@ struct TensorEvaluator, Device> Index m_colStride; Index m_row_strides; Index m_col_strides; + + Index m_in_row_strides; + Index m_in_col_strides; + Index m_row_inflate_strides; + Index m_col_inflate_strides; + + Index m_input_rows_eff; + Index m_input_cols_eff; + Index m_patch_rows_eff; + Index m_patch_cols_eff; + internal::TensorIntDivisor m_fastOtherStride; internal::TensorIntDivisor m_fastPatchStride; internal::TensorIntDivisor m_fastColStride; + internal::TensorIntDivisor m_fastInputRowStride; + internal::TensorIntDivisor m_fastInputColStride; + internal::TensorIntDivisor m_fastInputColsEff; Index m_rowInputStride; Index m_colInputStride; Index m_patchInputStride; + Index m_inputDepth; Index m_inputRows; Index m_inputCols; @@ -427,7 +541,9 @@ struct TensorEvaluator, Device> Index m_colPaddingLeft; internal::TensorIntDivisor m_fastOutputRows; - internal::TensorIntDivisor m_fastDimZero; + internal::TensorIntDivisor m_fastOutputDepth; + + Scalar m_paddingValue; TensorEvaluator m_impl; }; -- cgit v1.2.3