Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorImagePatch.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
0011 #define EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
0012 
0013 namespace Eigen {
0014 
0015 /** \class TensorImagePatch
0016   * \ingroup CXX11_Tensor_Module
0017   *
0018   * \brief Patch extraction specialized for image processing.
0019   * This assumes that the input has a least 3 dimensions ordered as follow:
0020   *  1st dimension: channels (of size d)
0021   *  2nd dimension: rows (of size r)
0022   *  3rd dimension: columns (of size c)
0023   *  There can be additional dimensions such as time (for video) or batch (for
0024   * bulk processing after the first 3.
0025   * Calling the image patch code with patch_rows and patch_cols is equivalent
0026   * to calling the regular patch extraction code with parameters d, patch_rows,
0027   * patch_cols, and 1 for all the additional dimensions.
0028   */
0029 namespace internal {
0030 
0031 template<DenseIndex Rows, DenseIndex Cols, typename XprType>
0032 struct traits<TensorImagePatchOp<Rows, Cols, XprType> > : public traits<XprType>
0033 {
0034   typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
0035   typedef traits<XprType> XprTraits;
0036   typedef typename XprTraits::StorageKind StorageKind;
0037   typedef typename XprTraits::Index Index;
0038   typedef typename XprType::Nested Nested;
0039   typedef typename remove_reference<Nested>::type _Nested;
0040   static const int NumDimensions = XprTraits::NumDimensions + 1;
0041   static const int Layout = XprTraits::Layout;
0042   typedef typename XprTraits::PointerType PointerType;
0043 };
0044 
0045 template<DenseIndex Rows, DenseIndex Cols, typename XprType>
0046 struct eval<TensorImagePatchOp<Rows, Cols, XprType>, Eigen::Dense>
0047 {
0048   typedef const TensorImagePatchOp<Rows, Cols, XprType>& type;
0049 };
0050 
0051 template<DenseIndex Rows, DenseIndex Cols, typename XprType>
0052 struct nested<TensorImagePatchOp<Rows, Cols, XprType>, 1, typename eval<TensorImagePatchOp<Rows, Cols, XprType> >::type>
0053 {
0054   typedef TensorImagePatchOp<Rows, Cols, XprType> type;
0055 };
0056 
0057 template <typename Self, bool Vectorizable>
0058 struct ImagePatchCopyOp {
0059   typedef typename Self::Index Index;
0060   typedef typename Self::Scalar Scalar;
0061   typedef typename Self::Impl Impl;
0062   static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
0063       const Self& self, const Index num_coeff_to_copy, const Index dst_index,
0064       Scalar* dst_data, const Index src_index) {
0065     const Impl& impl = self.impl();
0066     for (Index i = 0; i < num_coeff_to_copy; ++i) {
0067       dst_data[dst_index + i] = impl.coeff(src_index + i);
0068     }
0069   }
0070 };
0071 
0072 template <typename Self>
0073 struct ImagePatchCopyOp<Self, true> {
0074   typedef typename Self::Index Index;
0075   typedef typename Self::Scalar Scalar;
0076   typedef typename Self::Impl Impl;
0077   typedef typename packet_traits<Scalar>::type Packet;
0078   static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
0079       const Self& self, const Index num_coeff_to_copy, const Index dst_index,
0080       Scalar* dst_data, const Index src_index) {
0081     const Impl& impl = self.impl();
0082     const Index packet_size = internal::unpacket_traits<Packet>::size;
0083     const Index vectorized_size =
0084         (num_coeff_to_copy / packet_size) * packet_size;
0085     for (Index i = 0; i < vectorized_size; i += packet_size) {
0086       Packet p = impl.template packet<Unaligned>(src_index + i);
0087       internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index + i, p);
0088     }
0089     for (Index i = vectorized_size; i < num_coeff_to_copy; ++i) {
0090       dst_data[dst_index + i] = impl.coeff(src_index + i);
0091     }
0092   }
0093 };
0094 
0095 template <typename Self>
0096 struct ImagePatchPaddingOp {
0097   typedef typename Self::Index Index;
0098   typedef typename Self::Scalar Scalar;
0099   typedef typename packet_traits<Scalar>::type Packet;
0100   static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
0101       const Index num_coeff_to_pad, const Scalar padding_value,
0102       const Index dst_index, Scalar* dst_data) {
0103     const Index packet_size = internal::unpacket_traits<Packet>::size;
0104     const Packet padded_packet = internal::pset1<Packet>(padding_value);
0105     const Index vectorized_size =
0106         (num_coeff_to_pad / packet_size) * packet_size;
0107     for (Index i = 0; i < vectorized_size; i += packet_size) {
0108       internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index + i,
0109                                                    padded_packet);
0110     }
0111     for (Index i = vectorized_size; i < num_coeff_to_pad; ++i) {
0112       dst_data[dst_index + i] = padding_value;
0113     }
0114   }
0115 };
0116 
0117 }  // end namespace internal
0118 
0119 template<DenseIndex Rows, DenseIndex Cols, typename XprType>
0120 class TensorImagePatchOp : public TensorBase<TensorImagePatchOp<Rows, Cols, XprType>, ReadOnlyAccessors>
0121 {
0122   public:
0123   typedef typename Eigen::internal::traits<TensorImagePatchOp>::Scalar Scalar;
0124   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
0125   typedef typename XprType::CoeffReturnType CoeffReturnType;
0126   typedef typename Eigen::internal::nested<TensorImagePatchOp>::type Nested;
0127   typedef typename Eigen::internal::traits<TensorImagePatchOp>::StorageKind StorageKind;
0128   typedef typename Eigen::internal::traits<TensorImagePatchOp>::Index Index;
0129 
0130   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
0131                                                            DenseIndex row_strides, DenseIndex col_strides,
0132                                                            DenseIndex in_row_strides, DenseIndex in_col_strides,
0133                                                            DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
0134                                                            PaddingType padding_type, Scalar padding_value)
0135                                                            : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
0136                                                            m_row_strides(row_strides), m_col_strides(col_strides),
0137                                                            m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
0138                                                            m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
0139                                                            m_padding_explicit(false), m_padding_top(0), m_padding_bottom(0), m_padding_left(0), m_padding_right(0),
0140                                                            m_padding_type(padding_type), m_padding_value(padding_value) {}
0141 
0142   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
0143                                                            DenseIndex row_strides, DenseIndex col_strides,
0144                                                            DenseIndex in_row_strides, DenseIndex in_col_strides,
0145                                                            DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
0146                                                            DenseIndex padding_top, DenseIndex padding_bottom,
0147                                                            DenseIndex padding_left, DenseIndex padding_right,
0148                                                            Scalar padding_value)
0149                                                            : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
0150                                                            m_row_strides(row_strides), m_col_strides(col_strides),
0151                                                            m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
0152                                                            m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
0153                                                            m_padding_explicit(true), m_padding_top(padding_top), m_padding_bottom(padding_bottom),
0154                                                            m_padding_left(padding_left), m_padding_right(padding_right),
0155                                                            m_padding_type(PADDING_VALID), m_padding_value(padding_value) {}
0156 
0157 
0158     EIGEN_DEVICE_FUNC
0159     DenseIndex patch_rows() const { return m_patch_rows; }
0160     EIGEN_DEVICE_FUNC
0161     DenseIndex patch_cols() const { return m_patch_cols; }
0162     EIGEN_DEVICE_FUNC
0163     DenseIndex row_strides() const { return m_row_strides; }
0164     EIGEN_DEVICE_FUNC
0165     DenseIndex col_strides() const { return m_col_strides; }
0166     EIGEN_DEVICE_FUNC
0167     DenseIndex in_row_strides() const { return m_in_row_strides; }
0168     EIGEN_DEVICE_FUNC
0169     DenseIndex in_col_strides() const { return m_in_col_strides; }
0170     EIGEN_DEVICE_FUNC
0171     DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
0172     EIGEN_DEVICE_FUNC
0173     DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
0174     EIGEN_DEVICE_FUNC
0175     bool padding_explicit() const { return m_padding_explicit; }
0176     EIGEN_DEVICE_FUNC
0177     DenseIndex padding_top() const { return m_padding_top; }
0178     EIGEN_DEVICE_FUNC
0179     DenseIndex padding_bottom() const { return m_padding_bottom; }
0180     EIGEN_DEVICE_FUNC
0181     DenseIndex padding_left() const { return m_padding_left; }
0182     EIGEN_DEVICE_FUNC
0183     DenseIndex padding_right() const { return m_padding_right; }
0184     EIGEN_DEVICE_FUNC
0185     PaddingType padding_type() const { return m_padding_type; }
0186     EIGEN_DEVICE_FUNC
0187     Scalar padding_value() const { return m_padding_value; }
0188 
0189     EIGEN_DEVICE_FUNC
0190     const typename internal::remove_all<typename XprType::Nested>::type&
0191     expression() const { return m_xpr; }
0192 
0193   protected:
0194     typename XprType::Nested m_xpr;
0195     const DenseIndex m_patch_rows;
0196     const DenseIndex m_patch_cols;
0197     const DenseIndex m_row_strides;
0198     const DenseIndex m_col_strides;
0199     const DenseIndex m_in_row_strides;
0200     const DenseIndex m_in_col_strides;
0201     const DenseIndex m_row_inflate_strides;
0202     const DenseIndex m_col_inflate_strides;
0203     const bool m_padding_explicit;
0204     const DenseIndex m_padding_top;
0205     const DenseIndex m_padding_bottom;
0206     const DenseIndex m_padding_left;
0207     const DenseIndex m_padding_right;
0208     const PaddingType m_padding_type;
0209     const Scalar m_padding_value;
0210 };
0211 
0212 // Eval as rvalue
0213 template<DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
0214 struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
0215 {
0216   typedef TensorImagePatchOp<Rows, Cols, ArgType> XprType;
0217   typedef typename XprType::Index Index;
0218   static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
0219   static const int NumDims = NumInputDims + 1;
0220   typedef DSizes<Index, NumDims> Dimensions;
0221   typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
0222   typedef TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>,
0223                           Device> Self;
0224   typedef TensorEvaluator<ArgType, Device> Impl;
0225   typedef typename XprType::CoeffReturnType CoeffReturnType;
0226   typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
0227   static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
0228   typedef StorageMemory<CoeffReturnType, Device> Storage;
0229   typedef typename Storage::Type EvaluatorPointerType;
0230 
0231   enum {
0232     IsAligned         = false,
0233     PacketAccess      = TensorEvaluator<ArgType, Device>::PacketAccess,
0234     BlockAccess       = false,
0235     PreferBlockAccess = true,
0236     Layout            = TensorEvaluator<ArgType, Device>::Layout,
0237     CoordAccess       = false,
0238     RawAccess         = false
0239   };
0240 
0241   //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
0242   typedef internal::TensorBlockNotImplemented TensorBlock;
0243   //===--------------------------------------------------------------------===//
0244 
0245   EIGEN_STRONG_INLINE TensorEvaluator( const XprType& op, const Device& device)
0246       : m_device(device), m_impl(op.expression(), device)
0247   {
0248     EIGEN_STATIC_ASSERT((NumDims >= 4), YOU_MADE_A_PROGRAMMING_MISTAKE);
0249 
0250     m_paddingValue = op.padding_value();
0251 
0252     const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
0253 
0254     // Caches a few variables.
0255     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
0256       m_inputDepth = input_dims[0];
0257       m_inputRows = input_dims[1];
0258       m_inputCols = input_dims[2];
0259     } else {
0260       m_inputDepth = input_dims[NumInputDims-1];
0261       m_inputRows = input_dims[NumInputDims-2];
0262       m_inputCols = input_dims[NumInputDims-3];
0263     }
0264 
0265     m_row_strides = op.row_strides();
0266     m_col_strides = op.col_strides();
0267 
0268     // Input strides and effective input/patch size
0269     m_in_row_strides = op.in_row_strides();
0270     m_in_col_strides = op.in_col_strides();
0271     m_row_inflate_strides = op.row_inflate_strides();
0272     m_col_inflate_strides = op.col_inflate_strides();
0273     // The "effective" input rows and input cols are the input rows and cols
0274     // after inflating them with zeros.
0275     // For examples, a 2x3 matrix with row_inflate_strides and
0276     // col_inflate_strides of 2 comes from:
0277     //   A B C
0278     //   D E F
0279     //
0280     // to a matrix is 3 x 5:
0281     //
0282     //   A . B . C
0283     //   . . . . .
0284     //   D . E . F
0285 
0286     m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
0287     m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
0288     m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
0289     m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
0290 
0291     if (op.padding_explicit()) {
0292       m_outputRows = numext::ceil((m_input_rows_eff + op.padding_top() + op.padding_bottom() - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
0293       m_outputCols = numext::ceil((m_input_cols_eff + op.padding_left() + op.padding_right() - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
0294       m_rowPaddingTop = op.padding_top();
0295       m_colPaddingLeft = op.padding_left();
0296     } else {
0297       // Computing padding from the type
0298       switch (op.padding_type()) {
0299         case PADDING_VALID:
0300           m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
0301           m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
0302           // Calculate the padding
0303           m_rowPaddingTop = numext::maxi<Index>(0, ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2);
0304           m_colPaddingLeft = numext::maxi<Index>(0, ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2);
0305           break;
0306         case PADDING_SAME:
0307           m_outputRows = numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
0308           m_outputCols = numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
0309           // Calculate the padding
0310           m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2;
0311           m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2;
0312           // The padding size calculation for PADDING_SAME has been updated to
0313           // be consistent with how TensorFlow extracts its paddings.
0314           m_rowPaddingTop = numext::maxi<Index>(0, m_rowPaddingTop);
0315           m_colPaddingLeft = numext::maxi<Index>(0, m_colPaddingLeft);
0316           break;
0317         default:
0318           eigen_assert(false && "unexpected padding");
0319           m_outputCols=0; // silence the uninitialised warning;
0320           m_outputRows=0; //// silence the uninitialised warning;
0321       }
0322     }
0323     eigen_assert(m_outputRows > 0);
0324     eigen_assert(m_outputCols > 0);
0325 
0326     // Dimensions for result of extraction.
0327     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
0328       // ColMajor
0329       // 0: depth
0330       // 1: patch_rows
0331       // 2: patch_cols
0332       // 3: number of patches
0333       // 4 and beyond: anything else (such as batch).
0334       m_dimensions[0] = input_dims[0];
0335       m_dimensions[1] = op.patch_rows();
0336       m_dimensions[2] = op.patch_cols();
0337       m_dimensions[3] = m_outputRows * m_outputCols;
0338       for (int i = 4; i < NumDims; ++i) {
0339         m_dimensions[i] = input_dims[i-1];
0340       }
0341     } else {
0342       // RowMajor
0343       // NumDims-1: depth
0344       // NumDims-2: patch_rows
0345       // NumDims-3: patch_cols
0346       // NumDims-4: number of patches
0347       // NumDims-5 and beyond: anything else (such as batch).
0348       m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
0349       m_dimensions[NumDims-2] = op.patch_rows();
0350       m_dimensions[NumDims-3] = op.patch_cols();
0351       m_dimensions[NumDims-4] = m_outputRows * m_outputCols;
0352       for (int i = NumDims-5; i >= 0; --i) {
0353         m_dimensions[i] = input_dims[i];
0354       }
0355     }
0356 
0357     // Strides for moving the patch in various dimensions.
0358     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
0359       m_colStride = m_dimensions[1];
0360       m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
0361       m_otherStride = m_patchStride * m_dimensions[3];
0362     } else {
0363       m_colStride = m_dimensions[NumDims-2];
0364       m_patchStride = m_colStride * m_dimensions[NumDims-3] * m_dimensions[NumDims-1];
0365       m_otherStride = m_patchStride * m_dimensions[NumDims-4];
0366     }
0367 
0368     // Strides for navigating through the input tensor.
0369     m_rowInputStride = m_inputDepth;
0370     m_colInputStride = m_inputDepth * m_inputRows;
0371     m_patchInputStride = m_inputDepth * m_inputRows * m_inputCols;
0372 
0373     // Fast representations of different variables.
0374     m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
0375     m_fastPatchStride = internal::TensorIntDivisor<Index>(m_patchStride);
0376     m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
0377     m_fastInflateRowStride = internal::TensorIntDivisor<Index>(m_row_inflate_strides);
0378     m_fastInflateColStride = internal::TensorIntDivisor<Index>(m_col_inflate_strides);
0379     m_fastInputColsEff = internal::TensorIntDivisor<Index>(m_input_cols_eff);
0380 
0381     // Number of patches in the width dimension.
0382     m_fastOutputRows = internal::TensorIntDivisor<Index>(m_outputRows);
0383     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
0384       m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[0]);
0385     } else {
0386       m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[NumDims-1]);
0387     }
0388   }
0389 
0390   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
0391 
0392   EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
0393     m_impl.evalSubExprsIfNeeded(NULL);
0394     return true;
0395   }
0396 
0397 #ifdef EIGEN_USE_THREADS
0398   template <typename EvalSubExprsCallback>
0399   EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
0400       EvaluatorPointerType, EvalSubExprsCallback done) {
0401     m_impl.evalSubExprsIfNeededAsync(nullptr, [done](bool) { done(true); });
0402   }
0403 #endif  // EIGEN_USE_THREADS
0404 
0405   EIGEN_STRONG_INLINE void cleanup() {
0406     m_impl.cleanup();
0407   }
0408 
0409   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
0410   {
0411     // Patch index corresponding to the passed in index.
0412     const Index patchIndex = index / m_fastPatchStride;
0413     // Find the offset of the element wrt the location of the first element.
0414     const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
0415 
0416     // Other ways to index this element.
0417     const Index otherIndex = (NumDims == 4) ? 0 : index / m_fastOtherStride;
0418     const Index patch2DIndex = (NumDims == 4) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
0419 
0420     // Calculate col index in the input original tensor.
0421     const Index colIndex = patch2DIndex / m_fastOutputRows;
0422     const Index colOffset = patchOffset / m_fastColStride;
0423     const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
0424     const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInflateColStride) : 0);
0425     if (inputCol < 0 || inputCol >= m_input_cols_eff ||
0426         ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
0427       return Scalar(m_paddingValue);
0428     }
0429 
0430     // Calculate row index in the original input tensor.
0431     const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
0432     const Index rowOffset = patchOffset - colOffset * m_colStride;
0433     const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
0434     const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInflateRowStride) : 0);
0435     if (inputRow < 0 || inputRow >= m_input_rows_eff ||
0436         ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
0437       return Scalar(m_paddingValue);
0438     }
0439 
0440     const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
0441     const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
0442 
0443     const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride + otherIndex * m_patchInputStride;
0444     return m_impl.coeff(inputIndex);
0445   }
0446 
0447   template<int LoadMode>
0448   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
0449   {
0450     EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
0451     eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
0452 
0453     if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1) {
0454       return packetWithPossibleZero(index);
0455     }
0456 
0457     const Index indices[2] = {index, index + PacketSize - 1};
0458     const Index patchIndex = indices[0] / m_fastPatchStride;
0459     if (patchIndex != indices[1] / m_fastPatchStride) {
0460       return packetWithPossibleZero(index);
0461     }
0462     const Index otherIndex = (NumDims == 4) ? 0 : indices[0] / m_fastOtherStride;
0463     eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
0464 
0465     // Find the offset of the element wrt the location of the first element.
0466     const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
0467                                    (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
0468 
0469     const Index patch2DIndex = (NumDims == 4) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
0470     eigen_assert(patch2DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
0471 
0472     const Index colIndex = patch2DIndex / m_fastOutputRows;
0473     const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
0474 
0475     // Calculate col indices in the original input tensor.
0476     const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] -
0477       m_colPaddingLeft, colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
0478     if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
0479       return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
0480     }
0481 
0482     if (inputCols[0] == inputCols[1]) {
0483       const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
0484       const Index rowOffsets[2] = {patchOffsets[0] - colOffsets[0]*m_colStride, patchOffsets[1] - colOffsets[1]*m_colStride};
0485       eigen_assert(rowOffsets[0] <= rowOffsets[1]);
0486       // Calculate col indices in the original input tensor.
0487       const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] -
0488         m_rowPaddingTop, rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
0489 
0490       if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
0491         return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
0492       }
0493 
0494       if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) {
0495         // no padding
0496         const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
0497         const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
0498         const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride;
0499         return m_impl.template packet<Unaligned>(inputIndex);
0500       }
0501     }
0502 
0503     return packetWithPossibleZero(index);
0504   }
0505 
0506   EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
0507 
0508   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
0509 
0510 #ifdef EIGEN_USE_SYCL
0511   // binding placeholder accessors to a command group handler for SYCL
0512   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
0513     m_impl.bind(cgh);
0514   }
0515 #endif
0516 
0517   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowPaddingTop() const { return m_rowPaddingTop; }
0518   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colPaddingLeft() const { return m_colPaddingLeft; }
0519   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputRows() const { return m_outputRows; }
0520   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputCols() const { return m_outputCols; }
0521   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userRowStride() const { return m_row_strides; }
0522   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userColStride() const { return m_col_strides; }
0523   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInRowStride() const { return m_in_row_strides; }
0524   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInColStride() const { return m_in_col_strides; }
0525   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowInflateStride() const { return m_row_inflate_strides; }
0526   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colInflateStride() const { return m_col_inflate_strides; }
0527 
0528   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
0529   costPerCoeff(bool vectorized) const {
0530     // We conservatively estimate the cost for the code path where the computed
0531     // index is inside the original image and
0532     // TensorEvaluator<ArgType, Device>::CoordAccess is false.
0533     const double compute_cost = 3 * TensorOpCost::DivCost<Index>() +
0534                                 6 * TensorOpCost::MulCost<Index>() +
0535                                 8 * TensorOpCost::MulCost<Index>();
0536     return m_impl.costPerCoeff(vectorized) +
0537            TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
0538   }
0539 
0540  protected:
0541   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
0542   {
0543     EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
0544     EIGEN_UNROLL_LOOP
0545     for (int i = 0; i < PacketSize; ++i) {
0546       values[i] = coeff(index+i);
0547     }
0548     PacketReturnType rslt = internal::pload<PacketReturnType>(values);
0549     return rslt;
0550   }
0551 
0552   Dimensions m_dimensions;
0553 
0554   Index m_otherStride;
0555   Index m_patchStride;
0556   Index m_colStride;
0557   Index m_row_strides;
0558   Index m_col_strides;
0559 
0560   Index m_in_row_strides;
0561   Index m_in_col_strides;
0562   Index m_row_inflate_strides;
0563   Index m_col_inflate_strides;
0564 
0565   Index m_input_rows_eff;
0566   Index m_input_cols_eff;
0567   Index m_patch_rows_eff;
0568   Index m_patch_cols_eff;
0569 
0570   internal::TensorIntDivisor<Index> m_fastOtherStride;
0571   internal::TensorIntDivisor<Index> m_fastPatchStride;
0572   internal::TensorIntDivisor<Index> m_fastColStride;
0573   internal::TensorIntDivisor<Index> m_fastInflateRowStride;
0574   internal::TensorIntDivisor<Index> m_fastInflateColStride;
0575   internal::TensorIntDivisor<Index> m_fastInputColsEff;
0576 
0577   Index m_rowInputStride;
0578   Index m_colInputStride;
0579   Index m_patchInputStride;
0580 
0581   Index m_inputDepth;
0582   Index m_inputRows;
0583   Index m_inputCols;
0584 
0585   Index m_outputRows;
0586   Index m_outputCols;
0587 
0588   Index m_rowPaddingTop;
0589   Index m_colPaddingLeft;
0590 
0591   internal::TensorIntDivisor<Index> m_fastOutputRows;
0592   internal::TensorIntDivisor<Index> m_fastOutputDepth;
0593 
0594   Scalar m_paddingValue;
0595 
0596   const Device EIGEN_DEVICE_REF m_device;
0597   TensorEvaluator<ArgType, Device> m_impl;
0598 };
0599 
0600 
0601 } // end namespace Eigen
0602 
0603 #endif // EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H