Skip to content

Fix dimension indexing in separation_content_motion()#32

Open
Mr-Neutr0n wants to merge 1 commit intoVchitect:mainfrom
Mr-Neutr0n:fix/content-motion-separation-indexing
Open

Fix dimension indexing in separation_content_motion()#32
Mr-Neutr0n wants to merge 1 commit intoVchitect:mainfrom
Mr-Neutr0n:fix/content-motion-separation-indexing

Conversation

@Mr-Neutr0n
Copy link

Summary

separation_content_motion() in utils.py incorrectly indexes dimension 0 (batch) instead of dimension 1 (frames) when separating a video clip into its base frame and motion components.

Bug: The original code uses video_clip[0] and video_clip[i], which index into the batch dimension. According to the docstring, the input shape is [B, F, C, H, W], so frames live on dimension 1.

Before (broken):

base_frame = video_clip[0]           # selects first batch item, not first frame
motions = [video_clip[i] - base_frame for i in range(1, total_frames)]  # iterates over batch dim

After (fixed):

base_frame = video_clip[:, 0:1]  # [B, 1, C, H, W] - selects first frame across all batches
motions = video_clip[:, 1:] - video_clip[:, 0:1]  # [B, F-1, C, H, W] - vectorized frame differencing

This also simplifies the motion computation by replacing the loop + torch.cat with a single vectorized subtraction, which is both cleaner and more efficient.

Test plan

  • Verified the output shapes match the documented return shapes: [B, 1, C, H, W] for base frame and [B, F-1, C, H, W] for motions
  • The fix preserves batch dimension correctly for any batch size

The function was incorrectly indexing dimension 0 (batch) instead of
dimension 1 (frames) when extracting the base frame and computing
motion differences. This caused incorrect results for any batch size
other than the number of frames, and would fail entirely when batch
and frame dimensions differed.

Fix by using proper slicing along the frame dimension (dim=1):
- base_frame: video_clip[:, 0:1] instead of video_clip[0]
- motions: video_clip[:, 1:] - video_clip[:, 0:1] instead of
  iterating over batch indices

This also simplifies the motion computation by using vectorized
subtraction and removes the unnecessary torch.cat call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant