Spaces:
Runtime error
Runtime error
File size: 11,489 Bytes
14114e8 |
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
import warnings
from typing import Dict, List
import numpy as np
# Check if Numba is available
HAS_NUMBA = False
try:
import numba as nb
HAS_NUMBA = True
except ImportError:
warnings.warn(
"Numba not found. Using slower pure Python implementations.", UserWarning
)
# -------------------- Helper Functions --------------------
def is_zero_box(bbox: list) -> bool:
"""Check if bounding box is invalid"""
if bbox is None:
return True
return all(x <= 0 for x in bbox[:4]) or len(bbox) < 4
def convert_bbox_format(bbox: list) -> List[float]:
"""Convert bbox from (x,y,w,h) to (x1,y1,x2,y2)"""
x, y, w, h = bbox
return [x, y, x + w, y + h]
# -------------------- Track-level NMS --------------------
def process_track_level_nms(video_groups: Dict, nms_threshold: float) -> Dict:
"""Apply track-level NMS to all videos"""
for video_id, tracks in video_groups.items():
track_detections = []
# Process tracks
for track_idx, track in enumerate(tracks):
if not track["bboxes"]:
continue
converted_bboxes = []
valid_frames = []
for bbox in track["bboxes"]:
if bbox and not is_zero_box(bbox):
converted_bboxes.append(convert_bbox_format(bbox))
valid_frames.append(True)
else:
converted_bboxes.append([np.nan] * 4)
valid_frames.append(False)
if any(valid_frames):
track_detections.append(
{
"track_idx": track_idx,
"bboxes": np.array(converted_bboxes, dtype=np.float32),
"score": track["score"],
}
)
# Apply NMS
if track_detections:
scores = np.array([d["score"] for d in track_detections], dtype=np.float32)
keep = apply_track_nms(track_detections, scores, nms_threshold)
# Suppress non-kept tracks
for idx, track in enumerate(track_detections):
if idx not in keep:
tracks[track["track_idx"]]["bboxes"] = [None] * len(track["bboxes"])
return video_groups
# -------------------- Frame-level NMS --------------------
def process_frame_level_nms(video_groups: Dict, nms_threshold: float) -> Dict:
"""Apply frame-level NMS to all videos"""
for video_id, tracks in video_groups.items():
if not tracks:
continue
num_frames = len(tracks[0]["bboxes"])
for frame_idx in range(num_frames):
frame_detections = []
# Collect valid detections
for track_idx, track in enumerate(tracks):
bbox = track["bboxes"][frame_idx]
if bbox and not is_zero_box(bbox):
frame_detections.append(
{
"track_idx": track_idx,
"bbox": np.array(
convert_bbox_format(bbox), dtype=np.float32
),
"score": track["score"],
}
)
# Apply NMS
if frame_detections:
bboxes = np.stack([d["bbox"] for d in frame_detections])
scores = np.array(
[d["score"] for d in frame_detections], dtype=np.float32
)
keep = apply_frame_nms(bboxes, scores, nms_threshold)
# Suppress non-kept detections
for i, d in enumerate(frame_detections):
if i not in keep:
tracks[d["track_idx"]]["bboxes"][frame_idx] = None
return video_groups
# Track-level NMS helpers ------------------------------------------------------
def compute_track_iou_matrix(
bboxes_stacked: np.ndarray, valid_masks: np.ndarray, areas: np.ndarray
) -> np.ndarray:
"""IoU matrix computation for track-level NMS with fallback to pure Python"""
num_tracks = bboxes_stacked.shape[0]
iou_matrix = np.zeros((num_tracks, num_tracks), dtype=np.float32)
if HAS_NUMBA:
iou_matrix = _compute_track_iou_matrix_numba(bboxes_stacked, valid_masks, areas)
else:
# Pure Python implementation
for i in range(num_tracks):
for j in range(i + 1, num_tracks):
valid_ij = valid_masks[i] & valid_masks[j]
if not valid_ij.any():
continue
bboxes_i = bboxes_stacked[i, valid_ij]
bboxes_j = bboxes_stacked[j, valid_ij]
area_i = areas[i, valid_ij]
area_j = areas[j, valid_ij]
inter_total = 0.0
union_total = 0.0
for k in range(bboxes_i.shape[0]):
x1 = max(bboxes_i[k, 0], bboxes_j[k, 0])
y1 = max(bboxes_i[k, 1], bboxes_j[k, 1])
x2 = min(bboxes_i[k, 2], bboxes_j[k, 2])
y2 = min(bboxes_i[k, 3], bboxes_j[k, 3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = area_i[k] + area_j[k] - inter
inter_total += inter
union_total += union
if union_total > 0:
iou_matrix[i, j] = inter_total / union_total
iou_matrix[j, i] = iou_matrix[i, j]
return iou_matrix
if HAS_NUMBA:
@nb.jit(nopython=True, parallel=True)
def _compute_track_iou_matrix_numba(bboxes_stacked, valid_masks, areas):
"""Numba-optimized IoU matrix computation for track-level NMS"""
num_tracks = bboxes_stacked.shape[0]
iou_matrix = np.zeros((num_tracks, num_tracks), dtype=np.float32)
for i in nb.prange(num_tracks):
for j in range(i + 1, num_tracks):
valid_ij = valid_masks[i] & valid_masks[j]
if not valid_ij.any():
continue
bboxes_i = bboxes_stacked[i, valid_ij]
bboxes_j = bboxes_stacked[j, valid_ij]
area_i = areas[i, valid_ij]
area_j = areas[j, valid_ij]
inter_total = 0.0
union_total = 0.0
for k in range(bboxes_i.shape[0]):
x1 = max(bboxes_i[k, 0], bboxes_j[k, 0])
y1 = max(bboxes_i[k, 1], bboxes_j[k, 1])
x2 = min(bboxes_i[k, 2], bboxes_j[k, 2])
y2 = min(bboxes_i[k, 3], bboxes_j[k, 3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = area_i[k] + area_j[k] - inter
inter_total += inter
union_total += union
if union_total > 0:
iou_matrix[i, j] = inter_total / union_total
iou_matrix[j, i] = iou_matrix[i, j]
return iou_matrix
def apply_track_nms(
track_detections: List[dict], scores: np.ndarray, nms_threshold: float
) -> List[int]:
"""Vectorized track-level NMS implementation"""
if not track_detections:
return []
bboxes_stacked = np.stack([d["bboxes"] for d in track_detections], axis=0)
valid_masks = ~np.isnan(bboxes_stacked).any(axis=2)
areas = (bboxes_stacked[:, :, 2] - bboxes_stacked[:, :, 0]) * (
bboxes_stacked[:, :, 3] - bboxes_stacked[:, :, 1]
)
areas[~valid_masks] = 0
iou_matrix = compute_track_iou_matrix(bboxes_stacked, valid_masks, areas)
keep = []
order = np.argsort(-scores)
suppress = np.zeros(len(track_detections), dtype=bool)
for i in range(len(order)):
if not suppress[order[i]]:
keep.append(order[i])
suppress[order[i:]] = suppress[order[i:]] | (
iou_matrix[order[i], order[i:]] >= nms_threshold
)
return keep
# Frame-level NMS helpers ------------------------------------------------------
def compute_frame_ious(bbox: np.ndarray, bboxes: np.ndarray) -> np.ndarray:
"""IoU computation for frame-level NMS with fallback to pure Python"""
if HAS_NUMBA:
return _compute_frame_ious_numba(bbox, bboxes)
else:
# Pure Python implementation
ious = np.zeros(len(bboxes), dtype=np.float32)
for i in range(len(bboxes)):
x1 = max(bbox[0], bboxes[i, 0])
y1 = max(bbox[1], bboxes[i, 1])
x2 = min(bbox[2], bboxes[i, 2])
y2 = min(bbox[3], bboxes[i, 3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
area2 = (bboxes[i, 2] - bboxes[i, 0]) * (bboxes[i, 3] - bboxes[i, 1])
union = area1 + area2 - inter
ious[i] = inter / union if union > 0 else 0.0
return ious
if HAS_NUMBA:
@nb.jit(nopython=True, parallel=True)
def _compute_frame_ious_numba(bbox, bboxes):
"""Numba-optimized IoU computation"""
ious = np.zeros(len(bboxes), dtype=np.float32)
for i in nb.prange(len(bboxes)):
x1 = max(bbox[0], bboxes[i, 0])
y1 = max(bbox[1], bboxes[i, 1])
x2 = min(bbox[2], bboxes[i, 2])
y2 = min(bbox[3], bboxes[i, 3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
area2 = (bboxes[i, 2] - bboxes[i, 0]) * (bboxes[i, 3] - bboxes[i, 1])
union = area1 + area2 - inter
ious[i] = inter / union if union > 0 else 0.0
return ious
def apply_frame_nms(
bboxes: np.ndarray, scores: np.ndarray, nms_threshold: float
) -> List[int]:
"""Frame-level NMS implementation with fallback to pure Python"""
if HAS_NUMBA:
return _apply_frame_nms_numba(bboxes, scores, nms_threshold)
else:
# Pure Python implementation
order = np.argsort(-scores)
keep = []
suppress = np.zeros(len(bboxes), dtype=bool)
for i in range(len(order)):
if not suppress[order[i]]:
keep.append(order[i])
current_bbox = bboxes[order[i]]
remaining_bboxes = bboxes[order[i + 1 :]]
if len(remaining_bboxes) > 0: # Check if there are any remaining boxes
ious = compute_frame_ious(current_bbox, remaining_bboxes)
suppress[order[i + 1 :]] = suppress[order[i + 1 :]] | (
ious >= nms_threshold
)
return keep
if HAS_NUMBA:
@nb.jit(nopython=True)
def _apply_frame_nms_numba(bboxes, scores, nms_threshold):
"""Numba-optimized NMS implementation"""
order = np.argsort(-scores)
keep = []
suppress = np.zeros(len(bboxes), dtype=nb.boolean)
for i in range(len(order)):
if not suppress[order[i]]:
keep.append(order[i])
current_bbox = bboxes[order[i]]
if i + 1 < len(order): # Check bounds
ious = _compute_frame_ious_numba(
current_bbox, bboxes[order[i + 1 :]]
)
suppress[order[i + 1 :]] = suppress[order[i + 1 :]] | (
ious >= nms_threshold
)
return keep
|