MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / Keypoints

Class Keypoints

detectron2/structures/keypoints.py:8–80  ·  view source on GitHub ↗

Stores keypoint **annotation** data. GT Instances have a `gt_keypoints` property containing the x,y location and visibility flag of each keypoint. This tensor has shape (N, K, 3) where N is the number of instances and K is the number of keypoints per instance. The visibility flag f

Source from the content-addressed store, hash-verified

6
7
8class Keypoints:
9 """
10 Stores keypoint **annotation** data. GT Instances have a `gt_keypoints` property
11 containing the x,y location and visibility flag of each keypoint. This tensor has shape
12 (N, K, 3) where N is the number of instances and K is the number of keypoints per instance.
13
14 The visibility flag follows the COCO format and must be one of three integers:
15 * v=0: not labeled (in which case x=y=0)
16 * v=1: labeled but not visible
17 * v=2: labeled and visible
18 """
19
20 def __init__(self, keypoints: Union[torch.Tensor, np.ndarray, List[List[float]]]):
21 """
22 Arguments:
23 keypoints: A Tensor, numpy array, or list of the x, y, and visibility of each keypoint.
24 The shape should be (N, K, 3) where N is the number of
25 instances, and K is the number of keypoints per instance.
26 """
27 device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device("cpu")
28 keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device)
29 assert keypoints.dim() == 3 and keypoints.shape[2] == 3, keypoints.shape
30 self.tensor = keypoints
31
32 def __len__(self) -> int:
33 return self.tensor.size(0)
34
35 def to(self, *args: Any, **kwargs: Any) -> "Keypoints":
36 return type(self)(self.tensor.to(*args, **kwargs))
37
38 @property
39 def device(self) -> torch.device:
40 return self.tensor.device
41
42 def to_heatmap(self, boxes: torch.Tensor, heatmap_size: int) -> torch.Tensor:
43 """
44 Convert keypoint annotations to a heatmap of one-hot labels for training,
45 as described in :paper:`Mask R-CNN`.
46
47 Arguments:
48 boxes: Nx4 tensor, the boxes to draw the keypoints to
49
50 Returns:
51 heatmaps:
52 A tensor of shape (N, K), each element is integer spatial label
53 in the range [0, heatmap_size**2 - 1] for each keypoint in the input.
54 valid:
55 A tensor of shape (N, K) containing whether each keypoint is in the roi or not.
56 """
57 return _keypoints_to_heatmap(self.tensor, boxes, heatmap_size)
58
59 def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "Keypoints":
60 """
61 Create a new `Keypoints` by indexing on this `Keypoints`.
62
63 The following usage are allowed:
64
65 1. `new_kpts = kpts[3]`: return a `Keypoints` which contains only one instance.

Callers 3

annotations_to_instancesFunction · 0.90
to_d2_instances_listMethod · 0.90
__getitem__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected