Skip to content

Commit

Permalink
BUG: Fix transforms.functional.view_as_window_batch
Browse files Browse the repository at this point in the history
We were applying `np.squeeze` to the batch viewed as windows,
whenever the second dimension was 1, e.g. if
`batch_window.shape = (1, 1, 257, 4000)`, where `shape[1] == 1`.

This causes an error when we have a sample that is *less*
than the window size that we pad, since we throw away
*both* dimensions 1 and 2, and then the next transform
adds a "channel" dimension *after* the frequency dimension.

Note this only affects frame classification models
on the validation step,
because that is where we need to view a single sample
as a batch of windows.

Now we just *always* throw away the first dimension--
because it's *always* 1.

A separate question is whether there's a more efficient way to do this,
but this should fix the bug for now and lets us
run evaluation with windows larger than the size of some of the samples
(i.e. spectrograms whose number of time bins is less than the window size).
  • Loading branch information
NickleDave committed Oct 13, 2023
1 parent 725f0e3 commit aa094c0
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/vak/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def view_as_window_batch(arr, window_width):
batch_windows = np.lib.stride_tricks.as_strided(
arr, shape=new_shape, strides=new_strides
)
# when 2d, first dim 1 because new shape has height equal to original arr
if batch_windows.ndim == 4 and batch_windows.shape[0] == 1:
# we don't want that extra dim of size 1, we want first dim to be "batch"
batch_windows = np.squeeze(batch_windows)
# TODO: figure out if there's a better way to do this where we don't need to squeeze
# The current version always add an initial dim of size 1
batch_windows = np.squeeze(batch_windows, axis=0)
# By squeezing just that first axis, we always end up with (batch, freq. bins, time bins) for a spectrogram
return batch_windows


Expand Down

0 comments on commit aa094c0

Please sign in to comment.