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 | // Functors for 3d convolution. |
17 | |
18 | #ifndef TENSORFLOW_CORE_KERNELS_CONV_3D_H_ |
19 | #define TENSORFLOW_CORE_KERNELS_CONV_3D_H_ |
20 | |
21 | #include "tensorflow/core/framework/tensor_types.h" |
22 | #include "tensorflow/core/kernels/eigen_backward_cuboid_convolutions.h" |
23 | #include "tensorflow/core/kernels/eigen_cuboid_convolution.h" |
24 | |
25 | namespace tensorflow { |
26 | namespace functor { |
27 | |
28 | // Applies a 3D convolution to a batch of multi-channel volumes. |
29 | template <typename Device, typename T> |
30 | struct CuboidConvolution; |
31 | |
32 | // Backward input pass for the cuboid convolution. |
33 | template <typename Device, typename T> |
34 | struct CuboidConvolutionBackwardInput; |
35 | |
36 | // Backward filter pass for the cuboid convolution. |
37 | template <typename Device, typename T> |
38 | struct CuboidConvolutionBackwardFilter; |
39 | |
40 | typedef Eigen::ThreadPoolDevice CPUDevice; |
41 | |
42 | template <typename T> |
43 | struct CuboidConvolution<CPUDevice, T> { |
44 | void operator()(const CPUDevice& d, typename TTypes<T, 5>::Tensor output, |
45 | typename TTypes<T, 5>::ConstTensor input, |
46 | typename TTypes<T, 5>::ConstTensor filter, int stride_planes, |
47 | int stride_rows, int stride_cols, |
48 | const Eigen::PaddingType& padding) { |
49 | output.device(d) = Eigen::CuboidConvolution( |
50 | input, filter, stride_planes, stride_rows, stride_cols, padding); |
51 | } |
52 | }; |
53 | |
54 | template <typename T> |
55 | struct CuboidConvolutionBackwardInput<CPUDevice, T> { |
56 | void operator()(const CPUDevice& d, |
57 | typename TTypes<T, 5>::Tensor input_backward, |
58 | typename TTypes<T, 5>::ConstTensor filter, |
59 | typename TTypes<T, 5>::ConstTensor output_backward, |
60 | int stride_planes, int stride_rows, int stride_cols) { |
61 | // Need to swap the order of plane/row/col strides when calling Eigen. |
62 | input_backward.device(d) = Eigen::CuboidConvolutionBackwardInput( |
63 | filter, output_backward, |
64 | input_backward.dimension(3), // input_planes |
65 | input_backward.dimension(2), // input_rows |
66 | input_backward.dimension(1), // input_cols |
67 | stride_cols, stride_rows, stride_planes); |
68 | } |
69 | }; |
70 | |
71 | template <typename T> |
72 | struct CuboidConvolutionBackwardFilter<CPUDevice, T> { |
73 | void operator()(const CPUDevice& d, |
74 | typename TTypes<T, 5>::Tensor filter_backward, |
75 | typename TTypes<T, 5>::ConstTensor input, |
76 | typename TTypes<T, 5>::ConstTensor output_backward, |
77 | int stride_planes, int stride_rows, int stride_cols) { |
78 | // Need to swap the order of plane/row/col strides when calling Eigen. |
79 | filter_backward.device(d) = Eigen::CuboidConvolutionBackwardKernel( |
80 | input, output_backward, |
81 | filter_backward.dimension(2), // kernel_planes |
82 | filter_backward.dimension(1), // kernel_rows |
83 | filter_backward.dimension(0), // kernel_cols |
84 | stride_cols, stride_rows, stride_planes); |
85 | } |
86 | }; |
87 | |
88 | } // namespace functor |
89 | } // namespace tensorflow |
90 | |
91 | #endif // TENSORFLOW_CORE_KERNELS_CONV_3D_H_ |
92 | |