Skip to content
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

Customized op for WithinBox3d. #1020

Merged
merged 9 commits into from
Dec 5, 2022
Merged

Customized op for WithinBox3d. #1020

merged 9 commits into from
Dec 5, 2022

Conversation

tanzhenyu
Copy link
Contributor

@tanzhenyu tanzhenyu commented Nov 15, 2022

pip uninstall keras_cv
bazel build keras_cv/custom_ops:all
mv bazel-bin/keras_cv/custom_ops/*.so keras_cv/custom_ops/
bazel build build_pip_pkg
./bazel-bin/build_pip_pkg wheels
pip install wheels/keras_cv-0.0.0-cp39-cp39-linux_x86_64.whl

from tensorflow.python.framework import load_library
from tensorflow.python.platform import resource_loader

custom_ops = load_library.load_op_library(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to support wheels without custom binaries, I think we'll need to try-catch this so that we don't break at import time if the .so files are missing.

For IoU3D I did this by making IoU3D an object, but IIRC we decided that's probably not the right solution, so maybe we just want to try/except this and raise an Exception when the op is called if we weren't able to load the custom ops at load time.

wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Still much slower than v1.
@bhack
Copy link
Contributor

bhack commented Nov 15, 2022

Can we add a little bit of c++ docstrings?

@tanzhenyu tanzhenyu requested a review from Guowang November 30, 2022 22:26
box_dimension = 20.0


def get_points_boxes():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you ensure no overlap between two bounding boxes? The v2 op assumes no overlaps, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Added docstring

for _ in range(5):
print(datetime.now())
res = keras_cv.ops.within_box3d_index(points, boxes)
self.assertAllClose(res.shape, points.shape[:1])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to test assertAllClose(v1_points, v2_points), where v1_points and v2_points are generated by v1 and v2 OPs.
You can sort V1 and V2 points based on XYZ so that you can compare them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will compare this once we have "is_within_any_box3d"



# TODO(lengzhaoqi/tanzhenyu): compare the performance with v1
def is_within_box3d_v2(points, boxes):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, there's not a great way to efficiently verify that boxes don't overlap, so we should probably include info in the docstring warning users that we have a non-overlap precondition for boxes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// those points are within the box
auto within_fn = [&points_x_indices, &points_y_indices, &boxes_vec, &points_vec, &boxes_indices_t](int64_t begin, int64_t end) {
for (int64_t idx = begin; idx < end; ++idx) {
std::unordered_set<int>& set_a = points_x_indices[idx];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you elaborate? (I'm not sure if I understand the comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flat_hash_set is more efficient than std:: unordered_set, but we can check the profiling first to see whether this is the bottleneck.

std::unordered_set<int>& set_a = points_x_indices[idx];
std::unordered_set<int>& set_b = points_y_indices[idx];
std::unordered_set<int> p_set;
for (auto val : set_a) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: std::set_intersection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that only works for sorted range of set_a and set_b

@tanzhenyu
Copy link
Contributor Author

/gcbrun

@tanzhenyu tanzhenyu merged commit 12b28d4 into master Dec 5, 2022
@tanzhenyu tanzhenyu deleted the box3d branch December 5, 2022 23:22
@tanzhenyu
Copy link
Contributor Author

the failing test is due to previous random_choice test, not related to this PR

ghost pushed a commit to y-vectorfield/keras-cv that referenced this pull request Nov 16, 2023
* Customized op for WithinBox3d.

* add some parallelization.
Still much slower than v1.

* add more parallelization.

* Replace with std binary search, add unit test.

* add lazy loading.

* fix format.

* fix format.

* address comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants