Skip to content

Commit

Permalink
Merge pull request felixbur#118 from felixbur/make_run_test_works
Browse files Browse the repository at this point in the history
Make run test works
  • Loading branch information
felixbur authored Apr 29, 2024
2 parents 95a48de + e2697b8 commit 108ace2
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 68 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/basic_tests_for_aud_csv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ jobs:
- name: Run csv-ravdess-praat-xgb
run: |
cd data/ravdess
wget https://zenodo.org/record/1188976/files/Audio_Speech_Actors_01-24.zip
unzip Audio_Speech_Actors_01-24.zip
# wget https://zenodo.org/record/1188976/files/Audio_Speech_Actors_01-24.zip
wget https://zenodo.org/records/11063852/files/Audio_Speech_Actors_01-24_16k.zip
unzip Audio_Speech_Actors_01-24_16k.zip
cd ../..
python3 -m nkululeko.nkululeko --config data/ravdess/exp_praat_xgb.ini > output1.txt
if grep -q "DONE" output1.txt; then
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

Version 0.83.2
--------------
* added default cuda if present and not stated

Version 0.83.1
--------------
* add test module to nkuluflag
Expand Down
72 changes: 72 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Code of Conduct

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of
experience, education, socioeconomic status, nationality, personal appearance,
race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* using welcoming and inclusive language,
* being respectful of differing viewpoints and experiences,
* gracefully accepting constructive criticism,
* focusing on what is best for the community, and
* showing empathy towards other community members.

Examples of unacceptable behavior by participants include:

* the use of sexualized language or imagery and unwelcome sexual
attention or advances,
* trolling, insulting/derogatory comments, and personal or political
attacks,
* public or private harassment,
* publishing others' private information, such as a physical or
electronic address, without explicit permission, and
* other conduct which could reasonably be considered inappropriate in
a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, or to ban temporarily or permanently any
contributor for other behaviors that they deem inappropriate, threatening,
offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project email
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by emailing the project team. All complaints will be reviewed
and investigated and will result in a response that is deemed necessary and
appropriate to the circumstances. The project team is obligated to maintain
confidentiality with regard to the reporter of an incident. Further details of
specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][contributor_covenant]
version 1.4.

[contributor_covenant]: https://www.contributor-covenant.org/
2 changes: 1 addition & 1 deletion data/crema-d/load_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
audb.config.CACHE_ROOT = cwd

