Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_KERNELS_EXTRACT_IMAGE_PATCHES_OP_H_
     17 #define TENSORFLOW_KERNELS_EXTRACT_IMAGE_PATCHES_OP_H_
     18 
     19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     20 #include "tensorflow/core/framework/tensor_shape.h"
     21 #include "tensorflow/core/framework/tensor_types.h"
     22 
     23 namespace tensorflow {
     24 namespace functor {
     25 
     26 template <typename Device, typename T>
     27 struct ExtractImagePatchesForward {
     28   void operator()(const Device& d, typename TTypes<T, 4>::ConstTensor input,
     29                   int patch_rows, int patch_cols, int stride_rows,
     30                   int stride_cols, int rate_rows, int rate_cols,
     31                   const Eigen::PaddingType& padding,
     32                   typename TTypes<T, 4>::Tensor output) {
     33     // Need to swap row/col when calling Eigen, because our data is in
     34     // NHWC format while Eigen assumes NWHC format.
     35     const int64 N = std::max(input.size(), output.size());
     36     if (N <= std::numeric_limits<Index32>::max()) {
     37       auto output_32bit = To32Bit(output);
     38       output_32bit.device(d) =
     39           To32Bit(input)
     40               .extract_image_patches(patch_cols, patch_rows, stride_cols,
     41                                      stride_rows, rate_cols, rate_rows, padding)
     42               .reshape(output_32bit.dimensions());
     43     } else {
     44       output.device(d) =
     45           input
     46               .extract_image_patches(patch_cols, patch_rows, stride_cols,
     47                                      stride_rows, rate_cols, rate_rows, padding)
     48               .reshape(output.dimensions());
     49     }
     50   }
     51 };
     52 
     53 }  // namespace functor
     54 }  // namespace tensorflow
     55 
     56 #endif  // TENSORFLOW_KERNELS_EXTRACT_IMAGE_PATCHES_OP_H_
     57