Commit 2e0aa0ea authored by Colleen Beaumard's avatar Colleen Beaumard
Browse files

Modification of scorings scripts

parent 3cae3fd6
import torch
from tqdm import tqdm
import os
import re
import seaborn as sns
import pandas as pd
import numpy as np
......@@ -163,12 +164,15 @@ if model_type == "custom":
loss_epoch = [line for line in file if "Epoch" in line]
#loss_epoch = loss_epoch[::2] # 2 because only 1 gpu used: if more gpus are used, you have to do 2*nb_gpus
loss_epoch = [line for line in loss_epoch if "[1/" in line]
elif model_type == "half_resnet34":
loss_epoch = [line for line in file if "Epoch" in line][1:]
else:
loss_epoch = [line for line in file if "Epoch" in line]
reduce_lr = [line for line in file if "reducing" in line] # Can be empty
reduce_lr_list = [line for line in file if "reducing" in line] # Can be empty
vloss = [] # Validation losses
vacc = [] # Validation accuracy
tloss = [] # Training losses
aepoch = [] # Number of epochs
......@@ -176,13 +180,14 @@ for linev, linel in zip(valid_loss, loss_epoch):
linel = linel.split(":")
linev = linev.split("=")
vloss.append(round(float(linev[3].replace("\n", "").replace(" ", "")), 2))
vacc.append(round(float(linev[1].split("%")[0].replace(" ","")),2))
aepoch.append(linel[3].split(" ")[1])
tloss.append(round(float(linel[4].split("\t")[0].replace(" ", "")), 2))
assert len(aepoch) == len(tloss) == len(vloss)
assert len(aepoch) == len(tloss) == len(vloss) == len(vacc)
print("--------------")
for e, t, v in zip(aepoch, tloss, vloss):
print("Epoch:", e, "\ttrain loss:", t, "\tvalid loss:", v)
print("--------------\n")
for e, t, v ,a in zip(aepoch, tloss, vloss, vacc):
print("Epoch:", e, "\ttrain loss:", t, "\tvalid loss:", v, "\tvalid acc.:", a)
### Plotting of confusion matrix and losses ###
if model_type == "custom":
......@@ -209,21 +214,37 @@ plt.clf()
print("\nConfusion matrix done!")
# Plot losses
ticks = [nb for nb in range(0, 9)]
eticks = [nb for nb in range(0, len(aepoch), 5)]
plt.plot(aepoch, tloss, label="Training loss")
plt.plot(aepoch, vloss, label="Validation loss")
plt.yticks(ticks)
plt.xticks(eticks)
if len(reduce_lr) != 0:
colors = ["b", "g", "y", "c", "m", "r"]
fig, ax = plt.subplots()
twin = ax.twinx() # Add a 3rd axis on the right
plot_tl, = ax.plot(tloss, label="Training loss")
plot_vl, = ax.plot(vloss, label="Validation loss")
plot_vac, = twin.plot(vacc, "g-.", label="Validation accuracy")
handles = [plot_tl, plot_vl, plot_vac]
ax.set_xlabel("Epochs")
ax.set_ylabel("CrossEntropyLoss()")
twin.set_ylabel("Validation accuracy (%)")
ax.tick_params(axis='x', length=len(aepoch), width=5)
ax.tick_params(axis='y', length=8)
twin.set_ylim(0, 100)
if len(reduce_lr_list) != 0:
reduce_lr_list = [re.sub(r"\s+", " ", elmt) for elmt in reduce_lr_list]
reduce_lr_list = [elmt.split(" ") for elmt in reduce_lr_list]
reduce_lr_list = {elmt[1][:-1] : elmt[-1][:-1]for elmt in reduce_lr_list}
colors = ["b", "m", "y", "c", "g", "r"]
i = 0
for key, value in reduce_lr.items():
label = "lr: " + str(value)
plt.axvline(x=key, color=colors[i], linestyle='--', label=label)
handles.append(plt.axvline(x=key, color=colors[i], linestyle='--', label=label))
i += 1
plt.legend()
ax.legend(handles=handles)
plt.title("Model: {}{}{}_{}emo_{}batch\nEpoch: {} lr: {} Data: Test-IEMOCAP {}".format(model_type, model_2nd, freeze, cates, nb_batch, aepoch[-1], lr, ses_nb))
plt.savefig(os.path.join(path, "losses_{}{}{}_{}emo_{}batch_epoch-{}_lr-{}_Test-IEMOCAP{}.png".format(model_type, model_2nd, freeze, cates, nb_batch, aepoch[-1], lr, ses_nb)))
plt.show()
......
......@@ -39,7 +39,7 @@ else:
# Path to save the confusion matrix for cross-validation
if model_type == "custom":
path = "model_{}/Sess_all_cross-valid/{}/{}emo_{}batch_lr-{}{}".format(model_type, ses_nb, model_2nd[1:] ,cates, nb_batch, lr, freeze)
path = "model_{}/Sess_all_cross-valid/{}/{}emo_{}batch_lr-{}{}".format(model_type, model_2nd[1:] ,cates, nb_batch, lr, freeze)
if not os.path.isdir(path.rsplit("/", 2)[0]):
os.mkdir(path.rsplit("/", 2)[0])
......@@ -97,7 +97,10 @@ for i in range(1, 6):
print("Session {} is processing...".format(i))
# 1st is the model, 2nd is the weights and all
xtract, config = load_model("model_{}/Sess{}_test/{}emo_{}batch_lr-{}{}/best_{}{}_{}emo_{}batch_lr-{}_Test-IEMOCAP{}.pt".format(model_type, i, cates, nb_batch, lr, freeze, model_type, freeze, cates, nb_batch, lr, i), "cuda")
if model_type == "custom":
xtract, config = load_model("model_{}/Sess{}_test/{}/{}emo_{}batch_lr-{}{}/best_{}{}{}_{}emo_{}batch_lr-{}_Test-IEMOCAP{}.pt".format(model_type, i, model_2nd[1:], cates, nb_batch, lr, freeze, model_type, model_2nd, freeze, cates, nb_batch, lr, i), "cuda")
else:
xtract, config = load_model("model_{}/Sess{}_test/{}emo_{}batch_lr-{}{}/best_{}{}_{}emo_{}batch_lr-{}_Test-IEMOCAP{}.pt".format(model_type, i, cates, nb_batch, lr, freeze, model_type, freeze, cates, nb_batch, lr, i), "cuda")
path_wav = "data/IEMOCAP/Session{}/sentences/wav".format(i)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment