Skip to content

Commit

Permalink
Add normalized_instance_similarity method (#1939)
Browse files Browse the repository at this point in the history
* Add normalize function

* Expose normalization function

* Fix tests

* Expose object keypoint sim function

* Fix tests
  • Loading branch information
gitttt-1234 authored Sep 18, 2024
1 parent e4bb444 commit 3c7f5af
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/guides/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ optional arguments:
--tracking.clean_iou_threshold TRACKING.CLEAN_IOU_THRESHOLD
IOU to use when culling instances *after* tracking. (default: 0)
--tracking.similarity TRACKING.SIMILARITY
Options: instance, centroid, iou (default: instance)
Options: instance, normalized_instance, object_keypoint, centroid, iou (default: instance)
--tracking.match TRACKING.MATCH
Options: hungarian, greedy (default: greedy)
--tracking.robust TRACKING.ROBUST
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/proofreading.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ There are currently three methods for matching instances in frame N against thes
-**centroid**” measures similarity by the distance between the instance centroids
-**iou**” measures similarity by the intersection/overlap of the instance bounding boxes
-**instance**” measures similarity by looking at the distances between corresponding nodes in the instances, normalized by the number of valid nodes in the candidate instance.
-**normalized_instance**” measures similarity by looking at the distances between corresponding nodes in the instances, normalized by the number of valid nodes in the candidate instance and the keypoints normalized by the image size.
-**object_keypoint**” measures similarity by measuring the distance between each keypoints from a reference instance and a query instance, takes the exp(-d**2), sum for all the keypoints and divide by the number of visible keypoints in the reference instance.

Once SLEAP has measured the similarity between all the candidates and the instances in frame N, you need to choose a way to pair them up. You can do this either by picking the best match, and the picking the best remaining match for each remaining instance in turn—this is “**greedy**” matching—or you can find the way of matching identities which minimizes the total cost (or: maximizes the total similarity)—this is “**Hungarian**” matching.

Expand Down
4 changes: 2 additions & 2 deletions sleap/config/pipeline_form.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ inference:
label: Similarity Method
type: list
default: instance
options: "instance,centroid,iou,object keypoint"
options: "instance,normalized_instance,centroid,iou,object keypoint"
- name: tracking.match
label: Matching Method
type: list
Expand Down Expand Up @@ -538,7 +538,7 @@ inference:
label: Similarity Method
type: list
default: instance
options: "instance,centroid,iou,object keypoint"
options: "instance,normalized_instance,centroid,iou,object keypoint"
- name: tracking.match
label: Matching Method
type: list
Expand Down
2 changes: 2 additions & 0 deletions sleap/nn/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -2622,6 +2622,7 @@ def _object_builder():
# Set tracks for predicted instances in this frame.
predicted_instances = self.tracker.track(
untracked_instances=predicted_instances,
img_hw=ex["image"].shape[-3:-1],
img=image,
t=frame_ind,
)
Expand Down Expand Up @@ -3264,6 +3265,7 @@ def _object_builder():
# Set tracks for predicted instances in this frame.
predicted_instances = self.tracker.track(
untracked_instances=predicted_instances,
img_hw=ex["image"].shape[-3:-1],
img=image,
t=frame_ind,
)
Expand Down
16 changes: 16 additions & 0 deletions sleap/nn/tracker/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import operator
from collections import defaultdict
import logging
Expand All @@ -29,6 +30,21 @@
InstanceType = TypeVar("InstanceType", Instance, PredictedInstance)


def normalized_instance_similarity(
ref_instance: InstanceType, query_instance: InstanceType, img_hw: Tuple[int]
) -> float:
"""Computes similarity between instances with normalized keypoints."""

normalize_factors = np.array((img_hw[1], img_hw[0]))
ref_visible = ~(np.isnan(ref_instance.points_array).any(axis=1))
normalized_query_keypoints = query_instance.points_array / normalize_factors
normalized_ref_keypoints = ref_instance.points_array / normalize_factors
dists = np.sum((normalized_query_keypoints - normalized_ref_keypoints) ** 2, axis=1)
similarity = np.nansum(np.exp(-dists)) / np.sum(ref_visible)

return similarity


def instance_similarity(
ref_instance: InstanceType, query_instance: InstanceType
) -> float:
Expand Down
13 changes: 12 additions & 1 deletion sleap/nn/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import attr
import numpy as np
import cv2
import functools
from typing import Callable, Deque, Dict, Iterable, List, Optional, Tuple

from sleap import Track, LabeledFrame, Skeleton

from sleap.nn.tracker.components import (
factory_object_keypoint_similarity,
instance_similarity,
normalized_instance_similarity,
centroid_distance,
instance_iou,
hungarian_matching,
Expand Down Expand Up @@ -495,7 +497,8 @@ def get_candidates(
instance=instance_similarity,
centroid=centroid_distance,
iou=instance_iou,
object_keypoint=instance_similarity,
normalized_instance=normalized_instance_similarity,
object_keypoint=factory_object_keypoint_similarity,
)

match_policies = dict(
Expand Down Expand Up @@ -639,19 +642,26 @@ def uses_image(self):
def track(
self,
untracked_instances: List[InstanceType],
img_hw: Tuple[int],
img: Optional[np.ndarray] = None,
t: int = None,
) -> List[InstanceType]:
"""Performs a single step of tracking.
Args:
untracked_instances: List of instances to assign to tracks.
img_hw: (height, width) of the image used to normalize the keypoints.
img: Image data of the current frame for flow shifting.
t: Current timestep. If not provided, increments from the internal queue.
Returns:
A list of the instances that were tracked.
"""
if self.similarity_function == normalized_instance_similarity:
factory_normalized_instance = functools.partial(
normalized_instance_similarity, img_hw=img_hw
)
self.similarity_function = factory_normalized_instance

if self.candidate_maker is None:
return untracked_instances
Expand Down Expand Up @@ -1520,6 +1530,7 @@ def run_tracker(frames: List[LabeledFrame], tracker: BaseTracker) -> List[Labele
track_args["img"] = lf.video[lf.frame_idx]
else:
track_args["img"] = None
track_args["img_hw"] = lf.image.shape[-3:-1]

new_lf = LabeledFrame(
frame_idx=lf.frame_idx,
Expand Down
12 changes: 10 additions & 2 deletions tests/nn/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,11 @@ def test_flow_tracker(centered_pair_predictions_sorted: Labels, tmpdir):
for inst in lf.instances:
inst.track = None

track_args = dict(untracked_instances=lf.instances, img=lf.video[lf.frame_idx])
track_args = dict(
untracked_instances=lf.instances,
img=lf.video[lf.frame_idx],
img_hw=lf.image.shape[-3:-1],
)
tracker.track(**track_args)

# Check that saved instances are pruned to track window
Expand Down Expand Up @@ -1975,7 +1979,11 @@ def test_max_tracks_matching_queue(
for inst in lf.instances:
inst.track = None

track_args = dict(untracked_instances=lf.instances, img=lf.video[lf.frame_idx])
track_args = dict(
untracked_instances=lf.instances,
img=lf.video[lf.frame_idx],
img_hw=lf.image.shape[-3:-1],
)
tracker.track(**track_args)

if trackername == "flowmaxtracks":
Expand Down
19 changes: 11 additions & 8 deletions tests/nn/test_tracker_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ def tracker_by_name(frames=None, **kwargs):
inst.track = None

track_args = dict(untracked_instances=lf.instances, img=lf.video[lf.frame_idx])
t.track(**track_args)
t.track(**track_args, img_hw=(1, 1))
t.final_pass(frames)


@pytest.mark.parametrize(
"tracker", ["simple", "flow", "simplemaxtracks", "flowmaxtracks"]
)
@pytest.mark.parametrize("similarity", ["instance", "iou", "centroid"])
@pytest.mark.parametrize(
"similarity",
["instance", "normalized_instance", "iou", "centroid", "object_keypoint"],
)
@pytest.mark.parametrize("match", ["greedy", "hungarian"])
@pytest.mark.parametrize("count", [0, 2])
def test_tracker_by_name(
Expand Down Expand Up @@ -288,7 +291,7 @@ def test_max_tracking_large_gap_single_track():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand All @@ -305,7 +308,7 @@ def test_max_tracking_large_gap_single_track():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand Down Expand Up @@ -352,7 +355,7 @@ def test_max_tracking_small_gap_on_both_tracks():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand All @@ -369,7 +372,7 @@ def test_max_tracking_small_gap_on_both_tracks():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand Down Expand Up @@ -421,7 +424,7 @@ def test_max_tracking_extra_detections():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand All @@ -438,7 +441,7 @@ def test_max_tracking_extra_detections():

tracked = []
for insts in preds:
tracked_insts = tracker.track(insts)
tracked_insts = tracker.track(insts, img_hw=(1, 1))
tracked.append(tracked_insts)
all_tracks = list(set([inst.track for frame in tracked for inst in frame]))

Expand Down
4 changes: 3 additions & 1 deletion tests/nn/test_tracking_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def run_tracker(frames, tracker):
new_lf = LabeledFrame(
frame_idx=lf.frame_idx,
video=lf.video,
instances=tracker.track(**track_args),
instances=tracker.track(**track_args, img_hw=lf.image.shape[-3:-1]),
)
new_lfs.append(new_lf)

Expand Down Expand Up @@ -138,6 +138,8 @@ def main(f, dir):
instance=sleap.nn.tracker.components.instance_similarity,
centroid=sleap.nn.tracker.components.centroid_distance,
iou=sleap.nn.tracker.components.instance_iou,
normalized_instance=sleap.nn.tracker.components.normalized_instance_similarity,
object_keypoint=sleap.nn.tracker.components.factory_object_keypoint_similarity(),
)
scales = (
1,
Expand Down

0 comments on commit 3c7f5af

Please sign in to comment.