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| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
import torch
|
| 5 |
+
from torch.nn import functional as F
|
| 6 |
+
|
| 7 |
+
from detectron2.structures import BitMasks, Boxes, BoxMode
|
| 8 |
+
|
| 9 |
+
from .base import IntTupleBox, make_int_box
|
| 10 |
+
from .to_mask import ImageSizeType
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def resample_coarse_segm_tensor_to_bbox(coarse_segm: torch.Tensor, box_xywh_abs: IntTupleBox):
|
| 14 |
+
"""
|
| 15 |
+
Resample coarse segmentation tensor to the given
|
| 16 |
+
bounding box and derive labels for each pixel of the bounding box
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
coarse_segm: float tensor of shape [1, K, Hout, Wout]
|
| 20 |
+
box_xywh_abs (tuple of 4 int): bounding box given by its upper-left
|
| 21 |
+
corner coordinates, width (W) and height (H)
|
| 22 |
+
Return:
|
| 23 |
+
Labels for each pixel of the bounding box, a long tensor of size [1, H, W]
|
| 24 |
+
"""
|
| 25 |
+
x, y, w, h = box_xywh_abs
|
| 26 |
+
w = max(int(w), 1)
|
| 27 |
+
h = max(int(h), 1)
|
| 28 |
+
labels = F.interpolate(coarse_segm, (h, w), mode="bilinear", align_corners=False).argmax(dim=1)
|
| 29 |
+
return labels
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def resample_fine_and_coarse_segm_tensors_to_bbox(
|
| 33 |
+
fine_segm: torch.Tensor, coarse_segm: torch.Tensor, box_xywh_abs: IntTupleBox
|
| 34 |
+
):
|
| 35 |
+
"""
|
| 36 |
+
Resample fine and coarse segmentation tensors to the given
|
| 37 |
+
bounding box and derive labels for each pixel of the bounding box
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
fine_segm: float tensor of shape [1, C, Hout, Wout]
|
| 41 |
+
coarse_segm: float tensor of shape [1, K, Hout, Wout]
|
| 42 |
+
box_xywh_abs (tuple of 4 int): bounding box given by its upper-left
|
| 43 |
+
corner coordinates, width (W) and height (H)
|
| 44 |
+
Return:
|
| 45 |
+
Labels for each pixel of the bounding box, a long tensor of size [1, H, W]
|
| 46 |
+
"""
|
| 47 |
+
x, y, w, h = box_xywh_abs
|
| 48 |
+
w = max(int(w), 1)
|
| 49 |
+
h = max(int(h), 1)
|
| 50 |
+
# coarse segmentation
|
| 51 |
+
coarse_segm_bbox = F.interpolate(
|
| 52 |
+
coarse_segm,
|
| 53 |
+
(h, w),
|
| 54 |
+
mode="bilinear",
|
| 55 |
+
align_corners=False,
|
| 56 |
+
).argmax(dim=1)
|
| 57 |
+
# combined coarse and fine segmentation
|
| 58 |
+
labels = (
|
| 59 |
+
F.interpolate(fine_segm, (h, w), mode="bilinear", align_corners=False).argmax(dim=1)
|
| 60 |
+
* (coarse_segm_bbox > 0).long()
|
| 61 |
+
)
|
| 62 |
+
return labels
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def resample_fine_and_coarse_segm_to_bbox(predictor_output: Any, box_xywh_abs: IntTupleBox):
|
| 66 |
+
"""
|
| 67 |
+
Resample fine and coarse segmentation outputs from a predictor to the given
|
| 68 |
+
bounding box and derive labels for each pixel of the bounding box
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
predictor_output: DensePose predictor output that contains segmentation
|
| 72 |
+
results to be resampled
|
| 73 |
+
box_xywh_abs (tuple of 4 int): bounding box given by its upper-left
|
| 74 |
+
corner coordinates, width (W) and height (H)
|
| 75 |
+
Return:
|
| 76 |
+
Labels for each pixel of the bounding box, a long tensor of size [1, H, W]
|
| 77 |
+
"""
|
| 78 |
+
return resample_fine_and_coarse_segm_tensors_to_bbox(
|
| 79 |
+
predictor_output.fine_segm,
|
| 80 |
+
predictor_output.coarse_segm,
|
| 81 |
+
box_xywh_abs,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def predictor_output_with_coarse_segm_to_mask(
|
| 86 |
+
predictor_output: Any, boxes: Boxes, image_size_hw: ImageSizeType
|
| 87 |
+
) -> BitMasks:
|
| 88 |
+
"""
|
| 89 |
+
Convert predictor output with coarse and fine segmentation to a mask.
|
| 90 |
+
Assumes that predictor output has the following attributes:
|
| 91 |
+
- coarse_segm (tensor of size [N, D, H, W]): coarse segmentation
|
| 92 |
+
unnormalized scores for N instances; D is the number of coarse
|
| 93 |
+
segmentation labels, H and W is the resolution of the estimate
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
predictor_output: DensePose predictor output to be converted to mask
|
| 97 |
+
boxes (Boxes): bounding boxes that correspond to the DensePose
|
| 98 |
+
predictor outputs
|
| 99 |
+
image_size_hw (tuple [int, int]): image height Himg and width Wimg
|
| 100 |
+
Return:
|
| 101 |
+
BitMasks that contain a bool tensor of size [N, Himg, Wimg] with
|
| 102 |
+
a mask of the size of the image for each instance
|
| 103 |
+
"""
|
| 104 |
+
H, W = image_size_hw
|
| 105 |
+
boxes_xyxy_abs = boxes.tensor.clone()
|
| 106 |
+
boxes_xywh_abs = BoxMode.convert(boxes_xyxy_abs, BoxMode.XYXY_ABS, BoxMode.XYWH_ABS)
|
| 107 |
+
N = len(boxes_xywh_abs)
|
| 108 |
+
masks = torch.zeros((N, H, W), dtype=torch.bool, device=boxes.tensor.device)
|
| 109 |
+
for i in range(len(boxes_xywh_abs)):
|
| 110 |
+
box_xywh = make_int_box(boxes_xywh_abs[i])
|
| 111 |
+
box_mask = resample_coarse_segm_tensor_to_bbox(predictor_output[i].coarse_segm, box_xywh)
|
| 112 |
+
x, y, w, h = box_xywh
|
| 113 |
+
masks[i, y : y + h, x : x + w] = box_mask
|
| 114 |
+
|
| 115 |
+
return BitMasks(masks)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def predictor_output_with_fine_and_coarse_segm_to_mask(
|
| 119 |
+
predictor_output: Any, boxes: Boxes, image_size_hw: ImageSizeType
|
| 120 |
+
) -> BitMasks:
|
| 121 |
+
"""
|
| 122 |
+
Convert predictor output with coarse and fine segmentation to a mask.
|
| 123 |
+
Assumes that predictor output has the following attributes:
|
| 124 |
+
- coarse_segm (tensor of size [N, D, H, W]): coarse segmentation
|
| 125 |
+
unnormalized scores for N instances; D is the number of coarse
|
| 126 |
+
segmentation labels, H and W is the resolution of the estimate
|
| 127 |
+
- fine_segm (tensor of size [N, C, H, W]): fine segmentation
|
| 128 |
+
unnormalized scores for N instances; C is the number of fine
|
| 129 |
+
segmentation labels, H and W is the resolution of the estimate
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
predictor_output: DensePose predictor output to be converted to mask
|
| 133 |
+
boxes (Boxes): bounding boxes that correspond to the DensePose
|
| 134 |
+
predictor outputs
|
| 135 |
+
image_size_hw (tuple [int, int]): image height Himg and width Wimg
|
| 136 |
+
Return:
|
| 137 |
+
BitMasks that contain a bool tensor of size [N, Himg, Wimg] with
|
| 138 |
+
a mask of the size of the image for each instance
|
| 139 |
+
"""
|
| 140 |
+
H, W = image_size_hw
|
| 141 |
+
boxes_xyxy_abs = boxes.tensor.clone()
|
| 142 |
+
boxes_xywh_abs = BoxMode.convert(boxes_xyxy_abs, BoxMode.XYXY_ABS, BoxMode.XYWH_ABS)
|
| 143 |
+
N = len(boxes_xywh_abs)
|
| 144 |
+
masks = torch.zeros((N, H, W), dtype=torch.bool, device=boxes.tensor.device)
|
| 145 |
+
for i in range(len(boxes_xywh_abs)):
|
| 146 |
+
box_xywh = make_int_box(boxes_xywh_abs[i])
|
| 147 |
+
labels_i = resample_fine_and_coarse_segm_to_bbox(predictor_output[i], box_xywh)
|
| 148 |
+
x, y, w, h = box_xywh
|
| 149 |
+
masks[i, y : y + h, x : x + w] = labels_i > 0
|
| 150 |
+
return BitMasks(masks)
|