-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainer.py
More file actions
220 lines (204 loc) · 7.78 KB
/
Copy pathtrainer.py
File metadata and controls
220 lines (204 loc) · 7.78 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import random
import copy
from time import time
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
class DatasetSplit(Dataset):
def __init__(self, dataset, indices=None):
if indices is None:
indices = range(len(dataset))
self.dataset = dataset
self.indices = indices
for i in indices:
assert i < len(dataset)
def __len__(self):
return len(self.indices)
def __getitem__(self, idx):
return self.dataset[self.indices[idx]]
def shuffle_tensors(*tensors):
assert len(tensors) > 0
output = []
count = len(tensors[0])
indices = torch.randperm(count)
for tensor in tensors:
assert len(tensor) == count
output.append(tensor[indices])
return output
def split_tensors(*tensors, ratio):
assert len(tensors) > 0
split1, split2 = [], []
count = len(tensors[0])
for tensor in tensors:
assert len(tensor) == count
split1.append(tensor[:int(len(tensor) * ratio)])
split2.append(tensor[int(len(tensor) * ratio):])
if len(tensors) == 1:
split1, split2 = split1[0], split2[0]
return split1, split2
def shuffle_dataset(dataset):
indices = list(range(len(dataset)))
random.shuffle(indices)
return DatasetSplit(dataset, indices)
def split_dataset(dataset, ratio=0.7, shuffle=False):
if type(ratio) is not tuple:
ratio = (ratio, 1 - ratio)
ratio = list(ratio)
assert sum(ratio) <= 1
indices = list(range(len(dataset)))
if shuffle:
random.shuffle(indices)
splits = []
for i in range(len(ratio)):
start = int(sum(ratio[:i]) * len(dataset))
end = int(sum(ratio[:i + 1]) * len(dataset))
split = indices[start:end]
splits.append(DatasetSplit(dataset, split))
return tuple(splits)
def _epoch(scope, training=False):
model = scope["model"]
optimizers = scope["optimizers"]
loss_funcs = scope["loss_funcs"]
loader = scope["loader"]
scope = copy.copy(scope)
total_metrics = {}
total_loss = len(loss_funcs) * [0]
scope["total_metrics"] = total_metrics
scope["total_loss"] = total_loss
if training:
model.train()
else:
model.eval()
iterator = tqdm(loader)
iteration = 1
for batch in iterator:
metrics = {}
scope["iteration"] = iteration
scope["batch"] = batch
scope["metrics"] = metrics
if "process_batch" in scope and scope["process_batch"] is not None:
batch = scope["process_batch"](batch)
if "device" in scope and scope["device"] is not None:
batch = [tensor.to(scope["device"]) for tensor in batch]
losses = len(loss_funcs) * [None]
outputs = len(loss_funcs) * [None]
for i in range(len(loss_funcs)):
scope["model_id"] = i
scope["model"] = model
scope["optimizer"] = optimizers[i]
scope["loss_func"] = loss_funcs[i]
loss, output = loss_funcs[i](model, batch, scope)
if training:
optimizers[i].zero_grad()
loss.backward()
optimizers[i].step()
total_loss[i] += loss.item()
losses[i] = loss.item()
outputs[i] = output
scope["losses"] = losses
scope["outputs"] = outputs
scope["loss"] = losses[0]
scope["output"] = outputs[0]
scope["total_loss"] = total_loss
if "on_batch" in scope and scope["on_batch"] is not None:
scope["on_batch"](scope)
for key in metrics:
if key not in total_metrics:
total_metrics[key] = metrics[key]
else:
total_metrics[key] += metrics[key]
iteration += 1
for i in range(len(loss_funcs)):
total_loss[i] = total_loss[i] / iteration
return total_loss, total_metrics
def train(model, loss_funcs, train_dataset, val_dataset, optimizers,
epochs=100, batch_size=256, device=0, print_function=print,
on_train_batch=None, on_val_batch=None, on_train_epoch=None, on_val_epoch=None, after_epoch=None):
# Reseting tqdm
if hasattr(tqdm, "_instances"):
tqdm._instances.clear()
# Security Checks
if type(loss_funcs) is not list and type(loss_funcs) is not tuple:
loss_funcs = [loss_funcs]
if type(optimizers) is not list and type(optimizers) is not tuple:
optimizers = [optimizers]
assert len(loss_funcs) > 0
assert len(optimizers) > 0
if len(optimizers) == 1 and len(loss_funcs) != 1:
optimizers = len(loss_funcs) * optimizers
elif len(loss_funcs) == 1 and len(optimizers) != 1:
loss_funcs = len(optimizers) * loss_funcs
assert len(loss_funcs) == len(optimizers)
# Moving models to device
model = model.to(device)
# Creating dataset loaders
train_loader = None if train_dataset is None else torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,
shuffle=True)
val_loader = None if val_dataset is None else torch.utils.data.DataLoader(val_dataset, batch_size=batch_size,
shuffle=False)
# Generating Scope
scope = {
"model": model,
"loss_func": loss_funcs[0],
"loss_funcs": loss_funcs,
"train_dataset": train_dataset,
"val_dataset": val_dataset,
"optimizer": optimizers[0],
"optimizers": optimizers,
"epochs": epochs,
"batch_size": batch_size,
"device": device,
"train_loader": train_loader,
"val_loader": val_loader,
}
# Epochs
for epoch_id in range(1, epochs + 1):
scope["epoch"] = epoch_id
print_function("Epoch #" + str(epoch_id))
# Training
if train_loader is not None:
scope["dataset"] = train_dataset
scope["loader"] = train_loader
scope["on_batch"] = on_train_batch
train_loss, train_metrics = _epoch(scope, True)
scope["train_losses"] = train_loss
scope["train_loss"] = train_loss[0]
scope["train_metrics"] = train_metrics
if len(loss_funcs) == 1:
print_function("\tTrain Loss = " + str(train_loss[0]))
else:
for i in range(len(loss_funcs)):
print_function("\tTrain Loss " + str(i) + " = " + str(train_loss[i]))
for key in train_metrics:
print_function("\tTrain " + key + " = " + str(train_metrics[key]))
if on_train_epoch is not None:
on_train_epoch(scope)
# Validation
if val_loader is not None:
scope["dataset"] = val_dataset
scope["loader"] = val_loader
scope["on_batch"] = on_val_batch
with torch.no_grad():
val_loss, val_metrics = _epoch(scope, False)
scope["val_losses"] = val_loss
scope["val_loss"] = val_loss[0]
scope["val_metrics"] = val_metrics
if len(loss_funcs) == 1:
print_function("\tValidation Loss = " + str(val_loss[0]))
else:
for i in range(len(loss_funcs)):
print_function("\tValidation Loss " + str(i) + " = " + str(val_loss[i]))
for key in val_metrics:
print_function("\tValidation " + key + " = " + str(val_metrics[key]))
if on_val_epoch is not None:
on_val_epoch(scope)
# Clearing variables
del scope["dataset"]
del scope["loader"]
del scope["on_batch"]
# Saving model
if after_epoch is not None:
after_epoch(scope)
return model