bmcfee / muda

A library for augmenting annotated audio data
ISC License
230 stars 33 forks source link

BackgroundNoise fails if len(soundf)==n_target #59

Closed lostanlen closed 7 years ago

lostanlen commented 7 years ago

I have a dataset of audio clips of the same length. Half of these clips are positive (contain a bird flight call), while the other half is negative (contain only background noise). I want to augment the dataset by mixing clips together, without changing the label.

But I ran into an error in 'muda.deformers.background.sample_clip_indices'. If I understand the stack trace correctly (see below my signature), the error happens when executing start = np.random.randint(0, len(soundf) - n_target) with len(soundf) - n_target equal to zero.

I made a Gist to reproduce the bug: https://gist.github.com/lostanlen/15fe9c879fdd24fe9023fa430314cd51 It disappears when the difference in lengths is strictly larger than zero.

Is this expected behavior? It seems to me that my issue could be fixed with

if len(soundf) > n_target:
    start = np.random.randint(0, len(soundf) - n_target)
else:
    start = 0

Best, Vincent.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-9ecbf36219b3> in <module>()
     39 # Create short deformer
     40 short_deformer = muda.deformers.BackgroundNoise(files=[short_noise_path])
---> 41 short_jam_transformer = next(short_deformer.transform(jam_original)) # error

/Users/vl238/miniconda3/lib/python3.5/site-packages/muda/base.py in transform(self, jam)
    142         '''
    143 
--> 144         for state in self.states(jam):
    145             yield self._transform(jam, state)
    146 

/Users/vl238/miniconda3/lib/python3.5/site-packages/muda/deformers/background.py in states(self, jam)
    154         for fname in self.files:
    155             for _ in range(self.n_samples):
--> 156                 start, stop = sample_clip_indices(fname, len(mudabox._audio['y']), mudabox._audio['sr'])
    157                 yield dict(filename=fname,
    158                            weight=np.random.uniform(low=self.weight_min,

/Users/vl238/miniconda3/lib/python3.5/site-packages/muda/deformers/background.py in sample_clip_indices(filename, n_samples, sr)
     40 
     41         # Draw a random clip
---> 42         start = np.random.randint(0, len(soundf) - n_target)
     43         stop = start + n_target
     44 

mtrand.pyx in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:16117)()

ValueError: low >= high
bmcfee commented 7 years ago

Yup, that makes sense, thanks for catching it! We recently hit almost the same issue over in pumpp, so it's not too surprising. Do you have any interest in doing a PR for this?

lostanlen commented 7 years ago

Yes, I will write a PR later today.

lostanlen commented 7 years ago

On second thought, this looks more like an off-by-one error. The fix would then be:

start = np.random.randint(0, 1 + len(soundf) - n_target)

That would be a breaking change, though.

bmcfee commented 7 years ago

That would be a breaking change, though.

That's fine -- it's broken as is. I bumped the milestone for this to 0.2, so it's okay to not be backward-compatible with the 0.1 series.