1 /* Copyright 2015 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 // See docs in ../ops/image_ops.cc. 17 18 #include "tensorflow/core/framework/op_kernel.h" 19 #include "tensorflow/core/framework/register_types.h" 20 #include "tensorflow/core/framework/tensor.h" 21 #include "tensorflow/core/framework/types.h" 22 #include "tensorflow/core/lib/random/simple_philox.h" 23 #include "tensorflow/core/util/guarded_philox_random.h" 24 25 namespace tensorflow { 26 27 template <typename T> 28 class RandomCropOp : public OpKernel { 29 public: 30 explicit RandomCropOp(OpKernelConstruction* context) : OpKernel(context) { 31 OP_REQUIRES_OK(context, generator_.Init(context)); 32 } 33 34 void Compute(OpKernelContext* context) override { 35 const Tensor& input = context->input(0); 36 OP_REQUIRES(context, input.dims() == 3, 37 errors::InvalidArgument("input must be 3-dimensional", 38 input.shape().DebugString())); 39 const Tensor& shape_t = context->input(1); 40 OP_REQUIRES(context, shape_t.dims() == 1, 41 errors::InvalidArgument("shape_t must be 1-dimensional", 42 shape_t.shape().DebugString())); 43 OP_REQUIRES(context, shape_t.NumElements() == 2, 44 errors::InvalidArgument("shape_t must have two elements", 45 shape_t.shape().DebugString())); 46 47 auto shape_vec = shape_t.vec<int64>(); 48 const int32 target_height = shape_vec(0); 49 const int32 target_width = shape_vec(1); 50 51 const int32 height = input.dim_size(0); 52 const int32 width = input.dim_size(1); 53 const int32 channels = input.dim_size(2); 54 55 // Initialize shape to the batch size of the input, then add 56 // the rest of the dimensions 57 Tensor* output = nullptr; 58 const auto output_shape = 59 TensorShape({target_height, target_width, channels}); 60 OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output)); 61 62 // If the target size matches the actual size, then do nothing. 63 if ((target_height == height) && (target_width == width)) { 64 *output = context->input(0); 65 } 66 67 // TODO(shlens): Implement edge case to guarantee output size dimensions. 68 // Edge case. The target dimensions are larger then the image, so 69 // zero-pad the image. This guarantees that the image will *always* 70 // be [target_height, target_width] in size. 71 OP_REQUIRES(context, width >= target_width, 72 errors::FailedPrecondition( 73 "width must be >= target_width: width = ", width, 74 ", target_width = ", target_width)); 75 OP_REQUIRES(context, height >= target_height, 76 errors::FailedPrecondition( 77 "height must be >= target_height: height = ", height, 78 ", target_height = ", target_height)); 79 80 int32 offset_height = 0; 81 int32 offset_width = 0; 82 83 auto local_gen = generator_.ReserveSamples32(2); 84 random::SimplePhilox random(&local_gen); 85 86 if (width > target_width) { 87 offset_width = random.Rand32() % (width - target_width + 1); 88 } 89 if (height > target_height) { 90 offset_height = random.Rand32() % (height - target_height + 1); 91 } 92 93 // TODO(shlens): Do this more efficiently with memcpy once padding is 94 // available for smaller images. 95 typename TTypes<T, 3>::ConstTensor input_data(input.tensor<T, 3>()); 96 typename TTypes<T, 3>::Tensor output_data(output->tensor<T, 3>()); 97 98 for (int y = 0; y < target_height; ++y) { 99 for (int x = 0; x < target_width; ++x) { 100 for (int c = 0; c < channels; ++c) { 101 output_data(y, x, c) = 102 input_data(y + offset_height, x + offset_width, c); 103 } 104 } 105 } 106 } 107 108 private: 109 GuardedPhiloxRandom generator_; 110 }; 111 112 #define REGISTER_KERNELS(type) \ 113 REGISTER_KERNEL_BUILDER( \ 114 Name("RandomCrop").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 115 RandomCropOp<type>) 116 117 TF_CALL_REAL_NUMBER_TYPES(REGISTER_KERNELS); 118 #undef REGISTER_KERNELS 119 120 } // namespace tensorflow 121