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>
35 typedef typename XprTraits::StorageKind StorageKind;
36 typedef typename XprTraits::Index
Index;
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>
63 typedef typename XprType::CoeffReturnType CoeffReturnType;
68 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorImagePatchOp(
const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
69 DenseIndex row_strides, DenseIndex col_strides,
70 DenseIndex in_row_strides, DenseIndex in_col_strides,
71 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
72 PaddingType padding_type, Scalar padding_value)
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) {}
80 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(
const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
81 DenseIndex row_strides, DenseIndex col_strides,
82 DenseIndex in_row_strides, DenseIndex in_col_strides,
83 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
84 DenseIndex padding_top, DenseIndex padding_bottom,
85 DenseIndex padding_left, DenseIndex padding_right,
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) {}
96 DenseIndex patch_rows()
const {
return m_patch_rows; }
98 DenseIndex patch_cols()
const {
return m_patch_cols; }
100 DenseIndex row_strides()
const {
return m_row_strides; }
102 DenseIndex col_strides()
const {
return m_col_strides; }
104 DenseIndex in_row_strides()
const {
return m_in_row_strides; }
106 DenseIndex in_col_strides()
const {
return m_in_col_strides; }
108 DenseIndex row_inflate_strides()
const {
return m_row_inflate_strides; }
110 DenseIndex col_inflate_strides()
const {
return m_col_inflate_strides; }
112 bool padding_explicit()
const {
return m_padding_explicit; }
114 DenseIndex padding_top()
const {
return m_padding_top; }
116 DenseIndex padding_bottom()
const {
return m_padding_bottom; }
118 DenseIndex padding_left()
const {
return m_padding_left; }
120 DenseIndex padding_right()
const {
return m_padding_right; }
122 PaddingType padding_type()
const {
return m_padding_type; }
124 Scalar padding_value()
const {
return m_padding_value; }
128 expression()
const {
return m_xpr; }
131 typename XprType::Nested m_xpr;
132 const DenseIndex m_patch_rows;
133 const DenseIndex m_patch_cols;
134 const DenseIndex m_row_strides;
135 const DenseIndex m_col_strides;
136 const DenseIndex m_in_row_strides;
137 const DenseIndex m_in_col_strides;
138 const DenseIndex m_row_inflate_strides;
139 const DenseIndex m_col_inflate_strides;
140 const bool m_padding_explicit;
141 const DenseIndex m_padding_top;
142 const DenseIndex m_padding_bottom;
143 const DenseIndex m_padding_left;
144 const DenseIndex m_padding_right;
145 const PaddingType m_padding_type;
146 const Scalar m_padding_value;
150 template<DenseIndex Rows, DenseIndex Cols,
typename ArgType,
typename Device>
154 typedef typename XprType::Index
Index;
156 static const int NumDims = NumInputDims + 1;
162 typedef typename XprType::CoeffReturnType CoeffReturnType;
174 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorEvaluator(
const XprType& op,
const Device& device)
175 : m_impl(op.expression(), device)
177 EIGEN_STATIC_ASSERT((NumDims >= 4), YOU_MADE_A_PROGRAMMING_MISTAKE);
179 m_paddingValue = op.padding_value();
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];
194 m_row_strides = op.row_strides();
195 m_col_strides = op.col_strides();
198 m_in_row_strides = op.in_row_strides();
199 m_in_col_strides = op.in_col_strides();
200 m_row_inflate_strides = op.row_inflate_strides();
201 m_col_inflate_strides = op.col_inflate_strides();
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;
217 m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
218 m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
220 if (op.padding_explicit()) {
221 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));
222 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));
223 m_rowPaddingTop = op.padding_top();
224 m_colPaddingLeft = op.padding_left();
227 switch (op.padding_type()) {
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;
243 eigen_assert(
false &&
"unexpected padding");
246 eigen_assert(m_outputRows > 0);
247 eigen_assert(m_outputCols > 0);
250 if (static_cast<int>(Layout) == static_cast<int>(
ColMajor)) {
257 m_dimensions[0] = input_dims[0];
258 m_dimensions[1] = op.patch_rows();
259 m_dimensions[2] = op.patch_cols();
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];
272 m_dimensions[NumDims-2] = op.patch_rows();
273 m_dimensions[NumDims-3] = op.patch_cols();
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)) {
313 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
315 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(Scalar* ) {
316 m_impl.evalSubExprsIfNeeded(NULL);
320 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void cleanup() {
324 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const 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>
363 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const 365 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
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};
400 eigen_assert(rowOffsets[0] <= rowOffsets[1]);
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; }
425 Index rowPaddingTop()
const {
return m_rowPaddingTop; }
426 Index colPaddingLeft()
const {
return m_colPaddingLeft; }
427 Index outputRows()
const {
return m_outputRows; }
428 Index outputCols()
const {
return m_outputCols; }
429 Index userRowStride()
const {
return m_row_strides; }
430 Index userColStride()
const {
return m_col_strides; }
431 Index userInRowStride()
const {
return m_in_row_strides; }
432 Index userInColStride()
const {
return m_in_col_strides; }
433 Index rowInflateStride()
const {
return m_row_inflate_strides; }
434 Index colInflateStride()
const {
return m_col_inflate_strides; }
437 costPerCoeff(
bool vectorized)
const {
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);
449 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index)
const 452 for (
int i = 0; i < PacketSize; ++i) {
453 values[i] = coeff(index+i);
455 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
459 Dimensions m_dimensions;
467 Index m_in_row_strides;
468 Index m_in_col_strides;
469 Index m_row_inflate_strides;
470 Index m_col_inflate_strides;
472 Index m_input_rows_eff;
473 Index m_input_cols_eff;
474 Index m_patch_rows_eff;
475 Index m_patch_cols_eff;
484 Index m_rowInputStride;
485 Index m_colInputStride;
486 Index m_patchInputStride;
495 Index m_rowPaddingTop;
496 Index m_colPaddingLeft;
501 Scalar m_paddingValue;
509 #endif // EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H Definition: TensorCostModel.h:25
Storage order is column major (see TopicStorageOrders).
Definition: Constants.h:320
Definition: XprHelper.h:158
Definition: TensorImagePatch.h:151
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
Definition: TensorForwardDeclarations.h:45
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
The tensor base class.
Definition: TensorBase.h:827
Definition: BandTriangularSolver.h:13
Definition: TensorTraits.h:170
The type used to identify a dense storage.
Definition: Constants.h:491
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
Definition: ForwardDeclarations.h:17
Definition: XprHelper.h:312
Definition: EmulateArray.h:203