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

minor fixes for 4.1.0a1 #552

Merged
merged 14 commits into from
Nov 16, 2023
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Session.vim
/trash
/misc
/mdx
.mypy_cache
.mypy_cache
2 changes: 1 addition & 1 deletion demucs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

__version__ = "4.1.0a1"
__version__ = "4.1.0a2"
19 changes: 17 additions & 2 deletions demucs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from pathlib import Path
from typing import Optional, Callable, Dict, Tuple, Union

from .apply import apply_model, _replace_dict
from .apply import apply_model, _replace_dict, BagOfModels
from .audio import AudioFile, convert_audio, save_audio
from .pretrained import get_model, _parse_remote_files, REMOTE_ROOT
from .repo import RemoteRepo, LocalRepo, ModelOnlyRepo, BagOnlyRepo
Expand Down Expand Up @@ -195,7 +195,7 @@ def update_parameter(
self._jobs = jobs
if not isinstance(progress, _NotProvided):
self._progress = progress
if not isinstance(callback, _NotProvided) and (callback is None or callable(callback)):
if not isinstance(callback, _NotProvided):
self._callback = callback
if not isinstance(callback_arg, _NotProvided):
self._callback_arg = callback_arg
Expand Down Expand Up @@ -264,6 +264,21 @@ def separate_tensor(
"""
if sr is not None and sr != self.samplerate:
wav = convert_audio(wav, sr, self._samplerate, self._audio_channels)
if wav.max() == wav.min():
CarlGao4 marked this conversation as resolved.
Show resolved Hide resolved
self._callback( # type: ignore
_replace_dict(
self._callback_arg,
("state", "end"),
("model_idx_in_bag", (len(self._model.models) - 1)
if isinstance(self._model, BagOfModels) else 0),
("shift_idx", self._shifts - 1),
("segment_offset", wav.shape[1]),
("audio_length", wav.shape[1]),
("models", len(self._model.models) if isinstance(self._model, BagOfModels)
else 1)
)
)
return wav, {name: (wav / len(self._model.sources)) for name in self._model.sources}
ref = wav.mean(0)
wav -= ref.mean()
wav /= ref.std()
Expand Down
36 changes: 14 additions & 22 deletions demucs/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,11 @@ def apply_model(model: tp.Union[BagOfModels, Model],
estimates: tp.Union[float, th.Tensor] = 0.
totals = [0.] * len(model.sources)
callback_arg["models"] = len(model.models)
kwargs["callback"] = (
(
lambda d, i=callback_arg["model_idx_in_bag"]: callback(
_replace_dict(d, ("model_idx_in_bag", i))
)
)
if callable(callback)
else None
)
for sub_model, model_weights in zip(model.models, model.weights):
kwargs["callback"] = ((
lambda d, i=callback_arg["model_idx_in_bag"]: callback(
_replace_dict(d, ("model_idx_in_bag", i))) if callback else None)
)
original_model_device = next(iter(sub_model.parameters())).device
sub_model.to(device)

Expand Down Expand Up @@ -252,9 +247,8 @@ def apply_model(model: tp.Union[BagOfModels, Model],
offset = random.randint(0, max_shift)
shifted = TensorChunk(padded_mix, offset, length + max_shift - offset)
kwargs["callback"] = (
(lambda d, i=shift_idx: callback(_replace_dict(d, ("shift_idx", i))))
if callable(callback)
else None
(lambda d, i=shift_idx: callback(_replace_dict(d, ("shift_idx", i)))
if callback else None)
)
res = apply_model(model, shifted, **kwargs, callback_arg=callback_arg)
if res is None:
Expand Down Expand Up @@ -289,8 +283,8 @@ def apply_model(model: tp.Union[BagOfModels, Model],
chunk = TensorChunk(mix, offset, segment_length)
future = pool.submit(apply_model, model, chunk, **kwargs, callback_arg=callback_arg,
callback=(lambda d, i=offset:
callback(_replace_dict(d, ("segment_offset", i))))
if callable(callback) else None)
callback(_replace_dict(d, ("segment_offset", i)))
if callback else None))
futures.append((future, offset))
offset += segment_length
if progress:
Expand Down Expand Up @@ -321,19 +315,17 @@ def apply_model(model: tp.Union[BagOfModels, Model],
padded_mix = mix.padded(valid_length).to(device)
with lock:
try:
callback(_replace_dict(callback_arg, ("state", "start"))) # type: ignore
if callback is not None:
callback(_replace_dict(callback_arg, ("state", "start"))) # type: ignore
except KeyboardInterrupt:
raise
except Exception:
pass
return None
CarlGao4 marked this conversation as resolved.
Show resolved Hide resolved
with th.no_grad():
out = model(padded_mix)
with lock:
try:
callback(_replace_dict(callback_arg, ("state", "end"))) # type: ignore
if callback is not None:
callback(_replace_dict(callback_arg, ("state", "end"))) # type: ignore
except KeyboardInterrupt:
raise
except Exception:
pass
return None
assert isinstance(out, th.Tensor)
return center_trim(out, length)
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ progress: If true, show a progress bar.

##### Notes for callback

The function will be called with only one positional parameter whose type is `dict`. The `callback_arg` will be combined with information of current separation progress. The progress information will override the values in `callback_arg` if same key has been used. To abort the separation, raise `KeyboardInterrupt`.
The function will be called with only one positional parameter whose type is `dict`. The `callback_arg` will be combined with information of current separation progress. The progress information will override the values in `callback_arg` if same key has been used. To abort the separation, raise `KeyboardInterrupt`. This will directly make the function return `None` but not stop the program.

Progress information contains several keys (These keys will always exist):
- `model_idx_in_bag`: The index of the submodel in `BagOfModels`. Starts from 0.
Expand Down Expand Up @@ -127,7 +127,7 @@ progress: If true, show a progress bar.

##### Notes for callback

The function will be called with only one positional parameter whose type is `dict`. The `callback_arg` will be combined with information of current separation progress. The progress information will override the values in `callback_arg` if same key has been used. To abort the separation, raise `KeyboardInterrupt`.
The function will be called with only one positional parameter whose type is `dict`. The `callback_arg` will be combined with information of current separation progress. The progress information will override the values in `callback_arg` if same key has been used. To abort the separation, raise `KeyboardInterrupt`. This will directly make the function return `None` but not stop the program.
CarlGao4 marked this conversation as resolved.
Show resolved Hide resolved

Progress information contains several keys (These keys will always exist):
- `model_idx_in_bag`: The index of the submodel in `BagOfModels`. Starts from 0.
Expand Down
2 changes: 1 addition & 1 deletion docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Added `--other-method`: method to get `no_{STEM}`, add up all the other stems (a

Added type `HTDemucs` to type alias `AnyModel`.

## V4.0.1a1, TBD
## V4.0.1, 8th of September 2023

**From this version, Python 3.7 is no longer supported. This is not a problem since the latest PyTorch 2.0.0 no longer support it either.**

Expand Down
2 changes: 1 addition & 1 deletion docs/windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Start the Anaconda prompt, and run the following
bash
```
conda install -c conda-forge ffmpeg
python.exe -m pip install -U demucs PySoundFile
python.exe -m pip install -U demucs SoundFile
```

### Upgrade
Expand Down
Loading