Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion inference/core/utils/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ def cosine_similarity(a: np.ndarray, b: np.ndarray) -> Union[np.number, np.ndarr
Returns:
float: Cosine similarity between vectors A and B.
"""
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Use np.asarray to avoid unnecessary copying if already ndarray
a = np.asarray(a)
b = np.asarray(b)
# Fast norm calculation, avoids repeated full reduction/loop and extra function calls
a_norm = np.sqrt(np.dot(a, a))
b_norm = np.sqrt(np.dot(b, b))
return np.dot(a, b) / (a_norm * b_norm)


def masks2poly(masks: np.ndarray) -> List[np.ndarray]:
Expand Down