Skip to content

Commit

Permalink
Merge branch 'espnet:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
roshansh-cmu authored Feb 15, 2022
2 parents 4aefb6e + a3e1543 commit 969b333
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
18 changes: 13 additions & 5 deletions espnet2/main_funcs/average_nbest_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from pathlib import Path
from typing import Optional
from typing import Sequence
from typing import Union
import warnings
Expand All @@ -17,6 +18,7 @@ def average_nbest_models(
reporter: Reporter,
best_model_criterion: Sequence[Sequence[str]],
nbest: Union[Collection[int], int],
suffix: Optional[str] = None,
) -> None:
"""Generate averaged model from n-best models
Expand All @@ -25,7 +27,8 @@ def average_nbest_models(
reporter: Reporter instance
best_model_criterion: Give criterions to decide the best model.
e.g. [("valid", "loss", "min"), ("train", "acc", "max")]
nbest:
nbest: Number of best model files to be averaged
suffix: A suffix added to the averaged model file name
"""
assert check_argument_types()
if isinstance(nbest, int):
Expand All @@ -35,6 +38,11 @@ def average_nbest_models(
if len(nbests) == 0:
warnings.warn("At least 1 nbest values are required")
nbests = [1]
if suffix is not None:
suffix = suffix + "."
else:
suffix = ""

# 1. Get nbests: List[Tuple[str, str, List[Tuple[epoch, value]]]]
nbest_epochs = [
(ph, k, reporter.sort_epochs_and_values(ph, k, m)[: max(nbests)])
Expand All @@ -55,12 +63,12 @@ def average_nbest_models(
# The averaged model is same as the best model
e, _ = epoch_and_values[0]
op = output_dir / f"{e}epoch.pth"
sym_op = output_dir / f"{ph}.{cr}.ave_1best.pth"
sym_op = output_dir / f"{ph}.{cr}.ave_1best.{suffix}pth"
if sym_op.is_symlink() or sym_op.exists():
sym_op.unlink()
sym_op.symlink_to(op.name)
else:
op = output_dir / f"{ph}.{cr}.ave_{n}best.pth"
op = output_dir / f"{ph}.{cr}.ave_{n}best.{suffix}pth"
logging.info(
f"Averaging {n}best models: " f'criterion="{ph}.{cr}": {op}'
)
Expand Down Expand Up @@ -96,8 +104,8 @@ def average_nbest_models(
torch.save(avg, op)

# 3. *.*.ave.pth is a symlink to the max ave model
op = output_dir / f"{ph}.{cr}.ave_{max(_nbests)}best.pth"
sym_op = output_dir / f"{ph}.{cr}.ave.pth"
op = output_dir / f"{ph}.{cr}.ave_{max(_nbests)}best.{suffix}pth"
sym_op = output_dir / f"{ph}.{cr}.ave.{suffix}pth"
if sym_op.is_symlink() or sym_op.exists():
sym_op.unlink()
sym_op.symlink_to(op.name)
1 change: 1 addition & 0 deletions espnet2/train/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def run(
output_dir=output_dir,
best_model_criterion=trainer_options.best_model_criterion,
nbest=keep_nbest_models,
suffix=f"till{iepoch}epoch",
)

for e in range(1, iepoch):
Expand Down
1 change: 1 addition & 0 deletions test/espnet2/asr/transducer/test_beam_search_transducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from espnet2.lm.seq_rnn_lm import SequentialRNNLM


@pytest.mark.execution_timeout(5)
@pytest.mark.parametrize("rnn_type", ["lstm", "gru"])
@pytest.mark.parametrize(
"search_params",
Expand Down
6 changes: 3 additions & 3 deletions test/espnet2/gan_tts/hifigan/test_hifigan.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ def test_hifigan_generator_and_discriminator_and_loss(
def test_parallel_wavegan_compatibility():
from parallel_wavegan.models import HiFiGANGenerator as PWGHiFiGANGenerator

model_pwg = PWGHiFiGANGenerator()
model_espnet2 = HiFiGANGenerator()
model_pwg = PWGHiFiGANGenerator(**make_hifigan_generator_args())
model_espnet2 = HiFiGANGenerator(**make_hifigan_generator_args())
model_espnet2.load_state_dict(model_pwg.state_dict())
model_pwg.eval()
model_espnet2.eval()

with torch.no_grad():
c = torch.randn(3, 80)
c = torch.randn(3, 5)
out_pwg = model_pwg.inference(c)
out_espnet2 = model_espnet2.inference(c)
np.testing.assert_array_equal(
Expand Down
4 changes: 2 additions & 2 deletions test/espnet2/gan_tts/melgan/test_melgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def test_melgan_generator_and_discriminator(dict_g, dict_d):
def test_parallel_wavegan_compatibility():
from parallel_wavegan.models import MelGANGenerator as PWGMelGANGenerator

model_pwg = PWGMelGANGenerator()
model_espnet2 = MelGANGenerator()
model_pwg = PWGMelGANGenerator(**make_melgan_generator_args())
model_espnet2 = MelGANGenerator(**make_melgan_generator_args())
model_espnet2.load_state_dict(model_pwg.state_dict())
model_pwg.eval()
model_espnet2.eval()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def test_parallel_wavegan_compatibility():
ParallelWaveGANGenerator as PWGParallelWaveGANGenerator, # NOQA
)

model_pwg = PWGParallelWaveGANGenerator()
model_espnet2 = ParallelWaveGANGenerator()
model_pwg = PWGParallelWaveGANGenerator(**make_generator_args())
model_espnet2 = ParallelWaveGANGenerator(**make_generator_args())
model_espnet2.load_state_dict(model_pwg.state_dict())
model_pwg.eval()
model_espnet2.eval()

with torch.no_grad():
z = torch.randn(3 * 256, 1)
c = torch.randn(3, 80)
z = torch.randn(3 * 16, 1)
c = torch.randn(3, 10)
out_pwg = model_pwg.inference(c, z)
out_espnet2 = model_espnet2.inference(c, z)
np.testing.assert_array_equal(
Expand Down
6 changes: 3 additions & 3 deletions test/espnet2/gan_tts/style_melgan/test_style_melgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ def test_style_melgan_trainable(dict_g, dict_d):
def test_parallel_wavegan_compatibility():
from parallel_wavegan.models import StyleMelGANGenerator as PWGStyleMelGANGenerator

model_pwg = PWGStyleMelGANGenerator()
model_espnet2 = StyleMelGANGenerator()
model_pwg = PWGStyleMelGANGenerator(**make_style_melgan_generator_args())
model_espnet2 = StyleMelGANGenerator(**make_style_melgan_generator_args())
model_espnet2.load_state_dict(model_pwg.state_dict())
model_pwg.eval()
model_espnet2.eval()

with torch.no_grad():
c = torch.randn(3, 80)
c = torch.randn(3, 5)
torch.manual_seed(1)
out_pwg = model_pwg.inference(c)
torch.manual_seed(1)
Expand Down

0 comments on commit 969b333

Please sign in to comment.