edge_maps
sleap_nn.data.edge_maps
¶
Transformers for generating edge confidence maps and part affinity fields.
Functions:
Name | Description |
---|---|
distance_to_edge |
Compute pairwise distance between points and undirected edges. |
generate_pafs |
Generate part-affinity fields. |
get_edge_points |
Return the points in each instance that form a directed graph. |
make_edge_maps |
Generate confidence maps for a set of undirected edges. |
make_multi_pafs |
Make multiple instance PAFs with addition reduction. |
make_pafs |
Generate part affinity fields for a set of directed edges. |
distance_to_edge(points, edge_source, edge_destination)
¶
Compute pairwise distance between points and undirected edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points
|
Tensor
|
Tensor of dtype torch.float32 of shape (d_0, ..., d_n, 2) where the last axis corresponds to x- and y-coordinates. Distances will be broadcast across all point dimensions. |
required |
edge_source
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the source points of each edge. |
required |
edge_destination
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the source points of each edge. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of dtype torch.float32 of shape (d_0, ..., d_n, n_edges) where the first
axes correspond to the initial dimensions of |
Source code in sleap_nn/data/edge_maps.py
generate_pafs(instances, img_hw, sigma=1.5, output_stride=2, edge_inds=attrs.field(default=None, converter=(attrs.converters.optional(ensure_list))), flatten_channels=False)
¶
Generate part-affinity fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instances
|
Tensor
|
Input instances. (n_samples, n_instances, n_nodes, 2) |
required |
img_hw
|
Tuple[int]
|
Image size as tuple (height, width). |
required |
sigma
|
float
|
The standard deviation of the Gaussian distribution that is used to generate confidence maps. Default: 1.5. |
1.5
|
output_stride
|
The relative stride to use when generating confidence maps. A larger stride will generate smaller confidence maps. Default: 2. |
2
|
|
edge_inds
|
Optional[Tensor]
|
|
field(default=None, converter=optional(ensure_list))
|
flatten_channels
|
bool
|
If False, the generated tensors are of shape [n_edges, 2, height, width]. If True, generated tensors are of shape [n_edges * 2, height, width] by flattening the last 2 axes. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
The "part_affinity_fields" key will be a tensor of shape (n_edges, 2, grid_height, grid_width) containing the combined part affinity fields of all instances in the frame. If the |
Source code in sleap_nn/data/edge_maps.py
get_edge_points(instances, edge_inds)
¶
Return the points in each instance that form a directed graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instances
|
Tensor
|
A tensor of shape (n_instances, n_nodes, 2) and dtype torch.float32 containing instance points where the last axis corresponds to (x, y) pixel coordinates on the image. This must be rank-3 even if a single instance is present. |
required |
edge_inds
|
Tensor
|
A tensor of shape (n_edges, 2) and dtype torch.int32 containing the node indices that define a directed graph, where the last axis corresponds to the source and destination node indices. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of (edge_sources, edge_destinations) containing the edge and destination points respectively. Both will be tensors of shape (n_instances, n_edges, 2), where the last axis corresponds to (x, y) pixel coordinates on the image. |
Source code in sleap_nn/data/edge_maps.py
make_edge_maps(xv, yv, edge_source, edge_destination, sigma)
¶
Generate confidence maps for a set of undirected edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xv
|
Tensor
|
Sampling grid vector for x-coordinates of shape (grid_width,) and dtype
torch.float32. This can be generated by
|
required |
yv
|
Tensor
|
Sampling grid vector for y-coordinates of shape (grid_height,) and dtype
torch.float32. This can be generated by
|
required |
edge_source
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the source points of each edge. |
required |
edge_destination
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the destination points of each edge. |
required |
sigma
|
float
|
Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A set of confidence maps corresponding to the probability of each point on a sampling grid being on each edge. These will be in a tensor of shape (grid_height, grid_width, n_edges) of dtype torch.float32. |
Source code in sleap_nn/data/edge_maps.py
make_multi_pafs(xv, yv, edge_sources, edge_destinations, sigma)
¶
Make multiple instance PAFs with addition reduction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xv
|
Tensor
|
Sampling grid vector for x-coordinates of shape (grid_width,) and dtype
torch.float32. This can be generated by
|
required |
yv
|
Tensor
|
Sampling grid vector for y-coordinates of shape (grid_height,) and dtype
torch.float32. This can be generated by
|
required |
edge_sources
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_instances, n_edges, 2) where the last axis corresponds to x- and y-coordinates of the source points of each edge. |
required |
edge_destinations
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_instances, n_edges, 2) where the last axis corresponds to x- and y-coordinates of the destination points of each edge. |
required |
sigma
|
float
|
Standard deviation of the 2D Gaussian distribution sampled to generate the edge maps for masking the PAFs. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A set of part affinity fields generated for each instance. These will be in a tensor of shape (n_edges, 2, grid_height, grid_width). If multiple instance PAFs are defined on the same pixel, they will be summed. |
Source code in sleap_nn/data/edge_maps.py
make_pafs(xv, yv, edge_source, edge_destination, sigma)
¶
Generate part affinity fields for a set of directed edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xv
|
Tensor
|
Sampling grid vector for x-coordinates of shape (grid_width,) and dtype
torch.float32. This can be generated by
|
required |
yv
|
Tensor
|
Sampling grid vector for y-coordinates of shape (grid_height,) and dtype
torch.float32. This can be generated by
|
required |
edge_source
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the source points of each edge. |
required |
edge_destination
|
Tensor
|
Tensor of dtype torch.float32 of shape (n_edges, 2) where the last axis corresponds to x- and y-coordinates of the destination points of each edge. |
required |
sigma
|
float
|
Standard deviation of the 2D Gaussian distribution sampled to generate the edge maps for masking the PAFs. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A set of part affinity fields corresponding to the unit vector pointing along the direction of each edge weighted by the probability of each point on a sampling grid being on each edge. These will be in a tensor of shape (n_edges, 2, grid_height, grid_width) of dtype torch.float32. The last axis corresponds to the x- and y-coordinates of the unit vectors. |