1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_KERNELS_CROP_AND_RESIZE_OP_H_
17#define TENSORFLOW_CORE_KERNELS_CROP_AND_RESIZE_OP_H_
18
19#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
20#include "tensorflow/core/framework/numeric_types.h"
21#include "tensorflow/core/framework/op_kernel.h"
22#include "tensorflow/core/framework/tensor_types.h"
23
24namespace tensorflow {
25namespace functor {
26
27template <typename Device, typename T>
28struct CropAndResize {
29  // We assume that the tensor sizes are correct.
30  bool operator()(const OpKernelContext* context,
31                  typename TTypes<T, 4>::ConstTensor image,
32                  typename TTypes<float, 2>::ConstTensor boxes,
33                  typename TTypes<int32, 1>::ConstTensor box_ind,
34                  float extrapolation_value,
35                  typename TTypes<float, 4>::Tensor crops);
36};
37
38template <typename Device, typename T>
39struct CropAndResizeBackpropImage {
40  // We assume that the tensor sizes are correct.
41  bool operator()(const Device& d, typename TTypes<float, 4>::ConstTensor grads,
42                  typename TTypes<float, 2>::ConstTensor boxes,
43                  typename TTypes<int32, 1>::ConstTensor box_ind,
44                  typename TTypes<T, 4>::Tensor grads_image);
45};
46
47template <typename Device, typename T>
48struct CropAndResizeBackpropBoxes {
49  // We assume that the tensor sizes are correct.
50  bool operator()(const Device& d, typename TTypes<float, 4>::ConstTensor grads,
51                  typename TTypes<T, 4>::ConstTensor image,
52                  typename TTypes<float, 2>::ConstTensor boxes,
53                  typename TTypes<int32, 1>::ConstTensor box_ind,
54                  typename TTypes<float, 2>::Tensor grads_boxes);
55};
56
57template <typename Device>
58struct CheckValidBoxIndexHelper {
59  // Checks if all values in box_index are in [0, batch).
60  void operator()(const Device& d,
61                  typename TTypes<int32, 1>::ConstTensor box_index, int batch,
62                  typename TTypes<bool, 0>::Tensor isvalid) {
63    isvalid.device(d) = ((box_index >= 0) && (box_index < batch)).all();
64  }
65};
66
67}  // namespace functor
68}  // namespace tensorflow
69
70#endif  // TENSORFLOW_CORE_KERNELS_CROP_AND_RESIZE_OP_H_
71