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

Added crop_segmentation_mask op #5851

Merged
merged 5 commits into from
Apr 22, 2022
Merged

Conversation

vfdev-5
Copy link
Collaborator

@vfdev-5 vfdev-5 commented Apr 21, 2022

Related to #5514

Description:

  • Added functional crop_segmentation_mask op
  • Added tests

Results on synthetic images/bboxes/segm mask:

Code
import numpy as np

import torch
import torchvision
from torchvision.prototype import features
from torchvision.prototype.transforms.functional import crop_image_tensor, crop_bounding_box, crop_segmentation_mask

size = (64, 76)
# xyxy format
in_boxes = [
    [10, 15, 25, 35],
    [50, 5, 70, 22],
    [45, 46, 56, 62],
]
labels = [1, 2, 3]

im1 = 255 * np.ones(size + (3, ), dtype=np.uint8)
mask = np.zeros(size, dtype=np.int64)
for in_box, label in zip(in_boxes, labels):
    im1[in_box[1]:in_box[3], in_box[0]:in_box[2], :] = (127, 127, 127)
    mask[in_box[1]:in_box[3], in_box[0]:in_box[2]] = label
    
in_im = torch.tensor(im1).permute(2, 0, 1).view(1, 3, *size)

in_boxes = features.BoundingBox(
    in_boxes, format=features.BoundingBoxFormat.XYXY, image_size=size
)
in_mask = features.SegmentationMask(torch.tensor(mask)).view(1, *size)
    
top = -10
left = 5
height = size[0] - 15
width = size[1] + 20


out_boxes = crop_bounding_box(
    in_boxes, 
    in_boxes.format,
    top,
    left,
)

out_mask = crop_segmentation_mask(
    in_mask, 
    top,
    left,
    height,
    width,
)

out_im = crop_image_tensor(in_im, top, left, height, width)

image

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.

LGTM.

@pmeier thoughts on the test side?

@vfdev-5 vfdev-5 merged commit c66da5e into pytorch:main Apr 22, 2022
@vfdev-5 vfdev-5 deleted the proto-mask-crop branch April 22, 2022 14:14
@github-actions
Copy link

Hey @vfdev-5!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

@vfdev-5
Copy link
Collaborator Author

vfdev-5 commented Apr 22, 2022

@pmeier thoughts on the test side?

Let's see test side later once Philip coded his functional opinfo-like proposal.

facebook-github-bot pushed a commit that referenced this pull request May 6, 2022
Summary:
* Added `crop_segmentation_mask` op

* Fixed failed mypy

Reviewed By: jdsgomes, NicolasHug

Differential Revision: D36095716

fbshipit-source-id: 7e471babab53882870878d919a8c865836dff995
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