-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_utils.py
168 lines (141 loc) · 5.54 KB
/
train_utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import os
from pathlib import Path
import torch
from tqdm import tqdm
from sklearn.metrics import (
accuracy_score,
classification_report
)
def get_device(status=True):
if torch.cuda.is_available():
device = torch.device("cuda")
if status:
print("Using CUDA")
else:
device = torch.device("cpu")
if status:
print("Using CPU")
return device
def train_epoch(
model,
loader,
optimizer,
device,
index_2_emotion_class,
training=True,
):
if training:
model.train()
else:
model.eval()
epoch_loss = 0.0
epoch_contrastive_loss = 0.0
# keep track of the model predictions for computing accuracy
pred_labels = []
target_labels = []
# put model inputs to device
model = model.to(device)
# iterate over each batch in the dataloader
# NOTE: you may have additional outputs from the loader __getitem__, you can modify this
for loaded_inputs in tqdm(loader):
emoroberta_inputs = loaded_inputs["emoroberta_input"]
vilt_inputs = loaded_inputs["vilt_input"]
labels = loaded_inputs["labels"].to(device)
# emoroberta_inputs, vilt_inputs, labels = emoroberta_inputs.to(device), vilt_inputs.to(device), labels.to(
# device).long()
# calculate the loss and train accuracy and perform backprop
outputs = model(emoroberta_inputs, vilt_inputs, labels, device)
loss = outputs.loss
pred_logits = outputs.logits
contrastive_loss = outputs.contrastive_loss
# logging
epoch_loss += loss.item()
epoch_contrastive_loss += contrastive_loss.item()
# step optimizer and compute gradients during training
if training:
optimizer.zero_grad()
loss.backward()
optimizer.step()
# compute metrics
preds = pred_logits.detach().argmax(-1)
pred_labels.extend(preds.cpu().numpy())
target_labels.extend(labels.cpu().numpy())
acc = accuracy_score(pred_labels, target_labels)
epoch_loss /= len(loader)
epoch_contrastive_loss /= len(loader)
# def report_classification_accuracy(pred_labels, true_labels):
# from sklearn.metrics import confusion_matrix
# cm = confusion_matrix(pred_labels, true_labels)
#
# print(cm)
#
# import numpy as np
# cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#
# acc_results = cm.diagonal()
# emotion_class_2_acc = {}
# for idx, class_name in enumerate(index_2_emotion_class):
# emotion_class_2_acc[class_name] = acc_results[idx]
# # print(class_name, acc_results[idx])
# return emotion_class_2_acc
# emotion_class_2_acc = report_classification_accuracy(pred_labels=pred_labels, true_labels=target_labels)
report = classification_report(y_pred=pred_labels, y_true=target_labels)
return epoch_loss, acc, report, epoch_contrastive_loss
def validate(model, loader, optimizer, device, index_2_emotion_class) -> (float, float, dict):
# set model to eval mode
model.eval()
# don't compute gradients
with torch.no_grad():
val_loss, val_acc, val_report, val_contrastive_loss = train_epoch(
model=model,
loader=loader,
optimizer=optimizer,
device=device,
training=False,
index_2_emotion_class=index_2_emotion_class
)
return val_loss, val_acc, val_report, val_contrastive_loss
def train(num_epochs, model, loaders, optimizer, device, index_2_emotion_class, output_dir):
best_val_acc = 0
for epoch in range(num_epochs):
# train model for a single epoch
print(f"Epoch {epoch}")
train_loss, train_acc, train_report, train_contrastive_loss = train_epoch(
model=model,
loader=loaders["train"],
optimizer=optimizer,
device=device,
index_2_emotion_class=index_2_emotion_class
)
print(f"train loss : {train_loss} | train acc: {train_acc}")
val_loss, val_acc, val_report, val_contrastive_loss = validate(
model=model,
loader=loaders["val"],
optimizer=optimizer,
device=device,
index_2_emotion_class=index_2_emotion_class
)
print(f"val loss : {val_loss} | val contrastive loss: {val_contrastive_loss} | val acc: {val_acc}")
if val_acc > best_val_acc:
best_val_acc = val_acc
Path(output_dir).mkdir(parents=True, exist_ok=True)
ckpt_model_file = os.path.join(output_dir, "model.ckpt")
performance_file = os.path.join(output_dir, "results.txt")
print("saving model to ", ckpt_model_file)
torch.save(model, ckpt_model_file)
with open(performance_file, 'a') as writer:
writer.write(f"Epoch: {epoch} | Train acc: {train_acc} | Dev acccuracy: {best_val_acc}\n")
writer.write(f"Epoch: {epoch} | Train loss: {train_loss} | Dev acccuracy: {val_loss}\n")
writer.write(val_report)
writer.write('\n')
# for (emotion_class, acc) in val_emotion_class_2_acc.items():
# writer.write(f"{emotion_class}: {acc}\n")
def setup_optimizer(lr, model):
"""
return:
- criterion: loss_fn
- optimizer: torch.optim
"""
print("check model on device: ", next(model.parameters()).is_cuda)
optimizer = torch.optim.Adam(params=model.parameters(), lr=lr)
return optimizer