forked from dice-group/Ontolearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclip_trainer.py
162 lines (154 loc) · 7.51 KB
/
clip_trainer.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
# -----------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2024 Ontolearn Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
import numpy as np
import copy
import torch
from tqdm import trange
from collections import defaultdict
import os
import json
from torch.optim.lr_scheduler import ExponentialLR
from torch.nn import functional as F
from torch.nn.utils import clip_grad_value_
from torch.nn.utils.rnn import pad_sequence
from sklearn.metrics import f1_score, accuracy_score
import time
class CLIPTrainer:
"""CLIP trainer."""
def __init__(self, clip, epochs=300, learning_rate=1e-4, decay_rate=0, clip_value=5.0,
storage_path="./"):
self.clip = clip
self.epochs = epochs
self.learning_rate = learning_rate
self.decay_rate = decay_rate
self.clip_value = clip_value
self.storage_path = storage_path
def compute_eval_metric(self, target, prediction):
f1 = 100*f1_score(target, prediction, average="micro")
acc = 100*accuracy_score(target, prediction)
return f1, acc
def get_optimizer(self, length_predictor, optimizer='Adam'):
if optimizer == 'Adam':
return torch.optim.Adam(length_predictor.parameters(), lr=self.learning_rate)
elif optimizer == 'SGD':
return torch.optim.SGD(length_predictor.parameters(), lr=self.learning_rate)
elif optimizer == 'RMSprop':
return torch.optim.RMSprop(length_predictor.parameters(), lr=self.learning_rate)
else:
raise ValueError
print('Unsupported optimizer')
def show_num_learnable_params(self):
print("*"*20+"Trainable model size"+"*"*20)
size = sum([p.numel() for p in self.clip.length_predictor.parameters()])
size_ = 0
print("Length Predictor: ", size)
print("*"*20+"Trainable model size"+"*"*20)
print()
return size
def train(self, train_dataloader, save_model=True, optimizer='Adam', record_runtime=True):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if isinstance(self.clip.length_predictor, list):
self.clip.length_predictor = copy.deepcopy(self.clip.length_predictor[0])
model_size = self.show_num_learnable_params()
if device.type == "cpu":
print("Training on CPU, it may take long...")
else:
print("GPU available !")
print()
print("#"*50)
print()
print("{} starts training... \n".format(self.clip.length_predictor.name))
print("#"*50, "\n")
length_predictor = copy.deepcopy(self.clip.length_predictor).train()
desc = length_predictor.name
if device.type == "cuda":
length_predictor.cuda()
opt = self.get_optimizer(length_predictor=length_predictor, optimizer=optimizer)
if self.decay_rate:
self.scheduler = ExponentialLR(opt, self.decay_rate)
Train_loss = []
F1, Acc = [], []
best_score = 0.
if record_runtime:
t0 = time.time()
Epochs = trange(self.epochs, desc=f'Loss: {np.nan}, F1: {np.nan}, Acc: {np.nan}', leave=True)
for e in Epochs:
f1s, accs = [], []
train_losses = []
for x1, x2, labels in train_dataloader:
if device.type == "cuda":
x1, x2, labels = x1.cuda(), x2.cuda(), labels.cuda()
scores = length_predictor(x1, x2)
loss = length_predictor.loss(scores, labels)
predictions = scores.argmax(1).detach().cpu().numpy()
f1, acc = self.compute_eval_metric(labels.cpu().numpy(), predictions)
f1s.append(f1)
accs.append(acc)
train_losses.append(loss.item())
opt.zero_grad()
loss.backward()
clip_grad_value_(length_predictor.parameters(), clip_value=self.clip_value)
opt.step()
if self.decay_rate:
self.scheduler.step()
F1.append(np.mean(f1s))
Acc.append(np.mean(accs))
Train_loss.append(np.mean(train_losses))
Epochs.set_description('Loss: {:.4f}, F1: {:.2f}%, Acc: {:.2f}%'.format(Train_loss[-1],
F1[-1],
Acc[-1]))
Epochs.refresh()
weights = copy.deepcopy(length_predictor.state_dict())
if Acc and Acc[-1] > best_score:
best_score = Acc[-1]
best_weights = weights
length_predictor.load_state_dict(best_weights)
if record_runtime:
duration = time.time()-t0
runtime_info = {"Architecture": length_predictor.name,
"Number of Epochs": self.epochs, "Runtime (s)": duration}
if not os.path.exists(self.storage_path+"/runtime/"):
os.mkdir(self.storage_path+"/runtime/")
with open(self.storage_path+"/runtime/runtime"+"_"+desc+".json", "w") as file:
json.dump(runtime_info, file, indent=3)
results_dict = dict()
print("Top performance: loss: {:.4f}, f1: {:.2f}% ... "
"acc: {:.2f}%".format(min(Train_loss), max(F1), max(Acc)), "weights saved based on Acc best score!")
print()
results_dict.update({"Train Max F1": max(F1), "Train Acc": max(Acc),
"Train Min Loss": min(Train_loss)})
if not os.path.exists(self.storage_path+"/results/"):
os.mkdir(self.storage_path+"/results/")
with open(self.storage_path+"/results/"+"results"+"_"+desc+".json", "w") as file:
json.dump(results_dict, file, indent=3)
if save_model:
if not os.path.exists(self.storage_path+"/trained_models/"):
os.mkdir(self.storage_path+"/trained_models/")
torch.save(length_predictor.state_dict(), self.storage_path+"/trained_models/"+"trained_"+desc+".pt")
print("{} saved".format(length_predictor.name))
if not os.path.exists(self.storage_path+"/metrics/"):
os.mkdir(self.storage_path+"/metrics/")
with open(self.storage_path+"/metrics/"+"metrics_"+desc+".json", "w") as plot_file:
json.dump({"f1": F1, "acc": Acc, "loss": Train_loss}, plot_file,
indent=3)