# load the latest version of the data
db = audb.load("crema-d", format="wav", sampling_rate=16000, mixdown=True)
db = audb.load("crema-d", version="1.3.0", verbose=True)
4 changes: 4 additions & 0 deletions data/ravdess/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ I used the version downloadable from [Zenodo](https://zenodo.org/record/1188976)

Download and unzip the file Audio_Speech_Actors_01-24.zip
```bash
# download original dataset in 48k
$ wget https://zenodo.org/record/1188976/files/Audio_Speech_Actors_01-24.zip
$ unzip Audio_Speech_Actors_01-24.zip
```

Or, if you prefer the dataset in 16k, you can download from this link:
https://zenodo.org/records/11063852/files/Audio_Speech_Actors_01-24_16k.zip

run the file
```bash
python3 process_database.py
Expand Down
2 changes: 2 additions & 0 deletions data/ravdess/exp_praat_xgb.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ dev = ./data/ravdess/ravdess_dev.csv
dev.type = csv
dev.absolute_path = False
dev.split_strategy = train
dev.audio_path = Audio_Speech_Actors_01-24_16k/
test = ./data/ravdess/ravdess_test.csv
test.type = csv
test.absolute_path = False
test.split_strategy = test
test.audio_path = Audio_Speech_Actors_01-24_16k/
target = emotion
labels = ['angry', 'happy', 'neutral', 'sad']
[FEATS]
Expand Down
2 changes: 1 addition & 1 deletion nkululeko/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION="0.83.1"
VERSION="0.83.2"
SAMPLING_RATE = 16000
9 changes: 6 additions & 3 deletions nkululeko/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,12 @@ def predict_test_and_save(self, result_name):
return result

def load(self, filename):
f = open(filename, "rb")
tmp_dict = pickle.load(f)
f.close()
try:
f = open(filename, "rb")
tmp_dict = pickle.load(f)
f.close()
except EOFError as eof:
self.util.error(f"can't open file {filename}: {eof}")
self.__dict__.update(tmp_dict)
glob_conf.set_labels(self.labels)

Expand Down
6 changes: 4 additions & 2 deletions nkululeko/feat_extract/feats_agender_agender.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ def _load_model(self):
if not os.path.isdir(model_root):
cache_root = audeer.mkdir("cache")
model_root = audeer.mkdir(model_root)
archive_path = audeer.download_url(model_url, cache_root, verbose=True)
archive_path = audeer.download_url(
model_url, cache_root, verbose=True)
audeer.extract_archive(archive_path, model_root)
device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
device = self.util.config_val("MODEL", "device", cuda)
self.model = audonnx.load(model_root, device=device)
# pytorch_total_params = sum(p.numel() for p in self.model.parameters())
# self.util.debug(
Expand Down
11 changes: 8 additions & 3 deletions nkululeko/feat_extract/feats_squim.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@


class SquimSet(Featureset):
"""Class to predict SQUIM features"""
"""Class to predict SQUIM features."""

def __init__(self, name, data_df, feats_type):
"""Constructor. is_train is needed to distinguish from test/dev sets, because they use the codebook from the training"""
"""Constructor.
Is_train is needed to distinguish from test/dev sets,
because they use the codebook from the training.
"""
super().__init__(name, data_df, feats_type)
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
self.model_initialized = False

def init_model(self):
Expand Down
15 changes: 8 additions & 7 deletions nkululeko/feat_extract/feats_wav2vec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class Wav2vec2(Featureset):
"""Class to extract wav2vec2 embeddings"""

def __init__(self, name, data_df, feat_type):
"""Constructor. is_train is needed to distinguish from test/dev sets, because they use the codebook from the training"""
"""Constructor.
If_train is needed to distinguish from test/dev sets,
because they use the codebook from the training
"""
super().__init__(name, data_df, feat_type)
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
Expand All @@ -39,8 +43,7 @@ def init_model(self):
)
config = transformers.AutoConfig.from_pretrained(model_path)
layer_num = config.num_hidden_layers
hidden_layer = int(self.util.config_val(
"FEATS", "wav2vec2.layer", "0"))
hidden_layer = int(self.util.config_val("FEATS", "wav2vec2.layer", "0"))
config.num_hidden_layers = layer_num - hidden_layer
self.util.debug(f"using hidden layer #{config.num_hidden_layers}")
self.processor = Wav2Vec2FeatureExtractor.from_pretrained(model_path)
Expand All @@ -55,8 +58,7 @@ def extract(self):
"""Extract the features or load them from disk if present."""
store = self.util.get_path("store")
storage = f"{store}{self.name}.pkl"
extract = self.util.config_val(
"FEATS", "needs_feature_extraction", False)
extract = self.util.config_val("FEATS", "needs_feature_extraction", False)
no_reuse = eval(self.util.config_val("FEATS", "no_reuse", "False"))
if extract or no_reuse or not os.path.isfile(storage):
if not self.model_initialized:
Expand All @@ -77,8 +79,7 @@ def extract(self):
emb = self.get_embeddings(signal, sampling_rate, file)
emb_series[idx] = emb
# print(f"emb_series shape: {emb_series.shape}")
self.df = pd.DataFrame(
emb_series.values.tolist(), index=self.data_df.index)
self.df = pd.DataFrame(emb_series.values.tolist(), index=self.data_df.index)
# print(f"df shape: {self.df.shape}")
self.df.to_pickle(storage)
try:
Expand Down
9 changes: 6 additions & 3 deletions nkululeko/feat_extract/feats_whisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ def init_model(self):
model_name = f"openai/{self.feat_type}"
self.model = WhisperModel.from_pretrained(model_name).to(self.device)
print(f"intialized Whisper model on {self.device}")
self.feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
self.feature_extractor = AutoFeatureExtractor.from_pretrained(
model_name)
self.model_initialized = True

def extract(self):
"""Extract the features or load them from disk if present."""
store = self.util.get_path("store")
storage = f"{store}{self.name}.pkl"
extract = self.util.config_val("FEATS", "needs_feature_extraction", False)
extract = self.util.config_val(
"FEATS", "needs_feature_extraction", False)
no_reuse = eval(self.util.config_val("FEATS", "no_reuse", "False"))
if extract or no_reuse or not os.path.isfile(storage):
if not self.model_initialized:
self.init_model()
self.util.debug("extracting whisper embeddings, this might take a while...")
self.util.debug(
"extracting whisper embeddings, this might take a while...")
emb_series = []
for (file, start, end), _ in audeer.progress_bar(
self.data_df.iterrows(),
Expand Down
20 changes: 14 additions & 6 deletions nkululeko/models/model_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sklearn.metrics import recall_score
from collections import OrderedDict
from PIL import Image
from traitlets import default

from nkululeko.utils.util import Util
import nkululeko.glob_conf as glob_conf
Expand Down Expand Up @@ -48,6 +49,7 @@ def __init__(self, df_train, df_test, feats_train, feats_test):
self.util.error(f"unknown loss function: {criterion}")
self.util.debug(f"using model with cross entropy loss function")
# set up the model
# cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", "cpu")
try:
layers_string = glob_conf.config["MODEL"]["layers"]
Expand Down Expand Up @@ -84,7 +86,8 @@ def __init__(self, df_train, df_test, feats_train, feats_test):
train_set = self.Dataset_image(
feats_train, df_train, self.target, transformations
)
test_set = self.Dataset_image(feats_test, df_test, self.target, transformations)
test_set = self.Dataset_image(
feats_test, df_test, self.target, transformations)
# Define data loaders
self.trainloader = torch.utils.data.DataLoader(
train_set,
Expand Down Expand Up @@ -137,7 +140,8 @@ def train(self):
losses = []
for images, labels in self.trainloader:
logits = self.model(images.to(self.device))
loss = self.criterion(logits, labels.to(self.device, dtype=torch.int64))
loss = self.criterion(logits, labels.to(
self.device, dtype=torch.int64))
losses.append(loss.item())
self.optimizer.zero_grad()
loss.backward()
Expand Down Expand Up @@ -165,14 +169,16 @@ def evaluate_model(self, model, loader, device):

self.loss_eval = (np.asarray(losses)).mean()
predictions = logits.argmax(dim=1)
uar = recall_score(targets.numpy(), predictions.numpy(), average="macro")
uar = recall_score(
targets.numpy(), predictions.numpy(), average="macro")
return uar, targets, predictions

def predict(self):
_, truths, predictions = self.evaluate_model(
self.model, self.testloader, self.device
)
uar, _, _ = self.evaluate_model(self.model, self.trainloader, self.device)
uar, _, _ = self.evaluate_model(
self.model, self.trainloader, self.device)
report = Reporter(truths, predictions, self.run, self.epoch)
try:
report.result.loss = self.loss
Expand Down Expand Up @@ -209,7 +215,8 @@ def load(self, run, epoch):
dir = self.util.get_path("model_dir")
# name = f'{self.util.get_exp_name()}_{run}_{epoch:03d}.model'
name = f"{self.util.get_exp_name(only_train=True)}_{self.run}_{self.epoch:03d}.model"
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
layers = ast.literal_eval(glob_conf.config["MODEL"]["layers"])
self.store_path = dir + name
drop = self.util.config_val("MODEL", "drop", False)
Expand All @@ -222,7 +229,8 @@ def load(self, run, epoch):
def load_path(self, path, run, epoch):
self.set_id(run, epoch)
with open(path, "rb") as handle:
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
layers = ast.literal_eval(glob_conf.config["MODEL"]["layers"])
self.store_path = path
drop = self.util.config_val("MODEL", "drop", False)
Expand Down
23 changes: 16 additions & 7 deletions nkululeko/models/model_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def __init__(self, df_train, df_test, feats_train, feats_test):
else:
self.util.error(f"unknown loss function: {criterion}")
self.util.debug(f"using model with cross entropy loss function")
# set up the model
self.device = self.util.config_val("MODEL", "device", "cpu")
# set up the model, use GPU if availabe
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
try:
layers_string = glob_conf.config["MODEL"]["layers"]
except KeyError as ke:
Expand Down Expand Up @@ -86,7 +87,8 @@ def train(self):
losses = []
for features, labels in self.trainloader:
logits = self.model(features.to(self.device))
loss = self.criterion(logits, labels.to(self.device, dtype=torch.int64))
loss = self.criterion(logits, labels.to(
self.device, dtype=torch.int64))
losses.append(loss.item())
self.optimizer.zero_grad()
loss.backward()
Expand Down Expand Up @@ -114,14 +116,16 @@ def evaluate_model(self, model, loader, device):

self.loss_eval = (np.asarray(losses)).mean()
predictions = logits.argmax(dim=1)
uar = recall_score(targets.numpy(), predictions.numpy(), average="macro")
uar = recall_score(
targets.numpy(), predictions.numpy(), average="macro")
return uar, targets, predictions

def predict(self):
_, truths, predictions = self.evaluate_model(
self.model, self.testloader, self.device
)
uar, _, _ = self.evaluate_model(self.model, self.trainloader, self.device)
uar, _, _ = self.evaluate_model(
self.model, self.trainloader, self.device)
report = Reporter(truths, predictions, self.run, self.epoch)
try:
report.result.loss = self.loss
Expand Down Expand Up @@ -179,6 +183,9 @@ def predict_sample(self, features):
features = np.reshape(features, (-1, 1)).T
logits = self.model(features.to(self.device))
# logits = self.model(features)
# if tensor conver to cpu
if isinstance(logits, torch.Tensor):
logits = logits.cpu()
a = logits.numpy()
res = {}
for i in range(len(a[0])):
Expand All @@ -196,7 +203,8 @@ def load(self, run, epoch):
dir = self.util.get_path("model_dir")
# name = f'{self.util.get_exp_name()}_{run}_{epoch:03d}.model'
name = f"{self.util.get_exp_name(only_train=True)}_{self.run}_{self.epoch:03d}.model"
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
layers = ast.literal_eval(glob_conf.config["MODEL"]["layers"])
self.store_path = dir + name
drop = self.util.config_val("MODEL", "drop", False)
Expand All @@ -211,7 +219,8 @@ def load(self, run, epoch):
def load_path(self, path, run, epoch):
self.set_id(run, epoch)
with open(path, "rb") as handle:
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda.is_available() else "cpu"
self.device = self.util.config_val("MODEL", "device", cuda)
layers = ast.literal_eval(glob_conf.config["MODEL"]["layers"])
self.store_path = path
drop = self.util.config_val("MODEL", "drop", False)
Expand Down
Loading

0 comments on commit 108ace2

Please sign in to comment.