Eigen-unsupported  3.4.1 (git rev 28ded8800c26864e537852658428ab44c8399e87)
 
Loading...
Searching...
No Matches
TensorImagePatch.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
11#define EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
12
13namespace Eigen {
14
15namespace internal {
16
17template<DenseIndex Rows, DenseIndex Cols, typename XprType>
18struct traits<TensorImagePatchOp<Rows, Cols, XprType> > : public traits<XprType>
19{
20 typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
21 typedef traits<XprType> XprTraits;
22 typedef typename XprTraits::StorageKind StorageKind;
23 typedef typename XprTraits::Index Index;
24 typedef typename XprType::Nested Nested;
25 typedef typename remove_reference<Nested>::type _Nested;
26 static const int NumDimensions = XprTraits::NumDimensions + 1;
27 static const int Layout = XprTraits::Layout;
28 typedef typename XprTraits::PointerType PointerType;
29};
30
31template<DenseIndex Rows, DenseIndex Cols, typename XprType>
32struct eval<TensorImagePatchOp<Rows, Cols, XprType>, Eigen::Dense>
33{
34 typedef const TensorImagePatchOp<Rows, Cols, XprType>& type;
35};
36
37template<DenseIndex Rows, DenseIndex Cols, typename XprType>
38struct nested<TensorImagePatchOp<Rows, Cols, XprType>, 1, typename eval<TensorImagePatchOp<Rows, Cols, XprType> >::type>
39{
40 typedef TensorImagePatchOp<Rows, Cols, XprType> type;
41};
42
43template <typename Self, bool Vectorizable>
44struct ImagePatchCopyOp {
45 typedef typename Self::Index Index;
46 typedef typename Self::Scalar Scalar;
47 typedef typename Self::Impl Impl;
48 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
49 const Self& self, const Index num_coeff_to_copy, const Index dst_index,
50 Scalar* dst_data, const Index src_index) {
51 const Impl& impl = self.impl();
52 for (Index i = 0; i < num_coeff_to_copy; ++i) {
53 dst_data[dst_index + i] = impl.coeff(src_index + i);
54 }
55 }
56};
57
58template <typename Self>
59struct ImagePatchCopyOp<Self, true> {
60 typedef typename Self::Index Index;
61 typedef typename Self::Scalar Scalar;
62 typedef typename Self::Impl Impl;
63 typedef typename packet_traits<Scalar>::type Packet;
64 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
65 const Self& self, const Index num_coeff_to_copy, const Index dst_index,
66 Scalar* dst_data, const Index src_index) {
67 const Impl& impl = self.impl();
68 const Index packet_size = internal::unpacket_traits<Packet>::size;
69 const Index vectorized_size =
70 (num_coeff_to_copy / packet_size) * packet_size;
71 for (Index i = 0; i < vectorized_size; i += packet_size) {
72 Packet p = impl.template packet<Unaligned>(src_index + i);
73 internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index + i, p);
74 }
75 for (Index i = vectorized_size; i < num_coeff_to_copy; ++i) {
76 dst_data[dst_index + i] = impl.coeff(src_index + i);
77 }
78 }
79};
80
81template <typename Self>
82struct ImagePatchPaddingOp {
83 typedef typename Self::Index Index;
84 typedef typename Self::Scalar Scalar;
85 typedef typename packet_traits<Scalar>::type Packet;
86 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void Run(
87 const Index num_coeff_to_pad, const Scalar padding_value,
88 const Index dst_index, Scalar* dst_data) {
89 const Index packet_size = internal::unpacket_traits<Packet>::size;
90 const Packet padded_packet = internal::pset1<Packet>(padding_value);
91 const Index vectorized_size =
92 (num_coeff_to_pad / packet_size) * packet_size;
93 for (Index i = 0; i < vectorized_size; i += packet_size) {
94 internal::pstoret<Scalar, Packet, Unaligned>(dst_data + dst_index + i,
95 padded_packet);
96 }
97 for (Index i = vectorized_size; i < num_coeff_to_pad; ++i) {
98 dst_data[dst_index + i] = padding_value;
99 }
100 }
101};
102
103} // end namespace internal
104
119template <DenseIndex Rows, DenseIndex Cols, typename XprType>
120class TensorImagePatchOp : public TensorBase<TensorImagePatchOp<Rows, Cols, XprType>, ReadOnlyAccessors> {
121 public:
122 typedef typename Eigen::internal::traits<TensorImagePatchOp>::Scalar Scalar;
123 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
124 typedef typename XprType::CoeffReturnType CoeffReturnType;
125 typedef typename Eigen::internal::nested<TensorImagePatchOp>::type Nested;
126 typedef typename Eigen::internal::traits<TensorImagePatchOp>::StorageKind StorageKind;
127 typedef typename Eigen::internal::traits<TensorImagePatchOp>::Index Index;
128
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
130 DenseIndex row_strides, DenseIndex col_strides,
131 DenseIndex in_row_strides, DenseIndex in_col_strides,
132 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
133 PaddingType padding_type, Scalar padding_value)
134 : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
135 m_row_strides(row_strides), m_col_strides(col_strides),
136 m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
137 m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
138 m_padding_explicit(false), m_padding_top(0), m_padding_bottom(0), m_padding_left(0), m_padding_right(0),
139 m_padding_type(padding_type), m_padding_value(padding_value) {}
140
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorImagePatchOp(const XprType& expr, DenseIndex patch_rows, DenseIndex patch_cols,
142 DenseIndex row_strides, DenseIndex col_strides,
143 DenseIndex in_row_strides, DenseIndex in_col_strides,
144 DenseIndex row_inflate_strides, DenseIndex col_inflate_strides,
145 DenseIndex padding_top, DenseIndex padding_bottom,
146 DenseIndex padding_left, DenseIndex padding_right,
147 Scalar padding_value)
148 : m_xpr(expr), m_patch_rows(patch_rows), m_patch_cols(patch_cols),
149 m_row_strides(row_strides), m_col_strides(col_strides),
150 m_in_row_strides(in_row_strides), m_in_col_strides(in_col_strides),
151 m_row_inflate_strides(row_inflate_strides), m_col_inflate_strides(col_inflate_strides),
152 m_padding_explicit(true), m_padding_top(padding_top), m_padding_bottom(padding_bottom),
153 m_padding_left(padding_left), m_padding_right(padding_right),
154 m_padding_type(PADDING_VALID), m_padding_value(padding_value) {}
155
156
157 EIGEN_DEVICE_FUNC
158 DenseIndex patch_rows() const { return m_patch_rows; }
159 EIGEN_DEVICE_FUNC
160 DenseIndex patch_cols() const { return m_patch_cols; }
161 EIGEN_DEVICE_FUNC
162 DenseIndex row_strides() const { return m_row_strides; }
163 EIGEN_DEVICE_FUNC
164 DenseIndex col_strides() const { return m_col_strides; }
165 EIGEN_DEVICE_FUNC
166 DenseIndex in_row_strides() const { return m_in_row_strides; }
167 EIGEN_DEVICE_FUNC
168 DenseIndex in_col_strides() const { return m_in_col_strides; }
169 EIGEN_DEVICE_FUNC
170 DenseIndex row_inflate_strides() const { return m_row_inflate_strides; }
171 EIGEN_DEVICE_FUNC
172 DenseIndex col_inflate_strides() const { return m_col_inflate_strides; }
173 EIGEN_DEVICE_FUNC
174 bool padding_explicit() const { return m_padding_explicit; }
175 EIGEN_DEVICE_FUNC
176 DenseIndex padding_top() const { return m_padding_top; }
177 EIGEN_DEVICE_FUNC
178 DenseIndex padding_bottom() const { return m_padding_bottom; }
179 EIGEN_DEVICE_FUNC
180 DenseIndex padding_left() const { return m_padding_left; }
181 EIGEN_DEVICE_FUNC
182 DenseIndex padding_right() const { return m_padding_right; }
183 EIGEN_DEVICE_FUNC
184 PaddingType padding_type() const { return m_padding_type; }
185 EIGEN_DEVICE_FUNC
186 Scalar padding_value() const { return m_padding_value; }
187
188 EIGEN_DEVICE_FUNC
189 const typename internal::remove_all<typename XprType::Nested>::type&
190 expression() const { return m_xpr; }
191
192 protected:
193 typename XprType::Nested m_xpr;
194 const DenseIndex m_patch_rows;
195 const DenseIndex m_patch_cols;
196 const DenseIndex m_row_strides;
197 const DenseIndex m_col_strides;
198 const DenseIndex m_in_row_strides;
199 const DenseIndex m_in_col_strides;
200 const DenseIndex m_row_inflate_strides;
201 const DenseIndex m_col_inflate_strides;
202 const bool m_padding_explicit;
203 const DenseIndex m_padding_top;
204 const DenseIndex m_padding_bottom;
205 const DenseIndex m_padding_left;
206 const DenseIndex m_padding_right;
207 const PaddingType m_padding_type;
208 const Scalar m_padding_value;
209};
210
211// Eval as rvalue
212template<DenseIndex Rows, DenseIndex Cols, typename ArgType, typename Device>
213struct TensorEvaluator<const TensorImagePatchOp<Rows, Cols, ArgType>, Device>
214{
216 typedef typename XprType::Index Index;
217 static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
218 static const int NumDims = NumInputDims + 1;
219 typedef DSizes<Index, NumDims> Dimensions;
220 typedef typename internal::remove_const<typename XprType::Scalar>::type Scalar;
222 Device> Self;
224 typedef typename XprType::CoeffReturnType CoeffReturnType;
225 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
226 static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
227 typedef StorageMemory<CoeffReturnType, Device> Storage;
228 typedef typename Storage::Type EvaluatorPointerType;
229
230 enum {
231 IsAligned = false,
232 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
233 BlockAccess = false,
234 PreferBlockAccess = true,
235 Layout = TensorEvaluator<ArgType, Device>::Layout,
236 CoordAccess = false,
237 RawAccess = false
238 };
239
240 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
241 typedef internal::TensorBlockNotImplemented TensorBlock;
242 //===--------------------------------------------------------------------===//
243
244 EIGEN_STRONG_INLINE TensorEvaluator( const XprType& op, const Device& device)
245 : m_device(device), m_impl(op.expression(), device)
246 {
247 EIGEN_STATIC_ASSERT((NumDims >= 4), YOU_MADE_A_PROGRAMMING_MISTAKE);
248
249 m_paddingValue = op.padding_value();
250
251 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
252
253 // Caches a few variables.
254 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
255 m_inputDepth = input_dims[0];
256 m_inputRows = input_dims[1];
257 m_inputCols = input_dims[2];
258 } else {
259 m_inputDepth = input_dims[NumInputDims-1];
260 m_inputRows = input_dims[NumInputDims-2];
261 m_inputCols = input_dims[NumInputDims-3];
262 }
263
264 m_row_strides = op.row_strides();
265 m_col_strides = op.col_strides();
266
267 // Input strides and effective input/patch size
268 m_in_row_strides = op.in_row_strides();
269 m_in_col_strides = op.in_col_strides();
270 m_row_inflate_strides = op.row_inflate_strides();
271 m_col_inflate_strides = op.col_inflate_strides();
272 // The "effective" input rows and input cols are the input rows and cols
273 // after inflating them with zeros.
274 // For examples, a 2x3 matrix with row_inflate_strides and
275 // col_inflate_strides of 2 comes from:
276 // A B C
277 // D E F
278 //
279 // to a matrix is 3 x 5:
280 //
281 // A . B . C
282 // . . . . .
283 // D . E . F
284
285 m_input_rows_eff = (m_inputRows - 1) * m_row_inflate_strides + 1;
286 m_input_cols_eff = (m_inputCols - 1) * m_col_inflate_strides + 1;
287 m_patch_rows_eff = op.patch_rows() + (op.patch_rows() - 1) * (m_in_row_strides - 1);
288 m_patch_cols_eff = op.patch_cols() + (op.patch_cols() - 1) * (m_in_col_strides - 1);
289
290 if (op.padding_explicit()) {
291 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));
292 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));
293 m_rowPaddingTop = op.padding_top();
294 m_colPaddingLeft = op.padding_left();
295 } else {
296 // Computing padding from the type
297 switch (op.padding_type()) {
298 case PADDING_VALID:
299 m_outputRows = numext::ceil((m_input_rows_eff - m_patch_rows_eff + 1.f) / static_cast<float>(m_row_strides));
300 m_outputCols = numext::ceil((m_input_cols_eff - m_patch_cols_eff + 1.f) / static_cast<float>(m_col_strides));
301 // Calculate the padding
302 m_rowPaddingTop = numext::maxi<Index>(0, ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2);
303 m_colPaddingLeft = numext::maxi<Index>(0, ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2);
304 break;
305 case PADDING_SAME:
306 m_outputRows = numext::ceil(m_input_rows_eff / static_cast<float>(m_row_strides));
307 m_outputCols = numext::ceil(m_input_cols_eff / static_cast<float>(m_col_strides));
308 // Calculate the padding
309 m_rowPaddingTop = ((m_outputRows - 1) * m_row_strides + m_patch_rows_eff - m_input_rows_eff) / 2;
310 m_colPaddingLeft = ((m_outputCols - 1) * m_col_strides + m_patch_cols_eff - m_input_cols_eff) / 2;
311 // The padding size calculation for PADDING_SAME has been updated to
312 // be consistent with how TensorFlow extracts its paddings.
313 m_rowPaddingTop = numext::maxi<Index>(0, m_rowPaddingTop);
314 m_colPaddingLeft = numext::maxi<Index>(0, m_colPaddingLeft);
315 break;
316 default:
317 eigen_assert(false && "unexpected padding");
318 m_outputCols=0; // silence the uninitialised warning;
319 m_outputRows=0;
320 }
321 }
322 eigen_assert(m_outputRows > 0);
323 eigen_assert(m_outputCols > 0);
324
325 // Dimensions for result of extraction.
326 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
327 // ColMajor
328 // 0: depth
329 // 1: patch_rows
330 // 2: patch_cols
331 // 3: number of patches
332 // 4 and beyond: anything else (such as batch).
333 m_dimensions[0] = input_dims[0];
334 m_dimensions[1] = op.patch_rows();
335 m_dimensions[2] = op.patch_cols();
336 m_dimensions[3] = m_outputRows * m_outputCols;
337 for (int i = 4; i < NumDims; ++i) {
338 m_dimensions[i] = input_dims[i-1];
339 }
340 } else {
341 // RowMajor
342 // NumDims-1: depth
343 // NumDims-2: patch_rows
344 // NumDims-3: patch_cols
345 // NumDims-4: number of patches
346 // NumDims-5 and beyond: anything else (such as batch).
347 m_dimensions[NumDims-1] = input_dims[NumInputDims-1];
348 m_dimensions[NumDims-2] = op.patch_rows();
349 m_dimensions[NumDims-3] = op.patch_cols();
350 m_dimensions[NumDims-4] = m_outputRows * m_outputCols;
351 for (int i = NumDims-5; i >= 0; --i) {
352 m_dimensions[i] = input_dims[i];
353 }
354 }
355
356 // Strides for moving the patch in various dimensions.
357 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
358 m_colStride = m_dimensions[1];
359 m_patchStride = m_colStride * m_dimensions[2] * m_dimensions[0];
360 m_otherStride = m_patchStride * m_dimensions[3];
361 } else {
362 m_colStride = m_dimensions[NumDims-2];
363 m_patchStride = m_colStride * m_dimensions[NumDims-3] * m_dimensions[NumDims-1];
364 m_otherStride = m_patchStride * m_dimensions[NumDims-4];
365 }
366
367 // Strides for navigating through the input tensor.
368 m_rowInputStride = m_inputDepth;
369 m_colInputStride = m_inputDepth * m_inputRows;
370 m_patchInputStride = m_inputDepth * m_inputRows * m_inputCols;
371
372 // Fast representations of different variables.
373 m_fastOtherStride = internal::TensorIntDivisor<Index>(m_otherStride);
374 m_fastPatchStride = internal::TensorIntDivisor<Index>(m_patchStride);
375 m_fastColStride = internal::TensorIntDivisor<Index>(m_colStride);
376 m_fastInflateRowStride = internal::TensorIntDivisor<Index>(m_row_inflate_strides);
377 m_fastInflateColStride = internal::TensorIntDivisor<Index>(m_col_inflate_strides);
378 m_fastInputColsEff = internal::TensorIntDivisor<Index>(m_input_cols_eff);
379
380 // Number of patches in the width dimension.
381 m_fastOutputRows = internal::TensorIntDivisor<Index>(m_outputRows);
382 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
383 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[0]);
384 } else {
385 m_fastOutputDepth = internal::TensorIntDivisor<Index>(m_dimensions[NumDims-1]);
386 }
387 }
388
389 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
390
391 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) {
392 m_impl.evalSubExprsIfNeeded(NULL);
393 return true;
394 }
395
396#ifdef EIGEN_USE_THREADS
397 template <typename EvalSubExprsCallback>
398 EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
399 EvaluatorPointerType, EvalSubExprsCallback done) {
400 m_impl.evalSubExprsIfNeededAsync(nullptr, [done](bool) { done(true); });
401 }
402#endif // EIGEN_USE_THREADS
403
404 EIGEN_STRONG_INLINE void cleanup() {
405 m_impl.cleanup();
406 }
407
408 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
409 {
410 // Patch index corresponding to the passed in index.
411 const Index patchIndex = index / m_fastPatchStride;
412 // Find the offset of the element wrt the location of the first element.
413 const Index patchOffset = (index - patchIndex * m_patchStride) / m_fastOutputDepth;
414
415 // Other ways to index this element.
416 const Index otherIndex = (NumDims == 4) ? 0 : index / m_fastOtherStride;
417 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (index - otherIndex * m_otherStride) / m_fastPatchStride;
418
419 // Calculate col index in the input original tensor.
420 const Index colIndex = patch2DIndex / m_fastOutputRows;
421 const Index colOffset = patchOffset / m_fastColStride;
422 const Index inputCol = colIndex * m_col_strides + colOffset * m_in_col_strides - m_colPaddingLeft;
423 const Index origInputCol = (m_col_inflate_strides == 1) ? inputCol : ((inputCol >= 0) ? (inputCol / m_fastInflateColStride) : 0);
424 if (inputCol < 0 || inputCol >= m_input_cols_eff ||
425 ((m_col_inflate_strides != 1) && (inputCol != origInputCol * m_col_inflate_strides))) {
426 return Scalar(m_paddingValue);
427 }
428
429 // Calculate row index in the original input tensor.
430 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
431 const Index rowOffset = patchOffset - colOffset * m_colStride;
432 const Index inputRow = rowIndex * m_row_strides + rowOffset * m_in_row_strides - m_rowPaddingTop;
433 const Index origInputRow = (m_row_inflate_strides == 1) ? inputRow : ((inputRow >= 0) ? (inputRow / m_fastInflateRowStride) : 0);
434 if (inputRow < 0 || inputRow >= m_input_rows_eff ||
435 ((m_row_inflate_strides != 1) && (inputRow != origInputRow * m_row_inflate_strides))) {
436 return Scalar(m_paddingValue);
437 }
438
439 const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
440 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
441
442 const Index inputIndex = depth + origInputRow * m_rowInputStride + origInputCol * m_colInputStride + otherIndex * m_patchInputStride;
443 return m_impl.coeff(inputIndex);
444 }
445
446 template<int LoadMode>
447 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
448 {
449 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
450 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
451
452 if (m_in_row_strides != 1 || m_in_col_strides != 1 || m_row_inflate_strides != 1 || m_col_inflate_strides != 1) {
453 return packetWithPossibleZero(index);
454 }
455
456 const Index indices[2] = {index, index + PacketSize - 1};
457 const Index patchIndex = indices[0] / m_fastPatchStride;
458 if (patchIndex != indices[1] / m_fastPatchStride) {
459 return packetWithPossibleZero(index);
460 }
461 const Index otherIndex = (NumDims == 4) ? 0 : indices[0] / m_fastOtherStride;
462 eigen_assert(otherIndex == indices[1] / m_fastOtherStride);
463
464 // Find the offset of the element wrt the location of the first element.
465 const Index patchOffsets[2] = {(indices[0] - patchIndex * m_patchStride) / m_fastOutputDepth,
466 (indices[1] - patchIndex * m_patchStride) / m_fastOutputDepth};
467
468 const Index patch2DIndex = (NumDims == 4) ? patchIndex : (indices[0] - otherIndex * m_otherStride) / m_fastPatchStride;
469 eigen_assert(patch2DIndex == (indices[1] - otherIndex * m_otherStride) / m_fastPatchStride);
470
471 const Index colIndex = patch2DIndex / m_fastOutputRows;
472 const Index colOffsets[2] = {patchOffsets[0] / m_fastColStride, patchOffsets[1] / m_fastColStride};
473
474 // Calculate col indices in the original input tensor.
475 const Index inputCols[2] = {colIndex * m_col_strides + colOffsets[0] -
476 m_colPaddingLeft, colIndex * m_col_strides + colOffsets[1] - m_colPaddingLeft};
477 if (inputCols[1] < 0 || inputCols[0] >= m_inputCols) {
478 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
479 }
480
481 if (inputCols[0] == inputCols[1]) {
482 const Index rowIndex = patch2DIndex - colIndex * m_outputRows;
483 const Index rowOffsets[2] = {patchOffsets[0] - colOffsets[0]*m_colStride, patchOffsets[1] - colOffsets[1]*m_colStride};
484 eigen_assert(rowOffsets[0] <= rowOffsets[1]);
485 // Calculate col indices in the original input tensor.
486 const Index inputRows[2] = {rowIndex * m_row_strides + rowOffsets[0] -
487 m_rowPaddingTop, rowIndex * m_row_strides + rowOffsets[1] - m_rowPaddingTop};
488
489 if (inputRows[1] < 0 || inputRows[0] >= m_inputRows) {
490 return internal::pset1<PacketReturnType>(Scalar(m_paddingValue));
491 }
492
493 if (inputRows[0] >= 0 && inputRows[1] < m_inputRows) {
494 // no padding
495 const int depth_index = static_cast<int>(Layout) == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
496 const Index depth = index - (index / m_fastOutputDepth) * m_dimensions[depth_index];
497 const Index inputIndex = depth + inputRows[0] * m_rowInputStride + inputCols[0] * m_colInputStride + otherIndex * m_patchInputStride;
498 return m_impl.template packet<Unaligned>(inputIndex);
499 }
500 }
501
502 return packetWithPossibleZero(index);
503 }
504
505 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return NULL; }
506
507 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
508
509#ifdef EIGEN_USE_SYCL
510 // binding placeholder accessors to a command group handler for SYCL
511 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
512 m_impl.bind(cgh);
513 }
514#endif
515
516 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowPaddingTop() const { return m_rowPaddingTop; }
517 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colPaddingLeft() const { return m_colPaddingLeft; }
518 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputRows() const { return m_outputRows; }
519 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outputCols() const { return m_outputCols; }
520 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userRowStride() const { return m_row_strides; }
521 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userColStride() const { return m_col_strides; }
522 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInRowStride() const { return m_in_row_strides; }
523 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index userInColStride() const { return m_in_col_strides; }
524 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rowInflateStride() const { return m_row_inflate_strides; }
525 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index colInflateStride() const { return m_col_inflate_strides; }
526
527 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
528 costPerCoeff(bool vectorized) const {
529 // We conservatively estimate the cost for the code path where the computed
530 // index is inside the original image and
531 // TensorEvaluator<ArgType, Device>::CoordAccess is false.
532 const double compute_cost = 3 * TensorOpCost::DivCost<Index>() +
533 6 * TensorOpCost::MulCost<Index>() +
534 8 * TensorOpCost::MulCost<Index>();
535 return m_impl.costPerCoeff(vectorized) +
536 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
537 }
538
539 protected:
540 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packetWithPossibleZero(Index index) const
541 {
542 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
543 EIGEN_UNROLL_LOOP
544 for (int i = 0; i < PacketSize; ++i) {
545 values[i] = coeff(index+i);
546 }
547 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
548 return rslt;
549 }
550
551 Dimensions m_dimensions;
552
553 Index m_otherStride;
554 Index m_patchStride;
555 Index m_colStride;
556 Index m_row_strides;
557 Index m_col_strides;
558
559 Index m_in_row_strides;
560 Index m_in_col_strides;
561 Index m_row_inflate_strides;
562 Index m_col_inflate_strides;
563
564 Index m_input_rows_eff;
565 Index m_input_cols_eff;
566 Index m_patch_rows_eff;
567 Index m_patch_cols_eff;
568
569 internal::TensorIntDivisor<Index> m_fastOtherStride;
570 internal::TensorIntDivisor<Index> m_fastPatchStride;
571 internal::TensorIntDivisor<Index> m_fastColStride;
572 internal::TensorIntDivisor<Index> m_fastInflateRowStride;
573 internal::TensorIntDivisor<Index> m_fastInflateColStride;
574 internal::TensorIntDivisor<Index> m_fastInputColsEff;
575
576 Index m_rowInputStride;
577 Index m_colInputStride;
578 Index m_patchInputStride;
579
580 Index m_inputDepth;
581 Index m_inputRows;
582 Index m_inputCols;
583
584 Index m_outputRows;
585 Index m_outputCols;
586
587 Index m_rowPaddingTop;
588 Index m_colPaddingLeft;
589
590 internal::TensorIntDivisor<Index> m_fastOutputRows;
591 internal::TensorIntDivisor<Index> m_fastOutputDepth;
592
593 Scalar m_paddingValue;
594
595 const Device EIGEN_DEVICE_REF m_device;
596 TensorEvaluator<ArgType, Device> m_impl;
597};
598
599
600} // end namespace Eigen
601
602#endif // EIGEN_CXX11_TENSOR_TENSOR_IMAGE_PATCH_H
The tensor base class.
Definition TensorForwardDeclarations.h:56
Patch extraction specialized for image processing. This assumes that the input has a least 3 dimensio...
Definition TensorImagePatch.h:120
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27