|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Sadeep Jayasumana |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "tensorflow/core/framework/op.h" |
| 26 | +#include "tensorflow/core/framework/shape_inference.h" |
| 27 | +#include "tensorflow/core/framework/op_kernel.h" |
| 28 | +#include "tensorflow/core/framework/tensor_shape.h" |
| 29 | +#include "modified_permutohedral.h" |
| 30 | + |
| 31 | +using namespace tensorflow; |
| 32 | + |
| 33 | +void compute_spatial_kernel(float * const output_kernel, const int width, |
| 34 | + const int height, const float theta_gamma) { |
| 35 | + |
| 36 | + const int num_pixels = width * height; |
| 37 | + for (int p = 0; p < num_pixels; ++p) { |
| 38 | + output_kernel[2 * p] = static_cast<float>(p % width) / theta_gamma; |
| 39 | + output_kernel[2 * p + 1] = static_cast<float>(p / width) / theta_gamma; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +void compute_bilateral_kernel(float * const output_kernel, const Tensor& rgb_tensor, |
| 44 | + const float theta_alpha, const float theta_beta) { |
| 45 | + |
| 46 | + const int height = rgb_tensor.dim_size(1); |
| 47 | + const int width = rgb_tensor.dim_size(2); |
| 48 | + const int num_pixels = height * width; |
| 49 | + auto rgb = rgb_tensor.flat<float>(); |
| 50 | + |
| 51 | + for (int p = 0; p < num_pixels; ++p) { |
| 52 | + // Spatial terms |
| 53 | + output_kernel[5 * p] = static_cast<float>(p % width) / theta_alpha; |
| 54 | + output_kernel[5 * p + 1] = static_cast<float>(p / width) / theta_alpha; |
| 55 | + |
| 56 | + // Color terms |
| 57 | + output_kernel[5 * p + 2] = static_cast<float>(rgb(p) / theta_beta); |
| 58 | + output_kernel[5 * p + 3] = static_cast<float>(rgb(num_pixels + p) / theta_beta); |
| 59 | + output_kernel[5 * p + 4] = static_cast<float>(rgb(2 * num_pixels + p) / theta_beta); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +REGISTER_OP("HighDimFilter") |
| 64 | + .Attr("bilateral: bool") |
| 65 | + .Attr("theta_alpha: float = 1.0") |
| 66 | + .Attr("theta_beta: float = 1.0") |
| 67 | + .Attr("theta_gamma: float = 1.0") |
| 68 | + .Attr("backwards: bool = false") |
| 69 | + .Input("raw: float32") |
| 70 | + .Input("rgb: float32") |
| 71 | + .Output("filtered: float32") |
| 72 | + .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { |
| 73 | + c->set_output(0, c->input(0)); |
| 74 | + return Status::OK(); |
| 75 | + }); |
| 76 | + |
| 77 | +class HighDimFilterOp : public OpKernel { |
| 78 | + public: |
| 79 | + explicit HighDimFilterOp(OpKernelConstruction* context) : OpKernel(context) { |
| 80 | + |
| 81 | + OP_REQUIRES_OK(context, |
| 82 | + context->GetAttr("bilateral", &bilateral_)); |
| 83 | + OP_REQUIRES_OK(context, |
| 84 | + context->GetAttr("theta_alpha", &theta_alpha_)); |
| 85 | + OP_REQUIRES_OK(context, |
| 86 | + context->GetAttr("theta_beta", &theta_beta_)); |
| 87 | + OP_REQUIRES_OK(context, |
| 88 | + context->GetAttr("theta_gamma", &theta_gamma_)); |
| 89 | + OP_REQUIRES_OK(context, |
| 90 | + context->GetAttr("backwards", &backwards_)); |
| 91 | + } |
| 92 | + |
| 93 | + void Compute(OpKernelContext* context) override { |
| 94 | + |
| 95 | + // Grab the unary tensor |
| 96 | + const Tensor& input_tensor = context->input(0); |
| 97 | + // Grab the RGB image tensor |
| 98 | + const Tensor& image_tensor = context->input(1); |
| 99 | + |
| 100 | + const int channels = input_tensor.dim_size(0); |
| 101 | + const int height = input_tensor.dim_size(1); |
| 102 | + const int width = input_tensor.dim_size(2); |
| 103 | + const int num_pixels = width * height; |
| 104 | + |
| 105 | + // Create the output tensor |
| 106 | + Tensor* output_tensor = NULL; |
| 107 | + OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(), |
| 108 | + &output_tensor)); |
| 109 | + ModifiedPermutohedral mp; |
| 110 | + |
| 111 | + if (bilateral_) { |
| 112 | + float * const kernel_vals = new float[5 * num_pixels]; |
| 113 | + compute_bilateral_kernel(kernel_vals, image_tensor, |
| 114 | + theta_alpha_, theta_beta_); |
| 115 | + mp.init(kernel_vals, 5, num_pixels); |
| 116 | + mp.compute(*output_tensor, input_tensor, channels, backwards_); |
| 117 | + |
| 118 | + delete[] kernel_vals; |
| 119 | + } else { |
| 120 | + float * const kernel_vals = new float[2 * num_pixels]; |
| 121 | + compute_spatial_kernel(kernel_vals, width, height, theta_gamma_); |
| 122 | + mp.init(kernel_vals, 2, num_pixels); |
| 123 | + mp.compute(*output_tensor, input_tensor, channels, backwards_); |
| 124 | + |
| 125 | + delete[] kernel_vals; |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + private: |
| 131 | + bool bilateral_; |
| 132 | + float theta_alpha_; |
| 133 | + float theta_beta_; |
| 134 | + float theta_gamma_; |
| 135 | + bool backwards_; |
| 136 | +}; |
| 137 | + |
| 138 | +REGISTER_KERNEL_BUILDER(Name("HighDimFilter").Device(DEVICE_CPU), HighDimFilterOp); |
0 commit comments