pytorch / pytorch/audio

Random cropping for variable length sequences

Open
#3,704 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
799
Avg merge
58m
Merged PRs (30d)
3

Description

🚀 The feature

I am proposing to add a torch.nn.Module transform that automatically crops/pads signals (with different options for padding such as constant/mirroring). I have the implementation already local so I would push it myself if this is alright.

The interface would like as follows:

class RandomCrop(torch.nn.Module):
      def __init__(
             self,
             output_size,  # number of samples to be enforced on output  signal
             axis=-1,  # axis over which to crop
             pad="silence",  # a string controlling the behavior of padding (constant vs reflection)
      )
      def forward(self, signal):  # signal of arbitrary size
             signal = ...
             return signal  # signal now has a fixed size of  `output_size` at `axis`

I am looking for feedback to see if this is also needed/desired by others and whether I should open a PR to add it.

Motivation, pitch

This feature is needed for datasets with variable lengths (a common occurrence for audio). By default, this mismatch in lengths now needs to be handled in the collate function of the dataloader.

With the proposed transform, the user can add it directly to their transform pipeline and/or make it part of their model if they so wish. Moreover, they could simply utilize it in their collate_fn if they want to crop based on the particular batch statistics (e.g. crop/pad to the shortest/longest sample in the batch).

Alternatives

No response

Additional context

A reference implementation and interface can be seen here. As it is implemented with numpy, I would update to torch.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the referenced audtorch implementation in audtorch/transforms/transforms.py around line 113 and the proposed RandomCrop interface in this issue. Clarify the supported crop and padding behaviors, then verify that the transform produces the requested output_size along the selected axis for variable-length signals.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
audio-video-rtc
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.