Skip to content

add segmentation reference consistency tests #6591

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

Merged
merged 7 commits into from
Sep 23, 2022

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Sep 15, 2022

Follow-up to #6566 for segmentation references.

@@ -873,7 +877,7 @@ def make_datapoints(self, with_mask=True):

yield (pil_image, target)

tensor_image = torch.randint(0, 256, size=(3, *size), dtype=torch.uint8)
tensor_image = torch.Tensor(make_image(size=size, color_space=features.ColorSpace.RGB))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I missed this before. Let's use the utilities everywhere.


self.check(t, t_ref)

def test_random_resize_eval(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both this and the test above currently fail. The resized mask is off by quite a bit and so far I don't have an idea why or if that is expected. For the references the resize is performed as PIL image while the prototype transforms uses a features.Mask which internally is treated like a tensor image.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This happens because PIL and PyTorch don't agree on how exactly the values should be picked during nearest interpolation in case the upsampling is not an integer multiple. For example

from torchvision.prototype.transforms import functional as F
import torch


num_categories = 21
size = [520]

torch.manual_seed(123)
segmentation_mask = torch.randint(0, num_categories, (384, 640), dtype=torch.uint8)
print(segmentation_mask[:3, :3])

a = F.resize_mask(segmentation_mask, size)
print(a[:5, :5])

b = F.to_image_tensor(
    F.resize_image_pil(
        F.to_image_pil(segmentation_mask),
        size,
        interpolation=F.InterpolationMode.NEAREST,
    )
).squeeze(0)
print(b[:5, :5])

assert a.shape == b.shape
tensor([[16,  1,  0, 12],
        [ 3,  7, 20, 15],
        [12, 19, 11, 12],
        [ 2, 20,  6,  9]], dtype=torch.uint8)
tensor([[16, 16,  1,  0,  0],
        [16, 16,  1,  0,  0],
        [ 3,  3,  7, 20, 20],
        [12, 12, 19, 11, 11],
        [12, 12, 19, 11, 11]], dtype=torch.uint8)
tensor([[16,  1,  1,  0, 12],
        [ 3,  7,  7, 20, 15],
        [ 3,  7,  7, 20, 15],
        [12, 19, 19, 11, 12],
        [ 2, 20, 20,  6,  9]], dtype=torch.uint8)

This might have an effect on the accuracy. So far I don't see a way to change this other than aligning torch.nn.functional.interpolate with what PIL does for nearest interpolation.

# 1. transforms.RandomCrop uses a different scheme to pad images and masks of insufficient size than its name
# counterpart in the detection references. Thus, we cannot use it with `pad_if_needed=True`
# 2. transforms.Pad only supports a fixed padding, but the segmentation datasets don't have a fixed image size.
class PadIfSmaller(prototype_transforms.Transform):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This implements the option suggested in #6433 (comment).

I've also played around with my proposal from #6433 (comment), but it was a lot more complex.

This PR demonstrates that this transform in combination with our transforms.RandomCrop can be used to mimic the behavior of the RandomCrop from the segmentation references. Thus, if we go through with this PR, I will port this transform to #6433.

@pmeier pmeier marked this pull request as ready for review September 20, 2022 12:16
@pmeier pmeier requested review from vfdev-5 and datumbox September 20, 2022 12:16
@pmeier
Copy link
Collaborator Author

pmeier commented Sep 20, 2022

Test failure is unrelated.

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

The PadIfSmaller looks good to me. Let's unblock to continue the accuracy tests.

@pmeier pmeier mentioned this pull request Sep 21, 2022
@pmeier pmeier merged commit 7046e56 into pytorch:main Sep 23, 2022
@pmeier pmeier deleted the consistency-segmentation branch September 23, 2022 21:06
facebook-github-bot pushed a commit that referenced this pull request Sep 29, 2022
Summary:
* add segmentation reference consistency tests

* fall back to smoke tests for resize

* add test for RandomCrop

Reviewed By: YosuaMichael

Differential Revision: D39885419

fbshipit-source-id: 12692442af350ca9b97e8764ed429d2d56e0682a

Co-authored-by: Vasilis Vryniotis <[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