Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for repitching & tempo automix errors #593

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Ma5onic
Copy link
Contributor

@Ma5onic Ma5onic commented Apr 13, 2024

Fix for repitching TypeError: unsupported format string passed to numpy.ndarray.__format__ error:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/ubuntu/demucs/tools/automix.py", line 343, in <module>
    main()
  File "/home/ubuntu/demucs/tools/automix.py", line 327, in main
    track, origs = build_track(index, catalog)
  File "/home/ubuntu/demucs/tools/automix.py", line 245, in build_track
    wav, spec = get_part(spec, src, dt, dp)
  File "/home/ubuntu/demucs/tools/automix.py", line 209, in get_part
    wav = repitch(wav, dp, dt * 100, samplerate=SR, voice=source == 3)
  File "/home/ubuntu/demucs/demucs/repitch.py", line 74, in repitch
    f"-tempo={tempo:.6f}",
TypeError: unsupported format string passed to numpy.ndarray.__format__

and fix for AttributeError: module 'scipy.signal' has no attribute 'hann' :

concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/lib/python3.10/concurrent/futures/process.py", line 246, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/home/ubuntu/demucs/tools/automix.py", line 81, in analyse_track
    tempo, events = beat_track(y=drums.numpy(), units='time', sr=SR)
  File "/home/ubuntu/demucs/.venv/lib/python3.10/site-packages/librosa/beat.py", line 185, in beat_track
    beats = __beat_tracker(onset_envelope, bpm, float(sr) / hop_length, tightness, trim)
  File "/home/ubuntu/demucs/.venv/lib/python3.10/site-packages/librosa/beat.py", line 433, in __beat_tracker
    beats = __trim_beats(localscore, beats, trim)
  File "/home/ubuntu/demucs/.venv/lib/python3.10/site-packages/librosa/beat.py", line 507, in __trim_beats
    smooth_boe = scipy.signal.convolve(localscore[beats], scipy.signal.hann(5), "same")
AttributeError: module 'scipy.signal' has no attribute 'hann'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/ubuntu/demucs/tools/automix.py", line 343, in <module>
    main()
  File "/home/ubuntu/demucs/tools/automix.py", line 312, in main
    spec, track = pending.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
AttributeError: module 'scipy.signal' has no attribute 'hann'

Explanation of code changes:

Conditional Application:
The check (dt != 0 or dp != 0) ensures that repitch is only called if there is a change to be made, saving computational resources if the audio is to remain unaltered.

Type and Size Validation for dt:
It ensures that dt is a scalar by checking its type and size, which is crucial for correct format string usage in the repitch function's command construction.

Percentage Conversion:
The tempo_percentage_change calculation transforms dt from a decimal representation (e.g., -0.12 for a tempo slowing to 88% of the original) into the format expected by the repitch function (e.g., -12 to represent the same).

Application of Changes:
Calls repitch with the correct parameters, including checking if the current track is the voice track (assumed to be at index 3).

Onset Adjustment:
Adjusts the onsets of the spec to account for the new tempo. The onsets must be scaled by the reciprocal of (1 + dt) because a negative dt implies a reduction in speed (and hence longer duration between onsets).

This fixes `AttributeError: module 'scipy.signal' has no attribute 'hann'` that occurs when trying to augment data using `automix.py`.

In librosa version 0.10.2.dev0, the `__trim_beats` in beat.py no longer uses scipy for hanning computation & now uses `np.hanning` instead.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 13, 2024
Copy link
Contributor

@CarlGao4 CarlGao4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, please open this pull request to adefossez/demucs

And, please don't add this requirement to requirements.txt. It is only required when training the model.

@Ma5onic
Copy link
Contributor Author

Ma5onic commented Apr 24, 2024

@CarlGao4 Thanks for the feedback.
Sorry I missed the part in the readme about adefossezs' fork. I was just following the CONTRIBUTING.md which states:

Demucs is the implementation of a research paper. Therefore, we do not plan on accepting many pull requests for new features. We certainly welcome them for bug fixes.

This is a bug fix and not a pull request for a new feature, which is why I opened this PR.


As for the requirements.txt, could you explain why I shouldn't be placing librosa in it when the "Requirements" section of README.md states:

See requirements_minimal.txt for requirements for separation only,
and environment-[cpu|cuda].yml (or requirements.txt) if you want to train a new model.

This can be confirmed by inspecting the setup.py used by pip, which shows that the that the requirements_minimal.txt is used by default and the requirements.txt is only used if you specify [dev] during the installation (either with pip install -e .[dev] or pip install demucs[dev]) as seen in lines 59 to 62:

    extras_require={
        'dev': ALL_REQUIRED,
    },
    install_requires=REQUIRED,

where:

REQUIRED = load_requirements('requirements_minimal.txt')
ALL_REQUIRED = load_requirements('requirements.txt')

Fix README.md instructions for machine learning scientists, so that the training requirements/dependencies actually get installed.
@CarlGao4
Copy link
Contributor

Sorry that's my mistake. But did you add this requirement to environment-cuda.yml?

`-12` should have been `-0.12` but the comment was redundant, so I removed it.
@Ma5onic
Copy link
Contributor Author

Ma5onic commented Apr 24, 2024

@CarlGao4, No problem... It happens. 😉

But did you add this requirement to environment-cuda.yml?

No, not yet...
Short Answer: I'm waiting for release 0.10.2 of librosa to be distributed by pip

Long Answer:

  • The last step in the instructions for machine learning scientists is to install requirements.txt
  • Currently, the latest versions of librosa available via pip or conda-forge is 0.10.1, which is the version that was causing the bug. When trying to locate librosa/beat.py", line 507 from the Traceback output in the master branch, I noticed that smooth_boe = scipy.signal.convolve(localscore[beats], scipy.signal.hann(5), "same") no longer existed at its original location.
  • Once I located where smooth_boe is currently being defined, I noticed that it now used numpy instead of scipy.signal. Since the new smooth_boe implementation is what is being used in the planned 0.10.2 release, I decided to install from source instead of writing a fix that would break once version 0.10.2 of librosa gets officially released.
  • When using cloud GPU instances for training, not all of them have conda pre-installed. Setting librosa in requirements.txt allows it to be used using venv + pip as well.
  • The conda dependency resolver can sometimes take ridiculous amounts of time & still fail to resolve dependencies, which can be costly when using a cloud GPU instance.

However, I do agree that once version 0.10.2 of librosa gets released to the pip package manager (and/or conda), it should also be specified in the - pip: section of environment-cuda.yml & environment-cpu.yml.
Technically, the values of the - pip: requirements within the environment-*.yml should match what is in requirements.txt (and vice versa), which is a separate issue that I will fix in a later pull request/commit.

@JuYangFu
Copy link

Hi, why do I still have this problem after I modified it the way you did?
image

@CarlGao4
Copy link
Contributor

Maybe you should upgrade Python and numpy? (Like Python 3.9 and numpy 1.25.2 which is distributed with PyPI, not conda)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants