-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Laplacian GPU operator #3644
Merged
+961
−234
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da617d2
Add Laplacian GPU operator
stiepan 1863f24
Move LaplacianWindows to kernels
stiepan 2ec1e75
Clearer LaplacianWindows tests
stiepan 6328ca3
Replace exp2 with exp2f calls
stiepan c83234d
Split compilation into separate units
stiepan bc9dff8
Remove unnecessary using namespace
stiepan 85a2493
Review remarks
stiepan 51c6bb4
Add slow attr to heavy Laplacian tests
stiepan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ | ||
#define DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ | ||
|
||
#include <vector> | ||
|
||
#include "dali/core/tensor_view.h" | ||
|
||
namespace dali { | ||
namespace kernels { | ||
|
||
template <typename T> | ||
class LaplacianWindows { | ||
public: | ||
explicit LaplacianWindows(int max_window_size) : smooth_computed_{1}, deriv_computed_{1} { | ||
Resize(max_window_size); | ||
*smoothing_views_[0](0) = 1; | ||
*deriv_views_[0](0) = 1; | ||
} | ||
|
||
TensorView<StorageCPU, const T, 1> GetDerivWindow(int window_size) { | ||
assert(1 <= window_size && window_size <= max_window_size_); | ||
assert(window_size % 2 == 1); | ||
auto window_idx = window_size / 2; | ||
PrepareSmoothingWindow(window_size - 2); | ||
PrepareDerivWindow(window_size); | ||
return deriv_views_[window_idx]; | ||
} | ||
|
||
TensorView<StorageCPU, const T, 1> GetSmoothingWindow(int window_size) { | ||
assert(1 <= window_size && window_size <= max_window_size_); | ||
assert(window_size % 2 == 1); | ||
auto window_idx = window_size / 2; | ||
PrepareSmoothingWindow(window_size); | ||
return smoothing_views_[window_idx]; | ||
} | ||
|
||
private: | ||
/** | ||
* @brief Smoothing window of size 2n + 1 is [1, 2, 1] conv composed with itself n - 1 times | ||
* so that the window has appropriate size: it boils down to computing binominal coefficients: | ||
* (1 + 1) ^ (2n). | ||
*/ | ||
inline void PrepareSmoothingWindow(int window_size) { | ||
for (; smooth_computed_ < window_size; smooth_computed_++) { | ||
auto cur_size = smooth_computed_ + 1; | ||
auto cur_idx = cur_size / 2; | ||
auto &prev_view = smoothing_views_[cur_size % 2 == 0 ? cur_idx - 1 : cur_idx]; | ||
auto &view = smoothing_views_[cur_idx]; | ||
auto prev_val = *prev_view(0); | ||
*view(0) = prev_val; | ||
for (int j = 1; j < cur_size - 1; j++) { | ||
auto val = *prev_view(j); | ||
*view(j) = prev_val + *prev_view(j); | ||
prev_val = val; | ||
} | ||
*view(cur_size - 1) = prev_val; | ||
} | ||
} | ||
|
||
/** | ||
* @brief Derivative window of size 3 is [1, -2, 1] (which is [1, -1] composed with itself). | ||
* Bigger windows are convolutions of smoothing windows with [1, -2, 1]. | ||
*/ | ||
inline void PrepareDerivWindow(int window_size) { | ||
for (; deriv_computed_ < window_size; deriv_computed_++) { | ||
auto cur_size = deriv_computed_ + 1; | ||
auto cur_idx = cur_size / 2; | ||
auto &prev_view = cur_size % 2 == 0 ? smoothing_views_[cur_idx - 1] : deriv_views_[cur_idx]; | ||
auto &view = deriv_views_[cur_idx]; | ||
auto prev_val = *prev_view(0); | ||
*view(0) = -prev_val; | ||
for (int j = 1; j < cur_size - 1; j++) { | ||
auto val = *prev_view(j); | ||
*view(j) = prev_val - *prev_view(j); | ||
prev_val = val; | ||
} | ||
*view(cur_size - 1) = prev_val; | ||
} | ||
} | ||
|
||
void Resize(int max_window_size) { | ||
assert(1 <= max_window_size && max_window_size % 2 == 1); | ||
max_window_size_ = max_window_size; | ||
int num_windows = (max_window_size + 1) / 2; | ||
int num_elements = num_windows * num_windows; | ||
smoothing_memory_.resize(num_elements); | ||
deriv_memory_.resize(num_elements); | ||
smoothing_views_.resize(num_windows); | ||
deriv_views_.resize(num_windows); | ||
int offset = 0; | ||
int window_size = 1; | ||
for (int i = 0; i < num_windows; i++) { | ||
smoothing_views_[i] = {&smoothing_memory_[offset], {window_size}}; | ||
deriv_views_[i] = {&deriv_memory_[offset], {window_size}}; | ||
offset += window_size; | ||
window_size += 2; | ||
} | ||
} | ||
|
||
int smooth_computed_, deriv_computed_; | ||
int max_window_size_; | ||
std::vector<T> smoothing_memory_; | ||
std::vector<T> deriv_memory_; | ||
std::vector<TensorView<StorageCPU, T, 1>> smoothing_views_; | ||
std::vector<TensorView<StorageCPU, T, 1>> deriv_views_; | ||
}; | ||
|
||
} // namespace kernels | ||
} // namespace dali | ||
|
||
#endif // DALI_KERNELS_IMGPROC_CONVOLUTION_LAPLACIAN_WINDOWS_H_ |
76 changes: 76 additions & 0 deletions
76
dali/kernels/imgproc/convolution/laplacian_windows_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
#include <cmath> | ||
#include <opencv2/imgproc.hpp> | ||
|
||
#include "dali/kernels/common/utils.h" | ||
#include "dali/test/tensor_test_utils.h" | ||
#include "dali/test/test_tensors.h" | ||
|
||
#include "dali/kernels/imgproc/convolution/laplacian_windows.h" | ||
|
||
namespace dali { | ||
namespace kernels { | ||
|
||
void CheckDerivWindow(int window_size, LaplacianWindows<float> &windows) { | ||
cv::Mat d, s; | ||
cv::getDerivKernels(d, s, 2, 0, window_size, true, CV_32F); | ||
const auto &window_view = windows.GetDerivWindow(window_size); | ||
float d_scale = std::exp2f(-window_size + 3); | ||
for (int i = 0; i < window_size; i++) { | ||
EXPECT_NEAR(window_view.data[i] * d_scale, d.at<float>(i), 1e-6f) | ||
<< "window_size: " << window_size << ", position: " << i; | ||
} | ||
} | ||
|
||
void CheckSmoothingWindow(int window_size, LaplacianWindows<float> &windows) { | ||
cv::Mat d, s; | ||
cv::getDerivKernels(d, s, 2, 0, window_size, true, CV_32F); | ||
const auto &window_view = windows.GetSmoothingWindow(window_size); | ||
float s_scale = std::exp2f(-window_size + 1); | ||
for (int i = 0; i < window_size; i++) { | ||
EXPECT_NEAR(window_view.data[i] * s_scale, s.at<float>(i), 1e-6f) | ||
<< "window_size: " << window_size << ", position: " << i; | ||
} | ||
} | ||
|
||
TEST(LaplacianWindowsTest, GetDerivWindows) { | ||
int max_window = 31; | ||
LaplacianWindows<float> windows{max_window}; | ||
for (int window_size = 3; window_size <= max_window; window_size += 2) { | ||
CheckDerivWindow(window_size, windows); | ||
} | ||
} | ||
|
||
TEST(LaplacianWindowsTest, GetSmoothingWindows) { | ||
int max_window = 31; | ||
LaplacianWindows<float> windows{max_window}; | ||
for (int window_size = 3; window_size <= max_window; window_size += 2) { | ||
CheckSmoothingWindow(window_size, windows); | ||
} | ||
} | ||
|
||
TEST(LaplacianWindowsTest, CheckPrecomputed) { | ||
int max_window = 31; | ||
LaplacianWindows<float> windows{max_window}; | ||
for (int window_size = max_window; window_size >= 3; window_size -= 2) { | ||
CheckDerivWindow(window_size, windows); | ||
CheckSmoothingWindow(window_size, windows); | ||
} | ||
} | ||
|
||
} // namespace kernels | ||
} // namespace dali |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved from laplacian_params.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not 1:1 copy though :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the hindsight - it is the worst kind of moving the code around.