Skip to content

Commit 5522254

Browse files
committed
Fix top-down pose postprocessing device
Update PyTorch top-down pose postprocessing to keep tensors on the same device/dtype by using `new_zeros` and `new_tensor` instead of creating CPU tensors. Also replace `torch.cat(poses)` with `torch.stack(..., dim=0)` so batched pose outputs preserve the expected batch dimension.
1 parent e61f726 commit 5522254

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎dlclive/pose_estimation_pytorch/runner.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,21 +357,24 @@ def _postprocess_top_down(
357357
"""Post-processes pose for top-down models."""
358358
if len(batch_pose) == 0:
359359
bodyparts, coords = batch_pose.shape[-2:]
360-
return torch.zeros((0, bodyparts, coords))
360+
return batch_pose.new_zeros((0, bodyparts, coords))
361361

362362
poses = []
363+
363364
for pose, (offset, scale) in zip(batch_pose, offsets_and_scales, strict=False):
364-
poses.append(
365-
torch.cat(
366-
[
367-
pose[..., :2] * torch.tensor(scale) + torch.tensor(offset),
368-
pose[..., 2:3],
369-
],
370-
dim=-1,
371-
)
365+
scale_tensor = pose.new_tensor(scale)
366+
offset_tensor = pose.new_tensor(offset)
367+
368+
transformed_pose = torch.cat(
369+
[
370+
pose[..., :2] * scale_tensor + offset_tensor,
371+
pose[..., 2:3],
372+
],
373+
dim=-1,
372374
)
375+
poses.append(transformed_pose)
373376

374-
return torch.cat(poses)
377+
return torch.stack(poses, dim=0)
375378

376379

377380
def _parse_device(device: str | None) -> str:

0 commit comments

Comments
 (0)