-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
73 lines (51 loc) · 2.34 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import torch
import pickle
import pandas as pd
from tqdm import tqdm
from tabulate import tabulate
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix, accuracy_score
from utils.config import Config
from models.model import BERTClassModel
def evaluate(model, dataloader):
labels = []
predicted = []
print("Evaluating on Test set....")
with torch.no_grad():
for _, data in tqdm(enumerate(dataloader), total=len(dataloader), colour=Config.color):
ids = data['input_ids'].to(Config.device, dtype = torch.long)
mask = data['attention_mask'].to(Config.device, dtype = torch.long)
token_type_ids = data['token_type_ids'].to(Config.device, dtype = torch.long)
targets = data['targets'].to(Config.device)
outputs = model(ids, mask, token_type_ids)
outputs = torch.softmax(outputs, dim=1)
#Accuracy
_, max_idx = torch.max(outputs, 1)
predicted.extend(max_idx.tolist())
labels.extend(targets.tolist())
return \
precision_score(targets, predicted, average="macro"), \
recall_score(targets, predicted, average="macro"), \
accuracy_score(targets, predicted), \
f1_score(targets, predicted, average="macro"), \
confusion_matrix(targets, predicted)
if __name__ == "__main__":
#load trained model
model = BERTClassModel(5)
checkpoint = torch.load(Config.checkpoint_path)
model.load_state_dict(checkpoint['model_state_dict'])
#load test dataloader
with open(Config.dataloader_path, "rb") as f:
dataloader = pickle.load(f)
ps, rs, acc, f1, conf_matrix = evaluate(model.to(Config.device), dataloader["test"])
# Print the metrics in a fancy format
print(f"Precision: {ps:.4f}")
print(f"Recall: {rs:.4f}")
print(f"Accuracy: {acc:.4f}")
print(f"F1 Score: {f1:.4f}")
# Print the confusion matrix
print("Confusion Matrix:")
print(tabulate(conf_matrix, tablefmt="fancy_grid"))
# Save the DataFrame to an Excel file
data = {'Precision': [ps], 'Recall': [rs], 'Accuracy': [acc], 'F1 Score': [f1], "Confusion Matrix": [conf_matrix]}
df = pd.DataFrame(data)
df.to_excel(Config.metric_path, index=False)