Skip to content

Commit 305b863

Browse files
committed
Validate frame input and fix scale defaults
Improve the PyTorch runner’s frame handling by validating HWC input shape and allowed channel counts, correctly unpacking dimensions, and dropping alpha for 4-channel frames. Also fix the top-down fallback `offsets_and_scales` format to use paired float scales instead of a scalar, preventing downstream shape/format issues.
1 parent d35b17f commit 305b863

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

‎dlclive/pose_estimation_pytorch/runner.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""PyTorch and ONNX runners for DeepLabCut-Live"""
1212

1313
import copy
14+
import logging
1415
from dataclasses import dataclass
1516
from pathlib import Path
1617
from typing import Literal
@@ -25,6 +26,8 @@
2526
from dlclive.core.runner import BaseRunner
2627
from dlclive.pose_estimation_pytorch.data.image import AutoPadToDivisor
2728

29+
logger = logging.getLogger(__name__)
30+
2831

2932
@dataclass
3033
class SkipFrames:
@@ -192,8 +195,18 @@ def close(self) -> None:
192195

193196
@torch.inference_mode()
194197
def get_pose(self, frame: np.ndarray) -> np.ndarray:
195-
c, h, w = frame.shape
196-
tensor = torch.from_numpy(frame).permute(2, 0, 1) # CHW, still on CPU
198+
if frame.ndim != 3:
199+
raise ValueError(f"Expected HWC frame, got shape {frame.shape}")
200+
201+
h, w, c = frame.shape
202+
203+
if c not in (1, 3, 4):
204+
raise ValueError(f"Expected 1, 3, or 4 channels in the last dimension, got shape {frame.shape}")
205+
206+
if c == 4:
207+
frame = frame[..., :3]
208+
209+
tensor = torch.from_numpy(frame).permute(2, 0, 1)
197210

198211
offsets_and_scales = None
199212
if self.detector is not None:
@@ -367,7 +380,7 @@ def _prepare_top_down(self, frame: torch.Tensor, detections: dict[str, torch.Ten
367380
else:
368381
crop_w, crop_h = self.top_down_config.crop_size
369382
frame_batch = torch.zeros((1, 3, crop_h, crop_w), device=frame.device)
370-
offsets_and_scales = [(0, 0), 1]
383+
offsets_and_scales = [(0, 0), (1.0, 1.0)]
371384

372385
return frame_batch, offsets_and_scales
373386

0 commit comments

Comments
 (0)