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

🚨🚨🚨 [SuperPoint] Fix keypoint coordinate output and add post processing #33200

Merged
merged 32 commits into from
Oct 29, 2024

Conversation

sbucaille
Copy link
Contributor

@sbucaille sbucaille commented Aug 29, 2024

What does this PR do?

There is a bug highlighted by @merveenoyan where SuperPoint returns absolute keypoint coordinates at a wrong scale.
For example, an image of size (1000, 1000) will first be resized into (640, 480) by the image processor but SuperPoint will output keypoint coordinates between 0 and 640 for x and 0 and 480 for y.
This PR fixes this bug by returning keypoints in relative coordinates out of the SuperPoint forward method.
An additional post_process_keypoint_detection is added to the SuperPointImageProcessor to cast the keypoint coordinates into the original image sizes.
I also added an "unwrapping" logic where the output will be a list of keypoints, scores, descriptors for each image so that the user don't have to deal with the current mask mechanism. This is conditionned by a boolean argument in the signature.

I have not finished all the details but most of the implementation is there and I will add tests tomorrow but at least the PR is open as I promised Merve ☺️

Fixes #33825

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
    -> On Slack
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@amyeroberts @NielsRogge @merveenoyan

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for fixing and adding this method!

Overall looks good - just a few things to add / update

Comment on lines 261 to 262
# Convert to relative coordinates
keypoints = keypoints / torch.tensor([width, height], device=keypoints.device)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically a breaking change - but as this is more of a fix, I think it's OK

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for iterating!

Mostly small comments. Main one is about the indexing on image_sizes in the post-processing method

@sbucaille
Copy link
Contributor Author

@amyeroberts comments addressed, tests are passing, let me know if I can do a final rebase for the merge

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Just some minor comments

docs/source/en/model_doc/superpoint.md Outdated Show resolved Hide resolved
docs/source/en/model_doc/superpoint.md Outdated Show resolved Hide resolved
src/transformers/models/superpoint/modeling_superpoint.py Outdated Show resolved Hide resolved
sbucaille and others added 4 commits October 2, 2024 21:53
Co-authored-by: Pavel Iakubovskii <qubvel@gmail.com>
… statement and changed tests results to reflect changes to SuperPoint from absolute keypoints coordinates to relative
@sbucaille
Copy link
Contributor Author

@qubvel thanks for the review, I've addressed your comments. I initially forgot to change the tests to reflect the changes in terms of SuperPoint's outputs, so this is also fixed. Let me know if there is anything else to do

@qubvel qubvel changed the title [SuperPoint] Fix keypoint coordinate output and add post processing 🚨🚨🚨 [SuperPoint] Fix keypoint coordinate output and add post processing Oct 3, 2024
Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thanks quick iteration, a few more comments

src/transformers/models/superpoint/modeling_superpoint.py Outdated Show resolved Hide resolved
@@ -260,7 +260,7 @@ def test_inference(self):
inputs = preprocessor(images=images, return_tensors="pt").to(torch_device)
with torch.no_grad():
outputs = model(**inputs)
expected_number_keypoints_image0 = 567
Copy link
Member

Choose a reason for hiding this comment

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

Wondering why the number of keypoints changed :)

tests/models/superpoint/test_modeling_superpoint.py Outdated Show resolved Hide resolved
Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thank you for the fixes! Test diff looks fine now 👍

@qubvel qubvel self-requested a review October 4, 2024 17:30
Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thanks, looks good to me now!

cc @ArthurZucker for final review

docs/source/en/model_doc/superpoint.md Outdated Show resolved Hide resolved
Co-authored-by: Pavel Iakubovskii <qubvel@gmail.com>
Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Feel free to merge if ready for you @qubvel 🤗

@onuralpszr
Copy link

Hello, I was following this PR and curious that is it going to merge or any change still require ?

@sbucaille
Copy link
Contributor Author

Hi @onuralpszr ,
Thanks for asking, there is no change required on my side !

@qubvel qubvel merged commit a183519 into huggingface:main Oct 29, 2024
22 checks passed
@qubvel
Copy link
Member

qubvel commented Oct 29, 2024

@sbucaille thanks a lot for your contribution!

@sbucaille sbucaille deleted the superpoint_fix branch October 29, 2024 09:42
2015aroras pushed a commit to 2015aroras/transformers that referenced this pull request Nov 15, 2024
…ng (huggingface#33200)

* feat: Added int conversion and unwrapping

* test: added tests for post_process_keypoint_detection of SuperPointImageProcessor

* docs: changed docs to include post_process_keypoint_detection method and switched from opencv to matplotlib

* test: changed test to not depend on SuperPointModel forward

* test: added missing require_torch decorator

* docs: changed pyplot parameters for the keypoints to be more visible in the example

* tests: changed import torch location to make test_flax and test_tf

* Revert "tests: changed import torch location to make test_flax and test_tf"

This reverts commit 39b32a2.

* tests: fixed import

* chore: applied suggestions from code review

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>

* tests: fixed import

* tests: fixed import (bis)

* tests: fixed import (ter)

* feat: added choice of type for target_size and changed tests accordingly

* docs: updated code snippet to reflect the addition of target size type choice in post process method

* tests: fixed imports (...)

* tests: fixed imports (...)

* style: formatting file

* docs: fixed typo from image[0] to image.size[0]

* docs: added output image and fixed some tests

* Update docs/source/en/model_doc/superpoint.md

Co-authored-by: Pavel Iakubovskii <qubvel@gmail.com>

* fix: included SuperPointKeypointDescriptionOutput in TYPE_CHECKING if statement and changed tests results to reflect changes to SuperPoint from absolute keypoints coordinates to relative

* docs: changed SuperPoint's docs to print output instead of just accessing

* style: applied make style

* docs: added missing output type and precision in docstring of post_process_keypoint_detection

* perf: deleted loop to perform keypoint conversion in one statement

* fix: moved keypoint conversion at the end of model forward

* docs: changed SuperPointInterestPointDecoder to SuperPointKeypointDecoder class name and added relative (x, y) coordinates information to its method

* fix: changed type hint

* refactor: removed unnecessary brackets

* revert: SuperPointKeypointDecoder to SuperPointInterestPointDecoder

* Update docs/source/en/model_doc/superpoint.md

Co-authored-by: Pavel Iakubovskii <qubvel@gmail.com>

---------

Co-authored-by: Steven Bucaille <steven.bucaille@buawei.com>
Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
Co-authored-by: Pavel Iakubovskii <qubvel@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'SuperPointImageProcessor' object has no attribute 'post_process_keypoint_detection'
8 participants