Skip to content

Commit

Permalink
[SPLIT] evaluateDCASE into simpler modules
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCretois committed Feb 20, 2024
1 parent 70cb5da commit e5a8517
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 287 deletions.
154 changes: 0 additions & 154 deletions evaluate/_utils_compute.py

This file was deleted.

41 changes: 18 additions & 23 deletions evaluate/_utils_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,25 @@ def write_wav(
),
)

arr = np.pad(
arr, (0, len(gt_labels) - len(arr)), "constant", constant_values=(0,)
)


# pad with zeros
#gt_labels = np.pad(
# gt_labels, (0, len(gt_labels) - len(arr)), "constant", constant_values=(0,)
#)
#pred_labels = np.pad(
# pred_labels, (0, len(pred_labels) - len(arr) ), "constant", constant_values=(0,)
#)
#distances_to_pos = np.pad(
# distances_to_pos,
# (0, len(distances_to_pos) - len(arr)),
# "constant",
# constant_values=(0,),
#)
#z_scores_pos = np.pad(
# z_scores_pos,
# (0, len(z_scores_pos) - len(arr)),
# "constant",
# constant_values=(0,),
#)
gt_labels = np.pad(
gt_labels, (0, len(gt_labels) - len(arr)), "constant", constant_values=(0,)
)
pred_labels = np.pad(
pred_labels, (0, len(pred_labels) - len(arr) ), "constant", constant_values=(0,)
)
distances_to_pos = np.pad(
distances_to_pos,
(0, len(distances_to_pos) - len(arr)),
"constant",
constant_values=(0,),
)
z_scores_pos = np.pad(
z_scores_pos,
(0, len(z_scores_pos) - len(arr)),
"constant",
constant_values=(0,),
)

# Write the results
result_wav = np.vstack(
Expand Down
114 changes: 6 additions & 108 deletions evaluate/evaluateDCASE.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

from callbacks.callbacks import MilestonesFinetuning

from evaluate._utils_writing import write_wav, write_results


def to_dataframe(features, labels):
# Load the saved array and map the features and labels into a single dataframe
Expand All @@ -39,7 +41,7 @@ def to_dataframe(features, labels):


def train_model(
model_type="pann",
model_type=None,
datamodule_class=DCASEDataModule,
max_epochs=1,
enable_model_summary=False,
Expand Down Expand Up @@ -68,17 +70,9 @@ def train_model(
)

# create the model object
model = ProtoBEATsModel(model_type=model_type)

if pretrained_model:
# Load the pretrained model
try:
pretrained_model = ProtoBEATsModel.load_from_checkpoint(pretrained_model)
except KeyError:
print(
"Failed to load the pretrained model. Please check the checkpoint file."
)
return None
model = ProtoBEATsModel(model_type=model_type,
state=state,
model_path=pretrained_model)

# train the model
trainer.fit(model, datamodule=datamodule_class)
Expand Down Expand Up @@ -478,103 +472,7 @@ def compute(
)


def write_wav(
files,
cfg,
gt_labels,
pred_labels,
distances_to_pos,
z_scores_pos,
target_fs=16000,
target_path=None,
frame_shift=1,
support_spectrograms=None
):
from scipy.io import wavfile

# Some path management

filename = (
os.path.basename(support_spectrograms).split("data_")[1].split(".")[0] + ".wav"
)
# Return the final product
output = os.path.join(target_path, filename)

# Find the filepath for the file being analysed
for f in files:
if os.path.basename(f) == filename:
print(os.path.basename(f))
print(filename)
arr, _ = librosa.load(f, sr=target_fs, mono=True)
break

# Expand the dimensions
gt_labels = np.repeat(
np.squeeze(gt_labels, axis=1).T,
int(
cfg["data"]["tensor_length"]
* cfg["data"]["overlap"]
* target_fs
* frame_shift
/ 1000
),
)
pred_labels = np.repeat(
pred_labels.T,
int(
cfg["data"]["tensor_length"]
* cfg["data"]["overlap"]
* target_fs
* frame_shift
/ 1000
),
)
distances_to_pos = np.repeat(
distances_to_pos.T,
int(
cfg["data"]["tensor_length"]
* cfg["data"]["overlap"]
* target_fs
* frame_shift
/ 1000
),
)
z_scores_pos = np.repeat(
z_scores_pos.T,
int(
cfg["data"]["tensor_length"]
* cfg["data"]["overlap"]
* target_fs
* frame_shift
/ 1000
),
)

# pad with zeros
gt_labels = np.pad(
gt_labels, (0, len(arr) - len(gt_labels)), "constant", constant_values=(0,)
)
pred_labels = np.pad(
pred_labels, (0, len(arr) - len(pred_labels)), "constant", constant_values=(0,)
)
distances_to_pos = np.pad(
distances_to_pos,
(0, len(arr) - len(distances_to_pos)),
"constant",
constant_values=(0,),
)
z_scores_pos = np.pad(
z_scores_pos,
(0, len(arr) - len(z_scores_pos)),
"constant",
constant_values=(0,),
)

# Write the results
result_wav = np.vstack(
(arr, gt_labels, pred_labels, distances_to_pos / 10, z_scores_pos)
)
wavfile.write(output, target_fs, result_wav.T)

import hydra
from omegaconf import DictConfig, OmegaConf
Expand Down
3 changes: 1 addition & 2 deletions evaluate/evaluation_metrics/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ def evaluate(pred_file_path, ref_file_path, team_name, dataset, savepath, metada
parser.add_argument('-dataset', type=str, help="which set to evaluate: EVAL or VAL")
parser.add_argument('-savepath', type=str, help="path where to save the report to")
args = parser.parse_args()
# print(args)

evaluate( args.pred_file, args.ref_files_path, args.team_name, args.dataset, args.savepath)

# docker run -v $PWD:/app -v /data/Prosjekter3/823001_19_metodesats_analyse_23_36_cretois/:/data --gpus all dcase poetry run python evaluate/evaluation_metrics/evaluation.py -pred_file /data/eval_out.csv -ref_files_path /data/DCASE/Development_Set_annotations/Validation_Set -team_name BEATs -dataset VAL -savepath /data/.
# docker run -v $PWD:/app -v /data/Prosjekter3/823001_19_metodesats_analyse_23_36_cretois/:/data --gpus all beats poetry run python evaluate/evaluation_metrics/evaluation.py -pred_file /data/eval_out.csv -ref_files_path /data/DCASE/Development_Set_annotations/Validation_Set -team_name BEATs -dataset VAL -savepath /data/.

0 comments on commit e5a8517

Please sign in to comment.