Last active
September 4, 2024 17:02
-
-
Save rehno-lindeque/f268c78aec0e83ab00bcc683bee12d3f to your computer and use it in GitHub Desktop.
modified masks_to_boxes from torchvision/ops/boxes.py (PR #8194)
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
{ buildPythonPackage, python }: | |
buildPythonPackage { | |
pname = "torchvision_masks_to_boxes"; | |
version = "pr8194"; | |
src = ./.; | |
format = "other"; | |
phases = ["unpackPhase" "installPhase"]; | |
installPhase = '' | |
mkdir -p $out/${python.sitePackages} | |
cp $src/*.py $out/${python.sitePackages} | |
''; | |
} |
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
{ | |
description = "masks_to_boxes from torchvision"; | |
outputs = { self }: { | |
overlays.default = final: prev: { | |
python = prev.python.override (oldAttrs: { | |
self = final.python; | |
packageOverrides = prev.lib.composeExtensions oldAttrs.packageOverrides self.pythonOverrides.default; | |
}); | |
}; | |
pythonOverrides.default = final: prev: { | |
torchvision_masks_to_boxes = final.callPackage ./. {}; | |
}; | |
}; | |
} |
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
BSD 3-Clause License | |
Copyright (c) Soumith Chintala 2016, | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of the copyright holder nor the names of its | |
contributors may be used to endorse or promote products derived from | |
this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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
import torch | |
def masks_to_boxes(masks: torch.Tensor) -> torch.Tensor: | |
""" | |
Compute the bounding boxes around the provided masks. | |
Returns a [N, 4] tensor containing bounding boxes. The boxes are in ``(x1, y1, x2, y2)`` format with | |
``0 <= x1 < x2`` and ``0 <= y1 < y2``. | |
Args: | |
masks (Tensor[N, H, W]): masks to transform where N is the number of masks | |
and (H, W) are the spatial dimensions. | |
Returns: | |
Tensor[N, 4]: bounding boxes | |
""" | |
if masks.numel() == 0: | |
return torch.zeros((0, 4), device=masks.device, dtype=torch.float) | |
non_zero_xs = torch.any(masks, axis=1).float() | |
non_zero_ys = torch.any(masks, axis=2).float() | |
y1 = non_zero_ys.argmax(dim=1) | |
x1 = non_zero_xs.argmax(dim=1) | |
y2 = (masks.shape[1] - 1) - non_zero_ys.flip(dims=[1]).argmax(dim=1) | |
x2 = (masks.shape[2] - 1) - non_zero_xs.flip(dims=[1]).argmax(dim=1) | |
bounding_boxes = torch.stack((x1, y1, x2, y2), dim=1).float() | |
return bounding_boxes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See pytorch/vision#8194