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 // This is a helper struct to package up the input and output 17 // parameters of an image resizer (the height, widths, etc.). To 18 // reduce code duplication and ensure consistency across the different 19 // resizers, it performs the input validation. 20 21 #ifndef TENSORFLOW_KERNELS_IMAGE_RESIZER_STATE_H_ 22 #define TENSORFLOW_KERNELS_IMAGE_RESIZER_STATE_H_ 23 24 #define EIGEN_USE_THREADS 25 26 #include <math.h> 27 #include <algorithm> 28 #include <array> 29 30 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 31 #include "tensorflow/core/framework/op_kernel.h" 32 #include "tensorflow/core/framework/register_types.h" 33 #include "tensorflow/core/framework/tensor.h" 34 #include "tensorflow/core/framework/tensor_shape.h" 35 #include "tensorflow/core/framework/types.h" 36 #include "tensorflow/core/kernels/bounds_check.h" 37 38 namespace tensorflow { 39 40 // CalculateResizeScale determines the float scaling factor. 41 inline float CalculateResizeScale(int64 in_size, int64 out_size, 42 bool align_corners) { 43 return (align_corners && out_size > 1) 44 ? (in_size - 1) / static_cast<float>(out_size - 1) 45 : in_size / static_cast<float>(out_size); 46 } 47 48 struct ImageResizerState { 49 explicit ImageResizerState(bool align_corners) 50 : align_corners_(align_corners) {} 51 52 // ValidateAndCalculateOutputSize checks the bounds on the input tensors 53 // and requested size, sets up some of the resizing state such as the 54 // height_scale and width_scale, and calculates the output size. 55 // If any of these operations fails, it sets an error status in 56 // the context, which the caller must check. 57 void ValidateAndCalculateOutputSize(OpKernelContext* context, 58 const Tensor& input) { 59 OP_REQUIRES(context, input.dims() == 4, 60 errors::InvalidArgument("input must be 4-dimensional", 61 input.shape().DebugString())); 62 const Tensor& shape_t = context->input(1); 63 OP_REQUIRES(context, shape_t.dims() == 1, 64 errors::InvalidArgument("shape_t must be 1-dimensional", 65 shape_t.shape().DebugString())); 66 OP_REQUIRES(context, shape_t.NumElements() == 2, 67 errors::InvalidArgument("shape_t must have two elements", 68 shape_t.shape().DebugString())); 69 auto Svec = shape_t.vec<int32>(); 70 batch_size = input.dim_size(0); 71 out_height = internal::SubtleMustCopy(Svec(0)); 72 out_width = internal::SubtleMustCopy(Svec(1)); 73 OP_REQUIRES( 74 context, 75 FastBoundsCheck(input.dim_size(1), std::numeric_limits<int32>::max()) && 76 FastBoundsCheck(input.dim_size(2), 77 std::numeric_limits<int32>::max()), 78 errors::InvalidArgument("input sizes must be between 0 and max int32")); 79 80 in_height = static_cast<int32>(input.dim_size(1)); 81 in_width = static_cast<int32>(input.dim_size(2)); 82 channels = input.dim_size(3); 83 OP_REQUIRES(context, out_height > 0 && out_width > 0, 84 errors::InvalidArgument("output dimensions must be positive")); 85 OP_REQUIRES( 86 context, channels > 0, 87 errors::InvalidArgument("image must have at least one channel")); 88 OP_REQUIRES( 89 context, input.dim_size(1) > 0 && input.dim_size(2) > 0, 90 errors::InvalidArgument("input image must be of non-zero size")); 91 height_scale = CalculateResizeScale(in_height, out_height, align_corners_); 92 width_scale = CalculateResizeScale(in_width, out_width, align_corners_); 93 94 // Guard against overflows 95 OP_REQUIRES(context, 96 ceilf((out_height - 1) * height_scale) <= 97 static_cast<float>(std::numeric_limits<int64>::max()), 98 errors::InvalidArgument( 99 "input image height scale would cause an overflow")); 100 OP_REQUIRES( 101 context, 102 ceilf((out_width - 1) * width_scale) <= static_cast<float>(INT_MAX), 103 errors::InvalidArgument( 104 "input image width scale would cause an overflow")); 105 } 106 107 // Calculates all the required variables, and allocates the output. 108 void ValidateAndCreateOutput(OpKernelContext* context, const Tensor& input) { 109 ValidateAndCalculateOutputSize(context, input); 110 if (!context->status().ok()) return; 111 OP_REQUIRES_OK(context, context->allocate_output( 112 0, 113 TensorShape({input.dim_size(0), out_height, 114 out_width, input.dim_size(3)}), 115 &output)); 116 } 117 118 int64 batch_size; 119 int64 out_height; 120 int64 out_width; 121 int64 in_height; 122 int64 in_width; 123 int64 channels; 124 float height_scale; 125 float width_scale; 126 Tensor* output = nullptr; 127 128 private: 129 bool align_corners_; 130 }; 131 132 struct ImageResizerGradientState { 133 explicit ImageResizerGradientState(bool align_corners) 134 : align_corners_(align_corners) {} 135 136 void ValidateAndCreateOutput(OpKernelContext* context, const Tensor& input, 137 const Tensor& original_image) { 138 OP_REQUIRES(context, input.dims() == 4, 139 errors::InvalidArgument("input_grad must be 4-dimensional", 140 input.shape().DebugString())); 141 // Resizers always produce float images, so input gradient must 142 // always be a float. 143 OP_REQUIRES(context, input.dtype() == DT_FLOAT, 144 errors::InvalidArgument("input_grad must be of type float", 145 input.dtype())); 146 147 OP_REQUIRES(context, original_image.dims() == 4, 148 errors::InvalidArgument("original_image must be 4-dimensional", 149 original_image.shape().DebugString())); 150 151 // Allocate output and initialize to zeros. 152 batch_size = input.dim_size(0); 153 channels = input.dim_size(3); 154 resized_height = input.dim_size(1); 155 resized_width = input.dim_size(2); 156 original_height = original_image.dim_size(1); 157 original_width = original_image.dim_size(2); 158 159 OP_REQUIRES( 160 context, 161 FastBoundsCheck(original_height, std::numeric_limits<int32>::max()) && 162 FastBoundsCheck(original_width, std::numeric_limits<int32>::max()), 163 errors::InvalidArgument( 164 "original sizes must be between 0 and max int32")); 165 166 height_scale = 167 CalculateResizeScale(original_height, resized_height, align_corners_); 168 width_scale = 169 CalculateResizeScale(original_width, resized_width, align_corners_); 170 output = nullptr; 171 OP_REQUIRES_OK(context, context->allocate_output( 172 0, 173 TensorShape({batch_size, original_height, 174 original_width, channels}), 175 &output)); 176 } 177 178 int64 batch_size; 179 int64 channels; 180 int64 resized_height; 181 int64 resized_width; 182 int64 original_height; 183 int64 original_width; 184 float height_scale; 185 float width_scale; 186 Tensor* output; 187 188 private: 189 bool align_corners_; 190 }; 191 192 } // namespace tensorflow 193 194 #endif // TENSORFLOW_KERNELS_IMAGE_RESIZER_STATE_H_ 195