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

Set masks to zero where masks overlap #8213

Merged
merged 9 commits into from
Jan 19, 2024

Conversation

sam-watts
Copy link
Contributor

@sam-watts sam-watts commented Jan 16, 2024

No description provided.

Copy link

pytorch-bot bot commented Jan 16, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8213

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (32 Unrelated Failures)

As of commit 967fd1f with merge base 660868b (image):

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link

Hi @sam-watts!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@sam-watts
Copy link
Contributor Author

sam-watts commented Jan 16, 2024

In cases where a user might want to display overlapping segmentation masks (eg. ground truth vs. prediction) the current function is not suitable as it overwrites colours, meaning that masks processed first are not fully visible:

image

A simple fix is to use iterative multiplicative blending, which shows decent results even in the presence of multiple overlapping masks:

image

code for examples:

def show(imgs):
    if not isinstance(imgs, list):
        imgs = [imgs]
    fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)
    for i, img in enumerate(imgs):
        img = img.detach()
        img = F.to_pil_image(img)
        axs[0, i].imshow(np.asarray(img))
        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])

dims = 5

masks = torch.zeros(dims,256,256).bool()
img = torch.ones(3,256,256).to(torch.uint8) * 255

for i in range(dims):
    center = torch.randint(0, 256, (2,))
    size = torch.randint(20, 50, (2,))
    masks[i, center[0]-size[0]:center[0]+size[0], center[1]-size[1]:center[1]+size[1]] = True

colors = ["red", "green", "blue", "orange", "yellow"]
show(draw_segmentation_masks(img, masks, colors=colors)) # original function
show(_draw_segmentation_masks(img, masks, colors=colors)) # function rewritten in this PR

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@sam-watts sam-watts marked this pull request as ready for review January 17, 2024 10:48
@sam-watts
Copy link
Contributor Author

Outstanding question - do we want to include extra logic in tests.test_utils.test_draw_segmentation_masks to check that the multiplicative blending results in lower pixel values in cases where masks overlap?

@NicolasHug
Copy link
Member

Thanks a lot for this nice addition @sam-watts . The code LGTM.

do we want to include extra logic in tests.test_utils.test_draw_segmentation_masks to check that the multiplicative blending results in lower pixel values in cases where masks overlap?

Yes please! In order to keep it simple, I guess we would have to stop randomly generate the test masks, and instead manually generate 2 masks. 2 overlapping squares should do the job?

@sam-watts
Copy link
Contributor Author

sam-watts commented Jan 17, 2024

FYI @NicolasHug I simplified the code, as it was not working in certain cases. Instead of blending masks we now set areas to zero where there is more than one mask. This has the benefit of also working well when uint8 images are used instead of [0-1] floats

Tests now added

New version looks like this:

image

@sam-watts sam-watts changed the title add multiplicative blending to make overlapping masks display better … Set masks to zero where masks overlap Jan 17, 2024
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks a lot @sam-watts ! Just one question about the addition of pillow to the dev dependencies, but otherwise LGTM!

CONTRIBUTING.md Outdated
@@ -74,7 +74,7 @@ We don't officially support building from source using `pip`, but _if_ you do, y
#### Other development dependencies (some of these are needed to run tests):

```
pip install expecttest flake8 typing mypy pytest pytest-mock scipy
pip install expecttest flake8 typing mypy pytest pytest-mock scipy pillow
Copy link
Member

Choose a reason for hiding this comment

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

That shouldn't be needed here as it's already a hard-dependency of torchvision, so it should get installed when you install torchvision from source.

Curious if you encounter any issue here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm odd. I remember having to install it manually after building from source, as I got a package not installed error from the tests. I'm not familiar with the internals of python setup.py develop, but I can say that I've used pip install -e . instead before. There is some discussion on SO about dependencies not being handled correctly with the former, but I'm not sure how much weight to give that.

Happy to remove this though, I may have just done something odd / could be a personal configuration issue

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the info - let me just remove this so we can move forward with this PR and if we're getting more reports about this I'll investigate. Thank you!

CONTRIBUTING.md Outdated Show resolved Hide resolved
@NicolasHug NicolasHug merged commit 6f0deb9 into pytorch:main Jan 19, 2024
2 of 14 checks passed
@sam-watts sam-watts deleted the seg-masks-mult-blending branch January 19, 2024 14:26
facebook-github-bot pushed a commit that referenced this pull request Mar 19, 2024
Reviewed By: vmoens

Differential Revision: D55062792

fbshipit-source-id: e76e7e5596a619239cf3e01f9fbc7a7d0002504c

Co-authored-by: Nicolas Hug <[email protected]>
Co-authored-by: Nicolas Hug <[email protected]>
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.

3 participants