-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ops.masks_to_bounding_boxes * test fixtures * unit test * ignore lint e201 and e202 for in-lined matrix * ignore e121 and e241 linting rules for in-lined matrix * draft gallery example text * removed type annotations from pytest fixtures * inlined fixture * renamed masks_to_bounding_boxes to masks_to_boxes * reformat inline array * import cleanup * moved masks_to_boxes into boxes module * docstring cleanup * updated docstring * fix formatting issue * gallery example * use torch * use torch * use torch * use torch * updated docs and test * cleanup * updated import * use torch * Update gallery/plot_repurposing_annotations.py Co-authored-by: Aditya Oke <[email protected]> * Update gallery/plot_repurposing_annotations.py Co-authored-by: Aditya Oke <[email protected]> * Update gallery/plot_repurposing_annotations.py Co-authored-by: Aditya Oke <[email protected]> * Autodoc * use torch instead of numpy in tests * fix build_docs failure * Closing quotes. Co-authored-by: Vasilis Vryniotis <[email protected]> Co-authored-by: Aditya Oke <[email protected]>
- Loading branch information
1 parent
8a83cf2
commit f0422e7
Showing
8 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
sphinx==3.5.4 | ||
sphinx-gallery>=0.9.0 | ||
sphinx-copybutton>=0.3.1 | ||
matplotlib | ||
numpy | ||
sphinx-copybutton>=0.3.1 | ||
sphinx-gallery>=0.9.0 | ||
sphinx==3.5.4 | ||
-e git+git://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
======================= | ||
Repurposing annotations | ||
======================= | ||
The following example illustrates the operations available in the torchvision.ops module for repurposing object | ||
localization annotations for different tasks (e.g. transforming masks used by instance and panoptic segmentation | ||
methods into bounding boxes used by object detection methods). | ||
""" | ||
import os.path | ||
|
||
import PIL.Image | ||
import matplotlib.patches | ||
import matplotlib.pyplot | ||
import numpy | ||
import torch | ||
from torchvision.ops import masks_to_boxes | ||
|
||
ASSETS_DIRECTORY = "../test/assets" | ||
|
||
matplotlib.pyplot.rcParams["savefig.bbox"] = "tight" | ||
|
||
#################################### | ||
# Masks | ||
# ----- | ||
# In tasks like instance and panoptic segmentation, masks are commonly defined, and are defined by this package, | ||
# as a multi-dimensional array (e.g. a NumPy array or a PyTorch tensor) with the following shape: | ||
# | ||
# (objects, height, width) | ||
# | ||
# Where objects is the number of annotated objects in the image. Each (height, width) object corresponds to exactly | ||
# one object. For example, if your input image has the dimensions 224 x 224 and has four annotated objects the shape | ||
# of your masks annotation has the following shape: | ||
# | ||
# (4, 224, 224). | ||
# | ||
# A nice property of masks is that they can be easily repurposed to be used in methods to solve a variety of object | ||
# localization tasks. | ||
# | ||
# Masks to bounding boxes | ||
# ---------------------------------------- | ||
# For example, the masks to bounding_boxes operation can be used to transform masks into bounding boxes that can be | ||
# used in methods like Faster RCNN and YOLO. | ||
|
||
with PIL.Image.open(os.path.join(ASSETS_DIRECTORY, "masks.tiff")) as image: | ||
masks = torch.zeros((image.n_frames, image.height, image.width), dtype=torch.int) | ||
|
||
for index in range(image.n_frames): | ||
image.seek(index) | ||
|
||
frame = numpy.array(image) | ||
|
||
masks[index] = torch.tensor(frame) | ||
|
||
bounding_boxes = masks_to_boxes(masks) | ||
|
||
figure = matplotlib.pyplot.figure() | ||
|
||
a = figure.add_subplot(121) | ||
b = figure.add_subplot(122) | ||
|
||
labeled_image = torch.sum(masks, 0) | ||
|
||
a.imshow(labeled_image) | ||
b.imshow(labeled_image) | ||
|
||
for bounding_box in bounding_boxes: | ||
x0, y0, x1, y1 = bounding_box | ||
|
||
rectangle = matplotlib.patches.Rectangle((x0, y0), x1 - x0, y1 - y0, linewidth=1, edgecolor="r", facecolor="none") | ||
|
||
b.add_patch(rectangle) | ||
|
||
a.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) | ||
b.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os.path | ||
|
||
import PIL.Image | ||
import numpy | ||
import torch | ||
|
||
from torchvision.ops import masks_to_boxes | ||
|
||
ASSETS_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets") | ||
|
||
|
||
def test_masks_to_boxes(): | ||
with PIL.Image.open(os.path.join(ASSETS_DIRECTORY, "masks.tiff")) as image: | ||
masks = torch.zeros((image.n_frames, image.height, image.width), dtype=torch.int) | ||
|
||
for index in range(image.n_frames): | ||
image.seek(index) | ||
|
||
frame = numpy.array(image) | ||
|
||
masks[index] = torch.tensor(frame) | ||
|
||
expected = torch.tensor( | ||
[[127, 2, 165, 40], | ||
[2, 50, 44, 92], | ||
[56, 63, 98, 100], | ||
[139, 68, 175, 104], | ||
[160, 112, 198, 145], | ||
[49, 138, 99, 182], | ||
[108, 148, 152, 213]], | ||
dtype=torch.int32 | ||
) | ||
|
||
torch.testing.assert_close(masks_to_boxes(masks), expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters