10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
31 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
38 typedef typename XprType::Nested
Nested;
40 static const int NumDimensions = XprTraits::NumDimensions + 1;
41 static const int Layout = XprTraits::Layout;
45 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
51 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
57 template <
typename Self,
bool Vectorizable>
61 typedef typename Self::Impl
Impl;
63 const Self&
self,
const Index num_coeff_to_copy,
const Index dst_index,
65 const Impl& impl =
self.impl();
66 for (
Index i = 0;
i < num_coeff_to_copy; ++
i) {
67 dst_data[dst_index +
i] = impl.coeff(src_index +
i);
72 template <
typename Self>
76 typedef typename Self::Impl
Impl;
79 const Self&
self,
const Index num_coeff_to_copy,
const Index dst_index,
81 const Impl& impl =
self.impl();
83 const Index vectorized_size =
84 (num_coeff_to_copy / packet_size) * packet_size;
85 for (
Index i = 0;
i < vectorized_size;
i += packet_size) {
86 Packet p = impl.template packet<Unaligned>(src_index +
i);
87 internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index +
i,
p);
89 for (
Index i = vectorized_size;
i < num_coeff_to_copy; ++
i) {
90 dst_data[dst_index +
i] = impl.coeff(src_index +
i);
95 template <
typename Self>
101 const Index num_coeff_to_pad,
const Scalar padding_value,
104 const Packet padded_packet = internal::pset1<Packet>(padding_value);
105 const Index vectorized_size =
106 (num_coeff_to_pad / packet_size) * packet_size;
107 for (
Index i = 0;
i < vectorized_size;
i += packet_size) {
108 internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index +
i,
111 for (
Index i = vectorized_size;
i < num_coeff_to_pad; ++
i) {
112 dst_data[dst_index +
i] = padding_value;
119 template<DenseIndex Rows, DenseIndex Cols,
typename XprType>
120 class TensorImagePatchOp :
public TensorBase<TensorImagePatchOp<Rows, Cols, XprType>, ReadOnlyAccessors>
213 template<DenseIndex Rows, DenseIndex Cols,
typename ArgType,
typename Device>
219 static const int NumDims = NumInputDims + 1;
246 :
m_device(device), m_impl(op.expression(), device)
256 m_inputDepth = input_dims[0];
257 m_inputRows = input_dims[1];
258 m_inputCols = input_dims[2];
260 m_inputDepth = input_dims[NumInputDims-1];
261 m_inputRows = input_dims[NumInputDims-2];
262 m_inputCols = input_dims[NumInputDims-3];
286 m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
287 m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
300 m_outputRows =
numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.
f) /
static_cast<float>(m_row_strides));
301 m_outputCols =
numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.
f) /
static_cast<float>(m_col_strides));
303 m_rowPaddingTop = numext::maxi<Index>(0, ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2);
304 m_colPaddingLeft = numext::maxi<Index>(0, ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2);
307 m_outputRows =
numext::ceil(m_input_rows_eff /
static_cast<float>(m_row_strides));
308 m_outputCols =
numext::ceil(m_input_cols_eff /
static_cast<float>(m_col_strides));
310 m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2;
311 m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2;
314 m_rowPaddingTop = numext::maxi<Index>(0, m_rowPaddingTop);
315 m_colPaddingLeft = numext::maxi<Index>(0, m_colPaddingLeft);
334 m_dimensions[0] = input_dims[0];
337 m_dimensions[3] = m_outputRows * m_outputCols;
338 for (
int i = 4;
i < NumDims; ++
i) {
339 m_dimensions[
i] = input_dims[
i-1];
348 m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
351 m_dimensions[NumDims-4] = m_outputRows * m_outputCols;
352 for (
int i = NumDims-5;
i >= 0; --
i) {
353 m_dimensions[
i] = input_dims[
i];
359 m_colStride = m_dimensions[1];
360 m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
361 m_otherStride = m_patchStride * m_dimensions[3];
363 m_colStride = m_dimensions[NumDims-2];
364 m_patchStride = m_colStride * m_dimensions[NumDims-3] * m_dimensions[NumDims-1];
365 m_otherStride = m_patchStride * m_dimensions[NumDims-4];
369 m_rowInputStride = m_inputDepth;
370 m_colInputStride = m_inputDepth * m_inputRows;
371 m_patchInputStride = m_inputDepth * m_inputRows * m_inputCols;
393 m_impl.evalSubExprsIfNeeded(
NULL);
397 #ifdef EIGEN_USE_THREADS
398 template <
typename EvalSubExprsCallback>
401 m_impl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); });
403 #endif // EIGEN_USE_THREADS
412 const Index patchIndex = index / m_fastPatchStride;
414 const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
417 const Index otherIndex = (NumDims == 4) ? 0 : index / m_fastOtherStride;
418 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
421 const Index colIndex = patch2DIndex / m_fastOutputRows;
422 const Index colOffset = patchOffset / m_fastColStride;
423 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
424 const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInflateColStride) : 0);
425 if (inputCol < 0 || inputCol >= m_input_cols_eff ||
426 ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
427 return Scalar(m_paddingValue);
431 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
432 const Index rowOffset = patchOffset - colOffset * m_colStride;
433 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
434 const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInflateRowStride) : 0);
435 if (inputRow < 0 || inputRow >= m_input_rows_eff ||
436 ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
437 return Scalar(m_paddingValue);
440 const int depth_index =
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor) ? 0 : NumDims - 1;
441 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
443 const Index inputIndex =
depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride + otherIndex * m_patchInputStride;
444 return m_impl.coeff(inputIndex);
447 template<
int LoadMode>
453 if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1) {
454 return packetWithPossibleZero(index);
458 const Index patchIndex =
indices[0] / m_fastPatchStride;
459 if (patchIndex !=
indices[1] / m_fastPatchStride) {
460 return packetWithPossibleZero(index);
462 const Index otherIndex = (NumDims == 4) ? 0 :
indices[0] / m_fastOtherStride;
466 const Index patchOffsets[2] = {(
indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
467 (
indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
469 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (
indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
470 eigen_assert(patch2DIndex == (
indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
472 const Index colIndex = patch2DIndex / m_fastOutputRows;
473 const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
476 const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] -
477 m_colPaddingLeft, colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
478 if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
479 return internal::pset1<PacketReturnType>(
Scalar(m_paddingValue));
482 if (inputCols[0] == inputCols[1]) {
483 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
484 const Index rowOffsets[2] = {patchOffsets[0] - colOffsets[0]*m_colStride, patchOffsets[1] - colOffsets[1]*m_colStride};
487 const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] -
488 m_rowPaddingTop, rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
490 if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
491 return internal::pset1<PacketReturnType>(
Scalar(m_paddingValue));
494 if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) {
496 const int depth_index =
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor) ? 0 : NumDims - 1;
497 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
498 const Index inputIndex =
depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride;
499 return m_impl.template packet<Unaligned>(inputIndex);
503 return packetWithPossibleZero(index);
510 #ifdef EIGEN_USE_SYCL
533 const double compute_cost = 3 * TensorOpCost::DivCost<Index>() +
534 6 * TensorOpCost::MulCost<Index>() +
535 8 * TensorOpCost::MulCost<Index>();
536 return m_impl.costPerCoeff(vectorized) +
603 #endif // EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H