10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H 11 #define EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H 30 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
37 typedef typename XprType::Nested
Nested;
39 static const int NumDimensions = XprTraits::NumDimensions + 1;
40 static const int Layout = XprTraits::Layout;
43 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
49 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
57 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
73 : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
74 m_row_strides(row_strides), m_col_strides(col_strides),
75 m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
76 m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
77 m_padding_explicit(false), m_padding_top(0), m_padding_bottom(0), m_padding_left(0), m_padding_right(0),
78 m_padding_type(padding_type), m_padding_value(padding_value) {}
87 : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
88 m_row_strides(row_strides), m_col_strides(col_strides),
89 m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
90 m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
91 m_padding_explicit(true), m_padding_top(padding_top), m_padding_bottom(padding_bottom),
92 m_padding_left(padding_left), m_padding_right(padding_right),
93 m_padding_type(
PADDING_VALID), m_padding_value(padding_value) {}
150 template<DenseIndex Rows, DenseIndex Cols,
typename ArgType,
typename Device>
156 static const int NumDims = NumInputDims + 1;
175 : m_impl(op.expression(), device)
184 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
185 m_inputDepth = input_dims[0];
186 m_inputRows = input_dims[1];
187 m_inputCols = input_dims[2];
189 m_inputDepth = input_dims[NumInputDims-1];
190 m_inputRows = input_dims[NumInputDims-2];
191 m_inputCols = input_dims[NumInputDims-3];
215 m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
216 m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
229 m_outputRows =
numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.
f) / static_cast<float>(m_row_strides));
230 m_outputCols =
numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.
f) / static_cast<float>(m_col_strides));
232 m_rowPaddingTop = numext::maxi<Index>(0, ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2);
233 m_colPaddingLeft = numext::maxi<Index>(0, ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2);
236 m_outputRows =
numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
237 m_outputCols =
numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
239 m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2;
240 m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2;
250 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
257 m_dimensions[0] = input_dims[0];
260 m_dimensions[3] = m_outputRows * m_outputCols;
261 for (
int i = 4; i < NumDims; ++i) {
262 m_dimensions[i] = input_dims[i-1];
271 m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
274 m_dimensions[NumDims-4] = m_outputRows * m_outputCols;
275 for (
int i = NumDims-5; i >= 0; --i) {
276 m_dimensions[i] = input_dims[i];
281 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
282 m_colStride = m_dimensions[1];
283 m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
284 m_otherStride = m_patchStride * m_dimensions[3];
286 m_colStride = m_dimensions[NumDims-2];
287 m_patchStride = m_colStride * m_dimensions[NumDims-3] * m_dimensions[NumDims-1];
288 m_otherStride = m_patchStride * m_dimensions[NumDims-4];
292 m_rowInputStride = m_inputDepth;
293 m_colInputStride = m_inputDepth * m_inputRows;
294 m_patchInputStride = m_inputDepth * m_inputRows * m_inputCols;
306 if (static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
316 m_impl.evalSubExprsIfNeeded(NULL);
327 const Index patchIndex = index / m_fastPatchStride;
329 const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
332 const Index otherIndex = (NumDims == 4) ? 0 : index / m_fastOtherStride;
333 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
336 const Index colIndex = patch2DIndex / m_fastOutputRows;
337 const Index colOffset = patchOffset / m_fastColStride;
338 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
339 const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInflateColStride) : 0);
340 if (inputCol < 0 || inputCol >= m_input_cols_eff ||
341 ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
342 return Scalar(m_paddingValue);
346 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
347 const Index rowOffset = patchOffset - colOffset * m_colStride;
348 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
349 const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInflateRowStride) : 0);
350 if (inputRow < 0 || inputRow >= m_input_rows_eff ||
351 ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
352 return Scalar(m_paddingValue);
355 const int depth_index =
static_cast<int>(Layout) == static_cast<int>(
ColMajor) ? 0 : NumDims - 1;
356 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
358 const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride + otherIndex * m_patchInputStride;
359 return m_impl.coeff(inputIndex);
362 template<
int LoadMode>
366 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
368 if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1) {
369 return packetWithPossibleZero(index);
372 const Index indices[2] = {index, index + PacketSize - 1};
373 const Index patchIndex = indices[0] / m_fastPatchStride;
374 if (patchIndex != indices[1] / m_fastPatchStride) {
375 return packetWithPossibleZero(index);
377 const Index otherIndex = (NumDims == 4) ? 0 : indices[0] / m_fastOtherStride;
378 eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
381 const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
382 (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
384 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
385 eigen_assert(patch2DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
387 const Index colIndex = patch2DIndex / m_fastOutputRows;
388 const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
391 const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] -
392 m_colPaddingLeft, colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
393 if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
394 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
397 if (inputCols[0] == inputCols[1]) {
398 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
399 const Index rowOffsets[2] = {patchOffsets[0] - colOffsets[0]*m_colStride, patchOffsets[1] - colOffsets[1]*m_colStride};
402 const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] -
403 m_rowPaddingTop, rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
405 if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
406 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
409 if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) {
411 const int depth_index =
static_cast<int>(Layout) == static_cast<int>(
ColMajor) ? 0 : NumDims - 1;
412 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
413 const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride;
414 return m_impl.template packet<Unaligned>(inputIndex);
418 return packetWithPossibleZero(index);
421 EIGEN_DEVICE_FUNC Scalar*
data()
const {
return NULL; }
441 const double compute_cost = 3 * TensorOpCost::DivCost<Index>() +
442 6 * TensorOpCost::MulCost<Index>() +
443 8 * TensorOpCost::MulCost<Index>();
444 return m_impl.costPerCoeff(vectorized) +
445 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
452 for (
int i = 0; i < PacketSize; ++i) {
453 values[i] = coeff(index+i);
455 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
509 #endif // EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
EIGEN_DEVICE_FUNC DenseIndex padding_left() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType &expr, DenseIndex patch_rows, DenseIndex patch_cols, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, PaddingType padding_type, Scalar padding_value)
const DenseIndex m_in_col_strides
Eigen::internal::nested< TensorImagePatchOp >::type Nested
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType &expr, DenseIndex patch_rows, DenseIndex patch_cols, DenseIndex row_strides, DenseIndex col_strides, DenseIndex in_row_strides, DenseIndex in_col_strides, DenseIndex row_inflate_strides, DenseIndex col_inflate_strides, DenseIndex padding_top, DenseIndex padding_bottom, DenseIndex padding_left, DenseIndex padding_right, Scalar padding_value)
internal::remove_const< typename XprType::Scalar >::type Scalar
#define EIGEN_STRONG_INLINE
const DenseIndex m_padding_left
internal::TensorIntDivisor< Index > m_fastInflateRowStride
Eigen::internal::traits< TensorImagePatchOp >::StorageKind StorageKind
const PaddingType m_padding_type
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
EIGEN_DEVICE_FUNC PaddingType padding_type() const
Index m_row_inflate_strides
Index m_col_inflate_strides
internal::TensorIntDivisor< Index > m_fastColStride
std::vector< double > values
TensorImagePatchOp< Rows, Cols, ArgType > XprType
TensorEvaluator< const TensorImagePatchOp< Rows, Cols, ArgType >, Device > Self
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
const DenseIndex m_row_strides
const DenseIndex m_col_strides
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
EIGEN_DEVICE_FUNC bool padding_explicit() const
Eigen::internal::traits< TensorImagePatchOp >::Index Index
A cost model used to limit the number of threads used for evaluating tensor expression.
Index rowInflateStride() const
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
EIGEN_DEVICE_FUNC DenseIndex padding_bottom() const
const DenseIndex m_padding_top
PacketType< CoeffReturnType, Device >::type PacketReturnType
const DenseIndex m_patch_cols
Index colPaddingLeft() const
const TensorImagePatchOp< Rows, Cols, XprType > & type
TensorEvaluator< ArgType, Device > m_impl
const DenseIndex m_padding_bottom
DSizes< Index, NumDims > Dimensions
Eigen::internal::traits< TensorImagePatchOp >::Scalar Scalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
EIGEN_DEVICE_FUNC const CeilReturnType ceil() const
Index userRowStride() const
EIGEN_DEVICE_FUNC Scalar * data() const
EIGEN_DEVICE_FUNC DenseIndex col_strides() const
const bool m_padding_explicit
const TensorEvaluator< ArgType, Device > & impl() const
EIGEN_DEVICE_FUNC DenseIndex padding_top() const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
EIGEN_DEVICE_FUNC Scalar padding_value() const
const Scalar m_padding_value
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
remove_reference< Nested >::type _Nested
Index userColStride() const
XprType::CoeffReturnType CoeffReturnType
Eigen::NumTraits< Scalar >::Real RealScalar
Index userInColStride() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
internal::TensorIntDivisor< Index > m_fastOutputRows
internal::TensorIntDivisor< Index > m_fastOutputDepth
Index userInRowStride() const
internal::TensorIntDivisor< Index > m_fastInputColsEff
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
const DenseIndex m_patch_rows
internal::TensorIntDivisor< Index > m_fastOtherStride
Index colInflateStride() const
EIGEN_DEVICE_FUNC DenseIndex patch_rows() const
internal::TensorIntDivisor< Index > m_fastPatchStride
internal::remove_const< typename XprType::Scalar >::type Scalar
EIGEN_DEVICE_FUNC DenseIndex patch_cols() const
EIGEN_DEVICE_FUNC DenseIndex row_inflate_strides() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
EIGEN_DEVICE_FUNC DenseIndex in_row_strides() const
XprTraits::StorageKind StorageKind
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
TensorImagePatchOp< Rows, Cols, XprType > type
internal::TensorIntDivisor< Index > m_fastInflateColStride
EIGEN_DEVICE_FUNC DenseIndex col_inflate_strides() const
traits< XprType > XprTraits
EIGEN_DEVICE_FUNC DenseIndex row_strides() const
const DenseIndex m_in_row_strides
EIGEN_DEVICE_FUNC DenseIndex in_col_strides() const
const DenseIndex m_col_inflate_strides
const DenseIndex m_padding_right
XprType::CoeffReturnType CoeffReturnType
const DenseIndex m_row_inflate_strides
internal::packet_traits< Scalar >::type type
TensorEvaluator< ArgType, Device > Impl
EIGEN_DEVICE_FUNC DenseIndex padding_right() const
Index rowPaddingTop() const