Skip to content

discussion: segmenting algorithms #5

Open

Description

@NickleDave

This issue discusses segmenting algorithms.
The goal is to gather info here to guide planning of what to implement so people can benchmark.

List of resources:

Activity

  1. NickleDave commented on Aug 10, 2022

    @NickleDave
    ContributorAuthor

    Original algorithm (according to Fukuzawa): Rabiner and Sambur 1975 -- thresholds amplitude envelope, fine tunes using zero crossings.

    Attached, Matlab implementation from https://www.mathworks.com/matlabcentral/fileexchange/70424-voice-activity-detection-vad-rabiner1975
    upload.zip

  2. NickleDave commented on Aug 10, 2022

    @NickleDave
    ContributorAuthor

    Note that @dgmets said in discussion with @sthaar and @yangzheng-121 that a similar approach that worked well was:

    • take sliding window of amplitude
    • find first peak of autocorr of window
    • threshold amplitude of that peak <-- IIUC, makes this still a "thresholding amplitude" method. @dgmets please correct if I am not grokking
      • has a pretty sharp boundary at the begin + end of segments
      • can be sped up with array-oriented / vectorization tricks in numpy
      • also clear relation w/fundamental frequency
  3. NickleDave commented on Aug 18, 2022

    @NickleDave
    ContributorAuthor

    Talking with @nickjourjine who mentioned they are using the segmentation functions built into AVA.

    I'm guessing that it's similar to what's in evfuncs but it's always helpful to see multiple implementations -- and we might learn something?

    @nickjourjine can you post a link to that function?
    If you find the code on ava's GitHub repo, you can click on the line number to the left of the function signature and a context box with an ellipsis will pop up, that gives you the option to "copy permalink", as described here:
    https://docs.github.com/en/repositories/working-with-files/using-files/getting-permanent-links-to-files

    For example here's a link to the segmenting code in evfuncs:
    https://github.com/NickleDave/evfuncs/blob/fb4c71b845b3d0274848ab134b4c587578401775/src/evfuncs/evfuncs.py#L333
    (which is literally https://github.com/NickleDave/evfuncs/blob/fb4c71b845b3d0274848ab134b4c587578401775/src/evfuncs/evfuncs.py#L333 before the GitHub UI converts it to a link)

  4. nickjourjine commented on Aug 18, 2022

    @nickjourjine
  5. NickleDave commented on Aug 22, 2022

    @NickleDave
    ContributorAuthor

    Thank you @nickjourjine

    Looks like they segment the spectrogram, i.e. summing spectral power in each time bin then thresholding that.
    Interesting that they use multiple thresholds to find local maxima and minima. I wonder if that's more robust.

    But the function uses a helper function get_spec so the caller has no control over how the spectrogram gets made.
    Also there's a bunch of parameters hidden inside a dict p that gets passed to the function, can't say I'm crazy about that.
    E.g., min_syl_dur and max_syl_dur that get unpacked from p.
    Better to just make those explicit parameters of the function itself.
    Otherwise it's not even obvious the function has that functionality.
    Of course there's probably a reason that made sense at the time -- maybe it's a "config" that gets passed in from somewhere else?
    But in that case you could do segment(**p) and unpack the args into the function.

    Regardless (sorry for the impromptu code review) this is helpful to see. I bet segmenting from spectrograms is less variable than from audio

  6. nickjourjine commented on Aug 30, 2022

    @nickjourjine

    No problem @NickleDave - super helpful to hear your take on this! I’ve also been wondering/worrying about whether segmenting the spectrogram is better or worse than segmenting audio, but agree that segmenting from spectrograms is probably less variable.

  7. NickleDave commented on Aug 30, 2022

    @NickleDave
    ContributorAuthor

    Good idea, thank you @nickjourjine I should have thought to look at scikit-maad.

    They have refs to papers in their docstrings, which is helpful for trying to figure out what algorithms come from where.

    Interesting that find_rois_cwt uses wavelets, lets you specify flim, and returns segments as a dataframe ... those are three unrelated things, but I'm just trying to think about "what's the most useful API look like".

    For both that and the 2d functions, I'm not crazy about hiding a plot function inside of another function, would rather have a plot module with classes for each plot that call their respective business logic functions. E.g., a vocalpy.plot.Rois2d would call a vocalpy.seg.2d.rois under the hood.

  8. NickleDave commented on Sep 7, 2022

    @NickleDave
    ContributorAuthor

    Adding one more:
    @sthaar asked when we met last what @theresekoch is using to segment zebra finch song before training tweetynet

    (@theresekoch apologies in advance for dragging you into a random github issue but please do feel free to chime in)

    From the avn docs, looks like the default method is MFCC derivative threshold segmentation
    https://avn.readthedocs.io/en/latest/tutorial.html#Simple-Segmentation

    which segments syllables based on threshold crossing of an estimate of the first derivative of the first mel frequency cepstral coefficient (MFCC).
    https://github.com/theresekoch/avn/blob/6c8091d092bf610d63542d2da00ff20beceb46d6/avn/segmentation.py#L603

    The first derivative of the MFCC looks something like this (again from the avn docs)
    image
    (from https://avn.readthedocs.io/en/latest/tutorial.html#Manual-Threshold-Selection)

    @arnavraha you may also want to try this.
    Looks like there is also functionality to optimize the threshold and compute metrics like F1

  9. theresekoch commented on Sep 7, 2022

    @theresekoch

    The data that I use to train tweetynet for segmentation is actually just simple amplitude thresholding, but I review the syllable boundaries manually and and/remove or adjust them so that syllables are being segmented consistently within and across birds. I do this with the evsonganaly GUI for MATLAB. I really only coded up MFCC derivative and RMSE derivative segmentation as something to benchmark the tweetynet segmentation against, and in terms of recapitulating those manually corrected amplitude-based segmentations tweetynet does do much better than either of those methods. Does that help?

  10. NickleDave commented on Sep 8, 2022

    @NickleDave
    ContributorAuthor

    Does that help?

    Yes, very much so, thank you for letting us know @theresekoch

    To make sure I understand:

    Did you find that the MFCC and RMSE derivatives were any more consistent then good ol' amplitude thresholding?

  11. theresekoch commented on Sep 8, 2022

    @theresekoch

    Yes, that's all correct. I mean, I don't know if ZBs are the hardest, but they can be messier than people give them credit for. I haven't looked in detail at the implementation of amplitude thresholding in evsonganaly or your evfuncs, but my understanding is that RMSE is conceptually exactly the same as amplitude thresholding. I find that using the first derivative of RMSE is better than just using RMSE when it comes to syllable onsets, but it doesn't work great for syllable offsets, so using that method I include the subsequent silent gap in with the syllable (which isn't ideal for training tweetynet or doing any timing related analysis, but it can be good enough to cluster the syllables and get unsupervised labels). In my hands, MFCC derivative is more accurate than RMSE derivative but I have to admit I don't have a super strong grasp on why that is.

  12. NickleDave commented on Sep 8, 2022

    @NickleDave
    ContributorAuthor

    Very helpful to know, thank you.

    my understanding is that RMSE is conceptually exactly the same as amplitude thresholding

    Not sure how much it matters but evsonganaly uses audio not a spectrogram, and it smooths then rectifies the waveform. Also not sure if it this helps with offsets, but agree that offsets are generally noisier (for BF song at least), because the amplitude tends to trail off instead of having a sharp offset. I blame the syrinx.

    In my hands, MFCC derivative is more accurate than RMSE derivative but I have to admit I don't have a super strong grasp on why that is.

    Interesting. I wonder if it's because the MFCC is supposed to better capture slower varying lower frequencies? (I'm pretending I understand how they work, nota bene).
    For future reference, good post here:
    http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/

  13. NickleDave commented on Nov 5, 2022

    @NickleDave
    ContributorAuthor

    Returning to this after recent discussions with @dgmets @sthaar and @yangzheng-121

    This is captured somewhat above but there's basically two main aspects of thresholding algorithms that might matter:

    • is it 1d or 2d, i.e. does it operate on the audio waveform or the spectrogram
    • does it use a fixed threshold or is it adaptive/dynamic

    Anything else I'm missing?

    E.g., Otsu's method is adaptive, like most segmenting methods adopted from imaging that use a watershed-type approach
    NickleDave/songdkl#58

    Questions a practicioner might have is:

    • which ones are more reliable
    • for which setting / analysis?
      • e.g. for recording multiple animals housed in a group cage where the analysis focuses on calls, adaptive spectrogram thresholding might be most reliable
      • conversely for a single songbird isolated in a small soundbox with a mic directly over the cage where the analysis focuses on song elements, a fixed threshold method may be more reliable. Or not! If the bird moves around a lot

    Almost like someone needs to benchmark this 😈

  14. transferred this issue fromon Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions