File size: 8,664 Bytes
07d7c23 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import unittest
import torch
from detectron2.structures import Boxes, BoxMode, Instances
from densepose.modeling.losses.embed_utils import CseAnnotationsAccumulator
from densepose.structures import DensePoseDataRelative, DensePoseList
class TestCseAnnotationsAccumulator(unittest.TestCase):
def test_cse_annotations_accumulator_nodp(self):
instances_lst = [
self._create_instances_nodp(),
]
self._test_template(instances_lst)
def test_cse_annotations_accumulator_sparsedp(self):
instances_lst = [
self._create_instances_sparsedp(),
]
self._test_template(instances_lst)
def test_cse_annotations_accumulator_fulldp(self):
instances_lst = [
self._create_instances_fulldp(),
]
self._test_template(instances_lst)
def test_cse_annotations_accumulator_combined(self):
instances_lst = [
self._create_instances_nodp(),
self._create_instances_sparsedp(),
self._create_instances_fulldp(),
]
self._test_template(instances_lst)
def _test_template(self, instances_lst):
acc = CseAnnotationsAccumulator()
for instances in instances_lst:
acc.accumulate(instances)
packed_anns = acc.pack()
self._check_correspondence(packed_anns, instances_lst)
def _create_instances_nodp(self):
image_shape = (480, 640)
instances = Instances(image_shape)
instances.gt_boxes = Boxes(
torch.as_tensor(
[
[40.0, 40.0, 140.0, 140.0],
[160.0, 160.0, 270.0, 270.0],
[40.0, 160.0, 160.0, 280.0],
]
)
)
instances.proposal_boxes = Boxes(
torch.as_tensor(
[
[41.0, 39.0, 142.0, 138.0],
[161.0, 159.0, 272.0, 268.0],
[41.0, 159.0, 162.0, 278.0],
]
)
)
# do not add gt_densepose
return instances
def _create_instances_sparsedp(self):
image_shape = (540, 720)
instances = Instances(image_shape)
instances.gt_boxes = Boxes(
torch.as_tensor(
[
[50.0, 50.0, 130.0, 130.0],
[150.0, 150.0, 240.0, 240.0],
[50.0, 150.0, 230.0, 330.0],
]
)
)
instances.proposal_boxes = Boxes(
torch.as_tensor(
[
[49.0, 51.0, 131.0, 129.0],
[151.0, 149.0, 241.0, 239.0],
[51.0, 149.0, 232.0, 329.0],
]
)
)
instances.gt_densepose = DensePoseList(
[
None,
self._create_dp_data(
{
"dp_x": [81.69, 153.47, 151.00],
"dp_y": [162.24, 128.71, 113.81],
"dp_vertex": [0, 1, 2],
"ref_model": "zebra_5002",
"dp_masks": [],
},
{"c": (166, 133), "r": 64},
),
None,
],
instances.gt_boxes,
image_shape,
)
return instances
def _create_instances_fulldp(self):
image_shape = (680, 840)
instances = Instances(image_shape)
instances.gt_boxes = Boxes(
torch.as_tensor(
[
[65.0, 55.0, 165.0, 155.0],
[170.0, 175.0, 275.0, 280.0],
[55.0, 165.0, 165.0, 275.0],
]
)
)
instances.proposal_boxes = Boxes(
torch.as_tensor(
[
[66.0, 54.0, 166.0, 154.0],
[171.0, 174.0, 276.0, 279.0],
[56.0, 164.0, 166.0, 274.0],
]
)
)
instances.gt_densepose = DensePoseList(
[
self._create_dp_data(
{
"dp_x": [149.99, 198.62, 157.59],
"dp_y": [170.74, 197.73, 123.12],
"dp_vertex": [3, 4, 5],
"ref_model": "cat_5001",
"dp_masks": [],
},
{"c": (100, 100), "r": 50},
),
self._create_dp_data(
{
"dp_x": [234.53, 116.72, 71.66],
"dp_y": [107.53, 11.31, 142.32],
"dp_vertex": [6, 7, 8],
"ref_model": "dog_5002",
"dp_masks": [],
},
{"c": (200, 150), "r": 40},
),
self._create_dp_data(
{
"dp_x": [225.54, 202.61, 135.90],
"dp_y": [167.46, 181.00, 211.47],
"dp_vertex": [9, 10, 11],
"ref_model": "elephant_5002",
"dp_masks": [],
},
{"c": (100, 200), "r": 45},
),
],
instances.gt_boxes,
image_shape,
)
return instances
def _create_dp_data(self, anns, blob_def=None):
dp_data = DensePoseDataRelative(anns)
if blob_def is not None:
dp_data.segm[
blob_def["c"][0] - blob_def["r"] : blob_def["c"][0] + blob_def["r"],
blob_def["c"][1] - blob_def["r"] : blob_def["c"][1] + blob_def["r"],
] = 1
return dp_data
def _check_correspondence(self, packed_anns, instances_lst):
instance_idx = 0
data_idx = 0
pt_offset = 0
if packed_anns is not None:
bbox_xyxy_gt = BoxMode.convert(
packed_anns.bbox_xywh_gt.clone(), BoxMode.XYWH_ABS, BoxMode.XYXY_ABS
)
bbox_xyxy_est = BoxMode.convert(
packed_anns.bbox_xywh_est.clone(), BoxMode.XYWH_ABS, BoxMode.XYXY_ABS
)
for instances in instances_lst:
if not hasattr(instances, "gt_densepose"):
instance_idx += len(instances)
continue
for i, dp_data in enumerate(instances.gt_densepose):
if dp_data is None:
instance_idx += 1
continue
n_pts = len(dp_data.x)
self.assertTrue(
torch.allclose(dp_data.x, packed_anns.x_gt[pt_offset : pt_offset + n_pts])
)
self.assertTrue(
torch.allclose(dp_data.y, packed_anns.y_gt[pt_offset : pt_offset + n_pts])
)
self.assertTrue(torch.allclose(dp_data.segm, packed_anns.coarse_segm_gt[data_idx]))
self.assertTrue(
torch.allclose(
torch.ones(n_pts, dtype=torch.long) * dp_data.mesh_id,
packed_anns.vertex_mesh_ids_gt[pt_offset : pt_offset + n_pts],
)
)
self.assertTrue(
torch.allclose(
dp_data.vertex_ids, packed_anns.vertex_ids_gt[pt_offset : pt_offset + n_pts]
)
)
self.assertTrue(
torch.allclose(instances.gt_boxes.tensor[i], bbox_xyxy_gt[data_idx])
)
self.assertTrue(
torch.allclose(instances.proposal_boxes.tensor[i], bbox_xyxy_est[data_idx])
)
self.assertTrue(
torch.allclose(
torch.ones(n_pts, dtype=torch.long) * data_idx,
packed_anns.point_bbox_with_dp_indices[pt_offset : pt_offset + n_pts],
)
)
self.assertTrue(
torch.allclose(
torch.ones(n_pts, dtype=torch.long) * instance_idx,
packed_anns.point_bbox_indices[pt_offset : pt_offset + n_pts],
)
)
self.assertEqual(instance_idx, packed_anns.bbox_indices[data_idx])
pt_offset += n_pts
instance_idx += 1
data_idx += 1
if data_idx == 0:
self.assertIsNone(packed_anns)
|