Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/open-mmlab/Amphion
09 September 2024, 06:46:44 UTC
  • Code
  • Branches (2)
  • Releases (3)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/main
    • refs/heads/revert-154-FACodec-readme
    • v0.1.1-alpha
    • v0.1.0-alpha
    • v0.1.0
  • 320c54d
  • /
  • bins
  • /
  • calc_metrics.py
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
  • release
origin badgecontent badge
swh:1:cnt:04863c4f845114b59a8d4ff3565859691493790f
origin badgedirectory badge
swh:1:dir:e7579b26cc2de1fe02856a5d77d1a27ad92211ea
origin badgerevision badge
swh:1:rev:a4c23e2e1f15e4be0b0c7194e6b69a82a4bb4a07
origin badgesnapshot badge
swh:1:snp:bef780d851faeac80aef6db569e51e66f505bf34
origin badgerelease badge
swh:1:rel:acb10df35ee6dcf0c0dcb3216afce897fb0bc227

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
  • release
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: a4c23e2e1f15e4be0b0c7194e6b69a82a4bb4a07 authored by Xueyao Zhang on 18 December 2023, 14:14:33 UTC
Amphion v0.1 Release (#39)
Tip revision: a4c23e2
calc_metrics.py
# Copyright (c) 2023 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import os
import numpy as np
import json
import argparse

from glob import glob
from tqdm import tqdm
from collections import defaultdict

from evaluation.metrics.energy.energy_rmse import extract_energy_rmse
from evaluation.metrics.energy.energy_pearson_coefficients import (
    extract_energy_pearson_coeffcients,
)
from evaluation.metrics.f0.f0_pearson_coefficients import extract_fpc
from evaluation.metrics.f0.f0_periodicity_rmse import extract_f0_periodicity_rmse
from evaluation.metrics.f0.f0_rmse import extract_f0rmse
from evaluation.metrics.f0.v_uv_f1 import extract_f1_v_uv
from evaluation.metrics.intelligibility.character_error_rate import extract_cer
from evaluation.metrics.intelligibility.word_error_rate import extract_wer
from evaluation.metrics.similarity.speaker_similarity import extract_speaker_similarity
from evaluation.metrics.spectrogram.frechet_distance import extract_fad
from evaluation.metrics.spectrogram.mel_cepstral_distortion import extract_mcd
from evaluation.metrics.spectrogram.multi_resolution_stft_distance import extract_mstft
from evaluation.metrics.spectrogram.pesq import extract_pesq
from evaluation.metrics.spectrogram.scale_invariant_signal_to_distortion_ratio import (
    extract_si_sdr,
)
from evaluation.metrics.spectrogram.scale_invariant_signal_to_noise_ratio import (
    extract_si_snr,
)
from evaluation.metrics.spectrogram.short_time_objective_intelligibility import (
    extract_stoi,
)

METRIC_FUNC = {
    "energy_rmse": extract_energy_rmse,
    "energy_pc": extract_energy_pearson_coeffcients,
    "fpc": extract_fpc,
    "f0_periodicity_rmse": extract_f0_periodicity_rmse,
    "f0rmse": extract_f0rmse,
    "v_uv_f1": extract_f1_v_uv,
    "cer": extract_cer,
    "wer": extract_wer,
    "speaker_similarity": extract_speaker_similarity,
    "fad": extract_fad,
    "mcd": extract_mcd,
    "mstft": extract_mstft,
    "pesq": extract_pesq,
    "si_sdr": extract_si_sdr,
    "si_snr": extract_si_snr,
    "stoi": extract_stoi,
}


def calc_metric(ref_dir, deg_dir, dump_dir, metrics, fs=None):
    result = defaultdict()

    for metric in tqdm(metrics):
        if metric in ["fad", "speaker_similarity"]:
            result[metric] = str(METRIC_FUNC[metric](ref_dir, deg_dir))
            continue

        audios_ref = []
        audios_deg = []

        files = glob(ref_dir + "/*.wav")

        for file in files:
            audios_ref.append(file)
            uid = file.split("/")[-1].split(".wav")[0]
            file_gt = deg_dir + "/{}.wav".format(uid)
            audios_deg.append(file_gt)

        if metric in ["v_uv_f1"]:
            tp_total = 0
            fp_total = 0
            fn_total = 0

            for i in tqdm(range(len(audios_ref))):
                audio_ref = audios_ref[i]
                audio_deg = audios_deg[i]
                tp, fp, fn = METRIC_FUNC[metric](audio_ref, audio_deg, fs)
                tp_total += tp
                fp_total += fp
                fn_total += fn

            result[metric] = str(tp_total / (tp_total + (fp_total + fn_total) / 2))
        else:
            scores = []

            for i in tqdm(range(len(audios_ref))):
                audio_ref = audios_ref[i]
                audio_deg = audios_deg[i]

                score = METRIC_FUNC[metric](
                    audio_ref=audio_ref, audio_deg=audio_deg, fs=fs
                )
                if not np.isnan(score):
                    scores.append(score)

            scores = np.array(scores)
            result["{}_mean".format(metric)] = str(np.mean(scores))
            result["{}_std".format(metric)] = str(np.std(scores))

    data = json.dumps(result, indent=4)

    with open(os.path.join(dump_dir, "result.json"), "w", newline="\n") as f:
        f.write(data)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--ref_dir",
        type=str,
        help="Path to the target audio folder.",
    )
    parser.add_argument(
        "--deg_dir",
        type=str,
        help="Path to the reference audio folder.",
    )
    parser.add_argument(
        "--dump_dir",
        type=str,
        help="Path to dump the results.",
    )
    parser.add_argument(
        "--metrics",
        nargs="+",
        help="Metrics used to evaluate.",
    )
    args = parser.parse_args()

    calc_metric(args.ref_dir, args.deg_dir, args.dump_dir, args.metrics)

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API