Skip to content

Commit

Permalink
make test works
Browse files Browse the repository at this point in the history
  • Loading branch information
bagustris committed Apr 26, 2024
1 parent 2883a9a commit e9f3677
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 54 deletions.
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")
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
18 changes: 12 additions & 6 deletions nkululeko/feat_extract/feats_squim.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ class SquimSet(Featureset):
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"""
super().__init__(name, data_df, feats_type)
self.device = self.util.config_val("MODEL", "device", "cpu")
cuda = "cuda" if torch.cuda. is
self.device = self.util.config_val("MODEL", "device", cuda)
self.model_initialized = False

def init_model(self):
# load model
self.util.debug("loading model...")
self.objective_model = SQUIM_OBJECTIVE.get_model()
pytorch_total_params = sum(p.numel() for p in self.objective_model.parameters())
pytorch_total_params = sum(p.numel()
for p in self.objective_model.parameters())
self.util.debug(
f"initialized squim model with {pytorch_total_params} parameters in total"
)
Expand All @@ -51,7 +53,8 @@ def extract(self):
store = self.util.get_path("store")
store_format = self.util.config_val("FEATS", "store_format", "pkl")
storage = f"{store}{self.name}.{store_format}"
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 @@ -70,7 +73,8 @@ def extract(self):
)
emb = self.get_embeddings(signal, sampling_rate, file)
emb_series[idx] = emb
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)
self.df.columns = ["pesq", "sdr", "stoi"]
self.util.write_store(self.df, storage, store_format)
try:
Expand All @@ -91,9 +95,11 @@ def get_embeddings(self, signal, sampling_rate, file):
tmp_audio_name = "squim_audio_tmp.wav"
try:
audiofile.write(tmp_audio_name, signal, sampling_rate)
WAVEFORM_SPEECH, SAMPLE_RATE_SPEECH = torchaudio.load(tmp_audio_name)
WAVEFORM_SPEECH, SAMPLE_RATE_SPEECH = torchaudio.load(
tmp_audio_name)
with torch.no_grad():
stoi_hyp, pesq_hyp, si_sdr_hyp = self.objective_model(WAVEFORM_SPEECH)
stoi_hyp, pesq_hyp, si_sdr_hyp = self.objective_model(
WAVEFORM_SPEECH)
pesq = float(pesq_hyp[0].numpy())
stoi = float(stoi_hyp[0].numpy())
sdr = float(si_sdr_hyp[0].numpy())
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
22 changes: 15 additions & 7 deletions nkululeko/models/model_mlp_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from audmetric import concordance_cc
from audmetric import mean_absolute_error
from audmetric import mean_squared_error
from traitlets import default

import nkululeko.glob_conf as glob_conf
from nkululeko.losses.loss_ccc import ConcordanceCorCoeff
Expand Down Expand Up @@ -40,7 +41,8 @@ def __init__(self, df_train, df_test, feats_train, feats_test):
self.util.error(f"unknown loss function: {criterion}")
self.util.debug(f"training model with {criterion} loss function")
# set up the 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_string = glob_conf.config["MODEL"]["layers"]
self.util.debug(f"using layers {layers_string}")
try:
Expand All @@ -50,7 +52,8 @@ def __init__(self, df_train, df_test, feats_train, feats_test):
drop = self.util.config_val("MODEL", "drop", False)
if drop:
self.util.debug(f"training with dropout: {drop}")
self.model = self.MLP(feats_train.shape[1], layers, 1, drop).to(self.device)
self.model = self.MLP(
feats_train.shape[1], layers, 1, drop).to(self.device)
self.learning_rate = float(
self.util.config_val("MODEL", "learning_rate", 0.0001)
)
Expand Down Expand Up @@ -93,8 +96,10 @@ def predict(self):
_, truths, predictions = self.evaluate_model(
self.model, self.testloader, self.device
)
result, _, _ = self.evaluate_model(self.model, self.trainloader, self.device)
report = Reporter(truths.numpy(), predictions.numpy(), self.run, self.epoch)
result, _, _ = self.evaluate_model(
self.model, self.trainloader, self.device)
report = Reporter(truths.numpy(), predictions.numpy(),
self.run, self.epoch)
try:
report.result.loss = self.loss
except AttributeError: # if the model was loaded from disk the loss is unknown
Expand Down Expand Up @@ -128,9 +133,11 @@ def __len__(self):

def __getitem__(self, item):
index = self.df.index[item]
features = self.df_features.loc[index, :].values.astype("float32").squeeze()
features = self.df_features.loc[index, :].values.astype(
"float32").squeeze()
labels = (
np.array([self.df.loc[index, self.label]]).astype("float32").squeeze()
np.array([self.df.loc[index, self.label]]
).astype("float32").squeeze()
)
return features, labels

Expand Down Expand Up @@ -187,7 +194,8 @@ def evaluate_model(self, model, loader, device):
end_index = (index + 1) * loader.batch_size
if end_index > len(loader.dataset):
end_index = len(loader.dataset)
logits[start_index:end_index] = model(features.to(device)).reshape(-1)
logits[start_index:end_index] = model(
features.to(device)).reshape(-1)
targets[start_index:end_index] = labels
loss = self.criterion(
logits[start_index:end_index].to(
Expand Down
Loading

0 comments on commit e9f3677

Please sign in to comment.