-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into speechbrain_asr_eval
# Conflicts: # README.md # anonymization/modules/speaker_embeddings/anonymization/gan_anon.py # anonymization/modules/speaker_embeddings/anonymization/pool_anon.py # anonymization/modules/speaker_embeddings/speaker_extraction.py # anonymization/pipelines/sttts_pipeline.py # configs/anon_ims_sttts_pc.yaml # evaluation/privacy/asv/asv.py # evaluation/utility/voice_distinctiveness/deid_gvd.py # run_evaluation.py
- Loading branch information
Showing
54 changed files
with
1,092 additions
and
626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
############################### | ||
## CONFIGURATION | ||
############################### | ||
PHONY: install uninstall pretrained_models | ||
.ONESHELL: | ||
|
||
PROJECT_NAME = voicepat | ||
ENV_NAME = $(PROJECT_NAME)_env | ||
|
||
ifeq (, $(shell mamba --version)) | ||
CONDA = conda | ||
else | ||
CONDA = mamba | ||
endif | ||
|
||
############################### | ||
##@ INSTALLATION | ||
############################### | ||
|
||
install: $(ENV_NAME) ## performs the installation. Currently the only step is to install the conda environment | ||
|
||
uninstall: | ||
@rm -rf $(ENV_NAME) | ||
@rm -rf models/ | ||
|
||
pretrained_models: ## downloads the pretrained models from IMS repositories | ||
@echo Downloading models from IMS repositories | ||
@rm -rf models | ||
@mkdir -p models | ||
@wget -q -O models/anonymization.zip https://github.com/DigitalPhonetics/speaker-anonymization/releases/download/v2.0/anonymization.zip | ||
@wget -q -O models/asr.zip https://github.com/DigitalPhonetics/speaker-anonymization/releases/download/v2.0/asr.zip | ||
@wget -q -O models/tts.zip https://github.com/DigitalPhonetics/speaker-anonymization/releases/download/v2.0/tts.zip | ||
@wget -q -O models/pre_eval_models.zip https://github.com/DigitalPhonetics/VoicePAT/releases/download/v1/pre_eval_models.zip | ||
@unzip -oq models/asr.zip -d models | ||
@unzip -oq models/tts.zip -d models | ||
@unzip -oq models/anonymization.zip -d models | ||
@mkdir evaluation/utility/asr/exp | ||
@unzip -oq models/pre_eval_models.zip -d evaluation/utility/asr/exp | ||
@ln -srf evaluation/utility/asr/exp exp | ||
@cp evaluation/privacy/asv/ | ||
@rm models/*.zip | ||
|
||
|
||
$(ENV_NAME): environment.yaml | ||
@($(CONDA) env create -f $< -p ./$@ && echo Installation complete, please run `conda develop .` once.) || $(CONDA) env update -f $< -p ./$@ | ||
@conda config --set env_prompt '($$(basename {default_env})) ' | ||
@(cat .gitignore | grep -q $(ENV_NAME)) || echo $(ENV_NAME) >> .gitignore | ||
|
||
############################### | ||
##@ SELF-DOCUMENTING COMMAND | ||
############################### | ||
|
||
help: ## Display this help | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 53 additions & 5 deletions
58
anonymization/modules/speaker_embeddings/anonymization/base_anon.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,70 @@ | ||
from pathlib import Path | ||
import torch | ||
import ruamel.yaml as yaml | ||
from ruamel.yaml.representer import RoundTripRepresenter, SafeRepresenter | ||
from typing import Union | ||
|
||
|
||
class BaseAnonymizer: | ||
""" | ||
Base class for speaker embedding anonymizers, defining the API, | ||
that consists of the following methods: | ||
- anonymize_embeddings | ||
- to | ||
""" | ||
def __init__( | ||
self, | ||
vec_type: str, | ||
device: Union[str, torch.device, int, None], | ||
suffix: str, | ||
**kwargs, | ||
): | ||
assert suffix[0] == "_", "Suffix must be a string and start with an underscore." | ||
|
||
def __init__(self, vec_type='xvector', device=None, **kwargs): | ||
# Base class for speaker embedding anonymization. | ||
self.vec_type = vec_type | ||
self.suffix = suffix | ||
|
||
if isinstance(device, torch.device): | ||
self.device = device | ||
elif isinstance(device, str): | ||
self.device = torch.device(device) | ||
elif isinstance(device, int): | ||
self.device = torch.device(f'cuda:{device}') | ||
self.device = torch.device(f"cuda:{device}") | ||
else: | ||
self.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') | ||
self.device = ( | ||
torch.device("cuda") | ||
if torch.cuda.is_available() | ||
else torch.device("cpu") | ||
) | ||
|
||
def anonymize_embeddings(self, speaker_embeddings, emb_level='spk'): | ||
# ensure dumpability | ||
self.kwargs = kwargs | ||
self.kwargs["vec_type"] = self.vec_type | ||
self.kwargs["device"] = str(self.device) | ||
self.kwargs["suffix"] = self.suffix | ||
|
||
def __repr__(self): | ||
if hasattr(self, "kwargs"): | ||
return f"{self.__class__.__name__}({self.kwargs})" | ||
else: | ||
return f"{self.__class__.__name__}()" | ||
|
||
def to_yaml(self, representer: yaml.Representer): | ||
# first get data into dict format | ||
data = {f"!new:{type(self).__qualname__}": self.kwargs} | ||
return_str = representer.represent_dict(data) | ||
return return_str | ||
|
||
def anonymize_embeddings(self, speaker_embeddings: torch.Tensor, emb_level: str = "spk") -> torch.Tensor: | ||
# Template method for anonymizing a dataset. Not implemented. | ||
raise NotImplementedError('anonymize_data') | ||
raise NotImplementedError("anonymize_data") | ||
|
||
def to(self, device): | ||
self.device = device | ||
|
||
|
||
# necessary to make BaseAnonymizer and subclasses dumpable | ||
RoundTripRepresenter.add_multi_representer( | ||
BaseAnonymizer, lambda representer, data: data.to_yaml(representer) | ||
) |
Oops, something went wrong.