|
| 1 | +// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. 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 | +#ifndef DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ |
| 16 | +#define DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ |
| 17 | + |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "dali/core/tensor_view.h" |
| 21 | + |
| 22 | +namespace dali { |
| 23 | +namespace kernels { |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +class LaplacianWindows { |
| 27 | + public: |
| 28 | + explicit LaplacianWindows(int max_window_size) : smooth_computed_{1}, deriv_computed_{1} { |
| 29 | + Resize(max_window_size); |
| 30 | + *smoothing_views_[0](0) = 1; |
| 31 | + *deriv_views_[0](0) = 1; |
| 32 | + } |
| 33 | + |
| 34 | + TensorView<StorageCPU, const T, 1> GetDerivWindow(int window_size) { |
| 35 | + assert(1 <= window_size && window_size <= max_window_size_); |
| 36 | + assert(window_size % 2 == 1); |
| 37 | + auto window_idx = window_size / 2; |
| 38 | + PrepareSmoothingWindow(window_size - 2); |
| 39 | + PrepareDerivWindow(window_size); |
| 40 | + return deriv_views_[window_idx]; |
| 41 | + } |
| 42 | + |
| 43 | + TensorView<StorageCPU, const T, 1> GetSmoothingWindow(int window_size) { |
| 44 | + assert(1 <= window_size && window_size <= max_window_size_); |
| 45 | + assert(window_size % 2 == 1); |
| 46 | + auto window_idx = window_size / 2; |
| 47 | + PrepareSmoothingWindow(window_size); |
| 48 | + return smoothing_views_[window_idx]; |
| 49 | + } |
| 50 | + |
| 51 | + private: |
| 52 | + /** |
| 53 | + * @brief Smoothing window of size 2n + 1 is [1, 2, 1] conv composed with itself n - 1 times |
| 54 | + * so that the window has appropriate size: it boils down to computing binominal coefficients: |
| 55 | + * (1 + 1) ^ (2n). |
| 56 | + */ |
| 57 | + inline void PrepareSmoothingWindow(int window_size) { |
| 58 | + for (; smooth_computed_ < window_size; smooth_computed_++) { |
| 59 | + auto cur_size = smooth_computed_ + 1; |
| 60 | + auto cur_idx = cur_size / 2; |
| 61 | + auto &prev_view = smoothing_views_[cur_size % 2 == 0 ? cur_idx - 1 : cur_idx]; |
| 62 | + auto &view = smoothing_views_[cur_idx]; |
| 63 | + auto prev_val = *prev_view(0); |
| 64 | + *view(0) = prev_val; |
| 65 | + for (int j = 1; j < cur_size - 1; j++) { |
| 66 | + auto val = *prev_view(j); |
| 67 | + *view(j) = prev_val + *prev_view(j); |
| 68 | + prev_val = val; |
| 69 | + } |
| 70 | + *view(cur_size - 1) = prev_val; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Derivative window of size 3 is [1, -2, 1] (which is [1, -1] composed with itself). |
| 76 | + * Bigger windows are convolutions of smoothing windows with [1, -2, 1]. |
| 77 | + */ |
| 78 | + inline void PrepareDerivWindow(int window_size) { |
| 79 | + for (; deriv_computed_ < window_size; deriv_computed_++) { |
| 80 | + auto cur_size = deriv_computed_ + 1; |
| 81 | + auto cur_idx = cur_size / 2; |
| 82 | + auto &prev_view = cur_size % 2 == 0 ? smoothing_views_[cur_idx - 1] : deriv_views_[cur_idx]; |
| 83 | + auto &view = deriv_views_[cur_idx]; |
| 84 | + auto prev_val = *prev_view(0); |
| 85 | + *view(0) = -prev_val; |
| 86 | + for (int j = 1; j < cur_size - 1; j++) { |
| 87 | + auto val = *prev_view(j); |
| 88 | + *view(j) = prev_val - *prev_view(j); |
| 89 | + prev_val = val; |
| 90 | + } |
| 91 | + *view(cur_size - 1) = prev_val; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + void Resize(int max_window_size) { |
| 96 | + assert(1 <= max_window_size && max_window_size % 2 == 1); |
| 97 | + max_window_size_ = max_window_size; |
| 98 | + int num_windows = (max_window_size + 1) / 2; |
| 99 | + int num_elements = num_windows * num_windows; |
| 100 | + smoothing_memory_.resize(num_elements); |
| 101 | + deriv_memory_.resize(num_elements); |
| 102 | + smoothing_views_.resize(num_windows); |
| 103 | + deriv_views_.resize(num_windows); |
| 104 | + int offset = 0; |
| 105 | + int window_size = 1; |
| 106 | + for (int i = 0; i < num_windows; i++) { |
| 107 | + smoothing_views_[i] = {&smoothing_memory_[offset], {window_size}}; |
| 108 | + deriv_views_[i] = {&deriv_memory_[offset], {window_size}}; |
| 109 | + offset += window_size; |
| 110 | + window_size += 2; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + int smooth_computed_, deriv_computed_; |
| 115 | + int max_window_size_; |
| 116 | + std::vector<T> smoothing_memory_; |
| 117 | + std::vector<T> deriv_memory_; |
| 118 | + std::vector<TensorView<StorageCPU, T, 1>> smoothing_views_; |
| 119 | + std::vector<TensorView<StorageCPU, T, 1>> deriv_views_; |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace kernels |
| 123 | +} // namespace dali |
| 124 | + |
| 125 | +#endif // DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ |
0 commit comments